3 namespace Drupal\pathauto\Tests;
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;
15 * Helper test class with some added functions for testing.
17 trait PathautoTestHelperTrait {
20 * Creates a pathauto pattern.
22 * @param string $entity_type_id
24 * @param string $pattern
27 * (optional) The pattern weight.
29 * @return \Drupal\pathauto\PathautoPatternInterface
30 * The created pattern.
32 protected function createPattern($entity_type_id, $pattern, $weight = 10) {
33 $type = ($entity_type_id == 'forum') ? 'forum' : 'canonical_entities:' . $entity_type_id;
35 $pattern = PathautoPattern::create([
36 'id' => mb_strtolower($this->randomMachineName()),
38 'pattern' => $pattern,
46 * Add a bundle condition to a pathauto pattern.
48 * @param \Drupal\pathauto\PathautoPatternInterface $pattern
50 * @param string $entity_type
52 * @param string $bundle
55 protected function addBundleCondition(PathautoPatternInterface $pattern, $entity_type, $bundle) {
56 $plugin_id = $entity_type == 'node' ? 'node_type' : 'entity_bundle:' . $entity_type;
58 $pattern->addSelectionCondition(
65 'context_mapping' => [
66 $entity_type => $entity_type,
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)));
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);
84 public function saveEntityAlias(EntityInterface $entity, $alias, $langcode = NULL) {
85 // By default, use the entity language.
87 $langcode = $entity->language()->getId();
89 return $this->saveAlias('/' . $entity->toUrl()->getInternalPath(), $alias, $langcode);
92 public function assertEntityAlias(EntityInterface $entity, $expected_alias, $langcode = NULL) {
93 // By default, use the entity language.
95 $langcode = $entity->language()->getId();
97 $this->assertAlias('/' . $entity->toUrl()->getInternalPath(), $expected_alias, $langcode);
100 public function assertEntityAliasExists(EntityInterface $entity) {
101 return $this->assertAliasExists(array('source' => '/' . $entity->toUrl()->getInternalPath()));
104 public function assertNoEntityAlias(EntityInterface $entity, $langcode = NULL) {
105 // By default, use the entity language.
107 $langcode = $entity->language()->getId();
109 $this->assertEntityAlias($entity, '/' . $entity->toUrl()->getInternalPath(), $langcode);
112 public function assertNoEntityAliasExists(EntityInterface $entity, $alias = NULL) {
113 $path = array('source' => '/' . $entity->toUrl()->getInternalPath());
114 if (!empty($alias)) {
115 $path['alias'] = $alias;
117 $this->assertNoAliasExists($path);
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)));
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))));
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))));
137 public function deleteAllAliases() {
138 \Drupal::database()->delete('url_alias')->execute();
139 \Drupal::service('path.alias_manager')->cacheClear();
143 * @param array $values
145 * @return \Drupal\taxonomy\VocabularyInterface
147 public function addVocabulary(array $values = array()) {
148 $name = mb_strtolower($this->randomMachineName(5));
153 $vocabulary = Vocabulary::create($values);
159 public function addTerm(VocabularyInterface $vocabulary, array $values = array()) {
161 'name' => mb_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;