You are here

protected function PathautoTest::pathautoTestSetup in Entity Share 8.3

Same name and namespace in other branches
  1. 8.2 modules/entity_share_client/tests/src/Functional/PathautoTest.php \Drupal\Tests\entity_share_client\Functional\PathautoTest::pathautoTestSetup()

Helper function.

Parameters

string $behavior: The behavior of the pathauto field enhancer plugin.

1 call to PathautoTest::pathautoTestSetup()
PathautoTest::testPathautoFieldEnhancer in modules/entity_share_client/tests/src/Functional/PathautoTest.php
Test Pathauto resource field enhancer plugin.

File

modules/entity_share_client/tests/src/Functional/PathautoTest.php, line 145

Class

PathautoTest
General functional test class for path field.

Namespace

Drupal\Tests\entity_share_client\Functional

Code

protected function pathautoTestSetup($behavior) {
  if (empty($this->entityTypeManager
    ->getStorage('jsonapi_resource_config')
    ->load('node--es_test'))) {

    // This is the first run.
    $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 is not the first run, so certain actions should be done
    // to clean up the data from previous run.
    // 1. Clear "client" pathauto pattern configurations because of
    // the next test.
    $this
      ->deletePathautoPatterns();

    // 2. Reset "remote" response mapping.
    $this->remoteManager
      ->resetResponseMapping();

    // Alter the plugin definition of Path enhancer by changing the behavior.
    $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();
  }

  /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $pathauto_pattern_storage */
  $pathauto_pattern_storage = $this->entityTypeManager
    ->getStorage('pathauto_pattern');

  /** @var \Drupal\pathauto\PathautoPatternInterface $pattern */
  $pattern = $pathauto_pattern_storage
    ->create([
    'id' => 'server',
    'label' => 'Test',
    'type' => 'canonical_entities:node',
    'pattern' => 'server/[node:title]',
  ]);
  $pattern
    ->save();
  $this
    ->prepareContent();

  // (Re-)import data from JSON:API.
  $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();

  /** @var \Drupal\pathauto\PathautoPatternInterface $pattern */
  $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']));
}