annotate core/modules/taxonomy/src/Tests/TermTest.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\taxonomy\Tests;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Tags;
Chris@0 6 use Drupal\Component\Utility\Unicode;
Chris@0 7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
Chris@0 8 use Drupal\field\Entity\FieldConfig;
Chris@0 9 use Drupal\taxonomy\Entity\Term;
Chris@0 10 use Drupal\taxonomy\Entity\Vocabulary;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Tests load, save and delete for taxonomy terms.
Chris@0 14 *
Chris@0 15 * @group taxonomy
Chris@0 16 */
Chris@0 17 class TermTest extends TaxonomyTestBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Vocabulary for testing.
Chris@0 21 *
Chris@0 22 * @var \Drupal\taxonomy\VocabularyInterface
Chris@0 23 */
Chris@0 24 protected $vocabulary;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Taxonomy term reference field for testing.
Chris@0 28 *
Chris@0 29 * @var \Drupal\field\FieldConfigInterface
Chris@0 30 */
Chris@0 31 protected $field;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Modules to enable.
Chris@0 35 *
Chris@0 36 * @var string[]
Chris@0 37 */
Chris@0 38 public static $modules = ['block'];
Chris@0 39
Chris@0 40 /**
Chris@0 41 * {@inheritdoc}
Chris@0 42 */
Chris@0 43 protected function setUp() {
Chris@0 44 parent::setUp();
Chris@0 45
Chris@0 46 $this->drupalPlaceBlock('local_actions_block');
Chris@0 47 $this->drupalPlaceBlock('local_tasks_block');
Chris@0 48 $this->drupalPlaceBlock('page_title_block');
Chris@0 49
Chris@0 50 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy', 'bypass node access']));
Chris@0 51 $this->vocabulary = $this->createVocabulary();
Chris@0 52
Chris@0 53 $field_name = 'taxonomy_' . $this->vocabulary->id();
Chris@0 54
Chris@0 55 $handler_settings = [
Chris@0 56 'target_bundles' => [
Chris@0 57 $this->vocabulary->id() => $this->vocabulary->id(),
Chris@0 58 ],
Chris@0 59 'auto_create' => TRUE,
Chris@0 60 ];
Chris@0 61 $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
Chris@0 62 $this->field = FieldConfig::loadByName('node', 'article', $field_name);
Chris@0 63
Chris@0 64 entity_get_form_display('node', 'article', 'default')
Chris@0 65 ->setComponent($field_name, [
Chris@0 66 'type' => 'options_select',
Chris@0 67 ])
Chris@0 68 ->save();
Chris@0 69 entity_get_display('node', 'article', 'default')
Chris@0 70 ->setComponent($field_name, [
Chris@0 71 'type' => 'entity_reference_label',
Chris@0 72 ])
Chris@0 73 ->save();
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Test terms in a single and multiple hierarchy.
Chris@0 78 */
Chris@0 79 public function testTaxonomyTermHierarchy() {
Chris@0 80 // Create two taxonomy terms.
Chris@0 81 $term1 = $this->createTerm($this->vocabulary);
Chris@0 82 $term2 = $this->createTerm($this->vocabulary);
Chris@0 83
Chris@0 84 // Get the taxonomy storage.
Chris@0 85 $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
Chris@0 86
Chris@0 87 // Check that hierarchy is flat.
Chris@0 88 $vocabulary = Vocabulary::load($this->vocabulary->id());
Chris@0 89 $this->assertEqual(0, $vocabulary->getHierarchy(), 'Vocabulary is flat.');
Chris@0 90
Chris@0 91 // Edit $term2, setting $term1 as parent.
Chris@0 92 $edit = [];
Chris@0 93 $edit['parent[]'] = [$term1->id()];
Chris@0 94 $this->drupalPostForm('taxonomy/term/' . $term2->id() . '/edit', $edit, t('Save'));
Chris@0 95
Chris@0 96 // Check the hierarchy.
Chris@0 97 $children = $taxonomy_storage->loadChildren($term1->id());
Chris@0 98 $parents = $taxonomy_storage->loadParents($term2->id());
Chris@0 99 $this->assertTrue(isset($children[$term2->id()]), 'Child found correctly.');
Chris@0 100 $this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');
Chris@0 101
Chris@0 102 // Load and save a term, confirming that parents are still set.
Chris@0 103 $term = Term::load($term2->id());
Chris@0 104 $term->save();
Chris@0 105 $parents = $taxonomy_storage->loadParents($term2->id());
Chris@0 106 $this->assertTrue(isset($parents[$term1->id()]), 'Parent found correctly.');
Chris@0 107
Chris@0 108 // Create a third term and save this as a parent of term2.
Chris@0 109 $term3 = $this->createTerm($this->vocabulary);
Chris@0 110 $term2->parent = [$term1->id(), $term3->id()];
Chris@0 111 $term2->save();
Chris@0 112 $parents = $taxonomy_storage->loadParents($term2->id());
Chris@0 113 $this->assertTrue(isset($parents[$term1->id()]) && isset($parents[$term3->id()]), 'Both parents found successfully.');
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * Tests that many terms with parents show on each page
Chris@0 118 */
Chris@0 119 public function testTaxonomyTermChildTerms() {
Chris@0 120 // Set limit to 10 terms per page. Set variable to 9 so 10 terms appear.
Chris@0 121 $this->config('taxonomy.settings')->set('terms_per_page_admin', '9')->save();
Chris@0 122 $term1 = $this->createTerm($this->vocabulary);
Chris@0 123 $terms_array = [];
Chris@0 124
Chris@0 125 $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
Chris@0 126
Chris@0 127 // Create 40 terms. Terms 1-12 get parent of $term1. All others are
Chris@0 128 // individual terms.
Chris@0 129 for ($x = 1; $x <= 40; $x++) {
Chris@0 130 $edit = [];
Chris@0 131 // Set terms in order so we know which terms will be on which pages.
Chris@0 132 $edit['weight'] = $x;
Chris@0 133
Chris@0 134 // Set terms 1-20 to be children of first term created.
Chris@0 135 if ($x <= 12) {
Chris@0 136 $edit['parent'] = $term1->id();
Chris@0 137 }
Chris@0 138 $term = $this->createTerm($this->vocabulary, $edit);
Chris@0 139 $children = $taxonomy_storage->loadChildren($term1->id());
Chris@0 140 $parents = $taxonomy_storage->loadParents($term->id());
Chris@0 141 $terms_array[$x] = Term::load($term->id());
Chris@0 142 }
Chris@0 143
Chris@0 144 // Get Page 1.
Chris@0 145 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
Chris@0 146 $this->assertText($term1->getName(), 'Parent Term is displayed on Page 1');
Chris@0 147 for ($x = 1; $x <= 13; $x++) {
Chris@0 148 $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 1');
Chris@0 149 }
Chris@0 150
Chris@0 151 // Get Page 2.
Chris@0 152 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', ['query' => ['page' => 1]]);
Chris@0 153 $this->assertText($term1->getName(), 'Parent Term is displayed on Page 2');
Chris@0 154 for ($x = 1; $x <= 18; $x++) {
Chris@0 155 $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 2');
Chris@0 156 }
Chris@0 157
Chris@0 158 // Get Page 3.
Chris@0 159 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', ['query' => ['page' => 2]]);
Chris@0 160 $this->assertNoText($term1->getName(), 'Parent Term is not displayed on Page 3');
Chris@0 161 for ($x = 1; $x <= 17; $x++) {
Chris@0 162 $this->assertNoText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' not found on Page 3');
Chris@0 163 }
Chris@0 164 for ($x = 18; $x <= 25; $x++) {
Chris@0 165 $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 3');
Chris@0 166 }
Chris@0 167 }
Chris@0 168
Chris@0 169 /**
Chris@0 170 * Test that hook_node_$op implementations work correctly.
Chris@0 171 *
Chris@0 172 * Save & edit a node and assert that taxonomy terms are saved/loaded properly.
Chris@0 173 */
Chris@0 174 public function testTaxonomyNode() {
Chris@0 175 // Create two taxonomy terms.
Chris@0 176 $term1 = $this->createTerm($this->vocabulary);
Chris@0 177 $term2 = $this->createTerm($this->vocabulary);
Chris@0 178
Chris@0 179 // Post an article.
Chris@0 180 $edit = [];
Chris@0 181 $edit['title[0][value]'] = $this->randomMachineName();
Chris@0 182 $edit['body[0][value]'] = $this->randomMachineName();
Chris@0 183 $edit[$this->field->getName() . '[]'] = $term1->id();
Chris@0 184 $this->drupalPostForm('node/add/article', $edit, t('Save'));
Chris@0 185
Chris@0 186 // Check that the term is displayed when the node is viewed.
Chris@0 187 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
Chris@0 188 $this->drupalGet('node/' . $node->id());
Chris@0 189 $this->assertText($term1->getName(), 'Term is displayed when viewing the node.');
Chris@0 190
Chris@0 191 $this->clickLink(t('Edit'));
Chris@0 192 $this->assertText($term1->getName(), 'Term is displayed when editing the node.');
Chris@0 193 $this->drupalPostForm(NULL, [], t('Save'));
Chris@0 194 $this->assertText($term1->getName(), 'Term is displayed after saving the node with no changes.');
Chris@0 195
Chris@0 196 // Edit the node with a different term.
Chris@0 197 $edit[$this->field->getName() . '[]'] = $term2->id();
Chris@0 198 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
Chris@0 199
Chris@0 200 $this->drupalGet('node/' . $node->id());
Chris@0 201 $this->assertText($term2->getName(), 'Term is displayed when viewing the node.');
Chris@0 202
Chris@0 203 // Preview the node.
Chris@0 204 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Preview'));
Chris@0 205 $this->assertUniqueText($term2->getName(), 'Term is displayed when previewing the node.');
Chris@0 206 $this->drupalPostForm('node/' . $node->id() . '/edit', NULL, t('Preview'));
Chris@0 207 $this->assertUniqueText($term2->getName(), 'Term is displayed when previewing the node again.');
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * Test term creation with a free-tagging vocabulary from the node form.
Chris@0 212 */
Chris@0 213 public function testNodeTermCreationAndDeletion() {
Chris@0 214 // Enable tags in the vocabulary.
Chris@0 215 $field = $this->field;
Chris@0 216 entity_get_form_display($field->getTargetEntityTypeId(), $field->getTargetBundle(), 'default')
Chris@0 217 ->setComponent($field->getName(), [
Chris@0 218 'type' => 'entity_reference_autocomplete_tags',
Chris@0 219 'settings' => [
Chris@0 220 'placeholder' => 'Start typing here.',
Chris@0 221 ],
Chris@0 222 ])
Chris@0 223 ->save();
Chris@0 224 // Prefix the terms with a letter to ensure there is no clash in the first
Chris@0 225 // three letters.
Chris@0 226 // @see https://www.drupal.org/node/2397691
Chris@0 227 $terms = [
Chris@0 228 'term1' => 'a' . $this->randomMachineName(),
Chris@0 229 'term2' => 'b' . $this->randomMachineName(),
Chris@0 230 'term3' => 'c' . $this->randomMachineName() . ', ' . $this->randomMachineName(),
Chris@0 231 'term4' => 'd' . $this->randomMachineName(),
Chris@0 232 ];
Chris@0 233
Chris@0 234 $edit = [];
Chris@0 235 $edit['title[0][value]'] = $this->randomMachineName();
Chris@0 236 $edit['body[0][value]'] = $this->randomMachineName();
Chris@0 237 // Insert the terms in a comma separated list. Vocabulary 1 is a
Chris@0 238 // free-tagging field created by the default profile.
Chris@0 239 $edit[$field->getName() . '[target_id]'] = Tags::implode($terms);
Chris@0 240
Chris@0 241 // Verify the placeholder is there.
Chris@0 242 $this->drupalGet('node/add/article');
Chris@0 243 $this->assertRaw('placeholder="Start typing here."', 'Placeholder is present.');
Chris@0 244
Chris@0 245 // Preview and verify the terms appear but are not created.
Chris@0 246 $this->drupalPostForm(NULL, $edit, t('Preview'));
Chris@0 247 foreach ($terms as $term) {
Chris@0 248 $this->assertText($term, 'The term appears on the node preview.');
Chris@0 249 }
Chris@0 250 $tree = $this->container->get('entity.manager')->getStorage('taxonomy_term')->loadTree($this->vocabulary->id());
Chris@0 251 $this->assertTrue(empty($tree), 'The terms are not created on preview.');
Chris@0 252
Chris@0 253 // taxonomy.module does not maintain its static caches.
Chris@0 254 taxonomy_terms_static_reset();
Chris@0 255
Chris@0 256 // Save, creating the terms.
Chris@0 257 $this->drupalPostForm('node/add/article', $edit, t('Save'));
Chris@0 258 $this->assertText(t('@type @title has been created.', ['@type' => t('Article'), '@title' => $edit['title[0][value]']]), 'The node was created successfully.');
Chris@0 259
Chris@0 260 // Verify that the creation message contains a link to a node.
Chris@0 261 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/']);
Chris@0 262 $this->assert(isset($view_link), 'The message area contains a link to a node');
Chris@0 263
Chris@0 264 foreach ($terms as $term) {
Chris@0 265 $this->assertText($term, 'The term was saved and appears on the node page.');
Chris@0 266 }
Chris@0 267
Chris@0 268 // Get the created terms.
Chris@0 269 $term_objects = [];
Chris@0 270 foreach ($terms as $key => $term) {
Chris@0 271 $term_objects[$key] = taxonomy_term_load_multiple_by_name($term);
Chris@0 272 $term_objects[$key] = reset($term_objects[$key]);
Chris@0 273 }
Chris@0 274
Chris@0 275 // Get the node.
Chris@0 276 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
Chris@0 277
Chris@0 278 // Test editing the node.
Chris@0 279 $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
Chris@0 280 foreach ($terms as $term) {
Chris@0 281 $this->assertText($term, 'The term was retained after edit and still appears on the node page.');
Chris@0 282 }
Chris@0 283
Chris@0 284 // Delete term 1 from the term edit page.
Chris@0 285 $this->drupalGet('taxonomy/term/' . $term_objects['term1']->id() . '/edit');
Chris@0 286 $this->clickLink(t('Delete'));
Chris@0 287 $this->drupalPostForm(NULL, NULL, t('Delete'));
Chris@0 288
Chris@0 289 // Delete term 2 from the term delete page.
Chris@0 290 $this->drupalGet('taxonomy/term/' . $term_objects['term2']->id() . '/delete');
Chris@0 291 $this->drupalPostForm(NULL, [], t('Delete'));
Chris@0 292 $term_names = [$term_objects['term3']->getName(), $term_objects['term4']->getName()];
Chris@0 293
Chris@0 294 $this->drupalGet('node/' . $node->id());
Chris@0 295
Chris@0 296 foreach ($term_names as $term_name) {
Chris@0 297 $this->assertText($term_name, format_string('The term %name appears on the node page after two terms, %deleted1 and %deleted2, were deleted.', ['%name' => $term_name, '%deleted1' => $term_objects['term1']->getName(), '%deleted2' => $term_objects['term2']->getName()]));
Chris@0 298 }
Chris@0 299 $this->assertNoText($term_objects['term1']->getName(), format_string('The deleted term %name does not appear on the node page.', ['%name' => $term_objects['term1']->getName()]));
Chris@0 300 $this->assertNoText($term_objects['term2']->getName(), format_string('The deleted term %name does not appear on the node page.', ['%name' => $term_objects['term2']->getName()]));
Chris@0 301 }
Chris@0 302
Chris@0 303 /**
Chris@0 304 * Save, edit and delete a term using the user interface.
Chris@0 305 */
Chris@0 306 public function testTermInterface() {
Chris@0 307 \Drupal::service('module_installer')->install(['views']);
Chris@0 308 $edit = [
Chris@0 309 'name[0][value]' => $this->randomMachineName(12),
Chris@0 310 'description[0][value]' => $this->randomMachineName(100),
Chris@0 311 ];
Chris@0 312 // Explicitly set the parents field to 'root', to ensure that
Chris@0 313 // TermForm::save() handles the invalid term ID correctly.
Chris@0 314 $edit['parent[]'] = [0];
Chris@0 315
Chris@0 316 // Create the term to edit.
Chris@0 317 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
Chris@0 318
Chris@0 319 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
Chris@0 320 $term = reset($terms);
Chris@0 321 $this->assertNotNull($term, 'Term found in database.');
Chris@0 322
Chris@0 323 // Submitting a term takes us to the add page; we need the List page.
Chris@0 324 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
Chris@0 325
Chris@0 326 $this->clickLink(t('Edit'));
Chris@0 327
Chris@0 328 $this->assertRaw($edit['name[0][value]'], 'The randomly generated term name is present.');
Chris@0 329 $this->assertText($edit['description[0][value]'], 'The randomly generated term description is present.');
Chris@0 330
Chris@0 331 $edit = [
Chris@0 332 'name[0][value]' => $this->randomMachineName(14),
Chris@0 333 'description[0][value]' => $this->randomMachineName(102),
Chris@0 334 ];
Chris@0 335
Chris@0 336 // Edit the term.
Chris@0 337 $this->drupalPostForm('taxonomy/term/' . $term->id() . '/edit', $edit, t('Save'));
Chris@0 338
Chris@0 339 // Check that the term is still present at admin UI after edit.
Chris@0 340 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
Chris@0 341 $this->assertText($edit['name[0][value]'], 'The randomly generated term name is present.');
Chris@0 342 $this->assertLink(t('Edit'));
Chris@0 343
Chris@0 344 // Check the term link can be clicked through to the term page.
Chris@0 345 $this->clickLink($edit['name[0][value]']);
Chris@0 346 $this->assertResponse(200, 'Term page can be accessed via the listing link.');
Chris@0 347
Chris@0 348 // View the term and check that it is correct.
Chris@0 349 $this->drupalGet('taxonomy/term/' . $term->id());
Chris@0 350 $this->assertText($edit['name[0][value]'], 'The randomly generated term name is present.');
Chris@0 351 $this->assertText($edit['description[0][value]'], 'The randomly generated term description is present.');
Chris@0 352
Chris@0 353 // Did this page request display a 'term-listing-heading'?
Chris@0 354 $this->assertTrue($this->xpath('//div[contains(@class, "field--name-description")]'), 'Term page displayed the term description element.');
Chris@0 355 // Check that it does NOT show a description when description is blank.
Chris@0 356 $term->setDescription(NULL);
Chris@0 357 $term->save();
Chris@0 358 $this->drupalGet('taxonomy/term/' . $term->id());
Chris@0 359 $this->assertFalse($this->xpath('//div[contains(@class, "field--entity-taxonomy-term--description")]'), 'Term page did not display the term description when description was blank.');
Chris@0 360
Chris@0 361 // Check that the description value is processed.
Chris@0 362 $value = $this->randomMachineName();
Chris@0 363 $term->setDescription($value);
Chris@0 364 $term->save();
Chris@0 365 $this->assertEqual($term->description->processed, "<p>$value</p>\n");
Chris@0 366
Chris@0 367 // Check that the term feed page is working.
Chris@0 368 $this->drupalGet('taxonomy/term/' . $term->id() . '/feed');
Chris@0 369
Chris@0 370 // Delete the term.
Chris@0 371 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
Chris@0 372 $this->clickLink(t('Delete'));
Chris@0 373 $this->drupalPostForm(NULL, NULL, t('Delete'));
Chris@0 374
Chris@0 375 // Assert that the term no longer exists.
Chris@0 376 $this->drupalGet('taxonomy/term/' . $term->id());
Chris@0 377 $this->assertResponse(404, 'The taxonomy term page was not found.');
Chris@0 378 }
Chris@0 379
Chris@0 380 /**
Chris@0 381 * Save, edit and delete a term using the user interface.
Chris@0 382 */
Chris@0 383 public function testTermReorder() {
Chris@0 384 $this->createTerm($this->vocabulary);
Chris@0 385 $this->createTerm($this->vocabulary);
Chris@0 386 $this->createTerm($this->vocabulary);
Chris@0 387
Chris@0 388 $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
Chris@0 389
Chris@0 390 // Fetch the created terms in the default alphabetical order, i.e. term1
Chris@0 391 // precedes term2 alphabetically, and term2 precedes term3.
Chris@0 392 $taxonomy_storage->resetCache();
Chris@0 393 list($term1, $term2, $term3) = $taxonomy_storage->loadTree($this->vocabulary->id(), 0, NULL, TRUE);
Chris@0 394
Chris@0 395 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
Chris@0 396
Chris@0 397 // Each term has four hidden fields, "tid:1:0[tid]", "tid:1:0[parent]",
Chris@0 398 // "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2,
Chris@0 399 // term3, term1 by setting weight property, make term3 a child of term2 by
Chris@0 400 // setting the parent and depth properties, and update all hidden fields.
Chris@0 401 $edit = [
Chris@0 402 'terms[tid:' . $term2->id() . ':0][term][tid]' => $term2->id(),
Chris@0 403 'terms[tid:' . $term2->id() . ':0][term][parent]' => 0,
Chris@0 404 'terms[tid:' . $term2->id() . ':0][term][depth]' => 0,
Chris@0 405 'terms[tid:' . $term2->id() . ':0][weight]' => 0,
Chris@0 406 'terms[tid:' . $term3->id() . ':0][term][tid]' => $term3->id(),
Chris@0 407 'terms[tid:' . $term3->id() . ':0][term][parent]' => $term2->id(),
Chris@0 408 'terms[tid:' . $term3->id() . ':0][term][depth]' => 1,
Chris@0 409 'terms[tid:' . $term3->id() . ':0][weight]' => 1,
Chris@0 410 'terms[tid:' . $term1->id() . ':0][term][tid]' => $term1->id(),
Chris@0 411 'terms[tid:' . $term1->id() . ':0][term][parent]' => 0,
Chris@0 412 'terms[tid:' . $term1->id() . ':0][term][depth]' => 0,
Chris@0 413 'terms[tid:' . $term1->id() . ':0][weight]' => 2,
Chris@0 414 ];
Chris@0 415 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 416
Chris@0 417 $taxonomy_storage->resetCache();
Chris@0 418 $terms = $taxonomy_storage->loadTree($this->vocabulary->id());
Chris@0 419 $this->assertEqual($terms[0]->tid, $term2->id(), 'Term 2 was moved above term 1.');
Chris@0 420 $this->assertEqual($terms[1]->parents, [$term2->id()], 'Term 3 was made a child of term 2.');
Chris@0 421 $this->assertEqual($terms[2]->tid, $term1->id(), 'Term 1 was moved below term 2.');
Chris@0 422
Chris@0 423 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', [], t('Reset to alphabetical'));
Chris@0 424 // Submit confirmation form.
Chris@0 425 $this->drupalPostForm(NULL, [], t('Reset to alphabetical'));
Chris@0 426 // Ensure form redirected back to overview.
Chris@0 427 $this->assertUrl('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
Chris@0 428
Chris@0 429 $taxonomy_storage->resetCache();
Chris@0 430 $terms = $taxonomy_storage->loadTree($this->vocabulary->id(), 0, NULL, TRUE);
Chris@0 431 $this->assertEqual($terms[0]->id(), $term1->id(), 'Term 1 was moved to back above term 2.');
Chris@0 432 $this->assertEqual($terms[1]->id(), $term2->id(), 'Term 2 was moved to back below term 1.');
Chris@0 433 $this->assertEqual($terms[2]->id(), $term3->id(), 'Term 3 is still below term 2.');
Chris@0 434 $this->assertEqual($terms[2]->parents, [$term2->id()], 'Term 3 is still a child of term 2.');
Chris@0 435 }
Chris@0 436
Chris@0 437 /**
Chris@0 438 * Test saving a term with multiple parents through the UI.
Chris@0 439 */
Chris@0 440 public function testTermMultipleParentsInterface() {
Chris@0 441 // Add a new term to the vocabulary so that we can have multiple parents.
Chris@0 442 $parent = $this->createTerm($this->vocabulary);
Chris@0 443
Chris@0 444 // Add a new term with multiple parents.
Chris@0 445 $edit = [
Chris@0 446 'name[0][value]' => $this->randomMachineName(12),
Chris@0 447 'description[0][value]' => $this->randomMachineName(100),
Chris@0 448 'parent[]' => [0, $parent->id()],
Chris@0 449 ];
Chris@0 450 // Save the new term.
Chris@0 451 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
Chris@0 452
Chris@0 453 // Check that the term was successfully created.
Chris@0 454 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
Chris@0 455 $term = reset($terms);
Chris@0 456 $this->assertNotNull($term, 'Term found in database.');
Chris@0 457 $this->assertEqual($edit['name[0][value]'], $term->getName(), 'Term name was successfully saved.');
Chris@0 458 $this->assertEqual($edit['description[0][value]'], $term->getDescription(), 'Term description was successfully saved.');
Chris@0 459 // Check that the parent tid is still there. The other parent (<root>) is
Chris@0 460 // not added by \Drupal\taxonomy\TermStorageInterface::loadParents().
Chris@0 461 $parents = $this->container->get('entity.manager')->getStorage('taxonomy_term')->loadParents($term->id());
Chris@0 462 $parent = reset($parents);
Chris@0 463 $this->assertEqual($edit['parent[]'][1], $parent->id(), 'Term parents were successfully saved.');
Chris@0 464 }
Chris@0 465
Chris@0 466 /**
Chris@0 467 * Test taxonomy_term_load_multiple_by_name().
Chris@0 468 */
Chris@0 469 public function testTaxonomyGetTermByName() {
Chris@0 470 $term = $this->createTerm($this->vocabulary);
Chris@0 471
Chris@0 472 // Load the term with the exact name.
Chris@0 473 $terms = taxonomy_term_load_multiple_by_name($term->getName());
Chris@0 474 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded using exact name.');
Chris@0 475
Chris@0 476 // Load the term with space concatenated.
Chris@0 477 $terms = taxonomy_term_load_multiple_by_name(' ' . $term->getName() . ' ');
Chris@0 478 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with extra whitespace.');
Chris@0 479
Chris@0 480 // Load the term with name uppercased.
Chris@0 481 $terms = taxonomy_term_load_multiple_by_name(strtoupper($term->getName()));
Chris@0 482 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with uppercased name.');
Chris@0 483
Chris@0 484 // Load the term with name lowercased.
Chris@0 485 $terms = taxonomy_term_load_multiple_by_name(strtolower($term->getName()));
Chris@0 486 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded with lowercased name.');
Chris@0 487
Chris@0 488 // Try to load an invalid term name.
Chris@0 489 $terms = taxonomy_term_load_multiple_by_name('Banana');
Chris@0 490 $this->assertFalse($terms, 'No term loaded with an invalid name.');
Chris@0 491
Chris@0 492 // Try to load the term using a substring of the name.
Chris@0 493 $terms = taxonomy_term_load_multiple_by_name(Unicode::substr($term->getName(), 2), 'No term loaded with a substring of the name.');
Chris@0 494 $this->assertFalse($terms);
Chris@0 495
Chris@0 496 // Create a new term in a different vocabulary with the same name.
Chris@0 497 $new_vocabulary = $this->createVocabulary();
Chris@0 498 $new_term = Term::create([
Chris@0 499 'name' => $term->getName(),
Chris@0 500 'vid' => $new_vocabulary->id(),
Chris@0 501 ]);
Chris@0 502 $new_term->save();
Chris@0 503
Chris@0 504 // Load multiple terms with the same name.
Chris@0 505 $terms = taxonomy_term_load_multiple_by_name($term->getName());
Chris@0 506 $this->assertEqual(count($terms), 2, 'Two terms loaded with the same name.');
Chris@0 507
Chris@0 508 // Load single term when restricted to one vocabulary.
Chris@0 509 $terms = taxonomy_term_load_multiple_by_name($term->getName(), $this->vocabulary->id());
Chris@0 510 $this->assertEqual(count($terms), 1, 'One term loaded when restricted by vocabulary.');
Chris@0 511 $this->assertTrue(isset($terms[$term->id()]), 'Term loaded using exact name and vocabulary machine name.');
Chris@0 512
Chris@0 513 // Create a new term with another name.
Chris@0 514 $term2 = $this->createTerm($this->vocabulary);
Chris@0 515
Chris@0 516 // Try to load a term by name that doesn't exist in this vocabulary but
Chris@0 517 // exists in another vocabulary.
Chris@0 518 $terms = taxonomy_term_load_multiple_by_name($term2->getName(), $new_vocabulary->id());
Chris@0 519 $this->assertFalse($terms, 'Invalid term name restricted by vocabulary machine name not loaded.');
Chris@0 520
Chris@0 521 // Try to load terms filtering by a non-existing vocabulary.
Chris@0 522 $terms = taxonomy_term_load_multiple_by_name($term2->getName(), 'non_existing_vocabulary');
Chris@0 523 $this->assertEqual(count($terms), 0, 'No terms loaded when restricted by a non-existing vocabulary.');
Chris@0 524 }
Chris@0 525
Chris@0 526 /**
Chris@0 527 * Tests that editing and saving a node with no changes works correctly.
Chris@0 528 */
Chris@0 529 public function testReSavingTags() {
Chris@0 530 // Enable tags in the vocabulary.
Chris@0 531 $field = $this->field;
Chris@0 532 entity_get_form_display($field->getTargetEntityTypeId(), $field->getTargetBundle(), 'default')
Chris@0 533 ->setComponent($field->getName(), [
Chris@0 534 'type' => 'entity_reference_autocomplete_tags',
Chris@0 535 ])
Chris@0 536 ->save();
Chris@0 537
Chris@0 538 // Create a term and a node using it.
Chris@0 539 $term = $this->createTerm($this->vocabulary);
Chris@0 540 $edit = [];
Chris@0 541 $edit['title[0][value]'] = $this->randomMachineName(8);
Chris@0 542 $edit['body[0][value]'] = $this->randomMachineName(16);
Chris@0 543 $edit[$this->field->getName() . '[target_id]'] = $term->getName();
Chris@0 544 $this->drupalPostForm('node/add/article', $edit, t('Save'));
Chris@0 545
Chris@0 546 // Check that the term is displayed when editing and saving the node with no
Chris@0 547 // changes.
Chris@0 548 $this->clickLink(t('Edit'));
Chris@0 549 $this->assertRaw($term->getName(), 'Term is displayed when editing the node.');
Chris@0 550 $this->drupalPostForm(NULL, [], t('Save'));
Chris@0 551 $this->assertRaw($term->getName(), 'Term is displayed after saving the node with no changes.');
Chris@0 552 }
Chris@0 553
Chris@0 554 /**
Chris@0 555 * Check the breadcrumb on edit and delete a term page.
Chris@0 556 */
Chris@0 557 public function testTermBreadcrumbs() {
Chris@0 558 $edit = [
Chris@0 559 'name[0][value]' => $this->randomMachineName(14),
Chris@0 560 'description[0][value]' => $this->randomMachineName(100),
Chris@0 561 'parent[]' => [0],
Chris@0 562 ];
Chris@0 563
Chris@0 564 // Create the term.
Chris@0 565 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
Chris@0 566
Chris@0 567 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
Chris@0 568 $term = reset($terms);
Chris@0 569 $this->assertNotNull($term, 'Term found in database.');
Chris@0 570
Chris@0 571 // Check the breadcrumb on the term edit page.
Chris@0 572 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
Chris@0 573 $breadcrumbs = $this->cssSelect('nav.breadcrumb ol li a');
Chris@0 574 $this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.');
Chris@0 575 $this->assertIdentical((string) $breadcrumbs[0], 'Home', 'First breadcrumb text is Home');
Chris@0 576 $this->assertIdentical((string) $breadcrumbs[1], $term->label(), 'Second breadcrumb text is term name on term edit page.');
Chris@0 577 $this->assertEscaped((string) $breadcrumbs[1], 'breadcrumbs displayed and escaped.');
Chris@0 578
Chris@0 579 // Check the breadcrumb on the term delete page.
Chris@0 580 $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
Chris@0 581 $breadcrumbs = $this->cssSelect('nav.breadcrumb ol li a');
Chris@0 582 $this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.');
Chris@0 583 $this->assertIdentical((string) $breadcrumbs[0], 'Home', 'First breadcrumb text is Home');
Chris@0 584 $this->assertIdentical((string) $breadcrumbs[1], $term->label(), 'Second breadcrumb text is term name on term delete page.');
Chris@0 585 $this->assertEscaped((string) $breadcrumbs[1], 'breadcrumbs displayed and escaped.');
Chris@0 586 }
Chris@0 587
Chris@0 588 }