diff --git a/tests/src/Functional/DefaultOgImageTest.php b/tests/src/Functional/DefaultOgImageTest.php new file mode 100644 index 000000000..e69d634f7 --- /dev/null +++ b/tests/src/Functional/DefaultOgImageTest.php @@ -0,0 +1,66 @@ +save(); + } + } + + /** + * Tests [site:shareable-image] token. + */ + public function testDefaultOgImage() : void { + /** @var \Drupal\Core\Utility\Token $token_service */ + $token_service = $this->container->get('token'); + + /** @var \Drupal\language\ConfigurableLanguageManagerInterface $language_manager */ + $language_manager = $this->container->get('language_manager'); + + foreach (['fi', 'sv'] as $langcode) { + // Set content language. + $language_manager->setCurrentLanguage( + $language_manager->getLanguage($langcode), + LanguageInterface::TYPE_CONTENT, + ); + + $og_image_url = $token_service->replace('[site:shareable-image]'); + + $this->drupalGet($og_image_url); + + // Image should exist. + $this->assertSession()->statusCodeEquals(200); + } + } + +} diff --git a/tests/src/Kernel/OgImageManagerTest.php b/tests/src/Kernel/OgImageManagerTest.php new file mode 100644 index 000000000..65b80929a --- /dev/null +++ b/tests/src/Kernel/OgImageManagerTest.php @@ -0,0 +1,68 @@ +installEntitySchema('user'); + $this->installEntitySchema('crop'); + $this->installConfig(['system', 'helfi_image_styles']); + } + + /** + * Tests that images are processed through og_image image style. + */ + public function testImageStyle() : void { + $og_image_manager = $this->container->get('helfi_platform_config.og_image_manager'); + + // Add builder that always overwrites the result. + $builder = $this->prophesize(OGImageBuilderInterface::class); + $builder->applies(Argument::any())->willReturn(TRUE); + $builder->buildUri(Argument::any())->willReturn('public://my-image.jpg'); + $og_image_manager->add($builder->reveal(), 9999999); + + $entity = $this->prophesize(EntityInterface::class); + $og_image_url = $og_image_manager->buildUrl($entity->reveal()); + + // Assert that og_image image style was used. + $this->assertStringContainsString('/styles/og_image/', $og_image_url); + $this->assertStringContainsString('my-image.jpg', $og_image_url); + } + +} diff --git a/tests/src/Unit/OGImageManagerTest.php b/tests/src/Unit/OGImageManagerTest.php new file mode 100644 index 000000000..30119e67b --- /dev/null +++ b/tests/src/Unit/OGImageManagerTest.php @@ -0,0 +1,99 @@ +getSut(); + $entity = $this->prophesize(EntityInterface::class)->reveal(); + + // First builder does not apply. + $sut->add($this->createImageBuilderMock('https://1', FALSE)->reveal()); + $this->assertEquals(NULL, $sut->buildUrl($entity)); + + // Second builder applies but returns NULL. + $sut->add($this->createImageBuilderMock(NULL)->reveal()); + $this->assertEquals(NULL, $sut->buildUrl($entity)); + + // Third builder applies, priority is lower. + $sut->add($this->createImageBuilderMock('https://3')->reveal(), -10); + $this->assertEquals('https://3', $sut->buildUrl($entity)); + + // Builder with the lowers priority gets overwritten by '3'. + $builder4 = $this->createImageBuilderMock('https://4'); + $sut->add($builder4->reveal(), -100); + $this->assertEquals('https://3', $sut->buildUrl($entity)); + $builder4->buildUri(Argument::any())->shouldHaveBeenCalled(); + } + + /** + * Gets service under test. + * + * @param \Drupal\Core\File\FileUrlGeneratorInterface|null $fileUrlGenerator + * File url generator mock. + * + * @returns \Drupal\helfi_platform_config\Token\OGImageManager + * The open graph image manager. + */ + private function getSut(?FileUrlGeneratorInterface $fileUrlGenerator = NULL) : OGImageManager { + $moduleHandler = $this->prophesize(ModuleHandlerInterface::class); + + if (!$fileUrlGenerator) { + $prophecy = $this->prophesize(FileUrlGeneratorInterface::class); + $prophecy->generateAbsoluteString(Argument::any())->willReturnArgument(0); + $fileUrlGenerator = $prophecy->reveal(); + } + + return new OGImageManager( + $moduleHandler->reveal(), + $fileUrlGenerator, + ); + } + + /** + * Creates mock image builder. + * + * @param string|null $url + * Return value for buildUrl(). + * @param bool $applies + * Return value for applies(). + * + * @return \Drupal\helfi_platform_config\Token\OGImageBuilderInterface|\Prophecy\Prophecy\ObjectProphecy + * Builder mock. + */ + private function createImageBuilderMock(?string $url, bool $applies = TRUE) : OGImageBuilderInterface|ObjectProphecy { + $builder = $this->prophesize(OGImageBuilderInterface::class); + $builder->applies(Argument::any())->willReturn($applies); + $builder->buildUri(Argument::any())->willReturn($url); + return $builder; + } + +}