View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\node\NodeInterface;
class MetatagTest extends EntityShareClientFunctionalTestBase {
public static $modules = [
'jsonapi_extras',
];
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function getEntitiesDataArray() {
return [
'node' => [
'en' => [
'es_test' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
'field_es_test_metatag' => [
'value' => serialize([
'abstract' => 'test abstract',
]),
'checker_callback' => 'getValue',
],
]),
],
],
];
}
public function testExposeDefaultTags() {
$this->entityTypeManager
->getStorage('jsonapi_resource_config')
->create([
'id' => 'node--es_test',
'disabled' => FALSE,
'path' => 'node/es_test',
'resourceType' => 'node--es_test',
'resourceFields' => [
'field_es_test_metatag' => [
'fieldName' => 'field_es_test_metatag',
'publicName' => 'field_es_test_metatag',
'enhancer' => [
'id' => 'entity_share_metatag',
'settings' => [
'expose_default_tags' => TRUE,
'replace_tokens' => FALSE,
'clear_tokens' => FALSE,
],
],
'disabled' => FALSE,
],
],
])
->save();
$this
->prepareContent();
$this
->populateRequestService();
$this
->deleteContent();
$this
->pullEveryChannels();
$node = $this
->loadEntity('node', 'es_test');
$node_metatags = unserialize($node
->get('field_es_test_metatag')
->getValue()[0]['value']);
$expected_metatags = [
'abstract' => 'test abstract',
];
$this
->assertEquals($expected_metatags, $node_metatags, 'The node has the expected metatags.');
}
public function testExposeDefaultTagsAndTokenReplace() {
$this->entityTypeManager
->getStorage('jsonapi_resource_config')
->create([
'id' => 'node--es_test',
'disabled' => FALSE,
'path' => 'node/es_test',
'resourceType' => 'node--es_test',
'resourceFields' => [
'field_es_test_metatag' => [
'fieldName' => 'field_es_test_metatag',
'publicName' => 'field_es_test_metatag',
'enhancer' => [
'id' => 'entity_share_metatag',
'settings' => [
'expose_default_tags' => TRUE,
'replace_tokens' => TRUE,
'clear_tokens' => FALSE,
],
],
'disabled' => FALSE,
],
],
])
->save();
$this
->prepareContent();
$node = $this
->loadEntity('node', 'es_test');
$node_title = $node
->label();
$node_url = $node
->toUrl('canonical')
->setAbsolute()
->toString();
$this
->populateRequestService();
$this
->deleteContent();
$this
->pullEveryChannels();
$node = $this
->loadEntity('node', 'es_test');
$node_metatags = unserialize($node
->get('field_es_test_metatag')
->getValue()[0]['value']);
$expected_metatags = [
'canonical_url' => $node_url,
'title' => $node_title . ' | Drupal',
'abstract' => 'test abstract',
];
$this
->assertEquals($expected_metatags, $node_metatags, 'The node has the expected metatags.');
}
public function testExposeDefaultTagsAndTokenReplaceAndClearToken() {
$this->entityTypeManager
->getStorage('jsonapi_resource_config')
->create([
'id' => 'node--es_test',
'disabled' => FALSE,
'path' => 'node/es_test',
'resourceType' => 'node--es_test',
'resourceFields' => [
'field_es_test_metatag' => [
'fieldName' => 'field_es_test_metatag',
'publicName' => 'field_es_test_metatag',
'enhancer' => [
'id' => 'entity_share_metatag',
'settings' => [
'expose_default_tags' => TRUE,
'replace_tokens' => TRUE,
'clear_tokens' => TRUE,
],
],
'disabled' => FALSE,
],
],
])
->save();
$this
->prepareContent();
$node = $this
->loadEntity('node', 'es_test');
$node_title = $node
->label();
$node_url = $node
->toUrl('canonical')
->setAbsolute()
->toString();
$this
->populateRequestService();
$this
->deleteContent();
$this
->pullEveryChannels();
$node = $this
->loadEntity('node', 'es_test');
$node_metatags = unserialize($node
->get('field_es_test_metatag')
->getValue()[0]['value']);
$expected_metatags = [
'canonical_url' => $node_url,
'title' => $node_title . ' | Drupal',
'abstract' => 'test abstract',
];
$this
->assertEquals($expected_metatags, $node_metatags, 'The node has the expected metatags.');
}
}