View source
<?php
namespace Drupal\Tests\metatag\Kernel;
use Drupal\KernelTests\KernelTestBase;
class MetatagManagerTest extends KernelTestBase {
protected static $modules = [
'system',
'field',
'text',
'user',
'token',
'metatag',
'metatag_open_graph',
];
protected $entityTypeManager;
protected $metatagManager;
protected function setUp() {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->metatagManager = $this->container
->get('metatag.manager');
$this
->installConfig([
'system',
'field',
'text',
'user',
'metatag',
'metatag_open_graph',
]);
$this
->installEntitySchema('user');
$this
->installSchema('user', [
'users_data',
]);
}
public function testDefaultTagsFromEntity() {
$user = $this->entityTypeManager
->getStorage('user')
->create();
$default_tags = $this->metatagManager
->defaultTagsFromEntity($user);
$expected_tags = [
'canonical_url' => '[user:url]',
'title' => '[user:display-name] | [site:name]',
'description' => '[site:name]',
];
$this
->assertSame($expected_tags, $default_tags);
}
public function testMetatagOrder() {
$tags = $this->metatagManager
->generateElements([
'og_image_width' => 100,
'og_image_height' => 100,
'og_image_url' => 'https://www.example.com/example/foo.png',
]);
$expected = [
'#attached' => [
'html_head' => [
[
[
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:url',
'content' => 'https://www.example.com/example/foo.png',
],
],
'og_image_url_0',
],
[
[
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:width',
'content' => 100,
],
],
'og_image_width',
],
[
[
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:height',
'content' => 100,
],
],
'og_image_height',
],
],
],
];
$this
->assertEquals($expected, $tags);
}
public function testMetatagMultiple() {
$tags = $this->metatagManager
->generateElements([
'og_image_width' => 100,
'og_image_height' => 100,
'og_image_url' => 'https://www.example.com/example/foo.png, https://www.example.com/example/foo2.png',
]);
$expected = [
'#attached' => [
'html_head' => [
[
[
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:url',
'content' => 'https://www.example.com/example/foo.png',
],
],
'og_image_url_0',
],
[
[
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:url',
'content' => 'https://www.example.com/example/foo2.png',
],
],
'og_image_url_1',
],
[
[
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:width',
'content' => 100,
],
],
'og_image_width',
],
[
[
'#tag' => 'meta',
'#attributes' => [
'property' => 'og:image:height',
'content' => 100,
],
],
'og_image_height',
],
],
],
];
$this
->assertEquals($expected, $tags);
}
}