diff --git a/Core/H5PIntegration.php b/Core/H5PIntegration.php index c6e8a5a..0bf20b7 100644 --- a/Core/H5PIntegration.php +++ b/Core/H5PIntegration.php @@ -18,31 +18,37 @@ class H5PIntegration extends H5PUtils /** * @var EntityManager */ - private $entityManager; + private EntityManager $entityManager; + /** * @var \H5PCore */ - private $core; + private \H5PCore $core; + /** * @var RouterInterface */ - private $router; + private RouterInterface $router; + /** * @var H5POptions */ - private $options; + private H5POptions $options; + /** * @var RequestStack */ - private $requestStack; + private RequestStack $requestStack; + /** * @var Packages */ - private $assetsPaths; + private Packages $assetsPaths; + /** * @var \H5PContentValidator */ - private $contentValidator; + private \H5PContentValidator $contentValidator; /** * H5PContent constructor. diff --git a/Core/H5POptions.php b/Core/H5POptions.php index ef118db..d18ddfa 100644 --- a/Core/H5POptions.php +++ b/Core/H5POptions.php @@ -12,11 +12,11 @@ class H5POptions /** * @var array */ - private $config; + private array|null $config; /** * @var array */ - private $storedConfig = null; + private array|null $storedConfig = null; private $h5pPath; private $folderPath; @@ -24,7 +24,7 @@ class H5POptions /** * @var EntityManagerInterface */ - private $manager; + private EntityManagerInterface $manager; /** * H5POptions constructor. @@ -139,8 +139,7 @@ public function getAbsoluteH5PPath(): string { $dir = $this->getOption('storage_dir'); $dir = $dir[0] === '/' ? $dir : "/{$dir}"; - - return $this->getAbsoluteWebPath() . $dir; + return rtrim($this->getAbsoluteWebPath(), '/') . $dir; } public function getAbsoluteWebPath(): string diff --git a/Event/H5PEvents.php b/Event/H5PEvents.php index e537e29..6956c48 100644 --- a/Event/H5PEvents.php +++ b/Event/H5PEvents.php @@ -20,7 +20,8 @@ class H5PEvents extends \H5PEventBase /** * @var EntityManagerInterface $em */ - private $em; + private EntityManagerInterface $em; + /** * H5PEvents constructor. * @param $type diff --git a/Event/LibrarySemanticsEvent.php b/Event/LibrarySemanticsEvent.php index 73cdb52..f85b4ff 100644 --- a/Event/LibrarySemanticsEvent.php +++ b/Event/LibrarySemanticsEvent.php @@ -6,49 +6,54 @@ class LibrarySemanticsEvent extends Event { - private $semantics; - private $name; - private $majorVersion; - private $minorVersion; + private array $semantics; + private string $name; + private int $majorVersion; + private int $minorVersion; + /** * LibrarySemanticsEvent constructor. - * @param $semantics - * @param $name - * @param $majorVersion - * @param $minorVersion + * @param array $semantics array of semantics + * @param string $name Nom of package + * @param int $majorVersion number of major version + * @param int $minorVersion number of minor version */ - public function __construct($semantics, $name, $majorVersion, $minorVersion) + public function __construct(array $semantics, string $name, int $majorVersion, int $minorVersion) { $this->semantics = $semantics; $this->name = $name; $this->majorVersion = $majorVersion; $this->minorVersion = $minorVersion; } + /** - * @return mixed + * @return array */ - public function getSemantics() + public function getSemantics(): array { return $this->semantics; } + /** - * @return mixed + * @return string */ - public function getName() + public function getName(): string { return $this->name; } + /** - * @return mixed + * @return int */ - public function getMajorVersion() + public function getMajorVersion(): int { return $this->majorVersion; } + /** - * @return mixed + * @return int */ - public function getMinorVersion() + public function getMinorVersion(): int { return $this->minorVersion; } diff --git a/Tests/Core/H5PIntegrationTest.php b/Tests/Core/H5PIntegrationTest.php new file mode 100644 index 0000000..3bcfee1 --- /dev/null +++ b/Tests/Core/H5PIntegrationTest.php @@ -0,0 +1,116 @@ +core = $this->createMock(H5PCore::class); + $this->options = $this->createMock(H5POptions::class); + $tokenStorage = $this->createMock(TokenStorageInterface::class); + $this->entityManager = $this->createMock(EntityManager::class); + $this->router = $this->createMock(RouterInterface::class); + $this->requestStack = $this->createMock(RequestStack::class); + $this->assetsPaths = $this->createMock(Packages::class); + $this->contentValidator = $this->createMock(H5PContentValidator::class); + + // Création de l'instance de H5PIntegration pour les tests + $this->h5pIntegration = new H5PIntegration( + $this->core, + $this->options, + $tokenStorage, + $this->entityManager, + $this->router, + $this->requestStack, + $this->assetsPaths, + $this->contentValidator + ); + } + + public function testGetGenericH5PIntegrationSettings() + { + $request = new Request(); + $this->requestStack->method('getMainRequest')->willReturn($request); + + $this->options->method('getOption')->willReturnMap([ + ['save_content_state', false, true], + ['save_content_frequency', 30, 30], + ['hub_is_enabled', true, true] + ]); + $h5pFrameworkMock = $this->createMock(H5PFrameworkInterface::class); + $h5pFrameworkMock->method('getLibraryConfig')->willReturn(['someKey' => 'someValue']); + + // Injectez le mock H5PFramework dans H5PCore + $this->core->h5pF = $h5pFrameworkMock; + + $settings = $this->h5pIntegration->getGenericH5PIntegrationSettings(); + + $this->assertIsArray($settings); + $this->assertArrayHasKey('baseUrl', $settings); + $this->assertArrayHasKey('ajax', $settings); + $this->assertArrayHasKey('l10n', $settings); + } + + public function testGetCoreAssets() + { + $this->options->method('getH5PAssetPath')->willReturn('/assets/h5p'); + H5PCore::$scripts = ['script1.js', 'script2.js']; + H5PCore::$styles = ['style1.css', 'style2.css']; + + $assets = $this->h5pIntegration->getCoreAssets(); + + $this->assertIsArray($assets); + $this->assertArrayHasKey('scripts', $assets); + $this->assertArrayHasKey('styles', $assets); + $this->assertCount(2, $assets['scripts']); + $this->assertCount(2, $assets['styles']); + } + + public function testGetCacheBuster() + { + H5PCore::$coreApi = ['majorVersion' => 1, 'minorVersion' => 2]; + $cacheBuster = $this->h5pIntegration->getCacheBuster(); + + $this->assertEquals('?=1.2', $cacheBuster); + } + + public function testGetTranslationFilePath() + { + $request = new Request(); + $request->setLocale('en'); + $this->requestStack->method('getCurrentRequest')->willReturn($request); + + $this->options->method('getAbsoluteWebPath')->willReturn('/web'); + + $translationFilePath = $this->h5pIntegration->getTranslationFilePath(); + + $this->assertStringContainsString('/h5p-editor/language/en.js', $translationFilePath); + } + +} diff --git a/Tests/Core/H5POptionsTest.php b/Tests/Core/H5POptionsTest.php new file mode 100644 index 0000000..0b2ce2e --- /dev/null +++ b/Tests/Core/H5POptionsTest.php @@ -0,0 +1,111 @@ +entityManager = $this->createMock(EntityManagerInterface::class); + // Créez un mock pour l'EntityRepository + $this->repository = $this->createMock(EntityRepository::class); + + // Configurez l'EntityManager pour retourner un mock de repository + $this->entityManager->method('getRepository')->willReturn($this->repository); + + // Initialisez H5POptions avec les dépendances mockées + $this->h5pOptions = new H5POptions( + ['storage_dir' => '/var/www/html'], // Config de test + '/var/www', // projectRootDir de test + $this->entityManager + ); + } + + public function testGetOptionReturnsStoredConfigValue() + { + // Simule la méthode findAll() du repository pour retourner une option + $option = $this->createMock(Option::class); + $option->method('getName')->willReturn('storage_dir'); + $option->method('getValue')->willReturn('/tmp/h5p'); + + // Configurez le repository pour retourner cette option + $this->repository->method('findAll')->willReturn([$option]); + + // Testez la méthode getOption + $result = $this->h5pOptions->getOption('storage_dir'); + $this->assertEquals('/tmp/h5p', $result); + } + + public function testSetOptionStoresNewOptionValue() + { + // Créez un mock de l'option à persister + $option = $this->createMock(Option::class); + $option->method('getName')->willReturn('storage_dir'); + + // Simulez la recherche de l'option dans le repository + $this->repository->method('find')->willReturn(null); // Aucun option trouvée, il faut créer une nouvelle + + // Configurez l'EntityManager pour simuler les méthodes persist et flush + $this->entityManager->expects($this->once())->method('persist')->with($this->isInstanceOf(Option::class)); + $this->entityManager->expects($this->once())->method('flush'); + + // Appelez setOption et vérifiez que persist et flush sont bien appelées + $this->h5pOptions->setOption('storage_dir', '/new/path/h5p'); + } + + public function testGetOptionReturnsDefaultIfOptionNotFound() + { + // Configurez le mock pour retourner une liste vide d'options + $this->repository->method('findAll')->willReturn([]); + + // Testez la méthode getOption avec une option qui n'existe pas + $result = $this->h5pOptions->getOption('non_existent_option', 'default_value'); + $this->assertEquals('default_value', $result); + } + + public function testRetrieveStoredConfigHandlesDriverException() + { + // Testez la gestion de l'exception dans la méthode retrieveStoredConfig + $this->expectNotToPerformAssertions(); + try { + $this->h5pOptions->getOption('storage_dir'); + } catch (DriverException $e) { + // Vérifiez que l'exception est bien attrapée + $this->assertEquals('Database error', $e->getMessage()); + } + } + + public function testGetUploadedH5pFolderPath() + { + // Testez le getter et setter de folderPath + $this->h5pOptions->getUploadedH5pFolderPath('/custom/folder'); + $this->assertEquals('/custom/folder', $this->h5pOptions->getUploadedH5pFolderPath()); + } + + public function testGetRelativeH5PPath() + { + // Testez la méthode getRelativeH5PPath pour obtenir le chemin relatif + $this->h5pOptions->setOption('storage_dir', 'var/h5p'); + $this->assertEquals('/var/h5p', $this->h5pOptions->getRelativeH5PPath()); + } + + public function testGetAbsoluteH5PPath() + { + // Testez la méthode getAbsoluteH5PPath pour obtenir le chemin absolu + $this->h5pOptions->setOption('storage_dir', 'var/h5p'); + $this->assertStringContainsString('/var/www/var/h5p', $this->h5pOptions->getAbsoluteH5PPath()); + } +} diff --git a/Tests/Event/LibrarySemanticsEvent.php b/Tests/Event/LibrarySemanticsEvent.php new file mode 100644 index 0000000..a98f92d --- /dev/null +++ b/Tests/Event/LibrarySemanticsEvent.php @@ -0,0 +1,45 @@ +semantics = ['title' => 'Example', 'description' => 'Example description']; + $this->name = 'TestPackage'; + $this->majorVersion = 1; + $this->minorVersion = 0; + + $this->event = new LibrarySemanticsEvent($this->semantics, $this->name, $this->majorVersion, $this->minorVersion); + } + + public function testGetSemantics() + { + $this->assertEquals($this->semantics, $this->event->getSemantics(), 'The semantics should match the initialized value.'); + } + + public function testGetName() + { + $this->assertEquals($this->name, $this->event->getName(), 'The name should match the initialized value.'); + } + + public function testGetMajorVersion() + { + $this->assertEquals($this->majorVersion, $this->event->getMajorVersion(), 'The major version should match the initialized value.'); + } + + public function testGetMinorVersion() + { + $this->assertEquals($this->minorVersion, $this->event->getMinorVersion(), 'The minor version should match the initialized value.'); + } +}