Mercurial > hg > isophonics-drupal-site
comparison core/modules/taxonomy/tests/src/Functional/TokenReplaceTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\taxonomy\Functional; | |
4 | |
5 use Drupal\Core\Field\FieldStorageDefinitionInterface; | |
6 use Drupal\Core\Render\BubbleableMetadata; | |
7 | |
8 /** | |
9 * Generates text using placeholders for dummy content to check taxonomy token | |
10 * replacement. | |
11 * | |
12 * @group taxonomy | |
13 */ | |
14 class TokenReplaceTest extends TaxonomyTestBase { | |
15 | |
16 /** | |
17 * The vocabulary used for creating terms. | |
18 * | |
19 * @var \Drupal\taxonomy\VocabularyInterface | |
20 */ | |
21 protected $vocabulary; | |
22 | |
23 /** | |
24 * Name of the taxonomy term reference field. | |
25 * | |
26 * @var string | |
27 */ | |
28 protected $fieldName; | |
29 | |
30 protected function setUp() { | |
31 parent::setUp(); | |
32 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access'])); | |
33 $this->vocabulary = $this->createVocabulary(); | |
34 $this->fieldName = 'taxonomy_' . $this->vocabulary->id(); | |
35 | |
36 $handler_settings = [ | |
37 'target_bundles' => [ | |
38 $this->vocabulary->id() => $this->vocabulary->id(), | |
39 ], | |
40 'auto_create' => TRUE, | |
41 ]; | |
42 $this->createEntityReferenceField('node', 'article', $this->fieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); | |
43 | |
44 entity_get_form_display('node', 'article', 'default') | |
45 ->setComponent($this->fieldName, [ | |
46 'type' => 'options_select', | |
47 ]) | |
48 ->save(); | |
49 entity_get_display('node', 'article', 'default') | |
50 ->setComponent($this->fieldName, [ | |
51 'type' => 'entity_reference_label', | |
52 ]) | |
53 ->save(); | |
54 } | |
55 | |
56 /** | |
57 * Creates some terms and a node, then tests the tokens generated from them. | |
58 */ | |
59 public function testTaxonomyTokenReplacement() { | |
60 $token_service = \Drupal::token(); | |
61 $language_interface = \Drupal::languageManager()->getCurrentLanguage(); | |
62 | |
63 // Create two taxonomy terms. | |
64 $term1 = $this->createTerm($this->vocabulary); | |
65 $term2 = $this->createTerm($this->vocabulary); | |
66 | |
67 // Edit $term2, setting $term1 as parent. | |
68 $edit = []; | |
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')); | |
72 | |
73 // Create node with term2. | |
74 $edit = []; | |
75 $node = $this->drupalCreateNode(['type' => 'article']); | |
76 $edit[$this->fieldName . '[]'] = $term2->id(); | |
77 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save')); | |
78 | |
79 // Generate and test sanitized tokens for term1. | |
80 $tests = []; | |
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(); | |
89 | |
90 $base_bubbleable_metadata = BubbleableMetadata::createFromObject($term1); | |
91 | |
92 $metadata_tests = []; | |
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()); | |
102 | |
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]); | |
108 } | |
109 | |
110 // Generate and test sanitized tokens for term2. | |
111 $tests = []; | |
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(); | |
121 | |
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.'); | |
124 | |
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])); | |
128 } | |
129 | |
130 // Generate and test sanitized tokens. | |
131 $tests = []; | |
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; | |
137 | |
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.'); | |
140 | |
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])); | |
144 } | |
145 } | |
146 | |
147 } |