Skip to content

Commit

Permalink
[TASK] Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayacoo committed Feb 18, 2024
1 parent e1470c7 commit 971cbb0
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 67 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ name: CI with Composer scripts
on:
push:
branches:
- main
- quality
pull_request:
#schedule:
#- cron: '15 3 * * 1'
permissions:
contents: read
packages: read
Expand Down Expand Up @@ -67,6 +66,9 @@ jobs:
matrix:
command:
- "php:csfix"
- "php:sniff"
- "ts:lint"
- "xliff:lint"
php-version:
- "8.3"
unit-tests:
Expand Down
63 changes: 3 additions & 60 deletions Classes/Command/UpdateMetadataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@

namespace Ayacoo\AyacooSoundcloud\Command;

use Ayacoo\AyacooSoundcloud\Domain\Repository\FileRepository;
use Ayacoo\AyacooSoundcloud\Helper\SoundcloudHelper;
use Ayacoo\AyacooSoundcloud\Service\UpdateMetadataService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Index\MetaDataRepository;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper;
use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class UpdateMetadataCommand extends Command
{
Expand All @@ -34,10 +26,7 @@ protected function configure(): void
}

public function __construct(
protected FileRepository $fileRepository,
protected MetaDataRepository $metadataRepository,
protected ResourceFactory $resourceFactory,
protected ProcessedFileRepository $processedFileRepository
protected readonly UpdateMetadataService $updateMetadataService
) {
parent::__construct();
}
Expand All @@ -47,54 +36,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io = new SymfonyStyle($input, $output);
$limit = (int)($input->getOption('limit') ?? 10);

$soundcloudHelper = GeneralUtility::makeInstance(SoundcloudHelper::class, 'soundcloud');

$audios = $this->fileRepository->getVideosByFileExtension('soundcloud', $limit);
foreach ($audios as $audio) {
$file = $this->resourceFactory->getFileObject($audio['uid']);
$metaData = $soundcloudHelper->getMetaData($file);
if ($metaData !== []) {
$newMetaData = [
'width' => (int)$metaData['width'],
'height' => (int)$metaData['height'],
'soundcloud_html' => $metaData['soundcloud_html'],
'soundcloud_author_url' => $metaData['soundcloud_author_url'],
'soundcloud_thumbnail_url' => $metaData['soundcloud_thumbnail_url'],
];
if (isset($metaData['title'])) {
$newMetaData['title'] = $metaData['title'];
}
if (isset($metaData['author'])) {
$newMetaData['author'] = $metaData['author'];
}
$this->metadataRepository->update($file->getUid(), $newMetaData);
$this->handlePreviewImage($soundcloudHelper, $file);
$io->success($file->getProperty('title') . '(UID: ' . $file->getUid() . ') was processed');
}
}
$this->updateMetadataService->process($limit, $io);

return Command::SUCCESS;
}

protected function handlePreviewImage(AbstractOnlineMediaHelper $onlineMediaHelper, File $file): void
{
$processedFiles = $this->processedFileRepository->findAllByOriginalFile($file);
foreach ($processedFiles as $processedFile) {
$processedFile->delete();
}

$videoId = $onlineMediaHelper->getOnlineMediaId($file);
$temporaryFileName = $this->getTempFolderPath() . $file->getExtension() . '_' . md5($videoId) . '.jpg';
@unlink($temporaryFileName);
$onlineMediaHelper->getPreviewImage($file);
}

protected function getTempFolderPath(): string
{
$path = Environment::getPublicPath() . '/typo3temp/assets/online_media/';
if (!is_dir($path)) {
GeneralUtility::mkdir_deep($path);
}
return $path;
}
}
78 changes: 78 additions & 0 deletions Classes/Service/UpdateMetadataService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Ayacoo\AyacooSoundcloud\Service;

