View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\node\NodeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\entity_share\EntityShareUtility;
use Drupal\entity_share_client\ImportContext;
class MetatagTest extends EntityShareClientFunctionalTestBase {
public static $modules = [
'jsonapi_extras',
];
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected $configFactory;
protected $pluginCacheClearer;
protected function setUp() : void {
parent::setUp();
$this->configFactory = $this->container
->get('config.factory');
$this->pluginCacheClearer = $this->container
->get('plugin.cache_clearer');
}
protected function getEntitiesDataArray() {
return [
'node' => [
'en' => [
'es_test' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
'title' => [
'value' => 'Test title',
'checker_callback' => 'getValue',
],
'field_es_test_metatag' => [
'value' => serialize([
'abstract' => 'test abstract',
]),
'checker_callback' => 'getValue',
],
]),
],
],
];
}
public function testMetatagFieldEnhancer() {
$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
->importData();
$node = $this
->loadEntity('node', 'es_test');
$node_metatags = unserialize($node
->get('field_es_test_metatag')
->getValue()[0]['value']);
$expected_metatags = [
'abstract' => 'test abstract',
];
$node
->delete();
$this
->assertEquals($expected_metatags, $node_metatags, 'The node has the expected metatags.');
$this->remoteManager
->resetResponseMapping();
$this
->prepareContent();
$node = $this
->loadEntity('node', 'es_test');
$node_title = $node
->label();
$node_url = $node
->toUrl('canonical')
->setAbsolute()
->toString();
$expected_metatags = [
'canonical_url' => $node_url,
'title' => $node_title . ' | Drupal',
'abstract' => 'test abstract',
];
$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();
$this
->importData();
$node = $this
->loadEntity('node', 'es_test');
$node_metatags = unserialize($node
->get('field_es_test_metatag')
->getValue()[0]['value']);
$node
->delete();
$this
->assertEquals($expected_metatags, $node_metatags, 'The node has the expected metatags.');
$this->remoteManager
->resetResponseMapping();
$this
->prepareContent();
$node = $this
->loadEntity('node', 'es_test');
$node_title = $node
->label();
$node_url = $node
->toUrl('canonical')
->setAbsolute()
->toString();
$expected_metatags = [
'canonical_url' => $node_url,
'title' => $node_title . ' | Drupal',
'abstract' => 'test abstract',
];
$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();
$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.');
}
protected function importData() {
$channel_infos = $this->remoteManager
->getChannelsInfos($this->remote);
$channel_url = $channel_infos['node_es_test_en']['url'];
$response = $this->remoteManager
->jsonApiRequest($this->remote, 'GET', $channel_url);
$json = Json::decode((string) $response
->getBody());
$this
->deleteContent();
$this->entities = [];
$import_context = new ImportContext($this->remote
->id(), 'node_es_test_en', $this::IMPORT_CONFIG_ID);
$this->importService
->prepareImport($import_context);
$this->importService
->importEntityListData(EntityShareUtility::prepareData($json['data']));
}
}