| Drupal | PHP Cross Reference | Content Management Systems |
1 <?php 2 3 /** 4 * @file 5 * Tests for taxonomy.module. 6 */ 7 8 /** 9 * Provides common helper methods for Taxonomy module tests. 10 */ 11 class TaxonomyWebTestCase extends DrupalWebTestCase { 12 13 /** 14 * Returns a new vocabulary with random properties. 15 */ 16 function createVocabulary() { 17 // Create a vocabulary. 18 $vocabulary = new stdClass(); 19 $vocabulary->name = $this->randomName(); 20 $vocabulary->description = $this->randomName(); 21 $vocabulary->machine_name = drupal_strtolower($this->randomName()); 22 $vocabulary->help = ''; 23 $vocabulary->nodes = array('article' => 'article'); 24 $vocabulary->weight = mt_rand(0, 10); 25 taxonomy_vocabulary_save($vocabulary); 26 return $vocabulary; 27 } 28 29 /** 30 * Returns a new term with random properties in vocabulary $vid. 31 */ 32 function createTerm($vocabulary) { 33 $term = new stdClass(); 34 $term->name = $this->randomName(); 35 $term->description = $this->randomName(); 36 // Use the first available text format. 37 $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField(); 38 $term->vid = $vocabulary->vid; 39 taxonomy_term_save($term); 40 return $term; 41 } 42 43 } 44 45 /** 46 * Tests the taxonomy vocabulary interface. 47 */ 48 class TaxonomyVocabularyFunctionalTest extends TaxonomyWebTestCase { 49 50 public static function getInfo() { 51 return array( 52 'name' => 'Taxonomy vocabulary interface', 53 'description' => 'Test the taxonomy vocabulary interface.', 54 'group' => 'Taxonomy', 55 ); 56 } 57 58 function setUp() { 59 parent::setUp(); 60 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy')); 61 $this->drupalLogin($this->admin_user); 62 $this->vocabulary = $this->createVocabulary(); 63 } 64 65 /** 66 * Create, edit and delete a vocabulary via the user interface. 67 */ 68 function testVocabularyInterface() { 69 // Visit the main taxonomy administration page. 70 $this->drupalGet('admin/structure/taxonomy'); 71 72 // Create a new vocabulary. 73 $this->clickLink(t('Add vocabulary')); 74 $edit = array(); 75 $machine_name = drupal_strtolower($this->randomName()); 76 $edit['name'] = $this->randomName(); 77 $edit['description'] = $this->randomName(); 78 $edit['machine_name'] = $machine_name; 79 $this->drupalPost(NULL, $edit, t('Save')); 80 $this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), 'Vocabulary created successfully.'); 81 82 // Edit the vocabulary. 83 $this->drupalGet('admin/structure/taxonomy'); 84 $this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.'); 85 $this->clickLink(t('edit vocabulary')); 86 $edit = array(); 87 $edit['name'] = $this->randomName(); 88 $this->drupalPost(NULL, $edit, t('Save')); 89 $this->drupalGet('admin/structure/taxonomy'); 90 $this->assertText($edit['name'], 'Vocabulary found in the vocabulary overview listing.'); 91 92 // Try to submit a vocabulary with a duplicate machine name. 93 $edit['machine_name'] = $machine_name; 94 $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save')); 95 $this->assertText(t('The machine-readable name is already in use. It must be unique.')); 96 97 // Try to submit an invalid machine name. 98 $edit['machine_name'] = '!&^%'; 99 $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save')); 100 $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.')); 101 102 // Ensure that vocabulary titles are escaped properly. 103 $edit = array(); 104 $edit['name'] = 'Don\'t Panic'; 105 $edit['description'] = $this->randomName(); 106 $edit['machine_name'] = 'don_t_panic'; 107 $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save')); 108 109 $site_name = variable_get('site_name', 'Drupal'); 110 $this->drupalGet('admin/structure/taxonomy/don_t_panic'); 111 $this->assertTitle(t('Don\'t Panic | @site-name', array('@site-name' => $site_name))); 112 $this->assertNoTitle(t('Don't Panic | @site-name', array('@site-name' => $site_name))); 113 } 114 115 /** 116 * Changing weights on the vocabulary overview with two or more vocabularies. 117 */ 118 function testTaxonomyAdminChangingWeights() { 119 // Create some vocabularies. 120 for ($i = 0; $i < 10; $i++) { 121 $this->createVocabulary(); 122 } 123 // Get all vocabularies and change their weights. 124 $vocabularies = taxonomy_get_vocabularies(); 125 $edit = array(); 126 foreach ($vocabularies as $key => $vocabulary) { 127 $vocabulary->weight = -$vocabulary->weight; 128 $vocabularies[$key]->weight = $vocabulary->weight; 129 $edit[$key . '[weight]'] = $vocabulary->weight; 130 } 131 // Saving the new weights via the interface. 132 $this->drupalPost('admin/structure/taxonomy', $edit, t('Save')); 133 134 // Load the vocabularies from the database. 135 $new_vocabularies = taxonomy_get_vocabularies(); 136 137 // Check that the weights are saved in the database correctly. 138 foreach ($vocabularies as $key => $vocabulary) { 139 $this->assertEqual($new_vocabularies[$key]->weight, $vocabularies[$key]->weight, 'The vocabulary weight was changed.'); 140 } 141 } 142 143 /** 144 * Test the vocabulary overview with no vocabularies. 145 */ 146 function testTaxonomyAdminNoVocabularies() { 147 // Delete all vocabularies. 148 $vocabularies = taxonomy_get_vocabularies(); 149 foreach ($vocabularies as $key => $vocabulary) { 150 taxonomy_vocabulary_delete($key); 151 } 152 // Confirm that no vocabularies are found in the database. 153 $this->assertFalse(taxonomy_get_vocabularies(), 'No vocabularies found in the database.'); 154 $this->drupalGet('admin/structure/taxonomy'); 155 // Check the default message for no vocabularies. 156 $this->assertText(t('No vocabularies available.'), 'No vocabularies were found.'); 157 } 158 159 /** 160 * Deleting a vocabulary. 161 */ 162 function testTaxonomyAdminDeletingVocabulary() { 163 // Create a vocabulary. 164 $edit = array( 165 'name' => $this->randomName(), 166 'machine_name' => drupal_strtolower($this->randomName()), 167 ); 168 $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save')); 169 $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.'); 170 171 // Check the created vocabulary. 172 $vocabularies = taxonomy_get_vocabularies(); 173 $vid = $vocabularies[count($vocabularies) - 1]->vid; 174 entity_get_controller('taxonomy_vocabulary')->resetCache(); 175 $vocabulary = taxonomy_vocabulary_load($vid); 176 $this->assertTrue($vocabulary, 'Vocabulary found in database.'); 177 178 // Delete the vocabulary. 179 $edit = array(); 180 $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/edit', $edit, t('Delete')); 181 $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', array('%name' => $vocabulary->name)), '[confirm deletion] Asks for confirmation.'); 182 $this->assertText(t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'), '[confirm deletion] Inform that all terms will be deleted.'); 183 184 // Confirm deletion. 185 $this->drupalPost(NULL, NULL, t('Delete')); 186 $this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), 'Vocabulary deleted.'); 187 entity_get_controller('taxonomy_vocabulary')->resetCache(); 188 $this->assertFalse(taxonomy_vocabulary_load($vid), 'Vocabulary is not found in the database.'); 189 } 190 191 } 192 193 /** 194 * Tests for taxonomy vocabulary functions. 195 */ 196 class TaxonomyVocabularyTestCase extends TaxonomyWebTestCase { 197 198 public static function getInfo() { 199 return array( 200 'name' => 'Taxonomy vocabularies', 201 'description' => 'Test loading, saving and deleting vocabularies.', 202 'group' => 'Taxonomy', 203 ); 204 } 205 206 function setUp() { 207 parent::setUp('taxonomy', 'field_test'); 208 $admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy')); 209 $this->drupalLogin($admin_user); 210 $this->vocabulary = $this->createVocabulary(); 211 } 212 213 /** 214 * Ensure that when an invalid vocabulary vid is loaded, it is possible 215 * to load the same vid successfully if it subsequently becomes valid. 216 */ 217 function testTaxonomyVocabularyLoadReturnFalse() { 218 // Load a vocabulary that doesn't exist. 219 $vocabularies = taxonomy_get_vocabularies(); 220 $vid = count($vocabularies) + 1; 221 $vocabulary = taxonomy_vocabulary_load($vid); 222 // This should not return an object because no such vocabulary exists. 223 $this->assertTrue(empty($vocabulary), 'No object loaded.'); 224 225 // Create a new vocabulary. 226 $this->createVocabulary(); 227 // Load the vocabulary with the same $vid from earlier. 228 // This should return a vocabulary object since it now matches a real vid. 229 $vocabulary = taxonomy_vocabulary_load($vid); 230 $this->assertTrue(!empty($vocabulary) && is_object($vocabulary), 'Vocabulary is an object.'); 231 $this->assertEqual($vocabulary->vid, $vid, 'Valid vocabulary vid is the same as our previously invalid one.'); 232 } 233 234 /** 235 * Test deleting a taxonomy that contains terms. 236 */ 237 function testTaxonomyVocabularyDeleteWithTerms() { 238 // Delete any existing vocabularies. 239 foreach (taxonomy_get_vocabularies() as $vocabulary) { 240 taxonomy_vocabulary_delete($vocabulary->vid); 241 } 242 243 // Assert that there are no terms left. 244 $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField()); 245 246 // Create a new vocabulary and add a few terms to it. 247 $vocabulary = $this->createVocabulary(); 248 $terms = array(); 249 for ($i = 0; $i < 5; $i++) { 250 $terms[$i] = $this->createTerm($vocabulary); 251 } 252 253 // Set up hierarchy. term 2 is a child of 1 and 4 a child of 1 and 2. 254 $terms[2]->parent = array($terms[1]->tid); 255 taxonomy_term_save($terms[2]); 256 $terms[4]->parent = array($terms[1]->tid, $terms[2]->tid); 257 taxonomy_term_save($terms[4]); 258 259 // Assert that there are now 5 terms. 260 $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField()); 261 262 taxonomy_vocabulary_delete($vocabulary->vid); 263 264 // Assert that there are no terms left. 265 $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField()); 266 } 267 268 /** 269 * Ensure that the vocabulary static reset works correctly. 270 */ 271 function testTaxonomyVocabularyLoadStaticReset() { 272 $original_vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid); 273 $this->assertTrue(is_object($original_vocabulary), 'Vocabulary loaded successfully.'); 274 $this->assertEqual($this->vocabulary->name, $original_vocabulary->name, 'Vocabulary loaded successfully.'); 275 276 // Change the name and description. 277 $vocabulary = $original_vocabulary; 278 $vocabulary->name = $this->randomName(); 279 $vocabulary->description = $this->randomName(); 280 taxonomy_vocabulary_save($vocabulary); 281 282 // Load the vocabulary. 283 $new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid); 284 $this->assertEqual($new_vocabulary->name, $vocabulary->name); 285 $this->assertEqual($new_vocabulary->name, $vocabulary->name); 286 287 // Delete the vocabulary. 288 taxonomy_vocabulary_delete($this->vocabulary->vid); 289 $vocabularies = taxonomy_get_vocabularies(); 290 $this->assertTrue(!isset($vocabularies[$this->vocabulary->vid]), 'The vocabulary was deleted.'); 291 } 292 293 /** 294 * Tests for loading multiple vocabularies. 295 */ 296 function testTaxonomyVocabularyLoadMultiple() { 297 298 // Delete any existing vocabularies. 299 foreach (taxonomy_get_vocabularies() as $vocabulary) { 300 taxonomy_vocabulary_delete($vocabulary->vid); 301 } 302 303 // Create some vocabularies and assign weights. 304 $vocabulary1 = $this->createVocabulary(); 305 $vocabulary1->weight = 0; 306 taxonomy_vocabulary_save($vocabulary1); 307 $vocabulary2 = $this->createVocabulary(); 308 $vocabulary2->weight = 1; 309 taxonomy_vocabulary_save($vocabulary2); 310 $vocabulary3 = $this->createVocabulary(); 311 $vocabulary3->weight = 2; 312 taxonomy_vocabulary_save($vocabulary3); 313 314 // Fetch the names for all vocabularies, confirm that they are keyed by 315 // machine name. 316 $names = taxonomy_vocabulary_get_names(); 317 $this->assertEqual($names[$vocabulary1->machine_name]->name, $vocabulary1->name, 'Vocabulary 1 name found.'); 318 319 // Fetch all of the vocabularies using taxonomy_get_vocabularies(). 320 // Confirm that the vocabularies are ordered by weight. 321 $vocabularies = taxonomy_get_vocabularies(); 322 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, 'Vocabulary was found in the vocabularies array.'); 323 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, 'Vocabulary was found in the vocabularies array.'); 324 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, 'Vocabulary was found in the vocabularies array.'); 325 326 // Fetch the vocabularies with taxonomy_vocabulary_load_multiple(), specifying IDs. 327 // Ensure they are returned in the same order as the original array. 328 $vocabularies = taxonomy_vocabulary_load_multiple(array($vocabulary3->vid, $vocabulary2->vid, $vocabulary1->vid)); 329 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, 'Vocabulary loaded successfully by ID.'); 330 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, 'Vocabulary loaded successfully by ID.'); 331 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by ID.'); 332 333 // Fetch vocabulary 1 by name. 334 $vocabulary = current(taxonomy_vocabulary_load_multiple(array(), array('name' => $vocabulary1->name))); 335 $this->assertEqual($vocabulary->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by name.'); 336 337 // Fetch vocabulary 1 by name and ID. 338 $this->assertEqual(current(taxonomy_vocabulary_load_multiple(array($vocabulary1->vid), array('name' => $vocabulary1->name)))->vid, $vocabulary1->vid, 'Vocabulary loaded successfully by name and ID.'); 339 } 340 341 /** 342 * Tests that machine name changes are properly reflected. 343 */ 344 function testTaxonomyVocabularyChangeMachineName() { 345 // Add a field instance to the vocabulary. 346 $field = array( 347 'field_name' => 'field_test', 348 'type' => 'test_field', 349 ); 350 field_create_field($field); 351 $instance = array( 352 'field_name' => 'field_test', 353 'entity_type' => 'taxonomy_term', 354 'bundle' => $this->vocabulary->machine_name, 355 ); 356 field_create_instance($instance); 357 358 // Change the machine name. 359 $new_name = drupal_strtolower($this->randomName()); 360 $this->vocabulary->machine_name = $new_name; 361 taxonomy_vocabulary_save($this->vocabulary); 362 363 // Check that the field instance is still attached to the vocabulary. 364 $this->assertTrue(field_info_instance('taxonomy_term', 'field_test', $new_name), 'The bundle name was updated correctly.'); 365 } 366 367 /** 368 * Test uninstall and reinstall of the taxonomy module. 369 */ 370 function testUninstallReinstall() { 371 // Fields and field instances attached to taxonomy term bundles should be 372 // removed when the module is uninstalled. 373 $this->field_name = drupal_strtolower($this->randomName() . '_field_name'); 374 $this->field = array('field_name' => $this->field_name, 'type' => 'text', 'cardinality' => 4); 375 $this->field = field_create_field($this->field); 376 $this->instance = array( 377 'field_name' => $this->field_name, 378 'entity_type' => 'taxonomy_term', 379 'bundle' => $this->vocabulary->machine_name, 380 'label' => $this->randomName() . '_label', 381 ); 382 field_create_instance($this->instance); 383 384 module_disable(array('taxonomy')); 385 require_once DRUPAL_ROOT . '/includes/install.inc'; 386 drupal_uninstall_modules(array('taxonomy')); 387 module_enable(array('taxonomy')); 388 389 // Now create a vocabulary with the same name. All field instances 390 // connected to this vocabulary name should have been removed when the 391 // module was uninstalled. Creating a new field with the same name and 392 // an instance of this field on the same bundle name should be successful. 393 unset($this->vocabulary->vid); 394 taxonomy_vocabulary_save($this->vocabulary); 395 unset($this->field['id']); 396 field_create_field($this->field); 397 field_create_instance($this->instance); 398 } 399 400 } 401 402 /** 403 * Unit tests for taxonomy term functions. 404 */ 405 class TaxonomyTermFunctionTestCase extends TaxonomyWebTestCase { 406 407 public static function getInfo() { 408 return array( 409 'name' => 'Taxonomy term unit tests', 410 'description' => 'Unit tests for taxonomy term functions.', 411 'group' => 'Taxonomy', 412 ); 413 } 414 415 function testTermDelete() { 416 $vocabulary = $this->createVocabulary(); 417 $valid_term = $this->createTerm($vocabulary); 418 // Delete a valid term. 419 taxonomy_term_delete($valid_term->tid); 420 $terms = taxonomy_term_load_multiple(array(), array('vid' => $vocabulary->vid)); 421 $this->assertTrue(empty($terms), 'Vocabulary is empty after deletion.'); 422 423 // Delete an invalid term. Should not throw any notices. 424 taxonomy_term_delete(42); 425 } 426 427 /** 428 * Test a taxonomy with terms that have multiple parents of different depths. 429 */ 430 function testTaxonomyVocabularyTree() { 431 // Create a new vocabulary with 6 terms. 432 $vocabulary = $this->createVocabulary(); 433 $term = array(); 434 for ($i = 0; $i < 6; $i++) { 435 $term[$i] = $this->createTerm($vocabulary); 436 } 437 438 // $term[2] is a child of 1 and 5. 439 $term[2]->parent = array($term[1]->tid, $term[5]->tid); 440 taxonomy_term_save($term[2]); 441 // $term[3] is a child of 2. 442 $term[3]->parent = array($term[2]->tid); 443 taxonomy_term_save($term[3]); 444 // $term[5] is a child of 4. 445 $term[5]->parent = array($term[4]->tid); 446 taxonomy_term_save($term[5]); 447 448 /** 449 * Expected tree: 450 * term[0] | depth: 0 451 * term[1] | depth: 0 452 * -- term[2] | depth: 1 453 * ---- term[3] | depth: 2 454 * term[4] | depth: 0 455 * -- term[5] | depth: 1 456 * ---- term[2] | depth: 2 457 * ------ term[3] | depth: 3 458 */ 459 // Count $term[1] parents with $max_depth = 1. 460 $tree = taxonomy_get_tree($vocabulary->vid, $term[1]->tid, 1); 461 $this->assertEqual(1, count($tree), 'We have one parent with depth 1.'); 462 463 // Count all vocabulary tree elements. 464 $tree = taxonomy_get_tree($vocabulary->vid); 465 $this->assertEqual(8, count($tree), 'We have all vocabulary tree elements.'); 466 467 // Count elements in every tree depth. 468 foreach ($tree as $element) { 469 if (!isset($depth_count[$element->depth])) { 470 $depth_count[$element->depth] = 0; 471 } 472 $depth_count[$element->depth]++; 473 } 474 $this->assertEqual(3, $depth_count[0], 'Three elements in taxonomy tree depth 0.'); 475 $this->assertEqual(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.'); 476 $this->assertEqual(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.'); 477 $this->assertEqual(1, $depth_count[3], 'One element in taxonomy tree depth 3.'); 478 } 479 480 } 481 482 /** 483 * Test for legacy node bug. 484 */ 485 class TaxonomyLegacyTestCase extends TaxonomyWebTestCase { 486 487 public static function getInfo() { 488 return array( 489 'name' => 'Test for legacy node bug.', 490 'description' => 'Posts an article with a taxonomy term and a date prior to 1970.', 491 'group' => 'Taxonomy', 492 ); 493 } 494 495 function setUp() { 496 parent::setUp('taxonomy'); 497 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access')); 498 $this->drupalLogin($this->admin_user); 499 } 500 501 /** 502 * Test taxonomy functionality with nodes prior to 1970. 503 */ 504 function testTaxonomyLegacyNode() { 505 // Posts an article with a taxonomy term and a date prior to 1970. 506 $langcode = LANGUAGE_NONE; 507 $edit = array(); 508 $edit['title'] = $this->randomName(); 509 $edit['date'] = '1969-01-01 00:00:00 -0500'; 510 $edit["body[$langcode][0][value]"] = $this->randomName(); 511 $edit["field_tags[$langcode]"] = $this->randomName(); 512 $this->drupalPost('node/add/article', $edit, t('Save')); 513 // Checks that the node has been saved. 514 $node = $this->drupalGetNodeByTitle($edit['title']); 515 $this->assertEqual($node->created, strtotime($edit['date']), 'Legacy node was saved with the right date.'); 516 } 517 518 } 519 520 /** 521 * Tests for taxonomy term functions. 522 */ 523 class TaxonomyTermTestCase extends TaxonomyWebTestCase { 524 525 public static function getInfo() { 526 return array( 527 'name' => 'Taxonomy term functions and forms.', 528 'description' => 'Test load, save and delete for taxonomy terms.', 529 'group' => 'Taxonomy', 530 ); 531 } 532 533 function setUp() { 534 parent::setUp('taxonomy'); 535 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access')); 536 $this->drupalLogin($this->admin_user); 537 $this->vocabulary = $this->createVocabulary(); 538 539 $field = array( 540 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name, 541 'type' => 'taxonomy_term_reference', 542 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 543 'settings' => array( 544 'allowed_values' => array( 545 array( 546 'vocabulary' => $this->vocabulary->machine_name, 547 'parent' => 0, 548 ), 549 ), 550 ), 551 ); 552 field_create_field($field); 553 554 $this->instance = array( 555 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name, 556 'bundle' => 'article', 557 'entity_type' => 'node', 558 'widget' => array( 559 'type' => 'options_select', 560 ), 561 'display' => array( 562 'default' => array( 563 'type' => 'taxonomy_term_reference_link', 564 ), 565 ), 566 ); 567 field_create_instance($this->instance); 568 } 569 570 /** 571 * Test terms in a single and multiple hierarchy. 572 */ 573 function testTaxonomyTermHierarchy() { 574 // Create two taxonomy terms. 575 $term1 = $this->createTerm($this->vocabulary); 576 $term2 = $this->createTerm($this->vocabulary); 577 578 // Check that hierarchy is flat. 579 $vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid); 580 $this->assertEqual(0, $vocabulary->hierarchy, 'Vocabulary is flat.'); 581 582 // Edit $term2, setting $term1 as parent. 583 $edit = array(); 584 $edit['parent[]'] = array($term1->tid); 585 $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save')); 586 587 // Check the hierarchy. 588 $children = taxonomy_get_children($term1->tid); 589 $parents = taxonomy_get_parents($term2->tid); 590 $this->assertTrue(isset($children[$term2->tid]), 'Child found correctly.'); 591 $this->assertTrue(isset($parents[$term1->tid]), 'Parent found correctly.'); 592 593 // Load and save a term, confirming that parents are still set. 594 $term = taxonomy_term_load($term2->tid); 595 taxonomy_term_save($term); 596 $parents = taxonomy_get_parents($term2->tid); 597 $this->assertTrue(isset($parents[$term1->tid]), 'Parent found correctly.'); 598 599 // Create a third term and save this as a parent of term2. 600 $term3 = $this->createTerm($this->vocabulary); 601 $term2->parent = array($term1->tid, $term3->tid); 602 taxonomy_term_save($term2); 603 $parents = taxonomy_get_parents($term2->tid); 604 $this->assertTrue(isset($parents[$term1->tid]) && isset($parents[$term3->tid]), 'Both parents found successfully.'); 605 } 606 607 /** 608 * Test that hook_node_$op implementations work correctly. 609 * 610 * Save & edit a node and assert that taxonomy terms are saved/loaded properly. 611 */ 612 function testTaxonomyNode() { 613 // Create two taxonomy terms. 614 $term1 = $this->createTerm($this->vocabulary); 615 $term2 = $this->createTerm($this->vocabulary); 616 617 // Post an article. 618 $edit = array(); 619 $langcode = LANGUAGE_NONE; 620 $edit["title"] = $this->randomName(); 621 $edit["body[$langcode][0][value]"] = $this->randomName(); 622 $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid; 623 $this->drupalPost('node/add/article', $edit, t('Save')); 624 625 // Check that the term is displayed when the node is viewed. 626 $node = $this->drupalGetNodeByTitle($edit["title"]); 627 $this->drupalGet('node/' . $node->nid); 628 $this->assertText($term1->name, 'Term is displayed when viewing the node.'); 629 630 // Edit the node with a different term. 631 $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term2->tid; 632 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); 633 634 $this->drupalGet('node/' . $node->nid); 635 $this->assertText($term2->name, 'Term is displayed when viewing the node.'); 636 637 // Preview the node. 638 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Preview')); 639 $this->assertNoUniqueText($term2->name, 'Term is displayed when previewing the node.'); 640 $this->drupalPost(NULL, NULL, t('Preview')); 641 $this->assertNoUniqueText($term2->name, 'Term is displayed when previewing the node again.'); 642 } 643 644 /** 645 * Test term creation with a free-tagging vocabulary from the node form. 646 */ 647 function testNodeTermCreationAndDeletion() { 648 // Enable tags in the vocabulary. 649 $instance = $this->instance; 650 $instance['widget'] = array('type' => 'taxonomy_autocomplete'); 651 $instance['bundle'] = 'page'; 652 field_create_instance($instance); 653 $terms = array( 654 'term1' => $this->randomName(), 655 'term2' => $this->randomName() . ', ' . $this->randomName(), 656 'term3' => $this->randomName(), 657 ); 658 659 $edit = array(); 660 $langcode = LANGUAGE_NONE; 661 $edit["title"] = $this->randomName(); 662 $edit["body[$langcode][0][value]"] = $this->randomName(); 663 // Insert the terms in a comma separated list. Vocabulary 1 is a 664 // free-tagging field created by the default profile. 665 $edit[$instance['field_name'] . "[$langcode]"] = drupal_implode_tags($terms); 666 667 // Preview and verify the terms appear but are not created. 668 $this->drupalPost('node/add/page', $edit, t('Preview')); 669 foreach ($terms as $term) { 670 $this->assertText($term, 'The term appears on the node preview.'); 671 } 672 $tree = taxonomy_get_tree($this->vocabulary->vid); 673 $this->assertTrue(empty($tree), 'The terms are not created on preview.'); 674 675 // taxonomy.module does not maintain its static caches. 676 drupal_static_reset(); 677 678 // Save, creating the terms. 679 $this->drupalPost('node/add/page', $edit, t('Save')); 680 $this->assertRaw(t('@type %title has been created.', array('@type' => t('Basic page'), '%title' => $edit["title"])), 'The node was created successfully.'); 681 foreach ($terms as $term) { 682 $this->assertText($term, 'The term was saved and appears on the node page.'); 683 } 684 685 // Get the created terms. 686 $term_objects = array(); 687 foreach ($terms as $key => $term) { 688 $term_objects[$key] = taxonomy_get_term_by_name($term); 689 $term_objects[$key] = reset($term_objects[$key]); 690 } 691 692 // Delete term 1. 693 $this->drupalPost('taxonomy/term/' . $term_objects['term1']->tid . '/edit', array(), t('Delete')); 694 $this->drupalPost(NULL, NULL, t('Delete')); 695 $term_names = array($term_objects['term2']->name, $term_objects['term3']->name); 696 697 // Get the node. 698 $node = $this->drupalGetNodeByTitle($edit["title"]); 699 $this->drupalGet('node/' . $node->nid); 700 701 foreach ($term_names as $term_name) { 702 $this->assertText($term_name, format_string('The term %name appears on the node page after one term %deleted was deleted', array('%name' => $term_name, '%deleted' => $term_objects['term1']->name))); 703 } 704 $this->assertNoText($term_objects['term1']->name, format_string('The deleted term %name does not appear on the node page.', array('%name' => $term_objects['term1']->name))); 705 706 // Test autocomplete on term 2, which contains a comma. 707 // The term will be quoted, and the " will be encoded in unicode (\u0022). 708 $input = substr($term_objects['term2']->name, 0, 3); 709 $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input); 710 $this->assertRaw('{"\u0022' . $term_objects['term2']->name . '\u0022":"' . $term_objects['term2']->name . '"}', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term2']->name))); 711 712 // Test autocomplete on term 3 - it is alphanumeric only, so no extra 713 // quoting. 714 $input = substr($term_objects['term3']->name, 0, 3); 715 $this->drupalGet('taxonomy/autocomplete/taxonomy_' . $this->vocabulary->machine_name . '/' . $input); 716 $this->assertRaw('{"' . $term_objects['term3']->name . '":"' . $term_objects['term3']->name . '"}', format_string('Autocomplete returns term %term_name after typing the first 3 letters.', array('%term_name' => $term_objects['term3']->name))); 717 718 // Test taxonomy autocomplete with a nonexistent field. 719 $field_name = $this->randomName(); 720 $tag = $this->randomName(); 721 $message = t("Taxonomy field @field_name not found.", array('@field_name' => $field_name)); 722 $this->assertFalse(field_info_field($field_name), format_string('Field %field_name does not exist.', array('%field_name' => $field_name))); 723 $this->drupalGet('taxonomy/autocomplete/' . $field_name . '/' . $tag); 724 $this->assertRaw($message, 'Autocomplete returns correct error message when the taxonomy field does not exist.'); 725 } 726 727 /** 728 * Tests term autocompletion edge cases with slashes in the names. 729 */ 730 function testTermAutocompletion() { 731 // Add a term with a slash in the name. 732 $first_term = $this->createTerm($this->vocabulary); 733 $first_term->name = '10/16/2011'; 734 taxonomy_term_save($first_term); 735 // Add another term that differs after the slash character. 736 $second_term = $this->createTerm($this->vocabulary); 737 $second_term->name = '10/17/2011'; 738 taxonomy_term_save($second_term); 739 // Add another term that has both a comma and a slash character. 740 $third_term = $this->createTerm($this->vocabulary); 741 $third_term->name = 'term with, a comma and / a slash'; 742 taxonomy_term_save($third_term); 743 744 // Try to autocomplete a term name that matches both terms. 745 // We should get both term in a json encoded string. 746 $input = '10/'; 747 $path = 'taxonomy/autocomplete/taxonomy_'; 748 $path .= $this->vocabulary->machine_name . '/' . $input; 749 // The result order is not guaranteed, so check each term separately. 750 $url = url($path, array('absolute' => TRUE)); 751 $result = drupal_http_request($url); 752 $data = drupal_json_decode($result->data); 753 $this->assertEqual($data[$first_term->name], check_plain($first_term->name), 'Autocomplete returned the first matching term.'); 754 $this->assertEqual($data[$second_term->name], check_plain($second_term->name), 'Autocomplete returned the second matching term.'); 755 756 // Try to autocomplete a term name that matches first term. 757 // We should only get the first term in a json encoded string. 758 $input = '10/16'; 759 $url = 'taxonomy/autocomplete/taxonomy_'; 760 $url .= $this->vocabulary->machine_name . '/' . $input; 761 $this->drupalGet($url); 762 $target = array($first_term->name => check_plain($first_term->name)); 763 $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns only the expected matching term.'); 764 765 // Try to autocomplete a term name with both a comma and a slash. 766 $input = '"term with, comma and / a'; 767 $url = 'taxonomy/autocomplete/taxonomy_'; 768 $url .= $this->vocabulary->machine_name . '/' . $input; 769 $this->drupalGet($url); 770 $n = $third_term->name; 771 // Term names containing commas or quotes must be wrapped in quotes. 772 if (strpos($third_term->name, ',') !== FALSE || strpos($third_term->name, '"') !== FALSE) { 773 $n = '"' . str_replace('"', '""', $third_term->name) . '"'; 774 } 775 $target = array($n => check_plain($third_term->name)); 776 $this->assertRaw(drupal_json_encode($target), 'Autocomplete returns a term containing a comma and a slash.'); 777 } 778 779 /** 780 * Save, edit and delete a term using the user interface. 781 */ 782 function testTermInterface() { 783 $edit = array( 784 'name' => $this->randomName(12), 785 'description[value]' => $this->randomName(100), 786 ); 787 // Explicitly set the parents field to 'root', to ensure that 788 // taxonomy_form_term_submit() handles the invalid term ID correctly. 789 $edit['parent[]'] = array(0); 790 791 // Create the term to edit. 792 $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save')); 793 794 $terms = taxonomy_get_term_by_name($edit['name']); 795 $term = reset($terms); 796 $this->assertNotNull($term, 'Term found in database.'); 797 798 // Submitting a term takes us to the add page; we need the List page. 799 $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name); 800 801 // Test edit link as accessed from Taxonomy administration pages. 802 // Because Simpletest creates its own database when running tests, we know 803 // the first edit link found on the listing page is to our term. 804 $this->clickLink(t('edit')); 805 806 $this->assertRaw($edit['name'], 'The randomly generated term name is present.'); 807 $this->assertText($edit['description[value]'], 'The randomly generated term description is present.'); 808 809 $edit = array( 810 'name' => $this->randomName(14), 811 'description[value]' => $this->randomName(102), 812 ); 813 814 // Edit the term. 815 $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save')); 816 817 // Check that the term is still present at admin UI after edit. 818 $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name); 819 $this->assertText($edit['name'], 'The randomly generated term name is present.'); 820 $this->assertLink(t('edit')); 821 822 // View the term and check that it is correct. 823 $this->drupalGet('taxonomy/term/' . $term->tid); 824 $this->assertText($edit['name'], 'The randomly generated term name is present.'); 825 $this->assertText($edit['description[value]'], 'The randomly generated term description is present.'); 826 827 // Did this page request display a 'term-listing-heading'? 828 $this->assertPattern('|class="taxonomy-term-description"|', 'Term page displayed the term description element.'); 829 // Check that it does NOT show a description when description is blank. 830 $term->description = ''; 831 taxonomy_term_save($term); 832 $this->drupalGet('taxonomy/term/' . $term->tid); 833 $this->assertNoPattern('|class="taxonomy-term-description"|', 'Term page did not display the term description when description was blank.'); 834 835 // Check that the term feed page is working. 836 $this->drupalGet('taxonomy/term/' . $term->tid . '/feed'); 837 838 // Check that the term edit page does not try to interpret additional path 839 // components as arguments for taxonomy_form_term(). 840 $this->drupalGet('taxonomy/term/' . $term->tid . '/edit/' . $this->randomName()); 841 842 // Delete the term. 843 $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', array(), t('Delete')); 844 $this->drupalPost(NULL, NULL, t('Delete')); 845 846 // Assert that the term no longer exists. 847 $this->drupalGet('taxonomy/term/' . $term->tid); 848 $this->assertResponse(404, 'The taxonomy term page was not found.'); 849 } 850 851 /** 852 * Save, edit and delete a term using the user interface. 853 */ 854 function testTermReorder() { 855 $this->createTerm($this->vocabulary); 856 $this->createTerm($this->vocabulary); 857 $this->createTerm($this->vocabulary); 858 859 // Fetch the created terms in the default alphabetical order, i.e. term1 860 // precedes term2 alphabetically, and term2 precedes term3. 861 drupal_static_reset('taxonomy_get_tree'); 862 drupal_static_reset('taxonomy_get_treeparent'); 863 drupal_static_reset('taxonomy_get_treeterms'); 864 list($term1, $term2, $term3) = taxonomy_get_tree($this->vocabulary->vid); 865 866 $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name); 867 868 // Each term has four hidden fields, "tid:1:0[tid]", "tid:1:0[parent]", 869 // "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2, 870 // term3, term1 by setting weight property, make term3 a child of term2 by 871 // setting the parent and depth properties, and update all hidden fields. 872 $edit = array( 873 'tid:' . $term2->tid . ':0[tid]' => $term2->tid, 874 'tid:' . $term2->tid . ':0[parent]' => 0, 875 'tid:' . $term2->tid . ':0[depth]' => 0, 876 'tid:' . $term2->tid . ':0[weight]' => 0, 877 'tid:' . $term3->tid . ':0[tid]' => $term3->tid, 878 'tid:' . $term3->tid . ':0[parent]' => $term2->tid, 879 'tid:' . $term3->tid . ':0[depth]' => 1, 880 'tid:' . $term3->tid . ':0[weight]' => 1, 881 'tid:' . $term1->tid . ':0[tid]' => $term1->tid, 882 'tid:' . $term1->tid . ':0[parent]' => 0, 883 'tid:' . $term1->tid . ':0[depth]' => 0, 884 'tid:' . $term1->tid . ':0[weight]' => 2, 885 ); 886 $this->drupalPost(NULL, $edit, t('Save')); 887 888 drupal_static_reset('taxonomy_get_tree'); 889 drupal_static_reset('taxonomy_get_treeparent'); 890 drupal_static_reset('taxonomy_get_treeterms'); 891 $terms = taxonomy_get_tree($this->vocabulary->vid); 892 $this->assertEqual($terms[0]->tid, $term2->tid, 'Term 2 was moved above term 1.'); 893 $this->assertEqual($terms[1]->parents, array($term2->tid), 'Term 3 was made a child of term 2.'); 894 $this->assertEqual($terms[2]->tid, $term1->tid, 'Term 1 was moved below term 2.'); 895 896 $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name, array(), t('Reset to alphabetical')); 897 // Submit confirmation form. 898 $this->drupalPost(NULL, array(), t('Reset to alphabetical')); 899 900 drupal_static_reset('taxonomy_get_tree'); 901 drupal_static_reset('taxonomy_get_treeparent'); 902 drupal_static_reset('taxonomy_get_treeterms'); 903 $terms = taxonomy_get_tree($this->vocabulary->vid); 904 $this->assertEqual($terms[0]->tid, $term1->tid, 'Term 1 was moved to back above term 2.'); 905 $this->assertEqual($terms[1]->tid, $term2->tid, 'Term 2 was moved to back below term 1.'); 906 $this->assertEqual($terms[2]->tid, $term3->tid, 'Term 3 is still below term 2.'); 907 $this->assertEqual($terms[2]->parents, array($term2->tid), 'Term 3 is still a child of term 2.' . var_export($terms[1]->tid, 1)); 908 } 909 910 /** 911 * Test saving a term with multiple parents through the UI. 912 */ 913 function testTermMultipleParentsInterface() { 914 // Add a new term to the vocabulary so that we can have multiple parents. 915 $parent = $this->createTerm($this->vocabulary); 916 917 // Add a new term with multiple parents. 918 $edit = array( 919 'name' => $this->randomName(12), 920 'description[value]' => $this->randomName(100), 921 'parent[]' => array(0, $parent->tid), 922 ); 923 // Save the new term. 924 $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add', $edit, t('Save')); 925 926 // Check that the term was successfully created. 927 $terms = taxonomy_get_term_by_name($edit['name']); 928 $term = reset($terms); 929 $this->assertNotNull($term, 'Term found in database.'); 930 $this->assertEqual($edit['name'], $term->name, 'Term name was successfully saved.'); 931 $this->assertEqual($edit['description[value]'], $term->description, 'Term description was successfully saved.'); 932 // Check that the parent tid is still there. The other parent (<root>) is 933 // not added by taxonomy_get_parents(). 934 $parents = taxonomy_get_parents($term->tid); 935 $parent = reset($parents); 936 $this->assertEqual($edit['parent[]'][1], $parent->tid, 'Term parents were successfully saved.'); 937 } 938 939 /** 940 * Test taxonomy_get_term_by_name(). 941 */ 942 function testTaxonomyGetTermByName() { 943 $term = $this->createTerm($this->vocabulary); 944 945 // Load the term with the exact name. 946 $terms = taxonomy_get_term_by_name($term->name); 947 $this->assertTrue(isset($terms[$term->tid]), 'Term loaded using exact name.'); 948 949 // Load the term with space concatenated. 950 $terms = taxonomy_get_term_by_name(' ' . $term->name . ' '); 951 $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with extra whitespace.'); 952 953 // Load the term with name uppercased. 954 $terms = taxonomy_get_term_by_name(strtoupper($term->name)); 955 $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with uppercased name.'); 956 957 // Load the term with name lowercased. 958 $terms = taxonomy_get_term_by_name(strtolower($term->name)); 959 $this->assertTrue(isset($terms[$term->tid]), 'Term loaded with lowercased name.'); 960 961 // Try to load an invalid term name. 962 $terms = taxonomy_get_term_by_name('Banana'); 963 $this->assertFalse($terms); 964 965 // Try to load the term using a substring of the name. 966 $terms = taxonomy_get_term_by_name(drupal_substr($term->name, 2)); 967 $this->assertFalse($terms); 968 969 // Create a new term in a different vocabulary with the same name. 970 $new_vocabulary = $this->createVocabulary(); 971 $new_term = new stdClass(); 972 $new_term->name = $term->name; 973 $new_term->vid = $new_vocabulary->vid; 974 taxonomy_term_save($new_term); 975 976 // Load multiple terms with the same name. 977 $terms = taxonomy_get_term_by_name($term->name); 978 $this->assertEqual(count($terms), 2, 'Two terms loaded with the same name.'); 979 980 // Load single term when restricted to one vocabulary. 981 $terms = taxonomy_get_term_by_name($term->name, $this->vocabulary->machine_name); 982 $this->assertEqual(count($terms), 1, 'One term loaded when restricted by vocabulary.'); 983 $this->assertTrue(isset($terms[$term->tid]), 'Term loaded using exact name and vocabulary machine name.'); 984 985 // Create a new term with another name. 986 $term2 = $this->createTerm($this->vocabulary); 987 988 // Try to load a term by name that doesn't exist in this vocabulary but 989 // exists in another vocabulary. 990 $terms = taxonomy_get_term_by_name($term2->name, $new_vocabulary->machine_name); 991 $this->assertFalse($terms, 'Invalid term name restricted by vocabulary machine name not loaded.'); 992 993 // Try to load terms filtering by a non-existing vocabulary. 994 $terms = taxonomy_get_term_by_name($term2->name, 'non_existing_vocabulary'); 995 $this->assertEqual(count($terms), 0, 'No terms loaded when restricted by a non-existing vocabulary.'); 996 } 997 998 } 999 1000 /** 1001 * Tests the rendering of term reference fields in RSS feeds. 1002 */ 1003 class TaxonomyRSSTestCase extends TaxonomyWebTestCase { 1004 1005 public static function getInfo() { 1006 return array( 1007 'name' => 'Taxonomy RSS Content.', 1008 'description' => 'Ensure that data added as terms appears in RSS feeds if "RSS Category" format is selected.', 1009 'group' => 'Taxonomy', 1010 ); 1011 } 1012 1013 function setUp() { 1014 parent::setUp('taxonomy'); 1015 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access', 'administer content types')); 1016 $this->drupalLogin($this->admin_user); 1017 $this->vocabulary = $this->createVocabulary(); 1018 1019 $field = array( 1020 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name, 1021 'type' => 'taxonomy_term_reference', 1022 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 1023 'settings' => array( 1024 'allowed_values' => array( 1025 array( 1026 'vocabulary' => $this->vocabulary->machine_name, 1027 'parent' => 0, 1028 ), 1029 ), 1030 ), 1031 ); 1032 field_create_field($field); 1033 1034 $this->instance = array( 1035 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name, 1036 'bundle' => 'article', 1037 'entity_type' => 'node', 1038 'widget' => array( 1039 'type' => 'options_select', 1040 ), 1041 'display' => array( 1042 'default' => array( 1043 'type' => 'taxonomy_term_reference_link', 1044 ), 1045 ), 1046 ); 1047 field_create_instance($this->instance); 1048 } 1049 1050 /** 1051 * Tests that terms added to nodes are displayed in core RSS feed. 1052 * 1053 * Create a node and assert that taxonomy terms appear in rss.xml. 1054 */ 1055 function testTaxonomyRSS() { 1056 // Create two taxonomy terms. 1057 $term1 = $this->createTerm($this->vocabulary); 1058 1059 // RSS display must be added manually. 1060 $this->drupalGet("admin/structure/types/manage/article/display"); 1061 $edit = array( 1062 "view_modes_custom[rss]" => '1', 1063 ); 1064 $this->drupalPost(NULL, $edit, t('Save')); 1065 1066 // Change the format to 'RSS category'. 1067 $this->drupalGet("admin/structure/types/manage/article/display/rss"); 1068 $edit = array( 1069 "fields[taxonomy_" . $this->vocabulary->machine_name . "][type]" => 'taxonomy_term_reference_rss_category', 1070 ); 1071 $this->drupalPost(NULL, $edit, t('Save')); 1072 1073 // Post an article. 1074 $edit = array(); 1075 $langcode = LANGUAGE_NONE; 1076 $edit["title"] = $this->randomName(); 1077 $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $term1->tid; 1078 $this->drupalPost('node/add/article', $edit, t('Save')); 1079 1080 // Check that the term is displayed when the RSS feed is viewed. 1081 $this->drupalGet('rss.xml'); 1082 $test_element = array( 1083 'key' => 'category', 1084 'value' => $term1->name, 1085 'attributes' => array( 1086 'domain' => url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE)), 1087 ), 1088 ); 1089 $this->assertRaw(format_xml_elements(array($test_element)), 'Term is displayed when viewing the rss feed.'); 1090 } 1091 1092 } 1093 1094 /** 1095 * Tests the hook implementations that maintain the taxonomy index. 1096 */ 1097 class TaxonomyTermIndexTestCase extends TaxonomyWebTestCase { 1098 1099 public static function getInfo() { 1100 return array( 1101 'name' => 'Taxonomy term index', 1102 'description' => 'Tests the hook implementations that maintain the taxonomy index.', 1103 'group' => 'Taxonomy', 1104 ); 1105 } 1106 1107 function setUp() { 1108 parent::setUp('taxonomy'); 1109 1110 // Create an administrative user. 1111 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access')); 1112 $this->drupalLogin($this->admin_user); 1113 1114 // Create a vocabulary and add two term reference fields to article nodes. 1115 $this->vocabulary = $this->createVocabulary(); 1116 1117 $this->field_name_1 = drupal_strtolower($this->randomName()); 1118 $this->field_1 = array( 1119 'field_name' => $this->field_name_1, 1120 'type' => 'taxonomy_term_reference', 1121 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 1122 'settings' => array( 1123 'allowed_values' => array( 1124 array( 1125 'vocabulary' => $this->vocabulary->machine_name, 1126 'parent' => 0, 1127 ), 1128 ), 1129 ), 1130 ); 1131 field_create_field($this->field_1); 1132 $this->instance_1 = array( 1133 'field_name' => $this->field_name_1, 1134 'bundle' => 'article', 1135 'entity_type' => 'node', 1136 'widget' => array( 1137 'type' => 'options_select', 1138 ), 1139 'display' => array( 1140 'default' => array( 1141 'type' => 'taxonomy_term_reference_link', 1142 ), 1143 ), 1144 ); 1145 field_create_instance($this->instance_1); 1146 1147 $this->field_name_2 = drupal_strtolower($this->randomName()); 1148 $this->field_2 = array( 1149 'field_name' => $this->field_name_2, 1150 'type' => 'taxonomy_term_reference', 1151 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 1152 'settings' => array( 1153 'allowed_values' => array( 1154 array( 1155 'vocabulary' => $this->vocabulary->machine_name, 1156 'parent' => 0, 1157 ), 1158 ), 1159 ), 1160 ); 1161 field_create_field($this->field_2); 1162 $this->instance_2 = array( 1163 'field_name' => $this->field_name_2, 1164 'bundle' => 'article', 1165 'entity_type' => 'node', 1166 'widget' => array( 1167 'type' => 'options_select', 1168 ), 1169 'display' => array( 1170 'default' => array( 1171 'type' => 'taxonomy_term_reference_link', 1172 ), 1173 ), 1174 ); 1175 field_create_instance($this->instance_2); 1176 } 1177 1178 /** 1179 * Tests that the taxonomy index is maintained properly. 1180 */ 1181 function testTaxonomyIndex() { 1182 // Create terms in the vocabulary. 1183 $term_1 = $this->createTerm($this->vocabulary); 1184 $term_2 = $this->createTerm($this->vocabulary); 1185 1186 // Post an article. 1187 $edit = array(); 1188 $langcode = LANGUAGE_NONE; 1189 $edit["title"] = $this->randomName(); 1190 $edit["body[$langcode][0][value]"] = $this->randomName(); 1191 $edit["{$this->field_name_1}[$langcode][]"] = $term_1->tid; 1192 $edit["{$this->field_name_2}[$langcode][]"] = $term_1->tid; 1193 $this->drupalPost('node/add/article', $edit, t('Save')); 1194 1195 // Check that the term is indexed, and only once. 1196 $node = $this->drupalGetNodeByTitle($edit["title"]); 1197 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1198 ':nid' => $node->nid, 1199 ':tid' => $term_1->tid, 1200 ))->fetchField(); 1201 $this->assertEqual(1, $index_count, 'Term 1 is indexed once.'); 1202 1203 // Update the article to change one term. 1204 $edit["{$this->field_name_1}[$langcode][]"] = $term_2->tid; 1205 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); 1206 1207 // Check that both terms are indexed. 1208 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1209 ':nid' => $node->nid, 1210 ':tid' => $term_1->tid, 1211 ))->fetchField(); 1212 $this->assertEqual(1, $index_count, 'Term 1 is indexed.'); 1213 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1214 ':nid' => $node->nid, 1215 ':tid' => $term_2->tid, 1216 ))->fetchField(); 1217 $this->assertEqual(1, $index_count, 'Term 2 is indexed.'); 1218 1219 // Update the article to change another term. 1220 $edit["{$this->field_name_2}[$langcode][]"] = $term_2->tid; 1221 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); 1222 1223 // Check that only one term is indexed. 1224 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1225 ':nid' => $node->nid, 1226 ':tid' => $term_1->tid, 1227 ))->fetchField(); 1228 $this->assertEqual(0, $index_count, 'Term 1 is not indexed.'); 1229 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1230 ':nid' => $node->nid, 1231 ':tid' => $term_2->tid, 1232 ))->fetchField(); 1233 $this->assertEqual(1, $index_count, 'Term 2 is indexed once.'); 1234 1235 // Redo the above tests without interface. 1236 $update_node = array( 1237 'nid' => $node->nid, 1238 'vid' => $node->vid, 1239 'uid' => $node->uid, 1240 'type' => $node->type, 1241 'title' => $this->randomName(), 1242 ); 1243 1244 // Update the article with no term changed. 1245 $updated_node = (object) $update_node; 1246 node_save($updated_node); 1247 1248 // Check that the index was not changed. 1249 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1250 ':nid' => $node->nid, 1251 ':tid' => $term_1->tid, 1252 ))->fetchField(); 1253 $this->assertEqual(0, $index_count, 'Term 1 is not indexed.'); 1254 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1255 ':nid' => $node->nid, 1256 ':tid' => $term_2->tid, 1257 ))->fetchField(); 1258 $this->assertEqual(1, $index_count, 'Term 2 is indexed once.'); 1259 1260 // Update the article to change one term. 1261 $update_node[$this->field_name_1][$langcode] = array(array('tid' => $term_1->tid)); 1262 $updated_node = (object) $update_node; 1263 node_save($updated_node); 1264 1265 // Check that both terms are indexed. 1266 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1267 ':nid' => $node->nid, 1268 ':tid' => $term_1->tid, 1269 ))->fetchField(); 1270 $this->assertEqual(1, $index_count, 'Term 1 is indexed.'); 1271 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1272 ':nid' => $node->nid, 1273 ':tid' => $term_2->tid, 1274 ))->fetchField(); 1275 $this->assertEqual(1, $index_count, 'Term 2 is indexed.'); 1276 1277 // Update the article to change another term. 1278 $update_node[$this->field_name_2][$langcode] = array(array('tid' => $term_1->tid)); 1279 $updated_node = (object) $update_node; 1280 node_save($updated_node); 1281 1282 // Check that only one term is indexed. 1283 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1284 ':nid' => $node->nid, 1285 ':tid' => $term_1->tid, 1286 ))->fetchField(); 1287 $this->assertEqual(1, $index_count, 'Term 1 is indexed once.'); 1288 $index_count = db_query('SELECT COUNT(*) FROM {taxonomy_index} WHERE nid = :nid AND tid = :tid', array( 1289 ':nid' => $node->nid, 1290 ':tid' => $term_2->tid, 1291 ))->fetchField(); 1292 $this->assertEqual(0, $index_count, 'Term 2 is not indexed.'); 1293 } 1294 1295 /** 1296 * Tests that there is a link to the parent term on the child term page. 1297 */ 1298 function testTaxonomyTermHierarchyBreadcrumbs() { 1299 // Create two taxonomy terms and set term2 as the parent of term1. 1300 $term1 = $this->createTerm($this->vocabulary); 1301 $term2 = $this->createTerm($this->vocabulary); 1302 $term1->parent = array($term2->tid); 1303 taxonomy_term_save($term1); 1304 1305 // Verify that the page breadcrumbs include a link to the parent term. 1306 $this->drupalGet('taxonomy/term/' . $term1->tid); 1307 $this->assertRaw(l($term2->name, 'taxonomy/term/' . $term2->tid), 'Parent term link is displayed when viewing the node.'); 1308 } 1309 1310 } 1311 1312 /** 1313 * Test the taxonomy_term_load_multiple() function. 1314 */ 1315 class TaxonomyLoadMultipleTestCase extends TaxonomyWebTestCase { 1316 1317 public static function getInfo() { 1318 return array( 1319 'name' => 'Taxonomy term multiple loading', 1320 'description' => 'Test the loading of multiple taxonomy terms at once', 1321 'group' => 'Taxonomy', 1322 ); 1323 } 1324 1325 function setUp() { 1326 parent::setUp(); 1327 $this->taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy')); 1328 $this->drupalLogin($this->taxonomy_admin); 1329 } 1330 1331 /** 1332 * Create a vocabulary and some taxonomy terms, ensuring they're loaded 1333 * correctly using taxonomy_term_load_multiple(). 1334 */ 1335 function testTaxonomyTermMultipleLoad() { 1336 // Create a vocabulary. 1337 $vocabulary = $this->createVocabulary(); 1338 1339 // Create five terms in the vocabulary. 1340 $i = 0; 1341 while ($i < 5) { 1342 $i++; 1343 $this->createTerm($vocabulary); 1344 } 1345 // Load the terms from the vocabulary. 1346 $terms = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid)); 1347 $count = count($terms); 1348 $this->assertEqual($count, 5, format_string('Correct number of terms were loaded. !count terms.', array('!count' => $count))); 1349 1350 // Load the same terms again by tid. 1351 $terms2 = taxonomy_term_load_multiple(array_keys($terms)); 1352 $this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.'); 1353 $this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.'); 1354 1355 // Load the terms by tid, with a condition on vid. 1356 $terms3 = taxonomy_term_load_multiple(array_keys($terms2), array('vid' => $vocabulary->vid)); 1357 $this->assertEqual($terms2, $terms3); 1358 1359 // Remove one term from the array, then delete it. 1360 $deleted = array_shift($terms3); 1361 taxonomy_term_delete($deleted->tid); 1362 $deleted_term = taxonomy_term_load($deleted->tid); 1363 $this->assertFalse($deleted_term); 1364 1365 // Load terms from the vocabulary by vid. 1366 $terms4 = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid)); 1367 $this->assertEqual(count($terms4), 4, 'Correct number of terms were loaded.'); 1368 $this->assertFalse(isset($terms4[$deleted->tid])); 1369 1370 // Create a single term and load it by name. 1371 $term = $this->createTerm($vocabulary); 1372 $loaded_terms = taxonomy_term_load_multiple(array(), array('name' => $term->name)); 1373 $this->assertEqual(count($loaded_terms), 1, 'One term was loaded.'); 1374 $loaded_term = reset($loaded_terms); 1375 $this->assertEqual($term->tid, $loaded_term->tid, 'Term loaded by name successfully.'); 1376 } 1377 } 1378 1379 /** 1380 * Tests for taxonomy hook invocation. 1381 */ 1382 class TaxonomyHooksTestCase extends TaxonomyWebTestCase { 1383 public static function getInfo() { 1384 return array( 1385 'name' => 'Taxonomy term hooks', 1386 'description' => 'Hooks for taxonomy term load/save/delete.', 1387 'group' => 'Taxonomy', 1388 ); 1389 } 1390 1391 function setUp() { 1392 parent::setUp('taxonomy', 'taxonomy_test'); 1393 module_load_include('inc', 'taxonomy', 'taxonomy.pages'); 1394 $taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy')); 1395 $this->drupalLogin($taxonomy_admin); 1396 } 1397 1398 /** 1399 * Test that hooks are run correctly on creating, editing, viewing, 1400 * and deleting a term. 1401 * 1402 * @see taxonomy_test.module 1403 */ 1404 function testTaxonomyTermHooks() { 1405 $vocabulary = $this->createVocabulary(); 1406 1407 // Create a term with one antonym. 1408 $edit = array( 1409 'name' => $this->randomName(), 1410 'antonym' => 'Long', 1411 ); 1412 $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save')); 1413 $terms = taxonomy_get_term_by_name($edit['name']); 1414 $term = reset($terms); 1415 $this->assertEqual($term->antonym, $edit['antonym'], 'Antonym was loaded into the term object.'); 1416 1417 // Update the term with a different antonym. 1418 $edit = array( 1419 'name' => $this->randomName(), 1420 'antonym' => 'Short', 1421 ); 1422 $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save')); 1423 taxonomy_terms_static_reset(); 1424 $term = taxonomy_term_load($term->tid); 1425 $this->assertEqual($edit['antonym'], $term->antonym, 'Antonym was successfully edited.'); 1426 1427 // View the term and ensure that hook_taxonomy_term_view() and 1428 // hook_entity_view() are invoked. 1429 $term = taxonomy_term_load($term->tid); 1430 $term_build = taxonomy_term_page($term); 1431 $this->assertFalse(empty($term_build['term_heading']['term']['taxonomy_test_term_view_check']), 'hook_taxonomy_term_view() was invoked when viewing the term.'); 1432 $this->assertFalse(empty($term_build['term_heading']['term']['taxonomy_test_entity_view_check']), 'hook_entity_view() was invoked when viewing the term.'); 1433 1434 // Delete the term. 1435 taxonomy_term_delete($term->tid); 1436 $antonym = db_query('SELECT tid FROM {taxonomy_term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField(); 1437 $this->assertFalse($antonym, 'The antonym were deleted from the database.'); 1438 } 1439 1440 } 1441 1442 /** 1443 * Tests for taxonomy term field and formatter. 1444 */ 1445 class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase { 1446 1447 protected $instance; 1448 protected $vocabulary; 1449 1450 public static function getInfo() { 1451 return array( 1452 'name' => 'Taxonomy term reference field', 1453 'description' => 'Test the creation of term fields.', 1454 'group' => 'Taxonomy', 1455 ); 1456 } 1457 1458 function setUp() { 1459 parent::setUp('field_test'); 1460 1461 $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy')); 1462 $this->drupalLogin($web_user); 1463 $this->vocabulary = $this->createVocabulary(); 1464 1465 // Setup a field and instance. 1466 $this->field_name = drupal_strtolower($this->randomName()); 1467 $this->field = array( 1468 'field_name' => $this->field_name, 1469 'type' => 'taxonomy_term_reference', 1470 'settings' => array( 1471 'allowed_values' => array( 1472 array( 1473 'vocabulary' => $this->vocabulary->machine_name, 1474 'parent' => '0', 1475 ), 1476 ), 1477 ) 1478 ); 1479 field_create_field($this->field); 1480 $this->instance = array( 1481 'field_name' => $this->field_name, 1482 'entity_type' => 'test_entity', 1483 'bundle' => 'test_bundle', 1484 'widget' => array( 1485 'type' => 'options_select', 1486 ), 1487 'display' => array( 1488 'full' => array( 1489 'type' => 'taxonomy_term_reference_link', 1490 ), 1491 ), 1492 ); 1493 field_create_instance($this->instance); 1494 } 1495 1496 /** 1497 * Test term field validation. 1498 */ 1499 function testTaxonomyTermFieldValidation() { 1500 // Test valid and invalid values with field_attach_validate(). 1501 $langcode = LANGUAGE_NONE; 1502 $entity = field_test_create_stub_entity(); 1503 $term = $this->createTerm($this->vocabulary); 1504 $entity->{$this->field_name}[$langcode][0]['tid'] = $term->tid; 1505 try { 1506 field_attach_validate('test_entity', $entity); 1507 $this->pass('Correct term does not cause validation error.'); 1508 } 1509 catch (FieldValidationException $e) { 1510 $this->fail('Correct term does not cause validation error.'); 1511 } 1512 1513 $entity = field_test_create_stub_entity(); 1514 $bad_term = $this->createTerm($this->createVocabulary()); 1515 $entity->{$this->field_name}[$langcode][0]['tid'] = $bad_term->tid; 1516 try { 1517 field_attach_validate('test_entity', $entity); 1518 $this->fail('Wrong term causes validation error.'); 1519 } 1520 catch (FieldValidationException $e) { 1521 $this->pass('Wrong term causes validation error.'); 1522 } 1523 } 1524 1525 /** 1526 * Test widgets. 1527 */ 1528 function testTaxonomyTermFieldWidgets() { 1529 // Create a term in the vocabulary. 1530 $term = $this->createTerm($this->vocabulary); 1531 1532 // Display creation form. 1533 $langcode = LANGUAGE_NONE; 1534 $this->drupalGet('test-entity/add/test-bundle'); 1535 $this->assertFieldByName("{$this->field_name}[$langcode]", '', 'Widget is displayed.'); 1536 1537 // Submit with some value. 1538 $edit = array( 1539 "{$this->field_name}[$langcode]" => array($term->tid), 1540 ); 1541 $this->drupalPost(NULL, $edit, t('Save')); 1542 preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); 1543 $id = $match[1]; 1544 $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.'); 1545 1546 // Display the object. 1547 $entity = field_test_entity_test_load($id); 1548 $entities = array($id => $entity); 1549 field_attach_prepare_view('test_entity', $entities, 'full'); 1550 $entity->content = field_attach_view('test_entity', $entity, 'full'); 1551 $this->content = drupal_render($entity->content); 1552 $this->assertText($term->name, 'Term name is displayed.'); 1553 1554 // Delete the vocabulary and verify that the widget is gone. 1555 taxonomy_vocabulary_delete($this->vocabulary->vid); 1556 $this->drupalGet('test-entity/add/test-bundle'); 1557 $this->assertNoFieldByName("{$this->field_name}[$langcode]", '', 'Widget is not displayed.'); 1558 } 1559 1560 /** 1561 * Tests that vocabulary machine name changes are mirrored in field definitions. 1562 */ 1563 function testTaxonomyTermFieldChangeMachineName() { 1564 // Add several entries in the 'allowed_values' setting, to make sure that 1565 // they all get updated. 1566 $this->field['settings']['allowed_values'] = array( 1567 array( 1568 'vocabulary' => $this->vocabulary->machine_name, 1569 'parent' => '0', 1570 ), 1571 array( 1572 'vocabulary' => $this->vocabulary->machine_name, 1573 'parent' => '0', 1574 ), 1575 array( 1576 'vocabulary' => 'foo', 1577 'parent' => '0', 1578 ), 1579 ); 1580 field_update_field($this->field); 1581 // Change the machine name. 1582 $old_name = $this->vocabulary->machine_name; 1583 $new_name = drupal_strtolower($this->randomName()); 1584 $this->vocabulary->machine_name = $new_name; 1585 taxonomy_vocabulary_save($this->vocabulary); 1586 1587 // Check that entity bundles are properly updated. 1588 $info = entity_get_info('taxonomy_term'); 1589 $this->assertFalse(isset($info['bundles'][$old_name]), 'The old bundle name does not appear in entity_get_info().'); 1590 $this->assertTrue(isset($info['bundles'][$new_name]), 'The new bundle name appears in entity_get_info().'); 1591 1592 // Check that the field instance is still attached to the vocabulary. 1593 $field = field_info_field($this->field_name); 1594 $allowed_values = $field['settings']['allowed_values']; 1595 $this->assertEqual($allowed_values[0]['vocabulary'], $new_name, 'Index 0: Machine name was updated correctly.'); 1596 $this->assertEqual($allowed_values[1]['vocabulary'], $new_name, 'Index 1: Machine name was updated correctly.'); 1597 $this->assertEqual($allowed_values[2]['vocabulary'], 'foo', 'Index 2: Machine name was left untouched.'); 1598 } 1599 1600 } 1601 1602 /** 1603 * Tests a taxonomy term reference field that allows multiple vocabularies. 1604 */ 1605 class TaxonomyTermFieldMultipleVocabularyTestCase extends TaxonomyWebTestCase { 1606 1607 protected $instance; 1608 protected $vocabulary1; 1609 protected $vocabulary2; 1610 1611 public static function getInfo() { 1612 return array( 1613 'name' => 'Multiple vocabulary term reference field', 1614 'description' => 'Tests term reference fields that allow multiple vocabularies.', 1615 'group' => 'Taxonomy', 1616 ); 1617 } 1618 1619 function setUp() { 1620 parent::setUp('field_test'); 1621 1622 $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy')); 1623 $this->drupalLogin($web_user); 1624 $this->vocabulary1 = $this->createVocabulary(); 1625 $this->vocabulary2 = $this->createVocabulary(); 1626 1627 // Set up a field and instance. 1628 $this->field_name = drupal_strtolower($this->randomName()); 1629 $this->field = array( 1630 'field_name' => $this->field_name, 1631 'type' => 'taxonomy_term_reference', 1632 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 1633 'settings' => array( 1634 'allowed_values' => array( 1635 array( 1636 'vocabulary' => $this->vocabulary1->machine_name, 1637 'parent' => '0', 1638 ), 1639 array( 1640 'vocabulary' => $this->vocabulary2->machine_name, 1641 'parent' => '0', 1642 ), 1643 ), 1644 ) 1645 ); 1646 field_create_field($this->field); 1647 $this->instance = array( 1648 'field_name' => $this->field_name, 1649 'entity_type' => 'test_entity', 1650 'bundle' => 'test_bundle', 1651 'widget' => array( 1652 'type' => 'options_select', 1653 ), 1654 'display' => array( 1655 'full' => array( 1656 'type' => 'taxonomy_term_reference_link', 1657 ), 1658 ), 1659 ); 1660 field_create_instance($this->instance); 1661 } 1662 1663 /** 1664 * Tests term reference field and widget with multiple vocabularies. 1665 */ 1666 function testTaxonomyTermFieldMultipleVocabularies() { 1667 // Create a term in each vocabulary. 1668 $term1 = $this->createTerm($this->vocabulary1); 1669 $term2 = $this->createTerm($this->vocabulary2); 1670 1671 // Submit an entity with both terms. 1672 $langcode = LANGUAGE_NONE; 1673 $this->drupalGet('test-entity/add/test-bundle'); 1674 $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is displayed.'); 1675 $edit = array( 1676 "{$this->field_name}[$langcode][]" => array($term1->tid, $term2->tid), 1677 ); 1678 $this->drupalPost(NULL, $edit, t('Save')); 1679 preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); 1680 $id = $match[1]; 1681 $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.'); 1682 1683 // Render the entity. 1684 $entity = field_test_entity_test_load($id); 1685 $entities = array($id => $entity); 1686 field_attach_prepare_view('test_entity', $entities, 'full'); 1687 $entity->content = field_attach_view('test_entity', $entity, 'full'); 1688 $this->content = drupal_render($entity->content); 1689 $this->assertText($term1->name, 'Term 1 name is displayed.'); 1690 $this->assertText($term2->name, 'Term 2 name is displayed.'); 1691 1692 // Delete vocabulary 2. 1693 taxonomy_vocabulary_delete($this->vocabulary2->vid); 1694 1695 // Re-render the content. 1696 $entity = field_test_entity_test_load($id); 1697 $entities = array($id => $entity); 1698 field_attach_prepare_view('test_entity', $entities, 'full'); 1699 $entity->content = field_attach_view('test_entity', $entity, 'full'); 1700 $this->plainTextContent = FALSE; 1701 $this->content = drupal_render($entity->content); 1702 1703 // Term 1 should still be displayed; term 2 should not be. 1704 $this->assertText($term1->name, 'Term 1 name is displayed.'); 1705 $this->assertNoText($term2->name, 'Term 2 name is not displayed.'); 1706 1707 // Verify that field and instance settings are correct. 1708 $field_info = field_info_field($this->field_name); 1709 $this->assertEqual(sizeof($field_info['settings']['allowed_values']), 1, 'Only one vocabulary is allowed for the field.'); 1710 1711 // The widget should still be displayed. 1712 $this->drupalGet('test-entity/add/test-bundle'); 1713 $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is still displayed.'); 1714 1715 // Term 1 should still pass validation. 1716 $edit = array( 1717 "{$this->field_name}[$langcode][]" => array($term1->tid), 1718 ); 1719 $this->drupalPost(NULL, $edit, t('Save')); 1720 } 1721 1722 } 1723 1724 /** 1725 * Test taxonomy token replacement in strings. 1726 */ 1727 class TaxonomyTokenReplaceTestCase extends TaxonomyWebTestCase { 1728 1729 public static function getInfo() { 1730 return array( 1731 'name' => 'Taxonomy token replacement', 1732 'description' => 'Generates text using placeholders for dummy content to check taxonomy token replacement.', 1733 'group' => 'Taxonomy', 1734 ); 1735 } 1736 1737 function setUp() { 1738 parent::setUp(); 1739 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access')); 1740 $this->drupalLogin($this->admin_user); 1741 $this->vocabulary = $this->createVocabulary(); 1742 $this->langcode = LANGUAGE_NONE; 1743 1744 $field = array( 1745 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name, 1746 'type' => 'taxonomy_term_reference', 1747 'cardinality' => FIELD_CARDINALITY_UNLIMITED, 1748 'settings' => array( 1749 'allowed_values' => array( 1750 array( 1751 'vocabulary' => $this->vocabulary->machine_name, 1752 'parent' => 0, 1753 ), 1754 ), 1755 ), 1756 ); 1757 field_create_field($field); 1758 1759 $this->instance = array( 1760 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name, 1761 'bundle' => 'article', 1762 'entity_type' => 'node', 1763 'widget' => array( 1764 'type' => 'options_select', 1765 ), 1766 'display' => array( 1767 'default' => array( 1768 'type' => 'taxonomy_term_reference_link', 1769 ), 1770 ), 1771 ); 1772 field_create_instance($this->instance); 1773 } 1774 1775 /** 1776 * Creates some terms and a node, then tests the tokens generated from them. 1777 */ 1778 function testTaxonomyTokenReplacement() { 1779 global $language; 1780 1781 // Create two taxonomy terms. 1782 $term1 = $this->createTerm($this->vocabulary); 1783 $term2 = $this->createTerm($this->vocabulary); 1784 1785 // Edit $term2, setting $term1 as parent. 1786 $edit = array(); 1787 $edit['name'] = '<blink>Blinking Text</blink>'; 1788 $edit['parent[]'] = array($term1->tid); 1789 $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save')); 1790 1791 // Create node with term2. 1792 $edit = array(); 1793 $node = $this->drupalCreateNode(array('type' => 'article')); 1794 $edit[$this->instance['field_name'] . '[' . $this->langcode . '][]'] = $term2->tid; 1795 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save')); 1796 1797 // Generate and test sanitized tokens for term1. 1798 $tests = array(); 1799 $tests['[term:tid]'] = $term1->tid; 1800 $tests['[term:name]'] = check_plain($term1->name); 1801 $tests['[term:description]'] = check_markup($term1->description, $term1->format); 1802 $tests['[term:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE)); 1803 $tests['[term:node-count]'] = 0; 1804 $tests['[term:parent:name]'] = '[term:parent:name]'; 1805 $tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name); 1806 1807 foreach ($tests as $input => $expected) { 1808 $output = token_replace($input, array('term' => $term1), array('language' => $language)); 1809 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', array('%token' => $input))); 1810 } 1811 1812 // Generate and test sanitized tokens for term2. 1813 $tests = array(); 1814 $tests['[term:tid]'] = $term2->tid; 1815 $tests['[term:name]'] = check_plain($term2->name); 1816 $tests['[term:description]'] = check_markup($term2->description, $term2->format); 1817 $tests['[term:url]'] = url('taxonomy/term/' . $term2->tid, array('absolute' => TRUE)); 1818 $tests['[term:node-count]'] = 1; 1819 $tests['[term:parent:name]'] = check_plain($term1->name); 1820 $tests['[term:parent:url]'] = url('taxonomy/term/' . $term1->tid, array('absolute' => TRUE)); 1821 $tests['[term:parent:parent:name]'] = '[term:parent:parent:name]'; 1822 $tests['[term:vocabulary:name]'] = check_plain($this->vocabulary->name); 1823 1824 // Test to make sure that we generated something for each token. 1825 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); 1826 1827 foreach ($tests as $input => $expected) { 1828 $output = token_replace($input, array('term' => $term2), array('language' => $language)); 1829 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy term token %token replaced.', array('%token' => $input))); 1830 } 1831 1832 // Generate and test unsanitized tokens. 1833 $tests['[term:name]'] = $term2->name; 1834 $tests['[term:description]'] = $term2->description; 1835 $tests['[term:parent:name]'] = $term1->name; 1836 $tests['[term:vocabulary:name]'] = $this->vocabulary->name; 1837 1838 foreach ($tests as $input => $expected) { 1839 $output = token_replace($input, array('term' => $term2), array('language' => $language, 'sanitize' => FALSE)); 1840 $this->assertEqual($output, $expected, format_string('Unsanitized taxonomy term token %token replaced.', array('%token' => $input))); 1841 } 1842 1843 // Generate and test sanitized tokens. 1844 $tests = array(); 1845 $tests['[vocabulary:vid]'] = $this->vocabulary->vid; 1846 $tests['[vocabulary:name]'] = check_plain($this->vocabulary->name); 1847 $tests['[vocabulary:description]'] = filter_xss($this->vocabulary->description); 1848 $tests['[vocabulary:node-count]'] = 1; 1849 $tests['[vocabulary:term-count]'] = 2; 1850 1851 // Test to make sure that we generated something for each token. 1852 $this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.'); 1853 1854 foreach ($tests as $input => $expected) { 1855 $output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language)); 1856 $this->assertEqual($output, $expected, format_string('Sanitized taxonomy vocabulary token %token replaced.', array('%token' => $input))); 1857 } 1858 1859 // Generate and test unsanitized tokens. 1860 $tests['[vocabulary:name]'] = $this->vocabulary->name; 1861 $tests['[vocabulary:description]'] = $this->vocabulary->description; 1862 1863 foreach ($tests as $input => $expected) { 1864 $output = token_replace($input, array('vocabulary' => $this->vocabulary), array('language' => $language, 'sanitize' => FALSE)); 1865 $this->assertEqual($output, $expected, format_string('Unsanitized taxonomy vocabulary token %token replaced.', array('%token' => $input))); 1866 } 1867 } 1868 1869 } 1870 1871 /** 1872 * Tests for verifying that taxonomy pages use the correct theme. 1873 */ 1874 class TaxonomyThemeTestCase extends TaxonomyWebTestCase { 1875 1876 public static function getInfo() { 1877 return array( 1878 'name' => 'Taxonomy theme switching', 1879 'description' => 'Verifies that various taxonomy pages use the expected theme.', 1880 'group' => 'Taxonomy', 1881 ); 1882 } 1883 1884 function setUp() { 1885 parent::setUp(); 1886 1887 // Make sure we are using distinct default and administrative themes for 1888 // the duration of these tests. 1889 variable_set('theme_default', 'bartik'); 1890 variable_set('admin_theme', 'seven'); 1891 1892 // Create and log in as a user who has permission to add and edit taxonomy 1893 // terms and view the administrative theme. 1894 $admin_user = $this->drupalCreateUser(array('administer taxonomy', 'view the administration theme')); 1895 $this->drupalLogin($admin_user); 1896 } 1897 1898 /** 1899 * Test the theme used when adding, viewing and editing taxonomy terms. 1900 */ 1901 function testTaxonomyTermThemes() { 1902 // Adding a term to a vocabulary is considered an administrative action and 1903 // should use the administrative theme. 1904 $vocabulary = $this->createVocabulary(); 1905 $this->drupalGet('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add'); 1906 $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page for adding a taxonomy term."); 1907 1908 // Viewing a taxonomy term should use the default theme. 1909 $term = $this->createTerm($vocabulary); 1910 $this->drupalGet('taxonomy/term/' . $term->tid); 1911 $this->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page for viewing a taxonomy term."); 1912 1913 // Editing a taxonomy term should use the same theme as adding one. 1914 $this->drupalGet('taxonomy/term/' . $term->tid . '/edit'); 1915 $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page for editing a taxonomy term."); 1916 } 1917 1918 } 1919 1920 /** 1921 * Tests the functionality of EntityFieldQuery for taxonomy entities. 1922 */ 1923 class TaxonomyEFQTestCase extends TaxonomyWebTestCase { 1924 public static function getInfo() { 1925 return array( 1926 'name' => 'Taxonomy EntityFieldQuery', 1927 'description' => 'Verifies operation of a taxonomy-based EntityFieldQuery.', 1928 'group' => 'Taxonomy', 1929 ); 1930 } 1931 1932 function setUp() { 1933 parent::setUp(); 1934 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy')); 1935 $this->drupalLogin($this->admin_user); 1936 $this->vocabulary = $this->createVocabulary(); 1937 } 1938 1939 /** 1940 * Tests that a basic taxonomy EntityFieldQuery works. 1941 */ 1942 function testTaxonomyEFQ() { 1943 $terms = array(); 1944 for ($i = 0; $i < 5; $i++) { 1945 $term = $this->createTerm($this->vocabulary); 1946 $terms[$term->tid] = $term; 1947 } 1948 $query = new EntityFieldQuery(); 1949 $query->entityCondition('entity_type', 'taxonomy_term'); 1950 $result = $query->execute(); 1951 $result = $result['taxonomy_term']; 1952 asort($result); 1953 $this->assertEqual(array_keys($terms), array_keys($result), 'Taxonomy terms were retrieved by EntityFieldQuery.'); 1954 1955 // Create a second vocabulary and five more terms. 1956 $vocabulary2 = $this->createVocabulary(); 1957 $terms2 = array(); 1958 for ($i = 0; $i < 5; $i++) { 1959 $term = $this->createTerm($vocabulary2); 1960 $terms2[$term->tid] = $term; 1961 } 1962 1963 $query = new EntityFieldQuery(); 1964 $query->entityCondition('entity_type', 'taxonomy_term'); 1965 $query->entityCondition('bundle', $vocabulary2->machine_name); 1966 $result = $query->execute(); 1967 $result = $result['taxonomy_term']; 1968 asort($result); 1969 $this->assertEqual(array_keys($terms2), array_keys($result), format_string('Taxonomy terms from the %name vocabulary were retrieved by EntityFieldQuery.', array('%name' => $vocabulary2->name))); 1970 } 1971 1972 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
title