Version 1
[yaffs-website] / web / modules / contrib / pathauto / src / Tests / PathautoTestHelperTrait.php
1 <?php
2
3 namespace Drupal\pathauto\Tests;
4
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;
14
15 /**
16  * Helper test class with some added functions for testing.
17  */
18 trait PathautoTestHelperTrait {
19
20   /**
21    * Creates a pathauto pattern.
22    *
23    * @param string $entity_type_id
24    *   The entity type.
25    * @param string $pattern
26    *   The path pattern.
27    * @param int $weight
28    *   (optional) The pattern weight.
29    *
30    * @return \Drupal\pathauto\PathautoPatternInterface
31    *   The created pattern.
32    */
33   protected function createPattern($entity_type_id, $pattern, $weight = 10) {
34     $type = ($entity_type_id == 'forum') ? 'forum' : 'canonical_entities:' . $entity_type_id;
35
36     $pattern = PathautoPattern::create([
37       'id' => Unicode::strtolower($this->randomMachineName()),
38       'type' => $type,
39       'pattern' => $pattern,
40       'weight' => $weight,
41     ]);
42     $pattern->save();
43     return $pattern;
44   }
45
46   /**
47    * Add a bundle condition to a pathauto pattern.
48    *
49    * @param \Drupal\pathauto\PathautoPatternInterface $pattern
50    *   The pattern.
51    * @param string $entity_type
52    *   The entity type ID.
53    * @param string $bundle
54    *   The bundle
55    */
56   protected function addBundleCondition(PathautoPatternInterface $pattern, $entity_type, $bundle) {
57     $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type;
58
59     $pattern->addSelectionCondition(
60       [
61         'id' => $plugin_id,
62         'bundles' => [
63           $bundle => $bundle,
64         ],
65         'negate' => FALSE,
66         'context_mapping' => [
67           $entity_type => $entity_type,
68         ]
69       ]
70     );
71   }
72
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)));
78   }
79
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);
83   }
84
85   public function saveEntityAlias(EntityInterface $entity, $alias, $langcode = NULL) {
86     // By default, use the entity language.
87     if (!$langcode) {
88       $langcode = $entity->language()->getId();
89     }
90     return $this->saveAlias('/' . $entity->toUrl()->getInternalPath(), $alias, $langcode);
91   }
92
93   public function assertEntityAlias(EntityInterface $entity, $expected_alias, $langcode = NULL) {
94     // By default, use the entity language.
95     if (!$langcode) {
96       $langcode = $entity->language()->getId();
97     }
98     $this->assertAlias('/' . $entity->toUrl()->getInternalPath(), $expected_alias, $langcode);
99   }
100
101   public function assertEntityAliasExists(EntityInterface $entity) {
102     return $this->assertAliasExists(array('source' => '/' . $entity->toUrl()->getInternalPath()));
103   }
104
105   public function assertNoEntityAlias(EntityInterface $entity, $langcode = NULL) {
106     // By default, use the entity language.
107     if (!$langcode) {
108       $langcode = $entity->language()->getId();
109     }
110     $this->assertEntityAlias($entity, '/' . $entity->toUrl()->getInternalPath(), $langcode);
111   }
112
113   public function assertNoEntityAliasExists(EntityInterface $entity, $alias = NULL) {
114     $path = array('source' => '/' . $entity->toUrl()->getInternalPath());
115     if (!empty($alias)) {
116       $path['alias'] = $alias;
117     }
118     $this->assertNoAliasExists($path);
119   }
120
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)));
125   }
126
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))));
130     return $path;
131   }
132
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))));
136   }
137
138   public function deleteAllAliases() {
139     \Drupal::database()->delete('url_alias')->execute();
140     \Drupal::service('path.alias_manager')->cacheClear();
141   }
142
143   /**
144    * @param array $values
145    * @return \Drupal\taxonomy\VocabularyInterface
146    */
147   public function addVocabulary(array $values = array()) {
148     $name = Unicode::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' => Unicode::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 }