Skip to content

Commit cf6b77d

Browse files
author
Bartek
authored
EZP-32283: Fixed File reference storing (#160)
1 parent 04651cc commit cf6b77d

File tree

4 files changed

+261
-1
lines changed

4 files changed

+261
-1
lines changed

eZ/Publish/Core/FieldType/BinaryBase/BinaryBaseStorage.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ public function copyLegacyField(VersionInfo $versionInfo, Field $field, Field $o
107107
}
108108

109109
// field translations have their own file reference, but to the original file
110-
$originalField->value->externalData['id'];
110+
$field->value->externalData['id'] = $originalField->value->externalData['id'];
111+
$field->value->externalData['mimeType'] = $originalField->value->externalData['mimeType'];
112+
$field->value->externalData['uri'] = $originalField->value->externalData['uri'];
111113

112114
return $this->gateway->storeFileReference($versionInfo, $field);
113115
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace eZ\Publish\Core\FieldType\Tests\Integration\BinaryBase\BinaryBaseStorage;
10+
11+
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
12+
use eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway as BinaryBaseStorageGateway;
13+
use eZ\Publish\Core\FieldType\BinaryFile\BinaryFileStorage\Gateway\DoctrineStorage;
14+
use eZ\Publish\Core\FieldType\Tests\Integration\BaseCoreFieldTypeIntegrationTest;
15+
use eZ\Publish\SPI\Persistence\Content\Field;
16+
use eZ\Publish\SPI\Persistence\Content\FieldValue;
17+
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
18+
use eZ\Publish\SPI\Tests\Persistence\FixtureImporter;
19+
use eZ\Publish\SPI\Tests\Persistence\YamlFixture;
20+
21+
/**
22+
* BinaryBase Field Type external storage gateway tests.
23+
*/
24+
class BinaryBaseStorageGatewayTest extends BaseCoreFieldTypeIntegrationTest
25+
{
26+
protected function setUp(): void
27+
{
28+
parent::setUp();
29+
30+
$importer = new FixtureImporter($this->getDatabaseConnection());
31+
$importer->import(new YamlFixture(__DIR__ . '/_fixtures/ezbinaryfile.yaml'));
32+
}
33+
34+
protected function getGateway(): BinaryBaseStorageGateway
35+
{
36+
return new DoctrineStorage($this->getDatabaseConnection());
37+
}
38+
39+
public function testGetFileReferenceWithFixture(): void
40+
{
41+
$data = $this->getGateway()->getFileReferenceData(10, 1);
42+
43+
$expected = [
44+
'id' => 'image/a6bbf351175ad9c2f27e5b17c2c5d105.png',
45+
'mimeType' => 'image/png',
46+
'fileName' => 'test.png',
47+
'downloadCount' => 0,
48+
];
49+
50+
self::assertEquals($expected, $data);
51+
}
52+
53+
public function testStoreFileReference(): void
54+
{
55+
$field = new Field();
56+
$field->id = 123;
57+
$field->fieldDefinitionId = 231;
58+
$field->type = 'ezbinaryfile';
59+
$field->versionNo = 1;
60+
$field->value = new FieldValue([
61+
'externalData' => [
62+
'id' => 'image/809c753a26e11f363cd8c14d824d162a.jpg',
63+
'path' => '/tmp/phpR4tNSI',
64+
'inputUri' => '/tmp/phpR4tNSI',
65+
'fileName' => '1.jpg',
66+
'fileSize' => '372949',
67+
'mimeType' => 'image/jpg',
68+
'uri' => '/admin/content/download/75/320?version=1',
69+
'downloadCount' => 0,
70+
],
71+
]);
72+
73+
$versionInfo = new VersionInfo([
74+
'contentInfo' => new ContentInfo([
75+
'id' => 1,
76+
]),
77+
'versionNo' => 1,
78+
]);
79+
80+
$this->getGateway()->storeFileReference($versionInfo, $field);
81+
82+
$data = $this->getGateway()->getFileReferenceData(123, 1);
83+
84+
$expected = [
85+
'id' => 'image/809c753a26e11f363cd8c14d824d162a.jpg',
86+
'mimeType' => 'image/jpg',
87+
'fileName' => '1.jpg',
88+
'downloadCount' => 0,
89+
];
90+
91+
self::assertEquals($expected, $data);
92+
}
93+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace eZ\Publish\Core\FieldType\Tests\Integration\BinaryBase\BinaryBaseStorage;
10+
11+
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
12+
use eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage;
13+
use eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway;
14+
use eZ\Publish\Core\FieldType\BinaryFile\BinaryFileStorage\Gateway\DoctrineStorage;
15+
use eZ\Publish\Core\FieldType\Tests\Integration\BaseCoreFieldTypeIntegrationTest;
16+
use eZ\Publish\Core\IO\IOServiceInterface;
17+
use eZ\Publish\Core\IO\Values\BinaryFile;
18+
use eZ\Publish\Core\IO\Values\BinaryFileCreateStruct;
19+
use eZ\Publish\SPI\FieldType\BinaryBase\PathGenerator;
20+
use eZ\Publish\SPI\IO\MimeTypeDetector;
21+
use eZ\Publish\SPI\Persistence\Content\Field;
22+
use eZ\Publish\SPI\Persistence\Content\FieldValue;
23+
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
24+
25+
class BinaryBaseStorageTest extends BaseCoreFieldTypeIntegrationTest
26+
{
27+
/** @var \eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage\Gateway|\PHPUnit\Framework\MockObject\MockObject */
28+
protected $gateway;
29+
30+
/** @var \eZ\Publish\SPI\FieldType\BinaryBase\PathGenerator|\PHPUnit\Framework\MockObject\MockObject */
31+
protected $pathGeneratorMock;
32+
33+
/** @var \eZ\Publish\Core\IO\IOServiceInterface|\PHPUnit\Framework\MockObject\MockObject */
34+
protected $ioServiceMock;
35+
36+
/** @var \eZ\Publish\Core\FieldType\BinaryBase\BinaryBaseStorage|\PHPUnit\Framework\MockObject\MockObject */
37+
protected $storage;
38+
39+
protected function setUp(): void
40+
{
41+
parent::setUp();
42+
43+
$this->gateway = $this->getStorageGateway();
44+
$this->pathGeneratorMock = $this->createMock(PathGenerator::class);
45+
$this->ioServiceMock = $this->createMock(IOServiceInterface::class);
46+
$this->storage = $this->getMockBuilder(BinaryBaseStorage::class)
47+
->onlyMethods([])
48+
->setConstructorArgs(
49+
[
50+
$this->gateway,
51+
$this->ioServiceMock,
52+
$this->pathGeneratorMock,
53+
$this->createMock(MimeTypeDetector::class),
54+
]
55+
)
56+
->getMock();
57+
}
58+
59+
protected function getContext(): array
60+
{
61+
return ['context'];
62+
}
63+
64+
public function testHasFieldData(): void
65+
{
66+
self::assertTrue($this->storage->hasFieldData());
67+
}
68+
69+
/**
70+
* @dataProvider providerOfFieldData
71+
*/
72+
public function testStoreFieldData(VersionInfo $versionInfo, Field $field): void
73+
{
74+
$binaryFileCreateStruct = new BinaryFileCreateStruct([
75+
'id' => 'qwerty12345',
76+
'size' => '372949',
77+
'mimeType' => 'image/jpeg',
78+
]);
79+
80+
$this->ioServiceMock
81+
->expects(self::once())
82+
->method('newBinaryCreateStructFromLocalFile')
83+
->will($this->returnValue($binaryFileCreateStruct));
84+
85+
$this->pathGeneratorMock
86+
->expects(self::once())
87+
->method('getStoragePathForField')
88+
->with($field, $versionInfo)
89+
->willReturn('image/qwerty12345.jpg');
90+
91+
$this->ioServiceMock
92+
->expects(self::once())
93+
->method('createBinaryFile')
94+
->with($binaryFileCreateStruct)
95+
->willReturn(new BinaryFile());
96+
97+
$this->storage->storeFieldData($versionInfo, $field, $this->getContext());
98+
99+
$this->expectNotToPerformAssertions();
100+
}
101+
102+
/**
103+
* @depends testStoreFieldData
104+
*
105+
* @dataProvider providerOfFieldData
106+
*/
107+
public function testCopyLegacyField(VersionInfo $versionInfo, Field $originalField): void
108+
{
109+
$field = clone $originalField;
110+
$field->id = 124;
111+
$field->versionNo = 2;
112+
$field->value = new FieldValue([
113+
'externalData' => [
114+
'fileName' => '123.jpg',
115+
'downloadCount' => 0,
116+
'mimeType' => null,
117+
'uri' => null,
118+
],
119+
]);
120+
121+
$flag = $this->storage->copyLegacyField($versionInfo, $field, $originalField, $this->getContext());
122+
123+
self::assertFalse($flag);
124+
}
125+
126+
public function providerOfFieldData(): array
127+
{
128+
$field = new Field();
129+
$field->id = 124;
130+
$field->fieldDefinitionId = 231;
131+
$field->type = 'ezbinaryfile';
132+
$field->versionNo = 1;
133+
$field->value = new FieldValue([
134+
'externalData' => [
135+
'id' => 'image/aaac753a26e11f363cd8c14d824d162a.jpg',
136+
'path' => '/tmp/phpR4tNSV',
137+
'inputUri' => '/tmp/phpR4tNSV',
138+
'fileName' => '123.jpg',
139+
'fileSize' => '12345',
140+
'mimeType' => 'image/jpeg',
141+
'uri' => '/admin/content/download/75/320?version=1',
142+
'downloadCount' => 0,
143+
],
144+
]);
145+
146+
$versionInfo = new VersionInfo([
147+
'contentInfo' => new ContentInfo([
148+
'id' => 235,
149+
'contentTypeId' => 24,
150+
]),
151+
'versionNo' => 1,
152+
]);
153+
154+
return [
155+
[$versionInfo, $field],
156+
];
157+
}
158+
159+
protected function getStorageGateway(): Gateway
160+
{
161+
return new DoctrineStorage($this->getDatabaseConnection());
162+
}
163+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ezbinaryfile:
2+
- { contentobject_attribute_id: 10, version: 1, download_count: 0, filename: a6bbf351175ad9c2f27e5b17c2c5d105.png, mime_type: image/png, original_filename: test.png }

0 commit comments

Comments
 (0)