You are here

trait PathautoTestHelperTrait in Pathauto 8

Helper test class with some added functions for testing.

Hierarchy

4 files declare their use of PathautoTestHelperTrait
PathautoEntityWithStringIdTest.php in tests/src/Kernel/PathautoEntityWithStringIdTest.php
PathautoKernelTest.php in tests/src/Kernel/PathautoKernelTest.php
PathautoLocaleTest.php in tests/src/FunctionalJavascript/PathautoLocaleTest.php
PathautoUiTest.php in tests/src/FunctionalJavascript/PathautoUiTest.php

File

tests/src/Functional/PathautoTestHelperTrait.php, line 18

Namespace

Drupal\Tests\pathauto\Functional
View source
trait PathautoTestHelperTrait {
  use PathAliasTestTrait;

  /**
   * Creates a pathauto pattern.
   *
   * @param string $entity_type_id
   *   The entity type.
   * @param string $pattern
   *   The path pattern.
   * @param int $weight
   *   (optional) The pattern weight.
   *
   * @return \Drupal\pathauto\PathautoPatternInterface
   *   The created pattern.
   */
  protected function createPattern($entity_type_id, $pattern, $weight = 10) {
    $type = $entity_type_id == 'forum' ? 'forum' : 'canonical_entities:' . $entity_type_id;
    $pattern = PathautoPattern::create([
      'id' => mb_strtolower($this
        ->randomMachineName()),
      'type' => $type,
      'pattern' => $pattern,
      'weight' => $weight,
    ]);
    $pattern
      ->save();
    return $pattern;
  }

  /**
   * Add a bundle condition to a pathauto pattern.
   *
   * @param \Drupal\pathauto\PathautoPatternInterface $pattern
   *   The pattern.
   * @param string $entity_type
   *   The entity type ID.
   * @param string $bundle
   *   The bundle.
   */
  protected function addBundleCondition(PathautoPatternInterface $pattern, $entity_type, $bundle) {
    $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type;
    $pattern
      ->addSelectionCondition([
      'id' => $plugin_id,
      'bundles' => [
        $bundle => $bundle,
      ],
      'negate' => FALSE,
      'context_mapping' => [
        $entity_type => $entity_type,
      ],
    ]);
  }
  public function assertToken($type, $object, $token, $expected) {
    $bubbleable_metadata = new BubbleableMetadata();
    $tokens = \Drupal::token()
      ->generate($type, [
      $token => $token,
    ], [
      $type => $object,
    ], [], $bubbleable_metadata);
    $tokens += [
      $token => '',
    ];
    $this
      ->assertSame($tokens[$token], $expected, t("Token value for [@type:@token] was '@actual', expected value '@expected'.", [
      '@type' => $type,
      '@token' => $token,
      '@actual' => $tokens[$token],
      '@expected' => $expected,
    ]));
  }
  public function saveEntityAlias(EntityInterface $entity, $alias, $langcode = NULL) {

    // By default, use the entity language.
    if (!$langcode) {
      $langcode = $entity
        ->language()
        ->getId();
    }
    return $this
      ->createPathAlias('/' . $entity
      ->toUrl()
      ->getInternalPath(), $alias, $langcode);
  }
  public function assertEntityAlias(EntityInterface $entity, $expected_alias, $langcode = NULL) {

    // By default, use the entity language.
    if (!$langcode) {
      $langcode = $entity
        ->language()
        ->getId();
    }
    $this
      ->assertAlias('/' . $entity
      ->toUrl()
      ->getInternalPath(), $expected_alias, $langcode);
  }
  public function assertEntityAliasExists(EntityInterface $entity) {
    return $this
      ->assertAliasExists([
      'path' => '/' . $entity
        ->toUrl()
        ->getInternalPath(),
    ]);
  }
  public function assertNoEntityAlias(EntityInterface $entity, $langcode = NULL) {

    // By default, use the entity language.
    if (!$langcode) {
      $langcode = $entity
        ->language()
        ->getId();
    }
    $this
      ->assertEntityAlias($entity, '/' . $entity
      ->toUrl()
      ->getInternalPath(), $langcode);
  }
  public function assertNoEntityAliasExists(EntityInterface $entity, $alias = NULL) {
    $path = [
      'path' => '/' . $entity
        ->toUrl()
        ->getInternalPath(),
    ];
    if (!empty($alias)) {
      $path['alias'] = $alias;
    }
    $this
      ->assertNoAliasExists($path);
  }
  public function assertAlias($source, $expected_alias, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
    \Drupal::service('path_alias.manager')
      ->cacheClear($source);
    $entity_type_manager = \Drupal::entityTypeManager();
    if ($entity_type_manager
      ->hasDefinition('path_alias')) {
      $entity_type_manager
        ->getStorage('path_alias')
        ->resetCache();
    }
    $this
      ->assertEquals($expected_alias, \Drupal::service('path_alias.manager')
      ->getAliasByPath($source, $langcode), t("Alias for %source with language '@language' is correct.", [
      '%source' => $source,
      '@language' => $langcode,
    ]));
  }
  public function assertAliasExists($conditions) {
    $path = $this
      ->loadPathAliasByConditions($conditions);
    $this
      ->assertNotEmpty($path, t('Alias with conditions @conditions found.', [
      '@conditions' => var_export($conditions, TRUE),
    ]));
    return $path;
  }
  public function assertNoAliasExists($conditions) {
    $alias = $this
      ->loadPathAliasByConditions($conditions);
    $this
      ->assertEmpty($alias, t('Alias with conditions @conditions not found.', [
      '@conditions' => var_export($conditions, TRUE),
    ]));
  }
  public function deleteAllAliases() {
    \Drupal::service('pathauto.alias_storage_helper')
      ->deleteAll();
    \Drupal::service('path_alias.manager')
      ->cacheClear();
  }