use Ayacoo\AyacooSoundcloud\Domain\Repository\FileRepository;
use Ayacoo\AyacooSoundcloud\Helper\SoundcloudHelper;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Index\MetaDataRepository;
use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\AbstractOnlineMediaHelper;
use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class UpdateMetadataService
{
public function __construct(
protected FileRepository $fileRepository,
protected MetaDataRepository $metadataRepository,
protected ResourceFactory $resourceFactory,
protected ProcessedFileRepository $processedFileRepository
) {

}

public function process(int $limit, SymfonyStyle $io): void
{
$soundcloudHelper = GeneralUtility::makeInstance(SoundcloudHelper::class, 'soundcloud');
$audios = $this->fileRepository->getVideosByFileExtension('soundcloud', $limit);
foreach ($audios as $audio) {
$file = $this->resourceFactory->getFileObject($audio['uid']);
$metaData = $soundcloudHelper->getMetaData($file);
if ($metaData !== []) {
$newMetaData = [
'width' => (int)$metaData['width'],
'height' => (int)$metaData['height'],
'soundcloud_html' => $metaData['soundcloud_html'],
'soundcloud_author_url' => $metaData['soundcloud_author_url'],
'soundcloud_thumbnail_url' => $metaData['soundcloud_thumbnail_url'],
];
if (isset($metaData['title'])) {
$newMetaData['title'] = $metaData['title'];
}
if (isset($metaData['author'])) {
$newMetaData['author'] = $metaData['author'];
}
$this->metadataRepository->update($file->getUid(), $newMetaData);
$this->handlePreviewImage($soundcloudHelper, $file);
$io->success($file->getProperty('title') . '(UID: ' . $file->getUid() . ') was processed');
}
}
}

protected function handlePreviewImage(AbstractOnlineMediaHelper $onlineMediaHelper, File $file): void
{
$processedFiles = $this->processedFileRepository->findAllByOriginalFile($file);
foreach ($processedFiles as $processedFile) {
$processedFile->delete();
}

$videoId = $onlineMediaHelper->getOnlineMediaId($file);
$temporaryFileName = $this->getTempFolderPath() . $file->getExtension() . '_' . md5($videoId) . '.jpg';
@unlink($temporaryFileName);
$onlineMediaHelper->getPreviewImage($file);
}

protected function getTempFolderPath(): string
{
$path = Environment::getPublicPath() . '/typo3temp/assets/online_media/';
if (!is_dir($path)) {
GeneralUtility::mkdir_deep($path);
}
return $path;
}
}
152 changes: 147 additions & 5 deletions Tests/Unit/Rendering/SoundcloudRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace Ayacoo\AyacooSoundcloud\Tests\Unit\Rendering;

use Ayacoo\AyacooSoundcloud\Event\ModifySoundcloudOutputEvent;
use Ayacoo\AyacooSoundcloud\Helper\SoundcloudHelper;
use Ayacoo\AyacooSoundcloud\Rendering\SoundcloudRenderer;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\Rendering\FileRendererInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

final class SoundcloudRendererTest extends UnitTestCase
Expand All @@ -25,12 +29,12 @@ protected function setUp(): void
$eventDispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
//$eventDispatcherMock->expects(self::atLeastOnce())->method('dispatch')->with(self::anything())->willReturnArgument(0);

$configurationManager = $this->getMockBuilder(ConfigurationManager::class)
$configurationManagerMock = $this->getMockBuilder(ConfigurationManager::class)
->onlyMethods(['getConfiguration'])
->disableOriginalConstructor()
->getMock();

$this->subject = new SoundcloudRenderer($eventDispatcherMock, $configurationManager);
$this->subject = new SoundcloudRenderer($eventDispatcherMock, $configurationManagerMock);
}

