public function MetatagTest::testMetatagFieldEnhancer in Entity Share 8.3
Test the Metatag resource field enhancer plugin.
Test it with and without three plugin options.
File
- modules/
entity_share_client/ tests/ src/ Functional/ MetatagTest.php, line 99
Class
- MetatagTest
- General functional test class for metatag field.
Namespace
Drupal\Tests\entity_share_client\FunctionalCode
public function testMetatagFieldEnhancer() {
// Initially save JSON:API resource with default Metatag
// enhancer configuration.
$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();
// Import data from JSON:API.
$this
->importData();
// Load and remember the metatags of newly imported node.
$node = $this
->loadEntity('node', 'es_test');
$node_metatags = unserialize($node
->get('field_es_test_metatag')
->getValue()[0]['value']);
// In this case even if default metatags are exposed, as the exposed data
// is only token, it is not saved back into the field.
$expected_metatags = [
'abstract' => 'test abstract',
];
// This node must be deleted because of next import.
$node
->delete();
$this
->assertEquals($expected_metatags, $node_metatags, 'The node has the expected metatags.');
// Reset the responses in TestRemoteManager::doRequest because the
// response is supposed to change on "remote".
$this->remoteManager
->resetResponseMapping();
// Generate "remote" content again.
$this
->prepareContent();
// Load and remember the metatags of newly generated "remote" node.
$node = $this
->loadEntity('node', 'es_test');
$node_title = $node
->label();
$node_url = $node
->toUrl('canonical')
->setAbsolute()
->toString();
// In this case, default tags with tokens had been replaced by the real
// values. But as for the first case, when a value is only an unreplaced
// token, Metatag does not save back the value.
// So for example, we don't see in the result the [node:summary] token.
$expected_metatags = [
'canonical_url' => $node_url,
'title' => $node_title . ' | Drupal',
'abstract' => 'test abstract',
];
// Alter the plugin definition of Metatag enhancer:
// activate "Replace tokens" option.
$resource_config = $this->configFactory
->getEditable('jsonapi_extras.jsonapi_resource_config.node--es_test');
$resource_fields = $resource_config
->get('resourceFields');
$resource_fields['field_es_test_metatag']['enhancer']['settings']['replace_tokens'] = TRUE;
$resource_config
->set('resourceFields', $resource_fields);
$resource_config
->save();
$this->pluginCacheClearer
->clearCachedDefinitions();
// Re-import data from JSON:API.
$this
->importData();
$node = $this
->loadEntity('node', 'es_test');
$node_metatags = unserialize($node
->get('field_es_test_metatag')
->getValue()[0]['value']);
// This node must be deleted because of next import.
$node
->delete();
$this
->assertEquals($expected_metatags, $node_metatags, 'The node has the expected metatags.');
// Reset the responses in TestRemoteManager::doRequest because the
// response is supposed to change on "remote".
$this->remoteManager
->resetResponseMapping();
// Generate "remote" content again.
$this
->prepareContent();
// Load and remember the metatags of newly generated "remote" node.
$node = $this
->loadEntity('node', 'es_test');
$node_title = $node
->label();
$node_url = $node
->toUrl('canonical')
->setAbsolute()
->toString();
// Same as the second case, the difference will be in the JSON output, the
// [node:summary] token will be exposed but not replaced, so Metatag does
// not save back the value.
$expected_metatags = [
'canonical_url' => $node_url,
'title' => $node_title . ' | Drupal',
'abstract' => 'test abstract',
];
// Alter the plugin definition of Metatag enhancer:
// activate "Clear tokens" option (ie. all settings checked).
$resource_config = $this->configFactory
->getEditable('jsonapi_extras.jsonapi_resource_config.node--es_test');
$resource_fields = $resource_config
->get('resourceFields');
$resource_fields['field_es_test_metatag']['enhancer']['settings']['clear_tokens'] = TRUE;
$resource_config
->set('resourceFields', $resource_fields);
$resource_config
->save();
$this->pluginCacheClearer
->clearCachedDefinitions();
// Re-import data from JSON:API.
$this
->importData();
$node = $this
->loadEntity('node', 'es_test');
$node_metatags = unserialize($node
->get('field_es_test_metatag')
->getValue()[0]['value']);
$this
->assertEquals($expected_metatags, $node_metatags, 'The node has the expected metatags.');
}