summaryrefslogtreecommitdiff
path: root/modules/taxonomy/taxonomy.test
blob: dc3a4733d3ac388ba9130d27d0fc89acaf286b72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
<?php
// $Id$

/**
 * @file
 * Tests for Taxonomy module.
 */

/**
* Class with common helper methods.
*/
class TaxonomyWebTestCase extends DrupalWebTestCase {

  /**
   * Returns a new vocabulary with random properties.
   */
  function createVocabulary() {
    // Create a vocabulary.
    $vocabulary = new stdClass();
    $vocabulary->name = $this->randomName();
    $vocabulary->description = $this->randomName();
    $vocabulary->machine_name = drupal_strtolower($this->randomName());
    $vocabulary->help = '';
    $vocabulary->nodes = array('article' => 'article');
    $vocabulary->weight = mt_rand(0, 10);
    taxonomy_vocabulary_save($vocabulary);
    return $vocabulary;
  }

  /**
   * Returns a new term with random properties in vocabulary $vid.
   */
  function createTerm($vocabulary) {
    $term = new stdClass();
    $term->name = $this->randomName();
    $term->vid = $vocabulary->vid;
    taxonomy_term_save($term);
    return $term;
  }
}

/**
* Tests for the taxonomy vocabulary interface.
*/
class TaxonomyVocabularyFunctionalTest extends TaxonomyWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Taxonomy vocabulary interface',
      'description' => 'Test the taxonomy vocabulary interface.',
      'group' => 'Taxonomy',
    );
  }

  function setUp() {
    parent::setUp();
    $this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
    $this->drupalLogin($this->admin_user);
    $this->vocabulary = $this->createVocabulary();
  }

  /**
   * Create, edit and delete a vocabulary via the user interface.
   */
  function testVocabularyInterface() {
    // Visit the main taxonomy administration page.
    $this->drupalGet('admin/structure/taxonomy');

    // Create a new vocabulary.
    $this->clickLink(t('Add vocabulary'));
    $edit = array();
    $machine_name = drupal_strtolower($this->randomName());
    $edit['name'] = $this->randomName();
    $edit['description'] = $this->randomName();
    $edit['machine_name'] = $machine_name;
    $edit['help'] = $this->randomName();
    $edit['nodes[article]'] = 'article';
    $edit['tags'] = 1;
    $edit['multiple'] = 1;
    $edit['required'] = 1;
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), t('Vocabulary created successfully'));

    // Edit the vocabulary.
    $this->drupalGet('admin/structure/taxonomy');
    $this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
    $this->clickLink(t('edit vocabulary'));
    $edit = array();
    $edit['name'] = $this->randomName();
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->drupalGet('admin/structure/taxonomy');
    $this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));

    // Try to submit a vocabulary with a duplicate machine name.
    $edit['machine_name'] = $machine_name;
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->assertText(t('This machine-readable name is already in use by another vocabulary and must be unique.'), t('Duplicate machine name validation was successful'));

    // Try to submit an invalid machine name.
    $edit['machine_name'] = '!&^%';
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  }

  /**
   * Changing weights on the vocabulary overview with two or more vocabularies.
   */
  function testTaxonomyAdminChangingWeights() {
    // Create some vocabularies.
    for ($i = 0; $i < 10; $i++) {
      $this->createVocabulary();
    }
    // Get all vocabularies and change their weights.
    $vocabularies = taxonomy_get_vocabularies();
    $edit = array();
    foreach ($vocabularies as $key => $vocabulary) {
      $vocabulary->weight = -$vocabulary->weight;
      $vocabularies[$key]->weight = $vocabulary->weight;
      $edit[$key . '[weight]'] = $vocabulary->weight;
    }
    // Saving the new weights via the interface.
    $this->drupalPost('admin/structure/taxonomy/', $edit, t('Save'));

    // Load the vocabularies from the database.
    $new_vocabularies = taxonomy_get_vocabularies();

    // Check that the weights are saved in the database correctly.
    foreach ($vocabularies as $key => $vocabulary) {
      $this->assertEqual($new_vocabularies[$key]->weight, $vocabularies[$key]->weight, t('The vocabulary weight was changed.'));
    }
  }

  /**
   * Test the vocabulary overview with no vocabularies.
   */
  function testTaxonomyAdminNoVocabularies() {
    // Delete all vocabularies.
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $key => $vocabulary) {
      taxonomy_vocabulary_delete($key);
    }
    // Confirm that no vocabularies are found in the database.
    $this->assertFalse(taxonomy_get_vocabularies(), t('No vocabularies found in the database'));
    $this->drupalGet('admin/structure/taxonomy');
    // Check the default message for no vocabularies.
    $this->assertText(t('No vocabularies available.'), t('No vocabularies were found.'));
  }

  /**
   * Deleting a vocabulary.
   */
  function testTaxonomyAdminDeletingVocabulary() {
    // Create a vocabulary.
    $edit = array(
      'name' => $this->randomName(),
      'machine_name' => drupal_strtolower($this->randomName()),
      'nodes[article]' => 'article',
    );
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->assertText(t('Created new vocabulary'), t('New vocabulary was created.'));

    // Check the created vocabulary.
    $vocabularies = taxonomy_get_vocabularies();
    $vid = $vocabularies[count($vocabularies)-1]->vid;
    entity_get_controller('taxonomy_vocabulary')->resetCache();
    $vocabulary = taxonomy_vocabulary_load($vid);
    $this->assertTrue($vocabulary, t('Vocabulary found in database'));

    // Delete the vocabulary.
    $edit = array();
    $this->drupalPost('admin/structure/taxonomy/' . $vid, $edit, t('Delete'));
    $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', array('%name' => $vocabulary->name)), t('[confirm deletion] Asks for confirmation.'));
    $this->assertText(t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'), t('[confirm deletion] Inform that all terms will be deleted.'));

    // Confirm deletion.
    $this->drupalPost(NULL, NULL, t('Delete'));
    $this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), t('Vocabulary deleted'));
    entity_get_controller('taxonomy_vocabulary')->resetCache();
    $this->assertFalse(taxonomy_vocabulary_load($vid), t('Vocabulary is not found in the database'));
  }
}


