Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-32283: Fixed File reference storing #160

Merged
merged 7 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion eZ/Publish/Core/FieldType/BinaryBase/BinaryBaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ public function copyLegacyField(VersionInfo $versionInfo, Field $field, Field $o
}

// field translations have their own file reference, but to the original file
$originalField->value->externalData['id'];
$field->value->externalData['id'] = $originalField->value->externalData['id'];
barw4 marked this conversation as resolved.
Show resolved Hide resolved
$field->value->externalData['mimeType'] = $originalField->value->externalData['mimeType'];
$field->value->externalData['uri'] = $originalField->value->externalData['uri'];

return $this->gateway->storeFileReference($versionInfo, $field);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\Core\FieldType\Tests\Integration\BinaryBase\BinaryBaseStorage;
barw4 marked this conversation as resolved.
Show resolved Hide resolved

use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway as BinaryBaseStorageGateway;
use eZ\Publish\Core\FieldType\BinaryFile\BinaryFileStorage\Gateway\DoctrineStorage;
use eZ\Publish\Core\FieldType\Tests\Integration\BaseCoreFieldTypeIntegrationTest;
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
use eZ\Publish\SPI\Tests\Persistence\FixtureImporter;
use eZ\Publish\SPI\Tests\Persistence\YamlFixture;