/**
Expand Down Expand Up @@ -69,11 +73,149 @@ public function canRenderWithMatchingMimeTypeReturnsFalse(): void
self::assertFalse($result);
}

private function buildReflectionForProtectedFunction(string $methodName, array $params)
/**
* @test
* @dataProvider getPrivacySettingWithExistingConfigReturnsBooleanDataProvider
* @return void
*/
public function getPrivacySettingWithExistingConfigReturnsBoolean(array $pluginConfig, bool $expected)
{
$eventDispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();

$configurationManagerMock = $this->getMockBuilder(ConfigurationManager::class)
->onlyMethods(['getConfiguration'])
->disableOriginalConstructor()
->getMock();

$configurationManagerMock
->expects(self::atLeastOnce())
->method('getConfiguration')
->with(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
->willReturn($pluginConfig);

$subject = new SoundcloudRenderer($eventDispatcherMock, $configurationManagerMock);

$params = [];
$methodName = 'getPrivacySetting';
$result = $this->buildReflectionForProtectedFunction($methodName, $params, $subject);

self::assertEquals($expected, $result);
}

public static function getPrivacySettingWithExistingConfigReturnsBooleanDataProvider(): array
{
return [
'Privacy setting true' => [
[
'plugin.' => [
'tx_ayacoosoundcloud.' => [
'settings.' => [
'privacy' => true
]
]
]
],
true
],
'Privacy setting false' => [
[
'plugin.' => [
'tx_ayacoosoundcloud.' => [
'settings.' => [
'privacy' => false
]
]
]
],
false
],
'Privacy setting non-existing' => [
[],
false
]
];
}

/**
* @test
*/
public function renderReturnsSoundcloudHtml(): void
{
$iframe = '<iframe src="https://www.soundcloud.com" />';

$fileResourceMock = $this->createMock(File::class);
$fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('audio/soundcloud');
$fileResourceMock->expects(self::any())->method('getExtension')->willReturn('soundcloud');
$fileResourceMock->expects(self::any())->method('getProperty')->with('soundcloud_html')->willReturn($iframe);

$event = new ModifySoundcloudOutputEvent($iframe);

$eventDispatcherMock = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
$eventDispatcherMock->expects(self::once())->method('dispatch')->with(self::anything())->willReturn($event);

$configurationManagerMock = $this->getMockBuilder(ConfigurationManager::class)
->onlyMethods(['getConfiguration'])
->disableOriginalConstructor()
->getMock();

$subject = new SoundcloudRenderer($eventDispatcherMock, $configurationManagerMock);

$expected = $iframe;

$result = $subject->render($fileResourceMock, 100, 100);
self::assertSame($expected, $result);
}

/**
* @test
*/
public function renderWithPrivacyTrueReturnsModifiedSoundcloudHtml(): void
{
$iframe = '<iframe src="https://www.soundcloud.com" />';
$expected = '<iframe data-name="iframe-soundcloud" data-src="https://www.soundcloud.com" />';

$fileResourceMock = $this->createMock(File::class);
$fileResourceMock->expects(self::any())->method('getMimeType')->willReturn('audio/soundcloud');
$fileResourceMock->expects(self::any())->method('getExtension')->willReturn('soundcloud');
$fileResourceMock->expects(self::any())->method('getProperty')->with('soundcloud_html')->willReturn($iframe);

$event = new ModifySoundcloudOutputEvent($expected);
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
$eventDispatcherMock->expects(self::once())->method('dispatch')->with($event)->willReturn($event);

$configurationManagerMock = $this->getMockBuilder(ConfigurationManager::class)
->onlyMethods(['getConfiguration'])
->disableOriginalConstructor()
->getMock();

$pluginConfig = [
'plugin.' => [
'tx_ayacoosoundcloud.' => [
'settings.' => [
'privacy' => true
]
]
]
];

$configurationManagerMock
->expects(self::atLeastOnce())
->method('getConfiguration')
->with(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT)
->willReturn($pluginConfig);

$subject = new SoundcloudRenderer($eventDispatcherMock, $configurationManagerMock);

$result = $subject->render($fileResourceMock, 100, 100);
self::assertSame($expected, $result);
}


private function buildReflectionForProtectedFunction(string $methodName, array $params, SoundcloudRenderer $subject)
{
$reflectionCalendar = new \ReflectionClass($this->subject);
$reflectionCalendar = new \ReflectionClass($subject);
$method = $reflectionCalendar->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($this->subject, $params);
return $method->invokeArgs($subject, $params);
}
}

0 comments on commit 971cbb0

Please sign in to comment.