/**
 * Tests for taxonomy vocabulary functions.
 */
class TaxonomyVocabularyUnitTest extends TaxonomyWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Taxonomy vocabularies',
      'description' => 'Test loading, saving and deleting vocabularies.',
      'group' => 'Taxonomy',
    );
  }

  function setUp() {
    parent::setUp('taxonomy');
    $admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy'));
    $this->drupalLogin($admin_user);
    $this->vocabulary = $this->createVocabulary();
  }

  /**
   * Ensure that when an invalid vocabulary vid is loaded, it is possible
   * to load the same vid successfully if it subsequently becomes valid.
   */
  function testTaxonomyVocabularyLoadReturnFalse() {
    // Load a vocabulary that doesn't exist.
    $vocabularies = taxonomy_get_vocabularies();
    $vid = count($vocabularies) + 1;
    $vocabulary = taxonomy_vocabulary_load($vid);
    // This should not return an object because no such vocabulary exists.
    $this->assertTrue(empty($vocabulary), t('No object loaded.'));

    // Create a new vocabulary.
    $this->createVocabulary();
    // Load the vocabulary with the same $vid from earlier.
    // This should return a vocabulary object since it now matches a real vid.
    $vocabulary = taxonomy_vocabulary_load($vid);
    $this->assertTrue(!empty($vocabulary) && is_object($vocabulary), t('Vocabulary is an object'));
    $this->assertTrue($vocabulary->vid == $vid, t('Valid vocabulary vid is the same as our previously invalid one.'));
  }

  /**
   * Ensure that the vocabulary static reset works correctly.
   */
  function testTaxonomyVocabularyLoadStaticReset() {
    $original_vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
    $this->assertTrue(is_object($original_vocabulary), t('Vocabulary loaded successfully'));
    $this->assertEqual($this->vocabulary->name, $original_vocabulary->name, t('Vocabulary loaded successfully'));

    // Change the name and description.
    $vocabulary = $original_vocabulary;
    $vocabulary->name = $this->randomName();
    $vocabulary->description = $this->randomName();
    taxonomy_vocabulary_save($vocabulary);

    // Load the vocabulary.
    $new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid);
    $this->assertEqual($new_vocabulary->name, $vocabulary->name);
    $this->assertEqual($new_vocabulary->name, $vocabulary->name);

    // Delete the vocabulary.
    taxonomy_vocabulary_delete($this->vocabulary->vid);
    $vocabularies = taxonomy_get_vocabularies();
    $this->assertTrue(!isset($vocabularies[$this->vocabulary->vid]), t('The vocabulary was deleted'));
  }

  /**
   * Tests for loading multiple vocabularies.
   */
  function testTaxonomyVocabularyLoadMultiple() {

    // Delete any existing vocabularies.
    foreach (taxonomy_get_vocabularies() as $vocabulary) {
      taxonomy_vocabulary_delete($vocabulary->vid);
    }

    // Create some vocabularies and assign weights.
    $vocabulary1 = $this->createVocabulary();
    $vocabulary1->weight = 0;
    taxonomy_vocabulary_save($vocabulary1);
    $vocabulary2 = $this->createVocabulary();
    $vocabulary2->weight = 1;
    taxonomy_vocabulary_save($vocabulary2);
    $vocabulary3 = $this->createVocabulary();
    $vocabulary3->weight = 2;
    taxonomy_vocabulary_save($vocabulary3);

    // Fetch the names for all vocabularies, confirm that they are keyed by
    // machine name.
    $names = taxonomy_vocabulary_get_names();
    $this->assertEqual($names[$vocabulary1->machine_name]->name, $vocabulary1->name, t('Vocabulary 1 name found.'));

    // Fetch all of the vocabularies using taxonomy_get_vocabularies().
    // Confirm that the vocabularies are ordered by weight.
    $vocabularies = taxonomy_get_vocabularies();
    $this->assertEqual(array_shift($vocabularies), $vocabulary1, t('Vocabulary was found in the vocabularies array.'));
    $this->assertEqual(array_shift($vocabularies), $vocabulary2, t('Vocabulary was found in the vocabularies array.'));
    $this->assertEqual(array_shift($vocabularies), $vocabulary3, t('Vocabulary was found in the vocabularies array.'));

    // Fetch the vocabularies with taxonomy_vocabulary_load_multiple(), specifying IDs.
    // Ensure they are returned in the same order as the original array.
    $vocabularies = taxonomy_vocabulary_load_multiple(array($vocabulary3->vid, $vocabulary2->vid, $vocabulary1->vid));
    $this->assertEqual(array_shift($vocabularies), $vocabulary3, t('Vocabulary loaded successfully by ID.'));
    $this->assertEqual(array_shift($vocabularies), $vocabulary2, t('Vocabulary loaded successfully by ID.'));
    $this->assertEqual(array_shift($vocabularies), $vocabulary1, t('Vocabulary loaded successfully by ID.'));

    // Fetch vocabulary 1 by name.
    $this->assertTrue(current(taxonomy_vocabulary_load_multiple(array(), array('name' => $vocabulary1->name))) == $vocabulary1, t('Vocabulary loaded successfully by name.'));

    // Fetch vocabulary 1 by name and ID.
    $this->assertTrue(current(taxonomy_vocabulary_load_multiple(array($vocabulary1->vid), array('name' => $vocabulary1->name))) == $vocabulary1, t('Vocabulary loaded successfully by name and ID.'));

    // Fetch vocabulary 1 with specified node type.
    entity_get_controller('taxonomy_vocabulary')->resetCache();
    $vocabulary_node_type = current(taxonomy_vocabulary_load_multiple(array($vocabulary1->vid), array('type' => 'article')));
    $this->assertEqual($vocabulary_node_type, $vocabulary1, t('Vocabulary with specified node type loaded successfully.'));
  }
}

