-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] add event listener to enrich file data. (#541)
* [FEATURE] add event to enrich file data. * [FEATURE] fix missing constructor DI. * [FEATURE] add code review changes. * [FEATURE] remove nullable option on EnrichFileDataEvent. * [FEATURE] fix unit tests for FileUtility. * [FEATURE] add php cs changes. * [FEATURE] add PHP Unit test for EnrichFileDataEvent. * [FEATURE] change php cs issue.
- Loading branch information
Showing
4 changed files
with
179 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Event; | ||
|
||
use TYPO3\CMS\Core\Resource\FileInterface; | ||
|
||
final class EnrichFileDataEvent | ||
{ | ||
private FileInterface $fileReference; | ||
|
||
private array $properties; | ||
|
||
public function __construct(FileInterface $fileReference, array $properties = []) | ||
{ | ||
$this->fileReference = $fileReference; | ||
$this->properties = $properties; | ||
} | ||
|
||
public function getProperties(): array | ||
{ | ||
return $this->properties; | ||
} | ||
|
||
public function setProperties(array $properties): void | ||
{ | ||
$this->properties = $properties; | ||
} | ||
|
||
public function getFileReference(): FileInterface | ||
{ | ||
return $this->fileReference; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Test\Unit\Event; | ||
|
||
use FriendsOfTYPO3\Headless\Event\EnrichFileDataEvent; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use TYPO3\CMS\Core\Resource\AbstractFile; | ||
use TYPO3\CMS\Core\Resource\FileReference; | ||
use TYPO3\TestingFramework\Core\Unit\UnitTestCase; | ||
|
||
class EnrichFileDataEventTest extends UnitTestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function eventTest() | ||
{ | ||
$properties = [ | ||
'prop-1' => 'value-1', | ||
'prop-2' => 'value-2' | ||
]; | ||
$fileReferenceMock = $this->getMockFileReferenceForData($this->getFileReferenceBaselineData()); | ||
$enrichFileDataEvent = new EnrichFileDataEvent($fileReferenceMock, $properties); | ||
|
||
self::assertSame($fileReferenceMock, $enrichFileDataEvent->getFileReference()); | ||
self::assertSame($properties, $enrichFileDataEvent->getProperties()); | ||
|
||
$overwriteProperties = $enrichFileDataEvent->getProperties(); | ||
$overwriteProperties['prop-1'] = 'value-overwritten'; | ||
$overwriteProperties['prop-3'] = 'value-3'; | ||
$enrichFileDataEvent->setProperties($overwriteProperties); | ||
|
||
self::assertSame($overwriteProperties, $enrichFileDataEvent->getProperties()); | ||
} | ||
|
||
protected function getFileReferenceBaselineData(): array | ||
{ | ||
return [ | ||
'extension' => 'jpg', | ||
'size' => 72392, | ||
'title' => null, | ||
'description' => null, | ||
'alternative' => null, | ||
'name' => 'test-file.jpg', | ||
'link' => '', | ||
'crop' => '{"default":{"cropArea":{"x":0,"y":0,"width":1,"height":1},"selectedRatio":"NaN","focusArea":null}}', | ||
'autoplay' => 0, | ||
'minWidth' => null, | ||
'minHeight' => null, | ||
'maxWidth' => null, | ||
'maxHeight' => null, | ||
'width' => 526, | ||
'uid_local' => 103, | ||
'height' => 526, | ||
]; | ||
} | ||
|
||
protected function getMockFileReferenceForData($data, $type = 'image') | ||
{ | ||
$fileReference = $this->createPartialMock( | ||
FileReference::class, | ||
['getPublicUrl', 'getUid', 'getProperty', 'hasProperty', 'toArray', 'getType', 'getMimeType', 'getProperties', 'getSize'] | ||
); | ||
$fileReference->method('getUid')->willReturn(103); | ||
if ($type === 'video') { | ||
$fileReference->method('getMimeType')->willReturn('video/youtube'); | ||
$fileReference->method('getType')->willReturn(AbstractFile::FILETYPE_VIDEO); | ||
$fileReference->method('getPublicUrl')->willReturn('https://www.youtube.com/watch?v=123456789'); | ||
} else { | ||
$fileReference->method('getType')->willReturn(AbstractFile::FILETYPE_IMAGE); | ||
$fileReference->method('getPublicUrl')->willReturn('/fileadmin/test-file.jpg'); | ||
$fileReference->method('getMimeType')->willReturn('image/jpeg'); | ||
} | ||
|
||
$fileReference->method('getProperty')->willReturnCallback(static function ($key) use ($data) { | ||
return $data[$key] ?? null; | ||
}); | ||
|
||
$fileReference->method('hasProperty')->willReturnCallback(static function ($key) use ($data) { | ||
return array_key_exists($key, $data); | ||
}); | ||
|
||
$fileReference->method('toArray')->willReturn($data); | ||
$fileReference->method('getProperties')->willReturn($data); | ||
$fileReference->method('getSize')->willReturn($data['size']); | ||
return $fileReference; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters