/[drupal]/drupal/modules/taxonomy/taxonomy.test
ViewVC logotype

Contents of /drupal/modules/taxonomy/taxonomy.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.56 - (show annotations) (download) (as text)
Sat Oct 24 04:23:51 2009 UTC (5 weeks, 1 day ago) by webchick
Branch: MAIN
Changes since 1.55: +64 -1 lines
File MIME type: text/x-php
#595876 by c960657: Fixed taxonomy term reordering (with tests).
1 <?php
2 // $Id: taxonomy.test,v 1.55 2009/10/18 07:50:45 webchick Exp $
3
4 /**
5 * @file
6 * Tests for Taxonomy module.
7 */
8
9 /**
10 * Class with common helper methods.
11 */
12 class TaxonomyWebTestCase extends DrupalWebTestCase {
13
14 /**
15 * Returns a new vocabulary with random properties.
16 */
17 function createVocabulary() {
18 // Create a vocabulary.
19 $vocabulary = new stdClass();
20 $vocabulary->name = $this->randomName();
21 $vocabulary->description = $this->randomName();
22 $vocabulary->machine_name = drupal_strtolower($this->randomName());
23 $vocabulary->help = '';
24 $vocabulary->nodes = array('article' => 'article');
25 $vocabulary->weight = mt_rand(0, 10);
26 taxonomy_vocabulary_save($vocabulary);
27 return $vocabulary;
28 }
29
30 /**
31 * Returns a new term with random properties in vocabulary $vid.
32 */
33 function createTerm($vocabulary) {
34 $term = new stdClass();
35 $term->name = $this->randomName();
36 $term->vid = $vocabulary->vid;
37 taxonomy_term_save($term);
38 return $term;
39 }
40 }
41
42 /**
43 * Tests for the taxonomy vocabulary interface.
44 */
45 class TaxonomyVocabularyFunctionalTest extends TaxonomyWebTestCase {
46
47 public static function getInfo() {
48 return array(
49 'name' => 'Taxonomy vocabulary interface',
50 'description' => 'Test the taxonomy vocabulary interface.',
51 'group' => 'Taxonomy',
52 );
53 }
54
55 function setUp() {
56 parent::setUp();
57 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
58 $this->drupalLogin($this->admin_user);
59 $this->vocabulary = $this->createVocabulary();
60 }
61
62 /**
63 * Create, edit and delete a vocabulary via the user interface.
64 */
65 function testVocabularyInterface() {
66 // Visit the main taxonomy administration page.
67 $this->drupalGet('admin/structure/taxonomy');
68
69 // Create a new vocabulary.
70 $this->clickLink(t('Add vocabulary'));
71 $edit = array();
72 $machine_name = drupal_strtolower($this->randomName());
73 $edit['name'] = $this->randomName();
74 $edit['description'] = $this->randomName();
75 $edit['machine_name'] = $machine_name;
76 $this->drupalPost(NULL, $edit, t('Save'));
77 $this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name'])), t('Vocabulary created successfully'));
78
79 // Edit the vocabulary.
80 $this->drupalGet('admin/structure/taxonomy');
81 $this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
82 $this->clickLink(t('edit vocabulary'));
83 $edit = array();
84 $edit['name'] = $this->randomName();
85 $this->drupalPost(NULL, $edit, t('Save'));
86 $this->drupalGet('admin/structure/taxonomy');
87 $this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
88
89 // Try to submit a vocabulary with a duplicate machine name.
90 $edit['machine_name'] = $machine_name;
91 $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
92 $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'));
93
94 // Try to submit an invalid machine name.
95 $edit['machine_name'] = '!&^%';
96 $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
97 $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
98 }
99
100 /**
101 * Changing weights on the vocabulary overview with two or more vocabularies.
102 */
103 function testTaxonomyAdminChangingWeights() {
104 // Create some vocabularies.
105 for ($i = 0; $i < 10; $i++) {
106 $this->createVocabulary();
107 }
108 // Get all vocabularies and change their weights.
109 $vocabularies = taxonomy_get_vocabularies();
110 $edit = array();
111 foreach ($vocabularies as $key => $vocabulary) {
112 $vocabulary->weight = -$vocabulary->weight;
113 $vocabularies[$key]->weight = $vocabulary->weight;
114 $edit[$key . '[weight]'] = $vocabulary->weight;
115 }
116 // Saving the new weights via the interface.
117 $this->drupalPost('admin/structure/taxonomy/', $edit, t('Save'));
118
119 // Load the vocabularies from the database.
120 $new_vocabularies = taxonomy_get_vocabularies();
121
122 // Check that the weights are saved in the database correctly.
123 foreach ($vocabularies as $key => $vocabulary) {
124 $this->assertEqual($new_vocabularies[$key]->weight, $vocabularies[$key]->weight, t('The vocabulary weight was changed.'));
125 }
126 }
127
128 /**
129 * Test the vocabulary overview with no vocabularies.
130 */
131 function testTaxonomyAdminNoVocabularies() {
132 // Delete all vocabularies.
133 $vocabularies = taxonomy_get_vocabularies();
134 foreach ($vocabularies as $key => $vocabulary) {
135 taxonomy_vocabulary_delete($key);
136 }
137 // Confirm that no vocabularies are found in the database.
138 $this->assertFalse(taxonomy_get_vocabularies(), t('No vocabularies found in the database'));
139 $this->drupalGet('admin/structure/taxonomy');
140 // Check the default message for no vocabularies.
141 $this->assertText(t('No vocabularies available.'), t('No vocabularies were found.'));
142 }
143
144 /**
145 * Deleting a vocabulary.
146 */
147 function testTaxonomyAdminDeletingVocabulary() {
148 // Create a vocabulary.
149 $edit = array(
150 'name' => $this->randomName(),
151 'machine_name' => drupal_strtolower($this->randomName()),
152 );
153 $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));
154 $this->assertText(t('Created new vocabulary'), t('New vocabulary was created.'));
155
156 // Check the created vocabulary.
157 $vocabularies = taxonomy_get_vocabularies();
158 $vid = $vocabularies[count($vocabularies)-1]->vid;
159 entity_get_controller('taxonomy_vocabulary')->resetCache();
160 $vocabulary = taxonomy_vocabulary_load($vid);
161 $this->assertTrue($vocabulary, t('Vocabulary found in database'));
162
163 // Delete the vocabulary.
164 $edit = array();
165 $this->drupalPost('admin/structure/taxonomy/' . $vid, $edit, t('Delete'));
166 $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', array('%name' => $vocabulary->name)), t('[confirm deletion] Asks for confirmation.'));
167 $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.'));
168
169 // Confirm deletion.
170 $this->drupalPost(NULL, NULL, t('Delete'));
171 $this->assertRaw(t('Deleted vocabulary %name.', array('%name' => $vocabulary->name)), t('Vocabulary deleted'));
172 entity_get_controller('taxonomy_vocabulary')->resetCache();
173 $this->assertFalse(taxonomy_vocabulary_load($vid), t('Vocabulary is not found in the database'));
174 }
175 }
176
177
178 /**
179 * Tests for taxonomy vocabulary functions.
180 */
181 class TaxonomyVocabularyUnitTest extends TaxonomyWebTestCase {
182
183 public static function getInfo() {
184 return array(
185 'name' => 'Taxonomy vocabularies',
186 'description' => 'Test loading, saving and deleting vocabularies.',
187 'group' => 'Taxonomy',
188 );
189 }
190
191 function setUp() {
192 parent::setUp('taxonomy');
193 $admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy'));
194 $this->drupalLogin($admin_user);
195 $this->vocabulary = $this->createVocabulary();
196 }
197
198 /**
199 * Ensure that when an invalid vocabulary vid is loaded, it is possible
200 * to load the same vid successfully if it subsequently becomes valid.
201 */
202 function testTaxonomyVocabularyLoadReturnFalse() {
203 // Load a vocabulary that doesn't exist.
204 $vocabularies = taxonomy_get_vocabularies();
205 $vid = count($vocabularies) + 1;
206 $vocabulary = taxonomy_vocabulary_load($vid);
207 // This should not return an object because no such vocabulary exists.
208 $this->assertTrue(empty($vocabulary), t('No object loaded.'));
209
210 // Create a new vocabulary.
211 $this->createVocabulary();
212 // Load the vocabulary with the same $vid from earlier.
213 // This should return a vocabulary object since it now matches a real vid.
214 $vocabulary = taxonomy_vocabulary_load($vid);
215 $this->assertTrue(!empty($vocabulary) && is_object($vocabulary), t('Vocabulary is an object'));
216 $this->assertTrue($vocabulary->vid == $vid, t('Valid vocabulary vid is the same as our previously invalid one.'));
217 }
218
219 /**
220 * Ensure that the vocabulary static reset works correctly.
221 */
222 function testTaxonomyVocabularyLoadStaticReset() {
223 $original_vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
224 $this->assertTrue(is_object($original_vocabulary), t('Vocabulary loaded successfully'));
225 $this->assertEqual($this->vocabulary->name, $original_vocabulary->name, t('Vocabulary loaded successfully'));
226
227 // Change the name and description.
228 $vocabulary = $original_vocabulary;
229 $vocabulary->name = $this->randomName();
230 $vocabulary->description = $this->randomName();
231 taxonomy_vocabulary_save($vocabulary);
232
233 // Load the vocabulary.
234 $new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid);
235 $this->assertEqual($new_vocabulary->name, $vocabulary->name);
236 $this->assertEqual($new_vocabulary->name, $vocabulary->name);
237
238 // Delete the vocabulary.
239 taxonomy_vocabulary_delete($this->vocabulary->vid);
240 $vocabularies = taxonomy_get_vocabularies();
241 $this->assertTrue(!isset($vocabularies[$this->vocabulary->vid]), t('The vocabulary was deleted'));
242 }
243
244 /**
245 * Tests for loading multiple vocabularies.
246 */
247 function testTaxonomyVocabularyLoadMultiple() {
248
249 // Delete any existing vocabularies.
250 foreach (taxonomy_get_vocabularies() as $vocabulary) {
251 taxonomy_vocabulary_delete($vocabulary->vid);
252 }
253
254 // Create some vocabularies and assign weights.
255 $vocabulary1 = $this->createVocabulary();
256 $vocabulary1->weight = 0;
257 taxonomy_vocabulary_save($vocabulary1);
258 $vocabulary2 = $this->createVocabulary();
259 $vocabulary2->weight = 1;
260 taxonomy_vocabulary_save($vocabulary2);
261 $vocabulary3 = $this->createVocabulary();
262 $vocabulary3->weight = 2;
263 taxonomy_vocabulary_save($vocabulary3);
264
265 // Fetch the names for all vocabularies, confirm that they are keyed by
266 // machine name.
267 $names = taxonomy_vocabulary_get_names();
268 $this->assertEqual($names[$vocabulary1->machine_name]->name, $vocabulary1->name, t('Vocabulary 1 name found.'));
269
270 // Fetch all of the vocabularies using taxonomy_get_vocabularies().
271 // Confirm that the vocabularies are ordered by weight.
272 $vocabularies = taxonomy_get_vocabularies();
273 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, t('Vocabulary was found in the vocabularies array.'));
274 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, t('Vocabulary was found in the vocabularies array.'));
275 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, t('Vocabulary was found in the vocabularies array.'));
276
277 // Fetch the vocabularies with taxonomy_vocabulary_load_multiple(), specifying IDs.
278 // Ensure they are returned in the same order as the original array.
279 $vocabularies = taxonomy_vocabulary_load_multiple(array($vocabulary3->vid, $vocabulary2->vid, $vocabulary1->vid));
280 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary3->vid, t('Vocabulary loaded successfully by ID.'));
281 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary2->vid, t('Vocabulary loaded successfully by ID.'));
282 $this->assertEqual(array_shift($vocabularies)->vid, $vocabulary1->vid, t('Vocabulary loaded successfully by ID.'));
283
284 // Fetch vocabulary 1 by name.
285 $vocabulary = current(taxonomy_vocabulary_load_multiple(array(), array('name' => $vocabulary1->name)));
286 $this->assertTrue($vocabulary->vid == $vocabulary1->vid, t('Vocabulary loaded successfully by name.'));
287
288 // Fetch vocabulary 1 by name and ID.
289 $this->assertTrue(current(taxonomy_vocabulary_load_multiple(array($vocabulary1->vid), array('name' => $vocabulary1->name)))->vid == $vocabulary1->vid, t('Vocabulary loaded successfully by name and ID.'));
290 }
291 }
292
293 /**
294 * Unit tests for taxonomy term functions.
295 */
296 class TaxonomyTermUnitTest extends TaxonomyWebTestCase {
297
298 public static function getInfo() {
299 return array(
300 'name' => 'Taxonomy term unit tests',
301 'description' => 'Unit tests for taxonomy term functions.',
302 'group' => 'Taxonomy',
303 );
304 }
305 }
306
307 /**
308 * Tests for taxonomy term functions.
309 */
310 class TaxonomyTermTestCase extends TaxonomyWebTestCase {
311
312 public static function getInfo() {
313 return array(
314 'name' => 'Taxonomy term functions and forms.',
315 'description' => 'Test load, save and delete for taxonomy terms.',
316 'group' => 'Taxonomy',
317 );
318 }
319
320 function setUp() {
321 parent::setUp('taxonomy');
322 $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
323 $this->drupalLogin($this->admin_user);
324 $this->vocabulary = $this->createVocabulary();
325
326 $this->instance = array(
327 'field_name' => 'taxonomy_' . $this->vocabulary->machine_name,
328 'bundle' => 'article',
329 'object_type' => 'node',
330 'widget' => array(
331 'type' => 'options_select',
332 ),
333 'display' => array(
334 'full' => array(
335 'type' => 'taxonomy_term_link',
336 ),
337 ),
338 );
339 field_create_instance($this->instance);
340 }
341
342 /**
343 * Test terms in a single and multiple hierarchy.
344 */
345 function testTaxonomyTermHierarchy() {
346 // Create two taxonomy terms.
347 $term1 = $this->createTerm($this->vocabulary);
348 $term2 = $this->createTerm($this->vocabulary);
349
350 // Edit $term2, setting $term1 as parent.
351 $edit = array();
352 $edit['parent[]'] = array($term1->tid);
353 $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));
354
355 // Check the hierarchy.
356 $children = taxonomy_get_children($term1->tid);
357 $parents = taxonomy_get_parents($term2->tid);
358 $this->assertTrue(isset($children[$term2->tid]), t('Child found correctly.'));
359 $this->assertTrue(isset($parents[$term1->tid]), t('Parent found correctly.'));
360
361 // Create a third term and save this as a parent of term2.
362 $term3 = $this->createTerm($this->vocabulary);
363 $term2->parent = array($term1->tid, $term3->tid);
364 taxonomy_term_save($term2);
365 $parents = taxonomy_get_parents($term2->tid);
366 $this->assertTrue(isset($parents[$term1->tid]) && isset($parents[$term3->tid]), t('Both parents found successfully.'));
367 }
368
369 /**
370 * Test that hook_node_$op implementations work correctly.
371 *
372 * Save & edit a node and assert that taxonomy terms are saved/loaded properly.
373 */
374 function testTaxonomyNode() {
375 // Create two taxonomy terms.
376 $term1 = $this->createTerm($this->vocabulary);
377 $term2 = $this->createTerm($this->vocabulary);
378
379 // Post an article.
380 $edit = array();
381 $langcode = FIELD_LANGUAGE_NONE;
382 $edit["title[$langcode][0][value]"] = $this->randomName();
383 $edit["body[$langcode][0][value]"] = $this->randomName();
384 $edit[$this->instance['field_name'] . '[' . $langcode .'][value][]'] = $term1->tid;
385 $this->drupalPost('node/add/article', $edit, t('Save'));
386
387 // Check that the term is displayed when the node is viewed.
388 $node = $this->drupalGetNodeByTitle($edit["title[$langcode][0][value]"]);
389 $this->drupalGet('node/' . $node->nid);
390 $this->assertText($term1->name, t('Term is displayed when viewing the node.'));
391
392 // Edit the node with a different term.
393 $edit[$this->instance['field_name'] . '[' . $langcode . '][value][]'] = $term2->tid;
394 $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
395
396 $this->drupalGet('node/' . $node->nid);
397 $this->assertText($term2->name, t('Term is displayed when viewing the node.'));
398 }
399
400 /**
401 * Test term creation with a free-tagging vocabulary from the node form.
402 */
403 function testNodeTermCreation() {
404 // Enable tags in the vocabulary.
405 $instance = $this->instance;
406 $instance['widget'] = array('type' => 'taxonomy_autocomplete');
407 $instance['bundle'] = 'page';
408 field_create_instance($instance);
409 $terms = array(
410 $this->randomName(),
411 $this->randomName(),
412 $this->randomName(),
413 );
414
415 $edit = array();
416 $langcode = FIELD_LANGUAGE_NONE;
417 $edit["title[$langcode][0][value]"] = $this->randomName();
418 $edit["body[$langcode][0][value]"] = $this->randomName();
419 // Insert the terms in a comma separated list. Vocabulary 1 is a
420 // free-tagging field created by the default profile.
421 $edit[$instance['field_name'] . "[$langcode][value]"] = implode(', ', $terms);
422 $this->drupalPost('node/add/page', $edit, t('Save'));
423 $this->assertRaw(t('@type %title has been created.', array('@type' => t('Page'), '%title' => $edit["title[$langcode][0][value]"])), t('The node was created successfully'));
424 foreach ($terms as $term) {
425 $this->assertText($term, t('The term was saved and appears on the node page'));
426 }
427 }
428
429 /**
430 * Save, edit and delete a term using the user interface.
431 */
432 function testTermInterface() {
433 $edit = array(
434 'name' => $this->randomName(12),
435 'description' => $this->randomName(100),
436 );
437 // Explicitly set the parents field to 'root', to ensure that
438 // taxonomy_form_term_submit() handles the invalid term ID correctly.
439 $edit['parent[]'] = array(0);
440
441 // Create the term to edit.
442 $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->vid . '/list/add', $edit, t('Save'));
443
444 $term = reset(taxonomy_get_term_by_name($edit['name']));
445 $this->assertNotNull($term, t('Term found in database'));
446
447 // Submitting a term takes us to the add page; we need the List page.
448 $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->vid . '/list');
449
450 // Test edit link as accessed from Taxonomy administration pages.
451 // Because Simpletest creates its own database when running tests, we know
452 // the first edit link found on the listing page is to our term.
453 $this->clickLink(t('edit'));
454
455 // This failed inexplicably with assertText, so used assertRaw. @TODO: Why?
456 $this->assertText($edit['name'], t('The randomly generated term name is present.'));
457 $this->assertText($edit['description'], t('The randomly generated term description is present.'));
458
459 $edit = array(
460 'name' => $this->randomName(14),
461 'description' => $this->randomName(102),
462 );
463
464 // Edit the term.
465 $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
466
467 // View the term and check that it is correct.
468 $this->drupalGet('taxonomy/term/' . $term->tid);
469 $this->assertText($edit['name'], t('The randomly generated term name is present.'));
470 $this->assertText($edit['description'], t('The randomly generated term description is present.'));
471
472 // Delete the term.
473 $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', array(), t('Delete'));
474 $this->drupalPost(NULL, NULL, t('Delete'));
475
476 // Assert that the term no longer exists.
477 $this->drupalGet('taxonomy/term/' . $term->tid);
478 $this->assertResponse(404, t('The taxonomy term page was not found'));
479 }
480
481 /**
482 * Save, edit and delete a term using the user interface.
483 */
484 function testTermReorder() {
485 $this->createTerm($this->vocabulary);
486 $this->createTerm($this->vocabulary);
487 $this->createTerm($this->vocabulary);
488
489 // Fetch the created terms in the default alphabetical order, i.e. term1
490 // precedes term2 alphabetically, and term2 precedes term3.
491 drupal_static_reset('taxonomy_get_tree');
492 drupal_static_reset('taxonomy_get_treeparent');
493 drupal_static_reset('taxonomy_get_treeterms');
494 list($term1, $term2, $term3) = taxonomy_get_tree($this->vocabulary->vid);
495
496 // Change the order to term2, term3, term1. Emulate the reordering done by
497 // tabledrag.js by changing the page HTML source. Each term has three hidden
498 // fields, "tid:1:0[tid]", "tid:1:0[parent]", and "tid:1:0[depth]". The
499 // order of the input fields in the page is used when the form is processed.
500 $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->vid . '/list');
501 $reorder = array(
502 'tid:' . $term1->tid . ':0' => 'tid:' . $term2->tid . ':0',
503 'tid:' . $term2->tid . ':0' => 'tid:' . $term3->tid . ':0',
504 'tid:' . $term3->tid . ':0' => 'tid:' . $term1->tid . ':0',
505 );
506 $this->drupalSetContent(strtr($this->drupalGetContent(), $reorder));
507
508 // Make term3 a child of term2, and update all hidden fields.
509 $edit = array(
510 'tid:' . $term2->tid . ':0[tid]' => $term2->tid,
511 'tid:' . $term2->tid . ':0[parent]' => 0,
512 'tid:' . $term2->tid . ':0[depth]' => 0,
513 'tid:' . $term3->tid . ':0[tid]' => $term3->tid,
514 'tid:' . $term3->tid . ':0[parent]' => $term2->tid,
515 'tid:' . $term3->tid . ':0[depth]' => 1,
516 'tid:' . $term1->tid . ':0[tid]' => $term1->tid,
517 'tid:' . $term1->tid . ':0[parent]' => 0,
518 'tid:' . $term1->tid . ':0[depth]' => 0,
519 );
520 $this->drupalPost(NULL, $edit, t('Save'));
521
522 drupal_static_reset('taxonomy_get_tree');
523 drupal_static_reset('taxonomy_get_treeparent');
524 drupal_static_reset('taxonomy_get_treeterms');
525 $terms = taxonomy_get_tree($this->vocabulary->vid);
526 $this->assertEqual($terms[0]->tid, $term2->tid, t('Term 2 was moved above term 1.'));
527 $this->assertEqual($terms[1]->parents, array($term2->tid), t('Term 3 was made a child of term 2.'));
528 $this->assertEqual($terms[2]->tid, $term1->tid, t('Term 1 was moved below term 2.'));
529
530 $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->vid . '/list', array(), t('Reset to alphabetical'));
531 // Submit confirmation form.
532 $this->drupalPost(NULL, array(), t('Reset to alphabetical'));
533
534 drupal_static_reset('taxonomy_get_tree');
535 drupal_static_reset('taxonomy_get_treeparent');
536 drupal_static_reset('taxonomy_get_treeterms');
537 $terms = taxonomy_get_tree($this->vocabulary->vid);
538 $this->assertEqual($terms[0]->tid, $term1->tid, t('Term 1 was moved to back above term 2.'));
539 $this->assertEqual($terms[1]->tid, $term2->tid, t('Term 2 was moved to back below term 1.'));
540 $this->assertEqual($terms[2]->tid, $term3->tid, t('Term 3 is still below term 2.'));
541 $this->assertEqual($terms[2]->parents, array($term2->tid), t('Term 3 is still a child of term 2.').var_export($terms[1]->tid,1));
542 }
543
544 /**
545 * Test taxonomy_get_term_by_name().
546 */
547 function testTaxonomyGetTermByName() {
548 $term = $this->createTerm($this->vocabulary);
549
550 // Load the term with the exact name.
551 $terms = taxonomy_get_term_by_name($term->name);
552 $this->assertTrue(isset($terms[$term->tid]), t('Term loaded using exact name.'));
553
554 // Load the term with space concatenated.
555 $terms = taxonomy_get_term_by_name(' ' . $term->name . ' ');
556 $this->assertTrue(isset($terms[$term->tid]), t('Term loaded with extra whitespace.'));
557
558 // Load the term with name uppercased.
559 $terms = taxonomy_get_term_by_name(strtoupper($term->name));
560 $this->assertTrue(isset($terms[$term->tid]), t('Term loaded with uppercased name.'));
561
562 // Load the term with name lowercased.
563 $terms = taxonomy_get_term_by_name(strtolower($term->name));
564 $this->assertTrue(isset($terms[$term->tid]), t('Term loaded with lowercased name.'));
565
566 // Try to load an invalid term name.
567 $terms = taxonomy_get_term_by_name('Banana');
568 $this->assertFalse($terms);
569
570 // Try to load the term using a substring of the name.
571 $terms = taxonomy_get_term_by_name(drupal_substr($term->name, 2));
572 $this->assertFalse($terms);
573 }
574 }
575
576 /**
577 * Test the taxonomy_term_load_multiple() function.
578 */
579 class TaxonomyLoadMultipleUnitTest extends TaxonomyWebTestCase {
580
581 public static function getInfo() {
582 return array(
583 'name' => 'Taxonomy term multiple loading',
584 'description' => 'Test the loading of multiple taxonomy terms at once',
585 'group' => 'Taxonomy',
586 );
587 }
588
589 function setUp() {
590 parent::setUp();
591 $this->taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
592 $this->drupalLogin($this->taxonomy_admin);
593 }
594
595 /**
596 * Create a vocabulary and some taxonomy terms, ensuring they're loaded
597 * correctly using taxonomy_term_load_multiple().
598 */
599 function testTaxonomyTermMultipleLoad() {
600 // Create a vocabulary.
601 $vocabulary = $this->createVocabulary();
602
603 // Create five terms in the vocabulary.
604 $i = 0;
605 while ($i < 5) {
606 $i++;
607 $this->createTerm($vocabulary);
608 }
609 // Load the terms from the vocabulary.
610 $terms = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
611 $count = count($terms);
612 $this->assertTrue($count == 5, t('Correct number of terms were loaded. !count terms.', array('!count' => $count)));
613
614 // Load the same terms again by tid.
615 $terms2 = taxonomy_term_load_multiple(array_keys($terms));
616 $this->assertTrue($count == count($terms2), t('Five terms were loaded by tid'));
617 $this->assertEqual($terms, $terms2, t('Both arrays contain the same terms'));
618
619 // Load the terms by tid, with a condition on vid.
620 $terms3 = taxonomy_term_load_multiple(array_keys($terms2), array('vid' => $vocabulary->vid));
621 $this->assertEqual($terms2, $terms3);
622
623 // Remove one term from the array, then delete it.
624 $deleted = array_shift($terms3);
625 taxonomy_term_delete($deleted->tid);
626 $deleted_term = taxonomy_term_load($deleted->tid);
627 $this->assertFalse($deleted_term);
628
629 // Load terms from the vocabulary by vid.
630 $terms4 = taxonomy_term_load_multiple(NULL, array('vid' => $vocabulary->vid));
631 $this->assertTrue(count($terms4 == 4), t('Correct number of terms were loaded.'));
632 $this->assertFalse(isset($terms4[$deleted->tid]));
633
634 // Create a single term and load it by name.
635 $term = $this->createTerm($vocabulary);
636 $loaded_terms = taxonomy_term_load_multiple(array(), array('name' => $term->name));
637 $this->assertEqual(count($loaded_terms), 1, t('One term was loaded'));
638 $loaded_term = reset($loaded_terms);
639 $this->assertEqual($term->tid, $loaded_term->tid, t('Term loaded by name successfully.'));
640 }
641 }
642
643 /**
644 * Tests for taxonomy hook invocation.
645 */
646 class TaxonomyHooksTestCase extends TaxonomyWebTestCase {
647 public static function getInfo() {
648 return array(
649 'name' => 'Taxonomy term hooks',
650 'description' => 'Hooks for taxonomy term load/save/delete.',
651 'group' => 'Taxonomy',
652 );
653 }
654
655 function setUp() {
656 parent::setUp('taxonomy', 'taxonomy_test');
657 $taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
658 $this->drupalLogin($taxonomy_admin);
659 }
660
661 /**
662 * Test that hooks are run correctly on creating, editing and deleting a term.
663 */
664 function testTaxonomyTermHooks() {
665 $vocabulary = $this->createVocabulary();
666
667 // Create a term with one antonym.
668 $edit = array(
669 'name' => $this->randomName(),
670 'antonym' => 'Long',
671 );
672 $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->vid . '/list/add', $edit, t('Save'));
673 $term = reset(taxonomy_get_term_by_name($edit['name']));
674 $this->assertEqual($term->antonym, $edit['antonym'], t('Antonym was loaded into the term object'));
675
676 // Update the term with a different antonym.
677 $edit = array(
678 'name' => $this->randomName(),
679 'antonym' => 'Short',
680 );
681 $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
682 taxonomy_terms_static_reset();
683 $term = taxonomy_term_load($term->tid);
684 $this->assertEqual($edit['antonym'], $term->antonym, t('Antonym was successfully edited'));
685
686 // Delete the term.
687 taxonomy_term_delete($term->tid);
688 $antonym = db_query('SELECT tid FROM {taxonomy_term_antonym} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField();
689 $this->assertFalse($antonym, t('The antonym were deleted from the database.'));
690 }
691 }
692
693 /**
694 * Tests for taxonomy term field and formatter.
695 */
696 class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase {
697 protected $instance;
698 protected $vocabulary;
699
700 public static function getInfo() {
701 return array(
702 'name' => 'Taxonomy term field',
703 'description' => 'Test the creation of term fields.',
704 'group' => 'Taxonomy',
705 );
706 }
707
708 function setUp() {
709 parent::setUp('field_test');
710
711 $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy'));
712 $this->drupalLogin($web_user);
713 $this->vocabulary = $this->createVocabulary();
714 }
715
716 /**
717 * Test term field validation.
718 */
719 function testTaxonomyTermFieldValidation() {
720 $this->field_name = drupal_strtolower($this->randomName());
721
722 // Create a field with settings to validate.
723 $this->field = array(
724 'field_name' => $this->field_name,
725 'type' => 'taxonomy_term',
726 'settings' => array(
727 'allowed_values' => array(
728 array(
729 'vid' => $this->vocabulary->vid,
730 'parent' => '0',
731 ),
732 ),
733 )
734 );
735 field_create_field($this->field);
736 $this->instance = array(
737 'field_name' => $this->field_name,
738 'object_type' => 'test_entity',
739 'bundle' => FIELD_TEST_BUNDLE,
740 'widget' => array(
741 'type' => 'options_select',
742 ),
743 'display' => array(
744 'full' => array(
745 'type' => 'taxonomy_term_link',
746 ),
747 ),
748 );
749 field_create_instance($this->instance);
750
751 // Test valid and invalid values with field_attach_validate().
752 $langcode = FIELD_LANGUAGE_NONE;
753 $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
754 $term = $this->createTerm($this->vocabulary);
755 $entity->{$this->field_name}[$langcode][0]['value'] = $term->tid;
756 field_attach_validate('test_entity', $entity);
757 try {
758 $this->assertTrue($entity->{$this->field_name}[$langcode][0]['value'] == $term->tid, t('Correct term does not cause validation error'));
759 }
760 catch (FieldValidationException $e) {
761 $this->assertTrue($entity->{$this->field_name}[$langcode][0]['value'] != $term->tid, t('Term from wrong vocabulary does not cause validation error'));
762 }
763
764 $entity = field_test_create_stub_entity(0, 0, FIELD_TEST_BUNDLE);
765 $bad_term = $this->createTerm($this->createVocabulary());
766 $entity->{$this->field_name}[$langcode][0]['value'] = $bad_term->tid;
767 try {
768 field_attach_validate('test_entity', $entity);
769 }
770 catch (FieldValidationException $e) {
771 $this->assertTrue($this->field['settings']['allowed_values'][0]['vid'] != $bad_term->vid, t('Wrong term causes validation error'));
772 }
773 }
774
775 /**
776 * Test widgets.
777 */
778 function testTaxonomyTermFieldWidgets() {
779 // Setup a field and instance.
780 $entity_type = 'test_entity';
781 $this->field_name = drupal_strtolower($this->randomName());
782 $this->field = array(
783 'field_name' => $this->field_name,
784 'type' => 'taxonomy_term',
785 'settings' => array(
786 'allowed_values' => array(
787 array(
788 'vid' => $this->vocabulary->vid,
789 'parent' => '0',
790 ),
791 ),
792 )
793 );
794 field_create_field($this->field);
795 $this->instance = array(
796 'field_name' => $this->field_name,
797 'object_type' => 'test_entity',
798 'bundle' => FIELD_TEST_BUNDLE,
799 'label' => $this->randomName() . '_label',
800 'widget' => array(
801 'type' => 'options_select',
802 )
803 );
804 field_create_instance($this->instance);
805
806 // Create a term in the vocabulary.
807 $term = $this->createTerm($this->vocabulary);
808
809 // Display creation form.
810 $langcode = FIELD_LANGUAGE_NONE;
811 $this->drupalGet('test-entity/add/test-bundle');
812 $this->assertFieldByName("{$this->field_name}[$langcode][value]", '', t('Widget is displayed'));
813
814 // Submit with some value.
815 $edit = array(
816 "{$this->field_name}[$langcode][value]" => array($term->tid),
817 );
818 $this->drupalPost(NULL, $edit, t('Save'));
819 preg_match('|test-entity/(\d+)/edit|', $this->url, $match);
820 $id = $match[1];
821 $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
822
823 // Display the object.
824 $entity = field_test_entity_test_load($id);
825 $entities = array($id => $entity);
826 field_attach_prepare_view($entity_type, $entities, 'full');
827 $entity->content = field_attach_view($entity_type, $entity);
828 $this->content = drupal_render($entity->content);
829 $this->assertText($term->name, t('Term name is displayed'));
830 }
831 }

  ViewVC Help
Powered by ViewVC 1.1.2