/**
* BinaryBase Field Type external storage gateway tests.
*/
class BinaryBaseStorageGatewayTest extends BaseCoreFieldTypeIntegrationTest
alongosz marked this conversation as resolved.
Show resolved Hide resolved
{
protected function setUp(): void
{
parent::setUp();

$importer = new FixtureImporter($this->getDatabaseConnection());
$importer->import(new YamlFixture(__DIR__ . '/_fixtures/ezbinaryfile.yaml'));
}

protected function getGateway(): BinaryBaseStorageGateway
{
return new DoctrineStorage($this->getDatabaseConnection());
}

public function testGetFileReferenceWithFixture(): void
{
$data = $this->getGateway()->getFileReferenceData(10, 1);

$expected = [
'id' => 'image/a6bbf351175ad9c2f27e5b17c2c5d105.png',
'mimeType' => 'image/png',
'fileName' => 'test.png',
'downloadCount' => 0,
];

self::assertEquals($expected, $data);
}

public function testStoreFileReference(): void
{
$field = new Field();
$field->id = 123;
$field->fieldDefinitionId = 231;
$field->type = 'ezbinaryfile';
$field->versionNo = 1;
$field->value = new FieldValue([
'externalData' => [
'id' => 'image/809c753a26e11f363cd8c14d824d162a.jpg',
'path' => '/tmp/phpR4tNSI',
'inputUri' => '/tmp/phpR4tNSI',
'fileName' => '1.jpg',
'fileSize' => '372949',
'mimeType' => 'image/jpg',
'uri' => '/admin/content/download/75/320?version=1',
'downloadCount' => 0,
],
]);

$versionInfo = new VersionInfo([
'contentInfo' => new ContentInfo([
'id' => 1,
]),
'versionNo' => 1,
]);

$this->getGateway()->storeFileReference($versionInfo, $field);

$data = $this->getGateway()->getFileReferenceData(123, 1);

$expected = [
'id' => 'image/809c753a26e11f363cd8c14d824d162a.jpg',
'mimeType' => 'image/jpg',
'fileName' => '1.jpg',
'downloadCount' => 0,
];

self::assertEquals($expected, $data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\Core\FieldType\Tests\Integration\BinaryBase\BinaryBaseStorage;

use eZ\Publish\API\Repository\Values\Content\ContentInfo;
use eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage;
use eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway;
use eZ\Publish\Core\FieldType\BinaryFile\BinaryFileStorage\Gateway\DoctrineStorage;
use eZ\Publish\Core\FieldType\Tests\Integration\BaseCoreFieldTypeIntegrationTest;
use eZ\Publish\Core\IO\IOServiceInterface;
use eZ\Publish\Core\IO\Values\BinaryFile;
use eZ\Publish\Core\IO\Values\BinaryFileCreateStruct;
use eZ\Publish\SPI\FieldType\BinaryBase\PathGenerator;
use eZ\Publish\SPI\IO\MimeTypeDetector;
use eZ\Publish\SPI\Persistence\Content\Field;
use eZ\Publish\SPI\Persistence\Content\FieldValue;
use eZ\Publish\SPI\Persistence\Content\VersionInfo;

class BinaryBaseStorageTest extends BaseCoreFieldTypeIntegrationTest
{
/** @var \eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway|\PHPUnit\Framework\MockObject\MockObject */
protected $gateway;

/** @var \eZ\Publish\SPI\FieldType\BinaryBase\PathGenerator|\PHPUnit\Framework\MockObject\MockObject */
protected $pathGeneratorMock;

/** @var \eZ\Publish\Core\IO\IOServiceInterface|\PHPUnit\Framework\MockObject\MockObject */
protected $ioServiceMock;

/** @var \eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage|\PHPUnit\Framework\MockObject\MockObject */
protected $storage;

protected function setUp(): void
{
parent::setUp();

$this->gateway = $this->getStorageGateway();
$this->pathGeneratorMock = $this->createMock(PathGenerator::class);
$this->ioServiceMock = $this->createMock(IOServiceInterface::class);
$this->storage = $this->getMockBuilder(BinaryBaseStorage::class)
->onlyMethods([])
->setConstructorArgs(
[
$this->gateway,
$this->ioServiceMock,
$this->pathGeneratorMock,
$this->createMock(MimeTypeDetector::class),
]
)
->getMock();
}

protected function getContext(): array
{
return ['context'];
}

public function testHasFieldData(): void
{
self::assertTrue($this->storage->hasFieldData());
}

/**
* @dataProvider providerOfFieldData
*/
public function testStoreFieldData(VersionInfo $versionInfo, Field $field): void
{
$binaryFileCreateStruct = new BinaryFileCreateStruct([
'id' => 'qwerty12345',
'size' => '372949',
'mimeType' => 'image/jpeg',
]);

$this->ioServiceMock
->expects(self::once())
->method('newBinaryCreateStructFromLocalFile')
->will($this->returnValue($binaryFileCreateStruct));

$this->pathGeneratorMock
->expects(self::once())
->method('getStoragePathForField')
->with($field, $versionInfo)
->willReturn('image/qwerty12345.jpg');

$this->ioServiceMock
->expects(self::once())
->method('createBinaryFile')
->with($binaryFileCreateStruct)
->willReturn(new BinaryFile());

$this->storage->storeFieldData($versionInfo, $field, $this->getContext());

$this->expectNotToPerformAssertions();
alongosz marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @depends testStoreFieldData
*
* @dataProvider providerOfFieldData
*/
public function testCopyLegacyField(VersionInfo $versionInfo, Field $originalField): void
{
$field = clone $originalField;
$field->id = 124;
$field->versionNo = 2;
$field->value = new FieldValue([
'externalData' => [
'fileName' => '123.jpg',
'downloadCount' => 0,
'mimeType' => null,
'uri' => null,
],
]);

$flag = $this->storage->copyLegacyField($versionInfo, $field, $originalField, $this->getContext());

self::assertFalse($flag);
}

public function providerOfFieldData(): array
{
$field = new Field();
$field->id = 124;
$field->fieldDefinitionId = 231;
$field->type = 'ezbinaryfile';
$field->versionNo = 1;
$field->value = new FieldValue([
'externalData' => [
'id' => 'image/aaac753a26e11f363cd8c14d824d162a.jpg',
'path' => '/tmp/phpR4tNSV',
'inputUri' => '/tmp/phpR4tNSV',
'fileName' => '123.jpg',
'fileSize' => '12345',
'mimeType' => 'image/jpeg',
'uri' => '/admin/content/download/75/320?version=1',
'downloadCount' => 0,
],
]);

$versionInfo = new VersionInfo([
'contentInfo' => new ContentInfo([
'id' => 235,
'contentTypeId' => 24,
]),
'versionNo' => 1,
]);

return [
[$versionInfo, $field],
];
}

protected function getStorageGateway(): Gateway
{
return new DoctrineStorage($this->getDatabaseConnection());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ezbinaryfile:
- { contentobject_attribute_id: 10, version: 1, download_count: 0, filename: a6bbf351175ad9c2f27e5b17c2c5d105.png, mime_type: image/png, original_filename: test.png }