You are here

public function PathautoKernelTest::testPatternLoadByEntity in Pathauto 8

Test pathauto_pattern_load_by_entity().

File

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

Class

PathautoKernelTest
Unit tests for Pathauto functions.

Namespace

Drupal\Tests\pathauto\Kernel

Code

public function testPatternLoadByEntity() {
  $pattern = $this
    ->createPattern('node', '/article/[node:title]', -1);
  $this
    ->addBundleCondition($pattern, 'node', 'article');
  $pattern
    ->save();
  $pattern = $this
    ->createPattern('node', '/article/en/[node:title]', -2);
  $this
    ->addBundleCondition($pattern, 'node', 'article');
  $pattern
    ->addSelectionCondition([
    'id' => 'language',
    'langcodes' => [
      'en' => 'en',
    ],
    'negate' => FALSE,
    'context_mapping' => [
      'language' => 'node:langcode:language',
    ],
  ]);
  $pattern
    ->addRelationship('node:langcode:language');
  $pattern
    ->save();
  $pattern = $this
    ->createPattern('node', '/[node:title]', -1);
  $this
    ->addBundleCondition($pattern, 'node', 'page');
  $pattern
    ->save();
  $tests = [
    [
      'entity' => 'node',
      'values' => [
        'title' => 'Article fr',
        'type' => 'article',
        'langcode' => 'fr',
      ],
      'expected' => '/article/[node:title]',
    ],
    [
      'entity' => 'node',
      'values' => [
        'title' => 'Article en',
        'type' => 'article',
        'langcode' => 'en',
      ],
      'expected' => '/article/en/[node:title]',
    ],
    [
      'entity' => 'node',
      'values' => [
        'title' => 'Article und',
        'type' => 'article',
        'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ],
      'expected' => '/article/[node:title]',
    ],
    [
      'entity' => 'node',
      'values' => [
        'title' => 'Page',
        'type' => 'page',
      ],
      'expected' => '/[node:title]',
    ],
    [
      'entity' => 'user',
      'values' => [
        'name' => 'User',
      ],
      'expected' => '/users/[user:name]',
    ],
  ];
  foreach ($tests as $test) {
    $entity = \Drupal::entityTypeManager()
      ->getStorage($test['entity'])
      ->create($test['values']);
    $entity
      ->save();
    $actual = \Drupal::service('pathauto.generator')
      ->getPatternByEntity($entity);
    $this
      ->assertSame($actual
      ->getPattern(), $test['expected'], t("Correct pattern returned for @entity_type with @values", [
      '@entity' => $test['entity'],
      '@values' => print_r($test['values'], TRUE),
    ]));
  }
}