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;
class PathautoTest extends EntityShareClientFunctionalTestBase {
public static $modules = [
'jsonapi_extras',
'pathauto',
];
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
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 testExposeCurrentState() {
$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.');
}
public function testForceEnable() {
$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.');
}
public function testForceDisable() {
$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 deleteContent() {
parent::deleteContent();
$pathauto_patterns = $this->entityTypeManager
->getStorage('pathauto_pattern')
->loadMultiple();
foreach ($pathauto_patterns as $pathauto_pattern) {
$pathauto_pattern
->delete();
}
}
protected function pathautoTestSetup($behavior) {
$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();
$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();
$this
->populateRequestService();
$this
->deleteContent();
$pattern = $pathauto_pattern_storage
->create([
'id' => 'client',
'label' => 'Test',
'type' => 'canonical_entities:node',
'pattern' => 'client/[node:title]',
]);
$pattern
->save();
$this
->pullEveryChannels();
}
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);
}
}