From 759400f5b79783c2cacd4b94c0107f5dcce886ea Mon Sep 17 00:00:00 2001 From: Santeri Hurnanen Date: Tue, 12 Mar 2024 12:25:23 +0200 Subject: [PATCH] UHF-9712: Add tests --- tests/src/Functional/DefaultOgImageTest.php | 66 ++++++++++++++ tests/src/Functional/EntityOgImageTest.php | 63 +++++++++++++ tests/src/Kernel/OgImageManagerTest.php | 68 ++++++++++++++ tests/src/Unit/OGImageManagerTest.php | 99 +++++++++++++++++++++ 4 files changed, 296 insertions(+) create mode 100644 tests/src/Functional/DefaultOgImageTest.php create mode 100644 tests/src/Functional/EntityOgImageTest.php create mode 100644 tests/src/Kernel/OgImageManagerTest.php create mode 100644 tests/src/Unit/OGImageManagerTest.php 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/Functional/EntityOgImageTest.php b/tests/src/Functional/EntityOgImageTest.php new file mode 100644 index 000000000..3d11823e7 --- /dev/null +++ b/tests/src/Functional/EntityOgImageTest.php @@ -0,0 +1,63 @@ +getTestFiles('image')[0]->uri; + + $file = File::create([ + 'uri' => $uri, + ]); + $file->save(); + + $media = Media::create([ + 'bundle' => 'image', + 'name' => 'Custom name', + 'field_media_image' => $file->id(), + ]); + $media->save(); + + $node = $this->drupalCreateNode([ + 'title' => 'en title', + 'langcode' => 'en', + 'bundle' => 'page', + 'field_liftup_image' => $media->id(), + 'status' => 1, + ]); + $node->save(); + + $this->drupalGet($node->toUrl('canonical')); + $this->assertSession()->elementAttributeContains('css', 'meta[property="og:image"]', 'content', 'styles/og_image'); + } + +} 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; + } + +}