You are here

public function PathautoKernelTest::testParentChildPathTokens in Pathauto 8

Test using fields for path structures.

File

tests/src/Kernel/PathautoKernelTest.php, line 374

Class

PathautoKernelTest
Unit tests for Pathauto functions.

Namespace

Drupal\Tests\pathauto\Kernel

Code

public function testParentChildPathTokens() {

  // First create a field which will be used to create the path. It must
  // begin with a letter.
  $this
    ->installEntitySchema('taxonomy_term');
  Vocabulary::create([
    'vid' => 'tags',
  ])
    ->save();
  $fieldname = 'a' . mb_strtolower($this
    ->randomMachineName());
  $field_storage = FieldStorageConfig::create([
    'entity_type' => 'taxonomy_term',
    'field_name' => $fieldname,
    'type' => 'string',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'tags',
  ]);
  $field
    ->save();
  $display = \Drupal::service('entity_display.repository')
    ->getViewDisplay('taxonomy_term', 'tags');
  $display
    ->setComponent($fieldname, [
    'type' => 'string',
  ]);
  $display
    ->save();

  // Make the path pattern of a field use the value of this field appended
  // to the parent taxonomy term's pattern if there is one.
  $this
    ->createPattern('taxonomy_term', '/[term:parents:join-path]/[term:' . $fieldname . ']');

  // Start by creating a parent term.
  $parent = Term::create([
    'vid' => 'tags',
    $fieldname => $this
      ->randomMachineName(),
    'name' => $this
      ->randomMachineName(),
  ]);
  $parent
    ->save();

  // Create the child term.
  $child = Term::create([
    'vid' => 'tags',
    $fieldname => $this
      ->randomMachineName(),
    'parent' => $parent,
    'name' => $this
      ->randomMachineName(),
  ]);
  $child
    ->save();
  $this
    ->assertEntityAlias($child, '/' . mb_strtolower($parent
    ->getName() . '/' . $child->{$fieldname}->value));

  // Re-saving the parent term should not modify the child term's alias.
  $parent
    ->save();
  $this
    ->assertEntityAlias($child, '/' . mb_strtolower($parent
    ->getName() . '/' . $child->{$fieldname}->value));
}