-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\helfi_platform_config\Functional; | ||
|
||
use Drupal\file\Entity\File; | ||
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', | ||
]; | ||
|
||
/** | ||
* Test og images. | ||
*/ | ||
public function testOgImages() : void { | ||
// Test node og image. | ||
$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' => '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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |