Pull merge.
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / TermIndexTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6
7 /**
8  * Tests the hook implementations that maintain the taxonomy index.
9  *
10  * @group taxonomy
11  */
12 class TermIndexTest extends TaxonomyTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['views'];
20
21   /**
22    * Vocabulary for testing.
23    *
24    * @var \Drupal\taxonomy\VocabularyInterface
25    */
26   protected $vocabulary;
27
28   /**
29    * Name of the taxonomy term reference field.
30    *
31    * @var string
32    */
33   protected $fieldName1;
34
35   /**
36    * Name of the taxonomy term reference field.
37    *
38    * @var string
39    */
40   protected $fieldName2;
41
42   protected function setUp() {
43     parent::setUp();
44
45     // Create an administrative user.
46     $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
47
48     // Create a vocabulary and add two term reference fields to article nodes.
49     $this->vocabulary = $this->createVocabulary();
50
51     $this->fieldName1 = mb_strtolower($this->randomMachineName());
52     $handler_settings = [
53       'target_bundles' => [
54         $this->vocabulary->id() => $this->vocabulary->id(),
55        ],
56       'auto_create' => TRUE,
57     ];
58     $this->createEntityReferenceField('node', 'article', $this->fieldName1, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
59
60     entity_get_form_display('node', 'article', 'default')
61       ->setComponent($this->fieldName1, [
62         'type' => 'options_select',
63       ])
64       ->save();
65     entity_get_display('node', 'article', 'default')
66       ->setComponent($this->fieldName1, [
67         'type' => 'entity_reference_label',
68       ])
69       ->save();
70
71     $this->fieldName2 = mb_strtolower($this->randomMachineName());
72     $this->createEntityReferenceField('node', 'article', $this->fieldName2, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
73
74     entity_get_form_display('node', 'article', 'default')
75       ->setComponent($this->fieldName2, [
76         'type' => 'options_select',
77       ])
78       ->save();
79     entity_get_display('node', 'article', 'default')
80       ->setComponent($this->fieldName2, [
81         'type' => 'entity_reference_label',
82       ])
83       ->save();
84   }
85
86   /**
87    * Tests that the taxonomy index is maintained properly.
88    */
89   public function testTaxonomyIndex() {
90     $node_storage = $this->container->get('entity.manager')->getStorage('node');
91     // Create terms in the vocabulary.
92     $term_1 = $this->createTerm($this->vocabulary);
93     $term_2 = $this->createTerm($this->vocabulary);
94
95     // Post an article.
96     $edit = [];
97     $edit['title[0][value]'] = $this->randomMachineName();
98     $edit['body[0][value]'] = $this->randomMachineName();
99     $edit["{$this->fieldName1}[]"] = $term_1->id();
100     $edit["{$this->fieldName2}[]"] = $term_1->id();
101     $this->drupalPostForm('node/add/article', $edit, t('Save'));
102
103     // Check that the term is indexed, and only once.
104     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
105     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
106       ':nid' => $node->id(),
107       ':tid' => $term_1->id(),
108     ])->fetchField();
109     $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
110
111     // Update the article to change one term.
112     $edit["{$this->fieldName1}[]"] = $term_2->id();
113     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
114
115     // Check that both terms are indexed.
116     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
117       ':nid' => $node->id(),
118       ':tid' => $term_1->id(),
119     ])->fetchField();
120     $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
121     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
122       ':nid' => $node->id(),
123       ':tid' => $term_2->id(),
124     ])->fetchField();
125     $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
126
127     // Update the article to change another term.
128     $edit["{$this->fieldName2}[]"] = $term_2->id();
129     $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
130
131     // Check that only one term is indexed.
132     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
133       ':nid' => $node->id(),
134       ':tid' => $term_1->id(),
135     ])->fetchField();
136     $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
137     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
138       ':nid' => $node->id(),
139       ':tid' => $term_2->id(),
140     ])->fetchField();
141     $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
142
143     // Redo the above tests without interface.
144     $node_storage->resetCache([$node->id()]);
145     $node = $node_storage->load($node->id());
146     $node->title = $this->randomMachineName();
147
148     // Update the article with no term changed.
149     $node->save();
150
151     // Check that the index was not changed.
152     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
153       ':nid' => $node->id(),
154       ':tid' => $term_1->id(),
155     ])->fetchField();
156     $this->assertEqual(0, $index_count, 'Term 1 is not indexed.');
157     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
158       ':nid' => $node->id(),
159       ':tid' => $term_2->id(),
160     ])->fetchField();
161     $this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
162
163     // Update the article to change one term.
164     $node->{$this->fieldName1} = [['target_id' => $term_1->id()]];
165     $node->save();
166
167     // Check that both terms are indexed.
168     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
169       ':nid' => $node->id(),
170       ':tid' => $term_1->id(),
171     ])->fetchField();
172     $this->assertEqual(1, $index_count, 'Term 1 is indexed.');
173     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
174       ':nid' => $node->id(),
175       ':tid' => $term_2->id(),
176     ])->fetchField();
177     $this->assertEqual(1, $index_count, 'Term 2 is indexed.');
178
179     // Update the article to change another term.
180     $node->{$this->fieldName2} = [['target_id' => $term_1->id()]];
181     $node->save();
182
183     // Check that only one term is indexed.
184     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
185       ':nid' => $node->id(),
186       ':tid' => $term_1->id(),
187     ])->fetchField();
188     $this->assertEqual(1, $index_count, 'Term 1 is indexed once.');
189     $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', [
190       ':nid' => $node->id(),
191       ':tid' => $term_2->id(),
192     ])->fetchField();
193     $this->assertEqual(0, $index_count, 'Term 2 is not indexed.');
194   }
195
196   /**
197    * Tests that there is a link to the parent term on the child term page.
198    */
199   public function testTaxonomyTermHierarchyBreadcrumbs() {
200     // Create two taxonomy terms and set term2 as the parent of term1.
201     $term1 = $this->createTerm($this->vocabulary);
202     $term2 = $this->createTerm($this->vocabulary);
203     $term1->parent = [$term2->id()];
204     $term1->save();
205
206     // Verify that the page breadcrumbs include a link to the parent term.
207     $this->drupalGet('taxonomy/term/' . $term1->id());
208     // Breadcrumbs are not rendered with a language, prevent the term
209     // language from being added to the options.
210     $this->assertRaw(\Drupal::l($term2->getName(), $term2->urlInfo('canonical', ['language' => NULL])), 'Parent term link is displayed when viewing the node.');
211   }
212
213 }