/**
 * Unit tests for taxonomy term functions.
 */
class TaxonomyTermUnitTest extends TaxonomyWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Taxonomy term unit tests',
      'description' => 'Unit tests for taxonomy term functions.',
      'group' => 'Taxonomy',
    );
  }

  /**
   * Tests for taxonomy_term_count_nodes().
   *
   * Attach nodes to a hierarchical vocabulary and check they are counted
   * correctly.
   */
  function testTaxonomyTermCountNodes() {
    // Create a vocabulary with three terms.
    $vocabulary = $this->createVocabulary();
    $term1 = $this->createTerm($vocabulary);
    $term2 = $this->createTerm($vocabulary);
    $term3 = $this->createTerm($vocabulary);

    // Attach term1 to a node.
    $node1 = $this->drupalCreateNode(array('type' => 'page'));
    $node1->taxonomy = array($term1->tid);
    node_save($node1);
    $this->assertEqual(taxonomy_term_count_nodes($term1->tid), 1, t('Term has one valid node association.'));

    // Attach term2 to a node.
    $node2 = $this->drupalCreateNode(array('type' => 'article'));
    $node2->taxonomy = array($term2->tid);
    node_save($node2);
    $this->assertEqual(taxonomy_term_count_nodes($term2->tid), 1, t('Term has one valid node association.'));

    // Confirm that term3 is not associated with any nodes.
    $this->assertEqual(taxonomy_term_count_nodes($term3->tid), 0, t('Term is not associated with any nodes'));

    // Set term3 as the parent of term1.
    $term1->parent = array($term3->tid);
    taxonomy_term_save($term1);

    // Confirm that the term hierarchy is altered correctly.
    $children = taxonomy_get_children($term3->tid);
    $this->assertTrue(isset($children[$term1->tid]), t('Term 3 saved as parent of term 1'));

    $this->assertEqual(count(taxonomy_get_tree($term3->vid, $term3->tid)), 1, t('Term 3 has one child term'));

    // Confirm that term3's parental relationship with term1 leads to a
    // node assocation being counted.
    $this->assertEqual(taxonomy_term_count_nodes($term3->tid, NULL), 1, t('Term has one valid node association due to child term.'));

    // Set term3 as the parent of term2.
    $term2->parent = array($term3->tid);
    taxonomy_term_save($term2);

    // term3 should now have two node associations counted.
    $this->assertEqual(taxonomy_term_count_nodes($term3->tid, NULL), 2, t('Term has two valid node associations due to child terms.'));

    // Save node1 with both child taxonomy terms, this should still result
    // in term3 having two node associations.
    $node1->taxonomy = array($term1->tid, $term2->tid);
    node_save($node1);
    $this->assertEqual(taxonomy_term_count_nodes($term3->tid, NULL), 2, t('Term has two valid node associations.'));

    // Confirm that the node type argument returns a single node association.
    $this->assertEqual(taxonomy_term_count_nodes($term3->tid, 'page'), 1, t("Term is associated with one node of type 'page'."));
  }
}

/**
 * Tests for taxonomy term functions.
 */
class TaxonomyTermTestCase extends TaxonomyWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Taxonomy term functions and forms.',
      'description' => 'Test load, save and delete for taxonomy terms.',
      'group' => 'Taxonomy'
    );
  }

  function setUp() {
    parent::setUp('taxonomy');
    $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
    $this->drupalLogin($this->admin_user);
    $this->vocabulary = $this->createVocabulary();
  }

  /**
   * Test synonyms.
   */
  function testTaxonomySynonyms() {
    // Create a taxonomy term with one synonym.
    $term = $this->createTerm($this->vocabulary);
    $term->synonyms = $this->randomName();
    taxonomy_term_save($term);

    // Fetch the synonyms.
    $synonyms = taxonomy_get_synonyms($term->tid);
    $count = count($synonyms);
    $this->assertEqual($count, 1, t('@count synonyms were found.', array('@count' => $count)));

    // Fetch the term using the synonyms.
    $returned_term = taxonomy_get_synonym_root($synonyms[0]);
    $this->assertEqual($term->tid, $returned_term->tid, t('Term ID returned correctly'));
  }

  /**
   * Test terms in a single and multiple hierarchy.
   */
  function testTaxonomyTermHierarchy() {
    // Create two taxonomy terms.
    $term1 = $this->createTerm($this->vocabulary);
    $term2 = $this->createTerm($this->vocabulary);

    // Edit $term2, setting $term1 as parent.
    $edit = array();
    $edit['parent[]'] = $term1->tid;
    $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));

    // Check the hierarchy.
    $children = taxonomy_get_children($term1->tid);
    $parents = taxonomy_get_parents($term2->tid);
    $this->assertTrue(isset($children[$term2->tid]), t('Child found correctly.'));
    $this->assertTrue(isset($parents[$term1->tid]), t('Parent found correctly.'));

    // Create a third term and save this as a parent of term2.
    $term3 = $this->createTerm($this->vocabulary);
    $term2->parent = array($term1->tid, $term3->tid);
    taxonomy_term_save($term2);
    $parents = taxonomy_get_parents($term2->tid);
    $this->assertTrue(isset($parents[$term1->tid]) && isset($parents[$term3->tid]), t('Both parents found successfully.'));
  }

  /**
   * Test that hook_node_$op implementations work correctly.
   *
   * Save & edit a node and assert that taxonomy terms are saved/loaded properly.
   */
  function testTaxonomyNode() {
    // Create two taxonomy terms.
    $term1 = $this->createTerm($this->vocabulary);
    $term2 = $this->createTerm($this->vocabulary);

    // Post an article.
    $edit = array();
    $edit['title'] = $this->randomName();
    $langcode = FIELD_LANGUAGE_NONE;
    $edit["body[$langcode][0][value]"] = $this->randomName();
    $edit['taxonomy[' . $this->vocabulary->vid . ']'] = $term1->tid;
    $this->drupalPost('node/add/article', $edit, t('Save'));

    // Check that the term is displayed when the node is viewed.
    $node = $this->drupalGetNodeByTitle($edit['title']);
    $this->drupalGet('node/' . $node->nid);
    $this->assertText($term1->name, t('Term is displayed when viewing the node.'));

    // Edit the node with a different term.
    $edit['taxonomy[' . $this->vocabulary->vid . ']'] = $term2->tid;
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));

    $this->drupalGet('node/' . $node->nid);
    $this->assertText($term2->name, t('Term is displayed when viewing the node.'));

    // Delete node through browser.
    $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
    $this->drupalGet('node/' . $node->nid);
    $this->assertNoText($term2->name, t('Checking if node exists'));
    // Checking database fields.
    $result = db_query('SELECT * FROM {taxonomy_term_node} WHERE nid = :nid', array(':nid' => $node->nid))->fetch();
    $this->assertTrue(empty($result), t('Term/node relationships are no longer in the database table.'));
  }

  /**
   * Test term creation with a free-tagging vocabulary from the node form.
   */
  function testNodeTermCreation() {
    // Enable tags in the vocabulary.
    $this->vocabulary->tags = 1;
    taxonomy_vocabulary_save($this->vocabulary);
    $terms = array(
      $this->randomName(),
      $this->randomName(),
      $this->randomName(),
    );
    $edit = array();
    $edit['title'] = $this->randomName();
    // Insert the terms in a comma separated list. Vocabulary 1 is a
    // free-tagging field created by the default profile.
    $edit['taxonomy[tags][' . $this->vocabulary->vid . ']'] =  implode(', ', $terms);
    $langcode = FIELD_LANGUAGE_NONE;
    $edit["body[$langcode][0][value]"] = $this->randomName();
    $this->drupalPost('node/add/article', $edit, t('Save'));
    $this->assertRaw(t('@type %title has been created.', array('@type' => t('Article'), '%title' => $edit['title'])), t('The node was created successfully'));
    foreach ($terms as $term) {
      $this->assertText($term, t('The term was saved and appears on the node page'));
    }
  }

  /**
   * Save, edit and delete a term using the user interface.
   */
  function testTermInterface() {
    $edit = array(
      'name' => $this->randomName(12),
      'description' => $this->randomName(100),
    );
    // Explicitly set the parents field to 'root', to ensure that
    // taxonomy_form_term_submit() handles the invalid term ID correctly.
    $edit['parent[]'] = 0;

    // Create the term to edit.
    $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->vid . '/list/add', $edit, t('Save'));

    $term = reset(taxonomy_get_term_by_name($edit['name']));
    $this->assertNotNull($term, t('Term found in database'));

    // Submitting a term takes us to the add page; we need the List page.
    $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->vid . '/list');

    // Test edit link as accessed from Taxonomy administration pages.
    // Because Simpletest creates its own database when running tests, we know
    // the first edit link found on the listing page is to our term.
    $this->clickLink(t('edit'));

    // This failed inexplicably with assertText, so used assertRaw. @TODO: Why?
    $this->assertText($edit['name'], t('The randomly generated term name is present.'));
    $this->assertText($edit['description'], t('The randomly generated term description is present.'));

    $edit = array(
      'name' => $this->randomName(14),
      'description' => $this->randomName(102),
    );

    // Edit the term.
    $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));

    // View the term and check that it is correct.
    $this->drupalGet('taxonomy/term/' . $term->tid);
    $this->assertText($edit['name'], t('The randomly generated term name is present.'));
    $this->assertText($edit['description'], t('The randomly generated term description is present.'));

    // Delete the term.
    $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', array(), t('Delete'));
    $this->drupalPost(NULL, NULL, t('Delete'));

    // Assert that the term no longer exists.
    $this->drupalGet('taxonomy/term/' . $term->tid);
    $this->assertResponse(404, t('The taxonomy term page was not found'));
  }

  /**
   * Test taxonomy_get_term_by_name().
   */
  function testTaxonomyGetTermByName() {
    $term = $this->createTerm($this->vocabulary);

    // Load the term with the exact name.
    $terms = taxonomy_get_term_by_name($term->name);
    $this->assertTrue(isset($terms[$term->tid]), t('Term loaded using exact name.'));

    // Load the term with space concatenated.
    $terms  = taxonomy_get_term_by_name('  ' . $term->name . '   ');
    $this->assertTrue(isset($terms[$term->tid]), t('Term loaded with extra whitespace.'));

    // Load the term with name uppercased.
    $terms = taxonomy_get_term_by_name(strtoupper($term->name));
    $this->assertTrue(isset($terms[$term->tid]), t('Term loaded with uppercased name.'));

    // Load the term with name lowercased.
    $terms = taxonomy_get_term_by_name(strtolower($term->name));
    $this->assertTrue(isset($terms[$term->tid]), t('Term loaded with lowercased name.'));

    // Try to load an invalid term name.
    $terms = taxonomy_get_term_by_name('Banana');
    $this->assertFalse($terms);

    // Try to load the term using a substring of the name.
    $terms = taxonomy_get_term_by_name(drupal_substr($term->name, 2));
    $this->assertFalse($terms);
  }
}

