Skip to content

Commit

Permalink
EZP-32283: Fixed File reference storing (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartek committed Mar 11, 2021
1 parent 04651cc commit cf6b77d
Show file tree
Hide file tree
Showing 4 changed files with 261 additions and 1 deletion.
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'];
$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;

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
{
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();
}

/**
* @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 }

0 comments on commit cf6b77d

Please sign in to comment.