diff --git a/eZ/Publish/Core/FieldType/BinaryBase/BinaryBaseStorage.php b/eZ/Publish/Core/FieldType/BinaryBase/BinaryBaseStorage.php index 928ce4d36d..93e5ea4f10 100644 --- a/eZ/Publish/Core/FieldType/BinaryBase/BinaryBaseStorage.php +++ b/eZ/Publish/Core/FieldType/BinaryBase/BinaryBaseStorage.php @@ -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); } diff --git a/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/BinaryBaseStorageGatewayTest.php b/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/BinaryBaseStorageGatewayTest.php new file mode 100644 index 0000000000..876891e79b --- /dev/null +++ b/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/BinaryBaseStorageGatewayTest.php @@ -0,0 +1,93 @@ +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); + } +} diff --git a/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/BinaryBaseStorageTest.php b/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/BinaryBaseStorageTest.php new file mode 100644 index 0000000000..5ddb5e20fe --- /dev/null +++ b/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/BinaryBaseStorageTest.php @@ -0,0 +1,163 @@ +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()); + } +} diff --git a/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/_fixtures/ezbinaryfile.yaml b/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/_fixtures/ezbinaryfile.yaml new file mode 100644 index 0000000000..b3305ef10f --- /dev/null +++ b/eZ/Publish/Core/FieldType/Tests/Integration/BinaryBase/BinaryBaseStorage/_fixtures/ezbinaryfile.yaml @@ -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 }