  /**
   * @param array $values
   *
   * @return \Drupal\taxonomy\VocabularyInterface
   */
  public function addVocabulary(array $values = []) {
    $name = mb_strtolower($this
      ->randomMachineName(5));
    $values += [
      'name' => $name,
      'vid' => $name,
    ];
    $vocabulary = Vocabulary::create($values);
    $vocabulary
      ->save();
    return $vocabulary;
  }
  public function addTerm(VocabularyInterface $vocabulary, array $values = []) {
    $values += [
      'name' => mb_strtolower($this
        ->randomMachineName(5)),
      'vid' => $vocabulary
        ->id(),
    ];
    $term = Term::create($values);
    $term
      ->save();
    return $term;
  }
  public function assertEntityPattern($entity_type, $bundle, $langcode, $expected) {
    $values = [
      'langcode' => $langcode,
      \Drupal::entityTypeManager()
        ->getDefinition($entity_type)
        ->getKey('bundle') => $bundle,
    ];
    $entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->create($values);
    $pattern = \Drupal::service('pathauto.generator')
      ->getPatternByEntity($entity);
    $this
      ->assertSame($expected, $pattern
      ->getPattern());
  }
  public function drupalGetTermByName($name, $reset = FALSE) {
    if ($reset) {

      // @todo - implement cache reset.
    }
    $terms = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_term')
      ->loadByProperties([
      'name' => $name,
    ]);
    return !empty($terms) ? reset($terms) : FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathAliasTestTrait::assertPathAliasExists protected function Asserts that a path alias exists in the storage.
PathAliasTestTrait::assertPathAliasNotExists protected function Asserts that a path alias does not exist in the storage.
PathAliasTestTrait::createPathAlias protected function Creates a new path alias.
PathAliasTestTrait::loadPathAliasByConditions protected function Gets the first result from a 'load by properties' storage call.
PathautoTestHelperTrait::addBundleCondition protected function Add a bundle condition to a pathauto pattern.
PathautoTestHelperTrait::addTerm public function
PathautoTestHelperTrait::addVocabulary public function
PathautoTestHelperTrait::assertAlias public function
PathautoTestHelperTrait::assertAliasExists public function
PathautoTestHelperTrait::assertEntityAlias public function
PathautoTestHelperTrait::assertEntityAliasExists public function
PathautoTestHelperTrait::assertEntityPattern public function
PathautoTestHelperTrait::assertNoAliasExists public function
PathautoTestHelperTrait::assertNoEntityAlias public function
PathautoTestHelperTrait::assertNoEntityAliasExists public function
PathautoTestHelperTrait::assertToken public function
PathautoTestHelperTrait::createPattern protected function Creates a pathauto pattern.
PathautoTestHelperTrait::deleteAllAliases public function
PathautoTestHelperTrait::drupalGetTermByName public function
PathautoTestHelperTrait::saveEntityAlias public function