3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Render\BubbleableMetadata;
9 * Generates text using placeholders for dummy content to check taxonomy token
14 class TokenReplaceTest extends TaxonomyTestBase {
17 * The vocabulary used for creating terms.
19 * @var \Drupal\taxonomy\VocabularyInterface
21 protected $vocabulary;
24 * Name of the taxonomy term reference field.
30 protected function setUp() {
32 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
33 $this->vocabulary = $this->createVocabulary();
34 $this->fieldName = 'taxonomy_' . $this->vocabulary->id();
38 $this->vocabulary->id() => $this->vocabulary->id(),
40 'auto_create' => TRUE,
42 $this->createEntityReferenceField('node', 'article', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
44 entity_get_form_display('node', 'article', 'default')
45 ->setComponent($this->fieldName, [
46 'type' => 'options_select',
49 entity_get_display('node', 'article', 'default')
50 ->setComponent($this->fieldName, [
51 'type' => 'entity_reference_label',
57 * Creates some terms and a node, then tests the tokens generated from them.
59 public function testTaxonomyTokenReplacement() {
60 $token_service = \Drupal::token();
61 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
63 // Create two taxonomy terms.
64 $term1 = $this->createTerm($this->vocabulary);
65 $term2 = $this->createTerm($this->vocabulary);
67 // Edit $term2, setting $term1 as parent.
69 $edit['name[0][value]'] = '<blink>Blinking Text</blink>';
70 $edit['parent[]'] = [$term1->id()];
71 $this->drupalPostForm('taxonomy/term/' . $term2->id() . '/edit', $edit, t('Save'));
73 // Create node with term2.
75 $node = $this->drupalCreateNode(['type' => 'article']);
76 $edit[$this->fieldName . '[]'] = $term2->id();
77 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
79 // Generate and test sanitized tokens for term1.
81 $tests['[term:tid]'] = $term1->id();
82 $tests['[term:name]'] = $term1->getName();
83 $tests['[term:description]'] = $term1->description->processed;
84 $tests['[term:url]'] = $term1->url('canonical', ['absolute' => TRUE]);
85 $tests['[term:node-count]'] = 0;
86 $tests['[term:parent:name]'] = '[term:parent:name]';
87 $tests['[term:vocabulary:name]'] = $this->vocabulary->label();
88 $tests['[term:vocabulary]'] = $this->vocabulary->label();
90 $base_bubbleable_metadata = BubbleableMetadata::createFromObject($term1);
93 $metadata_tests['[term:tid]'] = $base_bubbleable_metadata;
94 $metadata_tests['[term:name]'] = $base_bubbleable_metadata;
95 $metadata_tests['[term:description]'] = $base_bubbleable_metadata;
96 $metadata_tests['[term:url]'] = $base_bubbleable_metadata;
97 $metadata_tests['[term:node-count]'] = $base_bubbleable_metadata;
98 $metadata_tests['[term:parent:name]'] = $base_bubbleable_metadata;
99 $bubbleable_metadata = clone $base_bubbleable_metadata;
100 $metadata_tests['[term:vocabulary:name]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags());
101 $metadata_tests['[term:vocabulary]'] = $bubbleable_metadata->addCacheTags($this->vocabulary->getCacheTags());
103 foreach ($tests as $input => $expected) {
104 $bubbleable_metadata = new BubbleableMetadata();
105 $output = $token_service->replace($input, ['term' => $term1], ['langcode' => $language_interface->getId()], $bubbleable_metadata);
106 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', ['%token' => $input]));
107 $this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
110 // Generate and test sanitized tokens for term2.
112 $tests['[term:tid]'] = $term2->id();
113 $tests['[term:name]'] = $term2->getName();
114 $tests['[term:description]'] = $term2->description->processed;
115 $tests['[term:url]'] = $term2->url('canonical', ['absolute' => TRUE]);
116 $tests['[term:node-count]'] = 1;
117 $tests['[term:parent:name]'] = $term1->getName();
118 $tests['[term:parent:url]'] = $term1->url('canonical', ['absolute' => TRUE]);
119 $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]';
120 $tests['[term:vocabulary:name]'] = $this->vocabulary->label();
122 // Test to make sure that we generated something for each token.
123 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
125 foreach ($tests as $input => $expected) {
126 $output = $token_service->replace($input, ['term' => $term2], ['langcode' => $language_interface->getId()]);
127 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', ['%token' => $input]));
130 // Generate and test sanitized tokens.
132 $tests['[vocabulary:vid]'] = $this->vocabulary->id();
133 $tests['[vocabulary:name]'] = $this->vocabulary->label();
134 $tests['[vocabulary:description]'] = $this->vocabulary->getDescription();
135 $tests['[vocabulary:node-count]'] = 1;
136 $tests['[vocabulary:term-count]'] = 2;
138 // Test to make sure that we generated something for each token.
139 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
141 foreach ($tests as $input => $expected) {
142 $output = $token_service->replace($input, ['vocabulary' => $this->vocabulary], ['langcode' => $language_interface->getId()]);
143 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy vocabulary token %token replaced.', ['%token' => $input]));