/**
 * Test the taxonomy_term_load_multiple() function.
 */
class TaxonomyLoadMultipleUnitTest extends TaxonomyWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Taxonomy term multiple loading',
      'description' => 'Test the loading of multiple taxonomy terms at once',
      'group' => 'Taxonomy',
    );
  }

  function setUp() {
    parent::setUp();
    $this->taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
    $this->drupalLogin($this->taxonomy_admin);
  }

  /**
   * Create a vocabulary and some taxonomy terms, ensuring they're loaded
   * correctly using taxonomy_term_load_multiple().
   */
  function testTaxonomyTermMultipleLoad() {
    // Create a vocabulary.
    $vocabulary = $this->createVocabulary();

    // Create five terms in the vocabulary.
    $i = 0;
    while ($i < 5) {
      $i++;
      $this->createTerm($vocabulary);
    }
    // Load the terms from the vocabulary.
    $terms = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
    $count = count($terms);
    $this->assertTrue($count == 5, t('Correct number of terms were loaded. !count terms.', array('!count' => $count)));

    // Load the same terms again by tid.
    $terms2 = taxonomy_term_load_multiple(array_keys($terms));
    $this->assertTrue($count == count($terms2), t('Five terms were loaded by tid'));
    $this->assertEqual($terms, $terms2, t('Both arrays contain the same terms'));

    // Load the terms by tid, with a condition on vid.
    $terms3 = taxonomy_term_load_multiple(array_keys($terms2), array('vid' => $vocabulary->vid));
    $this->assertEqual($terms2, $terms3);

    // Remove one term from the array, then delete it.
    $deleted = array_shift($terms3);
    taxonomy_term_delete($deleted->tid);
    $deleted_term = taxonomy_term_load($deleted->tid);
    $this->assertFalse($deleted_term);

    // Load terms from the vocabulary by vid.
    $terms4 = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
    $this->assertTrue(count($terms4 == 4), t('Correct number of terms were loaded.'));
    $this->assertFalse(isset($terms4[$deleted->tid]));

    // Create a single term and load it by name.
    $term = $this->createTerm($vocabulary);
    $loaded_terms = taxonomy_term_load_multiple(array(), array('name' => $term->name));
    $this->assertEqual(count($loaded_terms), 1, t('One term was loaded'));
    $loaded_term = reset($loaded_terms);
    $this->assertEqual($term->tid, $loaded_term->tid, t('Term loaded by name successfully.'));
  }
}

