Skip to content

Commit

Permalink
UHF-9712: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrsky committed Mar 15, 2024
1 parent 8ede79c commit b587246
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
111 changes: 111 additions & 0 deletions tests/src/Functional/EntityOgImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_platform_config\Functional;

use Drupal\file\Entity\File;
use Drupal\helfi_tpr\Entity\Unit;
use Drupal\media\Entity\Media;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;

/**
* Tests default og image.
*/
class EntityOgImageTest extends BrowserTestBase {

use TestFileCreationTrait;

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* {@inheritdoc}
*/
protected static $modules = [
'helfi_platform_config_base',
'helfi_tpr_config',
];

/**
* Test og images.
*/
public function testOgImages() : void {
$uri = $this->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.
*/
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
* Content 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');
}

}
99 changes: 99 additions & 0 deletions tests/src/Unit/OGImageManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_platform_config\Unit;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\helfi_platform_config\Token\OGImageBuilderInterface;
use Drupal\helfi_platform_config\Token\OGImageManager;
use Drupal\Tests\token\Kernel\UnitTest;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;

/**
* Tests og image builder collector.
*
* @coversDefaultClass \Drupal\helfi_platform_config\Token\OGImageManager
* @group helfi_platform_config
*/
class OGImageManagerTest extends UnitTest {

use ProphecyTrait;

/**
* Tests builder.
*
* @covers ::buildUrl
* @covers ::add
* @covers ::getBuilders
*/
public function testBuildUrl() : void {
$sut = $this->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;
}

}

0 comments on commit b587246

Please sign in to comment.