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 14, 2024
1 parent c5a3cb8 commit 003d1b4
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/src/Functional/DefaultOgImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\helfi_platform_config\Functional;

use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;

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

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

/**
* {@inheritdoc}
*/
protected static $modules = [
'language',
'helfi_platform_config',
'helfi_image_styles',
];

/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
foreach (['fi', 'sv'] as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)->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);
}
}

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

namespace Drupal\Tests\helfi_platform_config\Kernel;

use Drupal\Core\Entity\EntityInterface;
use Drupal\helfi_platform_config\Token\OGImageBuilderInterface;
use Drupal\KernelTests\KernelTestBase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

/**
* Tests DefaultImageBuilder.
*
* @group helfi_api_base
*/
class OgImageManagerTest extends KernelTestBase {

use ProphecyTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'user',
'image',
'responsive_image',
'image_style_quality',
'breakpoint',
'crop',
'focal_point',
'config_rewrite',
'helfi_platform_config',
'helfi_image_styles',
];

/**
* {@inheritDoc}
*/
protected function setUp(): void {
parent::setUp();

$this->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);
}

}
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 003d1b4

Please sign in to comment.