View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\entity_share\Plugin\jsonapi\FieldEnhancer\EntitySharePathautoEnhancer;
use Drupal\node\NodeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\entity_share\EntityShareUtility;
use Drupal\entity_share_client\ImportContext;
class PathautoTest extends EntityShareClientFunctionalTestBase {
public static $modules = [
'jsonapi_extras',
'pathauto',
];
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_path_auto' => $this
->getCompleteNodeInfos([
'title' => [
'value' => 'Automatic',
'checker_callback' => 'getValue',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_path_manual' => $this
->getCompleteNodeInfos([
'title' => [
'value' => 'Manual',
'checker_callback' => 'getValue',
],
'path' => [
'value' => [
[
'alias' => '/manual_path',
'pathauto' => 0,
],
],
'checker_callback' => 'getValue',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
],
],
];
}
public function testPathautoFieldEnhancer() {
$this
->pathautoTestSetup(EntitySharePathautoEnhancer::EXPOSE_CURRENT_PATHAUTO);
$this
->assetEntityPath('es_test_path_auto', '/client/automatic', 'As the pathauto state is preserved, the client website has generated an alias based on its own pathauto pattern.');
$this
->assetEntityPath('es_test_path_manual', '/manual_path', 'As the pathauto state is preserved, the client website has not generated an alias and has used the one provided by the server website.');
$this
->pathautoTestSetup(EntitySharePathautoEnhancer::FORCE_ENABLE_PATHAUTO);
$this
->assetEntityPath('es_test_path_auto', '/client/automatic', 'As the pathauto state is forced to be on, the client website has generated an alias based on its own pathauto pattern.');
$this
->assetEntityPath('es_test_path_manual', '/client/manual', 'As the pathauto state is forced to be on, the client website has generated an alias based on its own pathauto pattern.');
$this
->pathautoTestSetup(EntitySharePathautoEnhancer::FORCE_DISABLE_PATHAUTO);
$this
->assetEntityPath('es_test_path_auto', '/server/automatic', 'As the pathauto state is forced to be off, the client website has not generated an alias and has used the one provided by the server (automatically created on the server website).');
$this
->assetEntityPath('es_test_path_manual', '/manual_path', 'As the pathauto state is forced to be off, the client website has not generated an alias and has used the one provided by the server (manually created on the server website).');
}
protected function deletePathautoPatterns() {
$pathauto_patterns = $this->entityTypeManager
->getStorage('pathauto_pattern')
->loadMultiple();
foreach ($pathauto_patterns as $pathauto_pattern) {
$pathauto_pattern
->delete();
}
}
protected function pathautoTestSetup($behavior) {
if (empty($this->entityTypeManager
->getStorage('jsonapi_resource_config')
->load('node--es_test'))) {
$this->entityTypeManager
->getStorage('jsonapi_resource_config')
->create([
'id' => 'node--es_test',
'disabled' => FALSE,
'path' => 'node/es_test',
'resourceType' => 'node--es_test',
'resourceFields' => [
'path' => [
'fieldName' => 'path',
'publicName' => 'path',
'enhancer' => [
'id' => 'entity_share_pathauto',
'settings' => [
'behavior' => $behavior,
],
],
'disabled' => FALSE,
],
],
])
->save();
}
else {
$this
->deletePathautoPatterns();
$this->remoteManager
->resetResponseMapping();
$resource_config = $this->configFactory
->getEditable('jsonapi_extras.jsonapi_resource_config.node--es_test');
$resource_fields = $resource_config
->get('resourceFields');
$resource_fields['path']['enhancer']['settings']['behavior'] = $behavior;
$resource_config
->set('resourceFields', $resource_fields)
->save();
$this->pluginCacheClearer
->clearCachedDefinitions();
}
$pathauto_pattern_storage = $this->entityTypeManager
->getStorage('pathauto_pattern');
$pattern = $pathauto_pattern_storage
->create([
'id' => 'server',
'label' => 'Test',
'type' => 'canonical_entities:node',
'pattern' => 'server/[node:title]',
]);
$pattern
->save();
$this
->prepareContent();
$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 = [];
$this
->deletePathautoPatterns();
$pattern = $pathauto_pattern_storage
->create([
'id' => 'client',
'label' => 'Test',
'type' => 'canonical_entities:node',
'pattern' => 'client/[node:title]',
]);
$pattern
->save();
$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']));
}
protected function assetEntityPath($entity_uuid, $expected_path, $message = '') {
$path_auto_node = $this
->loadEntity('node', $entity_uuid);
$path = $path_auto_node
->get('path')
->getValue();
$this
->assertEquals($expected_path, $path[0]['alias'], $message);
$path_auto_node
->delete();
}
}