/**
 * Tests for taxonomy hook invocation.
 */
class TaxonomyHooksTestCase extends TaxonomyWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Taxonomy term hooks',
      'description' => 'Hooks for taxonomy term load/save/delete.',
      'group' => 'Taxonomy'
    );
  }

  function setUp() {
    parent::setUp('taxonomy', 'taxonomy_test');
    $taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
    $this->drupalLogin($taxonomy_admin);
  }

  /**
   * Test that hooks are run correctly on creating, editing and deleting a term.
   */
  function testTaxonomyTermHooks() {
    $vocabulary = $this->createVocabulary();

    // Create a term with one antonym.
    $edit = array(
      'name' => $this->randomName(),
      'antonym' => 'Long',
    );
    $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->vid . '/list/add', $edit, t('Save'));
    $term = reset(taxonomy_get_term_by_name($edit['name']));
    $this->assertEqual($term->antonym, $edit['antonym'], t('Antonym was loaded into the term object'));

    // Update the term with a different antonym.
    $edit = array(
      'name' => $this->randomName(),
      'antonym' => 'Short',
    );
    $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
    taxonomy_terms_static_reset();
    $term = taxonomy_term_load($term->tid);
    $this->assertEqual($edit['antonym'], $term->antonym, t('Antonym was successfully edited'));

    // Delete the term.
    taxonomy_term_delete($term->tid);
    $antonym = db_query('SELECT tid FROM {taxonomy_term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
    $this->assertFalse($antonym, t('The antonym were deleted from the database.'));
  }
}

