Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / pathauto / src / Tests / PathautoTestHelperTrait.php
1 <?php
2
3 namespace Drupal\pathauto\Tests;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Language\Language;
7 use Drupal\Core\Render\BubbleableMetadata;
8 use Drupal\pathauto\Entity\PathautoPattern;
9 use Drupal\pathauto\PathautoPatternInterface;
10 use Drupal\taxonomy\VocabularyInterface;
11 use Drupal\taxonomy\Entity\Vocabulary;
12 use Drupal\taxonomy\Entity\Term;
13
14 /**
15  * Helper test class with some added functions for testing.
16  */
17 trait PathautoTestHelperTrait {
18
19   /**
20    * Creates a pathauto pattern.
21    *
22    * @param string $entity_type_id
23    *   The entity type.
24    * @param string $pattern
25    *   The path pattern.
26    * @param int $weight
27    *   (optional) The pattern weight.
28    *
29    * @return \Drupal\pathauto\PathautoPatternInterface
30    *   The created pattern.
31    */
32   protected function createPattern($entity_type_id, $pattern, $weight = 10) {
33     $type = ($entity_type_id == 'forum') ? 'forum' : 'canonical_entities:' . $entity_type_id;
34
35     $pattern = PathautoPattern::create([
36       'id' => mb_strtolower($this->randomMachineName()),
37       'type' => $type,
38       'pattern' => $pattern,
39       'weight' => $weight,
40     ]);
41     $pattern->save();
42     return $pattern;
43   }
44
45   /**
46    * Add a bundle condition to a pathauto pattern.
47    *
48    * @param \Drupal\pathauto\PathautoPatternInterface $pattern
49    *   The pattern.
50    * @param string $entity_type
51    *   The entity type ID.
52    * @param string $bundle
53    *   The bundle.
54    */
55   protected function addBundleCondition(PathautoPatternInterface $pattern, $entity_type, $bundle) {
56     $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type;
57
58     $pattern->addSelectionCondition(
59       [
60         'id' => $plugin_id,
61         'bundles' => [
62           $bundle => $bundle,
63         ],
64         'negate' => FALSE,
65         'context_mapping' => [
66           $entity_type => $entity_type,
67         ]
68       ]
69     );
70   }
71
72   public function assertToken($type, $object, $token, $expected) {
73     $bubbleable_metadata = new BubbleableMetadata();
74     $tokens = \Drupal::token()->generate($type, array($token => $token), array($type => $object), [], $bubbleable_metadata);
75     $tokens += array($token => '');
76     $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)));
77   }
78
79   public function saveAlias($source, $alias, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
80     \Drupal::service('path.alias_storage')->delete(array('source' => $source, 'language', 'langcode' => $langcode));
81     return \Drupal::service('path.alias_storage')->save($source, $alias, $langcode);
82   }
83
84   public function saveEntityAlias(EntityInterface $entity, $alias, $langcode = NULL) {
85     // By default, use the entity language.
86     if (!$langcode) {
87       $langcode = $entity->language()->getId();
88     }
89     return $this->saveAlias('/' . $entity->toUrl()->getInternalPath(), $alias, $langcode);
90   }
91
92   public function assertEntityAlias(EntityInterface $entity, $expected_alias, $langcode = NULL) {
93     // By default, use the entity language.
94     if (!$langcode) {
95       $langcode = $entity->language()->getId();
96     }
97     $this->assertAlias('/' . $entity->toUrl()->getInternalPath(), $expected_alias, $langcode);
98   }
99
100   public function assertEntityAliasExists(EntityInterface $entity) {
101     return $this->assertAliasExists(array('source' => '/' . $entity->toUrl()->getInternalPath()));
102   }
103
104   public function assertNoEntityAlias(EntityInterface $entity, $langcode = NULL) {
105     // By default, use the entity language.
106     if (!$langcode) {
107       $langcode = $entity->language()->getId();
108     }
109     $this->assertEntityAlias($entity, '/' . $entity->toUrl()->getInternalPath(), $langcode);
110   }
111
112   public function assertNoEntityAliasExists(EntityInterface $entity, $alias = NULL) {
113     $path = array('source' => '/' . $entity->toUrl()->getInternalPath());
114     if (!empty($alias)) {
115       $path['alias'] = $alias;
116     }
117     $this->assertNoAliasExists($path);
118   }
119
120   public function assertAlias($source, $expected_alias, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
121     \Drupal::service('path.alias_manager')->cacheClear($source);
122     $this->assertEqual($expected_alias, \Drupal::service('path.alias_manager')->getAliasByPath($source, $langcode), t("Alias for %source with language '@language' is correct.",
123       array('%source' => $source, '@language' => $langcode)));
124   }
125
126   public function assertAliasExists($conditions) {
127     $path = \Drupal::service('path.alias_storage')->load($conditions);
128     $this->assertTrue($path, t('Alias with conditions @conditions found.', array('@conditions' => var_export($conditions, TRUE))));
129     return $path;
130   }
131
132   public function assertNoAliasExists($conditions) {
133     $alias = \Drupal::service('path.alias_storage')->load($conditions);
134     $this->assertFalse($alias, t('Alias with conditions @conditions not found.', array('@conditions' => var_export($conditions, TRUE))));
135   }
136
137   public function deleteAllAliases() {
138     \Drupal::database()->delete('url_alias')->execute();
139     \Drupal::service('path.alias_manager')->cacheClear();
140   }
141
142   /**
143    * @param array $values
144    *
145    * @return \Drupal\taxonomy\VocabularyInterface
146    */
147   public function addVocabulary(array $values = array()) {
148     $name = mb_strtolower($this->randomMachineName(5));
149     $values += array(
150       'name' => $name,
151       'vid' => $name,
152     );
153     $vocabulary = Vocabulary::create($values);
154     $vocabulary->save();
155
156     return $vocabulary;
157   }
158
159   public function addTerm(VocabularyInterface $vocabulary, array $values = array()) {
160     $values += array(
161       'name' => mb_strtolower($this->randomMachineName(5)),
162       'vid' => $vocabulary->id(),
163     );
164
165     $term = Term::create($values);
166     $term->save();
167     return $term;
168   }
169
170   public function assertEntityPattern($entity_type, $bundle, $langcode = Language::LANGCODE_NOT_SPECIFIED, $expected) {
171
172     $values = [
173       'langcode' => $langcode,
174       \Drupal::entityTypeManager()->getDefinition($entity_type)->getKey('bundle') => $bundle,
175     ];
176     $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
177
178     $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
179     $this->assertIdentical($expected, $pattern->getPattern());
180   }
181
182   public function drupalGetTermByName($name, $reset = FALSE) {
183     if ($reset) {
184       // @todo - implement cache reset.
185     }
186     $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(array('name' => $name));
187     return !empty($terms) ? reset($terms) : FALSE;
188   }
189
190 }