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