/**
 * Tests for taxonomy term field and formatter.
 */
class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase {
  protected $instance;
  protected $vocabulary;

  public static function getInfo() {
    return array(
      'name'  => t('Taxonomy term field'),
      'description'  => t('Test the creation of term fields.'),
      'group' => t('Taxonomy')
    );
  }

  function setUp() {
    parent::setUp('field_test');

    $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy'));
    $this->drupalLogin($web_user);

    $this->vocabulary = $this->createVocabulary();
  }

  /**
   * Test term field validation.
   */
  function testTaxonomyTermFieldValidation() {
    $this->field_name = drupal_strtolower($this->randomName());

    // Create a field with settings to validate.
    $this->field = array(
      'field_name' => $this->field_name,
      'type' => 'taxonomy_term',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vid' => $this->vocabulary->vid,
            'parent' => '0',
          ),
        ),
      )
    );
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'bundle' => FIELD_TEST_BUNDLE,
      'widget' => array(
        'type' => 'options_select',
      ),
      'display' => array(
        'full' => array(
          'type' => 'taxonomy_term_link',
        ),
      ),
    );
    field_create_instance($this->instance);

    // Test valid and invalid values with field_attach_validate().
    $langcode = FIELD_LANGUAGE_NONE;
    $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
    $term = $this->createTerm($this->vocabulary);
    $entity->{$this->field_name}[$langcode][0]['value'] = $term->tid;
    field_attach_validate('test_entity', $entity);
    try {
      $this->assertTrue($entity->{$this->field_name}[$langcode][0]['value'] == $term->tid, t('Correct term does not cause validation error'));
    }
    catch (FieldValidationException $e) {
      $this->assertTrue($entity->{$this->field_name}[$langcode][0]['value'] != $term->tid, t('Term from wrong vocabulary does not cause validation error'));
    }

    $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
    $bad_term = $this->createTerm($this->createVocabulary());
    $entity->{$this->field_name}[$langcode][0]['value'] = $bad_term->tid;
    try {
      field_attach_validate('test_entity', $entity);
    }
    catch (FieldValidationException $e) {
      $this->assertTrue($this->field['settings']['allowed_values'][0]['vid'] != $bad_term->vid, t('Wrong term causes validation error'));
    }
  }

  /**
   * Test widgets.
   */
  function testTaxonomyTermFieldWidgets() {
    // Setup a field and instance.
    $entity_type = 'test_entity';
    $this->field_name = drupal_strtolower($this->randomName());
    $this->field = array(
      'field_name' => $this->field_name,
      'type' => 'taxonomy_term',
      'settings' => array(
        'allowed_values' => array(
          array(
            'vid' => $this->vocabulary->vid,
            'parent' => '0',
          ),
        ),
      )
    );
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'bundle' => FIELD_TEST_BUNDLE,
      'label' => $this->randomName() . '_label',
      'widget' => array(
        'type' => 'options_select',
      )
    );
    field_create_instance($this->instance);

    // Create a term in the vocabulary.
    $term = $this->createTerm($this->vocabulary);

    // Display creation form.
    $langcode = FIELD_LANGUAGE_NONE;
    $this->drupalGet('test-entity/add/test-bundle');
    $this->assertFieldByName("{$this->field_name}[$langcode][value]", '', t('Widget is displayed'));

    // Submit with some value.
    $edit = array(
      "{$this->field_name}[$langcode][value]" => array($term->tid),
    );
    $this->drupalPost(NULL, $edit, t('Save'));
    preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
    $id = $match[1];
    $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));

    // Display the object.
    $entity = field_test_entity_load($id);
    $entity->content = field_attach_view($entity_type, $entity);
    $this->content = drupal_render($entity->content);
    $this->assertText($term->name, t('Term name is displayed'));
  }
}