From 500217e0c8e7612d897736c11399606dd7334157 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/EntityOgImageTest.php | 112 +++++++++++++++++++++ tests/src/Unit/OGImageManagerTest.php | 99 ++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 tests/src/Functional/EntityOgImageTest.php create mode 100644 tests/src/Unit/OGImageManagerTest.php diff --git a/tests/src/Functional/EntityOgImageTest.php b/tests/src/Functional/EntityOgImageTest.php new file mode 100644 index 000000000..fb075d8c1 --- /dev/null +++ b/tests/src/Functional/EntityOgImageTest.php @@ -0,0 +1,112 @@ +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' => 'title', + 'langcode' => 'fi', + 'bundle' => 'page', + 'status' => 1, + ]); + $node->save(); + + // Global image style is used when media field is not set. + $this->drupalGet($node->toUrl('canonical')); + $this->assertGlobalOgImage('fi'); + + // Media is used when 'field_liftup_image' is set. + $node->set('field_liftup_image', $media->id()); + $node->save(); + $this->drupalGet($node->toUrl('canonical')); + $this->assertImageStyle(); + + $unit = Unit::create([ + 'id' => 123, + 'title' => 'title', + 'langcode' => 'sv', + 'bundle' => 'tpr_unit', + ]); + $unit->save(); + + // Global image style is used when media field is not set. + $this->drupalGet($unit->toUrl('canonical')); + $this->assertGlobalOgImage('sv'); + + // Picture url override is used. + $unit->set('picture_url_override', $media->id()); + $unit->save(); + $this->drupalGet($unit->toUrl('canonical')); + $this->assertImageStyle(); + } + + /** + * Assert that og_image image style was used. + * + * @return void + */ + private function assertImageStyle() : void { + $this->assertSession()->elementAttributeContains('css', 'meta[property="og:image"]', 'content', 'styles/og_image'); + } + + /** + * Assert that global og image was used. + * + * @param string $langcode + */ + private function assertGlobalOgImage(string $langcode) : void { + $og_image_file = match($langcode) { + 'sv' => 'og-global-sv.png', + default => 'og-global.png', + }; + + $this->assertSession()->elementAttributeContains('css', 'meta[property="og:image"]', 'content', $og_image_file); + $this->assertSession()->elementAttributeNotContains('css', 'meta[property="og:image"]', 'content', 'styles/og_image'); + } + +} 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; + } + +}