3 namespace Drupal\pathauto\Tests;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Language\Language;
8 use Drupal\Core\Render\BubbleableMetadata;
9 use Drupal\pathauto\Entity\PathautoPattern;
10 use Drupal\pathauto\PathautoPatternInterface;
11 use Drupal\taxonomy\VocabularyInterface;
12 use Drupal\taxonomy\Entity\Vocabulary;
13 use Drupal\taxonomy\Entity\Term;
16 * Helper test class with some added functions for testing.
18 trait PathautoTestHelperTrait {
21 * Creates a pathauto pattern.
23 * @param string $entity_type_id
25 * @param string $pattern
28 * (optional) The pattern weight.
30 * @return \Drupal\pathauto\PathautoPatternInterface
31 * The created pattern.
33 protected function createPattern($entity_type_id, $pattern, $weight = 10) {
34 $type = ($entity_type_id == 'forum') ? 'forum' : 'canonical_entities:' . $entity_type_id;
36 $pattern = PathautoPattern::create([
37 'id' => Unicode::strtolower($this->randomMachineName()),
39 'pattern' => $pattern,
47 * Add a bundle condition to a pathauto pattern.
49 * @param \Drupal\pathauto\PathautoPatternInterface $pattern
51 * @param string $entity_type
53 * @param string $bundle
56 protected function addBundleCondition(PathautoPatternInterface $pattern, $entity_type, $bundle) {
57 $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type;
59 $pattern->addSelectionCondition(
66 'context_mapping' => [
67 $entity_type => $entity_type,
73 public function assertToken($type, $object, $token, $expected) {
74 $bubbleable_metadata = new BubbleableMetadata();
75 $tokens = \Drupal::token()->generate($type, array($token => $token), array($type => $object), [], $bubbleable_metadata);
76 $tokens += array($token => '');
77 $this->assertIdentical($tokens[$token], $expected, t("Token value for [@type:@token] was '@actual', expected value '@expected'.", array('@type' => $type, '@token' => $token, '@actual' => $tokens[$token], '@expected' => $expected)));
80 public function saveAlias($source, $alias, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
81 \Drupal::service('path.alias_storage')->delete(array('source' => $source, 'language', 'langcode' => $langcode));
82 return \Drupal::service('path.alias_storage')->save($source, $alias, $langcode);
85 public function saveEntityAlias(EntityInterface $entity, $alias, $langcode = NULL) {
86 // By default, use the entity language.
88 $langcode = $entity->language()->getId();
90 return $this->saveAlias('/' . $entity->toUrl()->getInternalPath(), $alias, $langcode);
93 public function assertEntityAlias(EntityInterface $entity, $expected_alias, $langcode = NULL) {
94 // By default, use the entity language.
96 $langcode = $entity->language()->getId();
98 $this->assertAlias('/' . $entity->toUrl()->getInternalPath(), $expected_alias, $langcode);
101 public function assertEntityAliasExists(EntityInterface $entity) {
102 return $this->assertAliasExists(array('source' => '/' . $entity->toUrl()->getInternalPath()));
105 public function assertNoEntityAlias(EntityInterface $entity, $langcode = NULL) {
106 // By default, use the entity language.
108 $langcode = $entity->language()->getId();
110 $this->assertEntityAlias($entity, '/' . $entity->toUrl()->getInternalPath(), $langcode);
113 public function assertNoEntityAliasExists(EntityInterface $entity, $alias = NULL) {
114 $path = array('source' => '/' . $entity->toUrl()->getInternalPath());
115 if (!empty($alias)) {
116 $path['alias'] = $alias;
118 $this->assertNoAliasExists($path);
121 public function assertAlias($source, $expected_alias, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
122 \Drupal::service('path.alias_manager')->cacheClear($source);
123 $this->assertEqual($expected_alias, \Drupal::service('path.alias_manager')->getAliasByPath($source, $langcode), t("Alias for %source with language '@language' is correct.",
124 array('%source' => $source, '@language' => $langcode)));
127 public function assertAliasExists($conditions) {
128 $path = \Drupal::service('path.alias_storage')->load($conditions);
129 $this->assertTrue($path, t('Alias with conditions @conditions found.', array('@conditions' => var_export($conditions, TRUE))));
133 public function assertNoAliasExists($conditions) {
134 $alias = \Drupal::service('path.alias_storage')->load($conditions);
135 $this->assertFalse($alias, t('Alias with conditions @conditions not found.', array('@conditions' => var_export($conditions, TRUE))));
138 public function deleteAllAliases() {
139 \Drupal::database()->delete('url_alias')->execute();
140 \Drupal::service('path.alias_manager')->cacheClear();
144 * @param array $values
145 * @return \Drupal\taxonomy\VocabularyInterface
147 public function addVocabulary(array $values = array()) {
148 $name = Unicode::strtolower($this->randomMachineName(5));
153 $vocabulary = Vocabulary::create($values);
159 public function addTerm(VocabularyInterface $vocabulary, array $values = array()) {
161 'name' => Unicode::strtolower($this->randomMachineName(5)),
162 'vid' => $vocabulary->id(),
165 $term = Term::create($values);
170 public function assertEntityPattern($entity_type, $bundle, $langcode = Language::LANGCODE_NOT_SPECIFIED, $expected) {
173 'langcode' => $langcode,
174 \Drupal::entityTypeManager()->getDefinition($entity_type)->getKey('bundle') => $bundle,
176 $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
178 $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
179 $this->assertIdentical($expected, $pattern->getPattern());
182 public function drupalGetTermByName($name, $reset = FALSE) {
184 // @todo - implement cache reset.
186 $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(array('name' => $name));
187 return !empty($terms) ? reset($terms) : FALSE;