|
| 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 | +} |
0 commit comments