Removed a TODO from the list of tests to be written.
[project/taxonomy_menu.git] / tests / taxonomy_menu.test
1 <?php
2
3 /**
4 * @file
5 * Tests for taxonomy_menu.module.
6 *
7 * @TODO Add tests for other configuration options:
8 * - Display children.
9 * - Display number of children.
10 * - Edition of nodes.
11 */
12
13 /**
14 * Provides common helper methods for Taxonomy menu module tests.
15 */
16 class TaxonomyMenuWebTestCase extends TaxonomyWebTestCase {
17
18 function setUp() {
19 parent::setUp('taxonomy_menu');
20 // Create and login administrative user.
21 $admin_user = $this->drupalCreateUser(array('administer taxonomy', 'administer menu', 'bypass node access'));
22 $this->drupalLogin($admin_user);
23 // Create a vocabulary and a hierarchy of taxonomy terms for it.
24 $this->vocabulary = $this->createVocabulary();
25 $this->hierarchy = $this->createTermsHierarchy($this->vocabulary);
26 }
27
28 /**
29 * Asserts that the number of menu links is equal to the number of taxonomy
30 * terms for a given menu name.
31 *
32 * @param $terms
33 * The terms, which are created in this class.
34 */
35 protected function assertEqualNumberTermsMenuLinks($terms_count, $vocabulary, $menu_name) {
36 $vid = $vocabulary->vid;
37 // Building a query getting the number of menu links for this vocabulary.
38 $query = db_select('menu_links', 'ml')
39 ->fields('ml')
40 ->condition('ml.module', 'taxonomy_menu')
41 ->condition('ml.menu_name', $menu_name);
42 $query->join('taxonomy_menu', 'tm', 'ml.mlid = tm.mlid');
43 $query->condition('tm.vid', $vid, '=');
44 $query_count = $query->execute()->rowCount();
45 $message = $query_count . ' menu links were found for the ' . $terms_count . ' taxonomy terms of vocabulary ' . $vocabulary->name . '.';
46 $this->assertEqual($terms_count, $query_count, $message);
47 }
48
49 /**
50 * Creates a hierarchy of taxonomy terms for the vocabulary defined in the
51 * current class. The hierarchy is as follow:
52 *
53 * terms[1] | depth: 0
54 * -- terms[2] | depth: 1
55 * -- terms[3] | depth: 1
56 * ---- terms[4] | depth: 2
57 * -- terms[5] | depth: 1
58 * terms[6] | depth: 0
59 * -- terms[7] | depth: 1
60 *
61 * @TODO Add multiple parents when taxonomy_menu can deal with it.
62 */
63 protected function createTermsHierarchy($vocabulary) {
64 $terms = array();
65 // Create taxonomy terms.
66 for ($i = 1; $i < 8; $i++) {
67 $terms[$i] = $this->createTerm($vocabulary);
68 }
69 // Set the hierarchy.
70 $terms[2]->parent = array($terms[1]->tid);
71 taxonomy_term_save($terms[2]);
72 $terms[3]->parent = array($terms[1]->tid);
73 taxonomy_term_save($terms[3]);
74 $terms[4]->parent = array($terms[3]->tid);
75 taxonomy_term_save($terms[4]);
76 $terms[5]->parent = array($terms[1]->tid);
77 taxonomy_term_save($terms[5]);
78 $terms[7]->parent = array($terms[6]->tid);
79 taxonomy_term_save($terms[7]);
80
81 return $terms;
82 }
83
84 /**
85 * Fetch the menu item from the database and compare it to the specified
86 * array.
87 *
88 * @param $mlid
89 * Menu item id.
90 * @param $item
91 * Array containing properties to verify.
92 */
93 function assertMenuLink($mlid, array $expected_item) {
94 // Retrieve menu link.
95 $item = db_query('SELECT * FROM {menu_links} WHERE mlid = :mlid', array(':mlid' => $mlid))->fetchAssoc();
96 $options = unserialize($item['options']);
97 if (!empty($options['query'])) {
98 $item['link_path'] .= '?' . drupal_http_build_query($options['query']);
99 }
100 if (!empty($options['fragment'])) {
101 $item['link_path'] .= '#' . $options['fragment'];
102 }
103 foreach ($expected_item as $key => $value) {
104 $this->assertEqual($item[$key], $value, t('Parameter %key had expected value.', array('%key' => $key)));
105 }
106 }
107 }
108
109 /**
110 * Tests the taxonomy vocabulary interface.
111 */
112 class TaxonomyMenuFunctionalTest extends TaxonomyMenuWebTestCase {
113
114 public static function getInfo() {
115 return array(
116 'name' => 'Vocabulary interface',
117 'description' => 'Test the taxonomy menu vocabulary interface.',
118 'group' => 'Taxonomy menu',
119 );
120 }
121
122 function setUp() {
123 parent::setUp('taxonomy_menu');
124 }
125
126 /**
127 * Save, edit and delete a taxonomy vocabulary using the user interface.
128 */
129 function testTaxonomyMenuVocabularyInterface() {
130 $menu_name = 'user-menu';
131 $vocab = $this->vocabulary->machine_name;
132 // Visit the main taxonomy administration page.
133 $this->drupalGet('admin/structure/taxonomy/' . $vocab . '/edit');
134 // Options for the taxonomy vocabulary edit form.
135 $edit = array();
136 // Try to submit a vocabulary when menu location is a root menu item.
137 $edit['taxonomy_menu[vocab_parent]'] = $menu_name . ':0';
138 $this->drupalPost(NULL, $edit, t('Save'));
139 $this->assertRaw(t('The Taxonomy menu has been created.'));
140 // Try to re-submit a vocabulary when an option has changed.
141 $edit['taxonomy_menu[options][hide_empty_terms]'] = TRUE;
142 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
143 $this->assertRaw(t('The Taxonomy menu has been updated.'));
144 // Try to re-submit a vocabulary without changing any option.
145 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
146 $this->assertRaw(t('The Taxonomy menu was not updated. Nothing to update.'));
147 // Try to submit a vocabulary removing the menu location.
148 $edit['taxonomy_menu[vocab_parent]'] = '0';
149 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
150 $this->assertRaw(t('The Taxonomy menu has been removed.'));
151 }
152
153 /**
154 * Re-order terms on the terms' overview page.
155 */
156 function testTaxonomyMenuTermsOverviewInterface() {
157 $term7 = $this->hierarchy[7]; // last term of our hierarchy.
158 // Submit the main taxonomy administration page.
159 $edit = array(
160 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
161 'taxonomy_menu[sync]' => TRUE,
162 );
163 $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/edit', $edit, t('Save'));
164 // Visit the terms overview page.
165 $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name);
166 // Take last term, place it on top and save.
167 $edit = array(
168 'tid:' . $term7->tid . ':0[tid]' => $term7->tid,
169 'tid:' . $term7->tid . ':0[parent]' => 0,
170 'tid:' . $term7->tid . ':0[depth]' => 0,
171 'tid:' . $term7->tid . ':0[weight]' => -5,
172 );
173 $this->drupalPost(NULL, $edit, t('Save'));
174 $this->assertRaw(t('The Taxonomy menu has been updated.'));
175 // Test "Reset to alphabetical".
176 $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name, array(), t('Reset to alphabetical'));
177 $this->drupalPost(NULL, array(), t('Reset to alphabetical'));
178 $this->assertRaw(t('The Taxonomy menu has been updated.'));
179 }
180
181 /**
182 * Save, edit and delete a term using the user interface.
183 */
184 function testTaxonomyMenuTermInterface() {
185 $menu_name = 'main-menu';
186 $vocab_name = $this->vocabulary->machine_name;
187 $edit = array();
188 $edit['taxonomy_menu[vocab_parent]'] = $menu_name . ':0';
189 $this->drupalPost('admin/structure/taxonomy/' . $vocab_name . '/edit', $edit, t('Save'));
190 // Create a new term from the interface.
191 $edit = array();
192 $edit = array(
193 'name' => $this->randomName(12),
194 'description[value]' => $this->randomName(100),
195 );
196 $this->drupalPost('admin/structure/taxonomy/' . $vocab_name . '/add', $edit, t('Save'));
197 $terms = taxonomy_get_term_by_name($edit['name']);
198 $term = reset($terms);
199 $options = array('%term' => $edit['name'], '%menu_name' => $menu_name);
200 $this->assertRaw(t('Added term %term to taxonomy menu %menu_name.', $options));
201 // Update an existing term from the interface.
202 $edit = array(
203 'name' => $this->randomName(12),
204 'description[value]' => $this->randomName(100),
205 );
206 $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Save'));
207 $options = array('%term' => $edit['name'], '%menu_name' => $menu_name);
208 $this->assertRaw(t('Updated term %term in taxonomy menu %menu_name.', $options));
209 // Delete an existing term from the interface.
210 $this->drupalPost('taxonomy/term/' . $term->tid . '/edit', $edit, t('Delete'));
211 $this->drupalPost(NULL, NULL, t('Delete')); // Confirm deletion.
212 $options = array('%term' => $edit['name'], '%menu_name' => $menu_name);
213 $this->assertRaw(t('Deleted term %term from taxonomy menu %menu_name.', $options));
214 }
215
216 }
217
218 /**
219 * Tests for taxonomy vocabulary functions.
220 */
221 class TaxonomyMenuUnitTest extends TaxonomyMenuWebTestCase {
222
223 public static function getInfo() {
224 return array(
225 'name' => 'CRUD functions',
226 'description' => 'Test CRUD functions',
227 'group' => 'Taxonomy menu',
228 );
229 }
230
231 function setUp() {
232 parent::setUp('taxonomy_menu');
233 }
234
235 /**
236 * Tests CRUD functions.
237 */
238 function testTaxonomyMenuCRUD() {
239 $menu_name = 'main-menu';
240 $vocabulary = $this->vocabulary;
241 $terms = $this->hierarchy;
242 $hierarchy_term = $this->hierarchy[3]; // Arbitrary term for hierarchy.
243 $vid = $this->vocabulary->vid;
244
245 // Ensure that the taxonomy vocabulary form is successfully submitted.
246 $edit['taxonomy_menu[vocab_parent]'] = $menu_name . ':0';
247 $vocab = $this->vocabulary->machine_name;
248 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
249 $this->assertResponse(200);
250
251 // Ensure that the same number of menu links are created from the taxonomy
252 // terms of the vocabulary.
253 $this->assertEqualNumberTermsMenuLinks(count($terms), $vocabulary, $menu_name);
254
255 // Ensure that the menu link is updated when the taxonomy term is updated.
256 $new_name = $this->randomName();
257 $hierarchy_term->name = $new_name;
258 taxonomy_term_save($hierarchy_term);
259 $this->drupalGet('admin/structure/menu/manage/' . $menu_name);
260 $this->assertLink($new_name);
261
262 // Ensure that the menu link is deleted when the taxonomy term is deleted.
263 // Hierarchy term [3] has 1 children, if we delete it, 2 taxonomy terms
264 // should be deleted.
265 $orig_mlid = _taxonomy_menu_get_mlid($hierarchy_term->tid, $vid);
266 taxonomy_term_delete($hierarchy_term->tid);
267 $this->assertEqualNumberTermsMenuLinks(count($terms) - 2, $vocabulary, $menu_name);
268
269 $menu_link = menu_link_load($orig_mlid);
270 $message = 'The menu link ' . $orig_mlid . ' associated to the term ' . $hierarchy_term->tid . ' could not be found.';
271 $this->assertFalse($menu_link, $message);
272
273 $mlid = _taxonomy_menu_get_mlid($hierarchy_term->tid, $vid);
274 $message = 'The ( mlid = ' . $orig_mlid . ' / tid = ' . $hierarchy_term->tid . ') association could not be found in {taxonomy_menu} table.';
275 $this->assertFalse($mlid, $message);
276
277 // Ensure that all menu links and all associations in {taxonomy_menu} table
278 // are deleted when a vocabulary is deleted.
279 $mlids = _taxonomy_menu_get_menu_items($vid);
280 taxonomy_vocabulary_delete($vid);
281 $this->assertEqualNumberTermsMenuLinks(0, $vocabulary, $menu_name);
282 }
283
284 /**
285 * Tests the hierarchy of menu links in a menu.
286 */
287 function testTaxonomyMenuTermsHierarchy() {
288 $vocab_machine_name = $this->vocabulary->machine_name;
289 $vid = $this->vocabulary->vid;
290 $edit = array();
291
292 // Settings
293 $edit['taxonomy_menu[vocab_parent]'] = 'main-menu:0';
294 $this->drupalPost('admin/structure/taxonomy/' . $vocab_machine_name . '/edit', $edit, t('Save'));
295 $this->assertResponse(200);
296
297 // Given a taxonomy term, which id is tid:
298 // - ptid --> - plid
299 // - tid --> - mlid
300 // Do the following verification for each term in the hierarchy; the ending
301 // plid determined by the methods below should be equal.
302 // - method1: tid --> mlid --> plid
303 // - method2: tid --> ptid --> plid
304 foreach ($this->hierarchy as $term) {
305 // 1. Get plid by getting the associated mlid for the term tid.
306 $mlid = _taxonomy_menu_get_mlid($term->tid, $vid);
307 if ($mlid) {
308 $menu_link = menu_link_load($mlid);
309 $plid_from_mlid = $menu_link['plid'];
310 // 2. Get plid by getting the associated mlid for the parent term ptid.
311 // We don't handle multiple parents, break after first one.
312 $parents = taxonomy_get_parents($term->tid);
313 if (!empty($parents)) {
314 foreach ($parents as $ptid => $parent) {
315 $plid_from_ptid = _taxonomy_menu_get_mlid($ptid, $vid);
316 // Assert that both plid found by the two different methods are equal.
317 $message = 'Parent mlids from taxonomy term ' . $term->tid . ' are a match.';
318 $this->assertEqual($plid_from_mlid, $plid_from_ptid, $message);
319 break;
320 }
321 }
322 else {
323 // Current term has no parent term. This means that the name of the
324 // vocabulary should be associated to the 'navigation' root.
325 // Menu link of the current term as defined by taxonomy menu table.
326 $this->assertEqual($menu_link['plid'], 0);
327 }
328 }
329 else {
330 $this->fail("mlid for taxonomy term " . $term->tid . " could not be found.");
331 }
332 }
333 }
334
335 /**
336 * Tests creation of menu links in a custom menu.
337 */
338 function testTaxonomyMenuCustomMenu() {
339 $vocabulary = $this->vocabulary;
340 $terms = $this->hierarchy;
341 $custom_menu_name = $this->randomName(16);
342 $menu_machine_name = drupal_substr(hash('sha256', $custom_menu_name), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
343
344 // Submit the menu creation form.
345 $menu_edit = array();
346 $menu_edit['title'] = $custom_menu_name;
347 $menu_edit['menu_name'] = $menu_machine_name;
348 $this->drupalPost('admin/structure/menu/add', $menu_edit, 'Save');
349 $this->assertResponse(200);
350
351 // Submit the vocabulary edit form.
352 $vocab_edit = array();
353 $vocab_edit['taxonomy_menu[vocab_parent]'] = 'menu-' . $menu_machine_name . ':0';
354 $this->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/edit', $vocab_edit, 'Save');
355 $this->assertResponse(200);
356
357 // Check that the menu links were created in the custom menu.
358 $this->assertEqualNumberTermsMenuLinks(count($terms), $vocabulary, 'menu-' . $menu_machine_name);
359 }
360
361 }
362
363 /**
364 * Tests Taxonomy menu configuration options.
365 */
366 class TaxonomyMenuConfigurationTest extends TaxonomyMenuWebTestCase {
367
368 public static function getInfo() {
369 return array(
370 'name' => 'Configuration options',
371 'description' => 'Test configuration options.',
372 'group' => 'Taxonomy menu',
373 );
374 }
375
376 function setUp() {
377 parent::setUp('taxonomy_menu');
378 }
379
380 /**
381 * Tests Taxonommy Menu sync option.
382 */
383 function testTaxonomyMenuSyncOption() {
384 $vocab = $this->vocabulary->machine_name;
385
386 // Set settings (no sync on main-menu).
387 $edit = array(
388 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
389 'taxonomy_menu[sync]' => FALSE,
390 );
391 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
392 $this->assertResponse(200);
393
394 // Prepare changes.
395 $new_name = $this->randomName(12);
396 $test_term = $this->hierarchy[3]; // Arbitrary term from hierarchy
397 $test_term->name = $new_name;
398
399 // Save new term's name with sync option off.
400 taxonomy_term_save($test_term);
401 $mlid = _taxonomy_menu_get_mlid($test_term->tid, $this->vocabulary->vid);
402 $menu_link = menu_link_load($mlid);
403 $this->assertNotEqual($new_name, $menu_link['link_title']);
404
405 // Switch to sync option on and save.
406 $edit['taxonomy_menu[sync]'] = TRUE;
407 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
408 $this->assertResponse(200);
409 $mlid = _taxonomy_menu_get_mlid($test_term->tid, $this->vocabulary->vid);
410 $menu_link = menu_link_load($mlid);
411 $this->assertEqual($new_name, $menu_link['link_title']);
412 }
413
414 /**
415 * Tests Taxonommy Menu expand option.
416 */
417 function testTaxonomyMenuExpandedOption() {
418 $vocab = $this->vocabulary->machine_name;
419
420 // Set settings on expanded.
421 $edit = array(
422 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
423 'taxonomy_menu[options][expanded]' => TRUE,
424 );
425 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
426
427 // Build the base query.
428 $base_query = db_select('menu_links', 'ml');
429 $base_query->join('taxonomy_menu', 'tm', 'ml.mlid = tm.mlid');
430 $base_query->fields('ml')
431 ->condition('tm.vid', $this->vocabulary->vid)
432 ->condition('ml.module', 'taxonomy_menu');
433
434 // Assert that menu links are expanded.
435 $query = $base_query->condition('ml.expanded', TRUE);
436 $row_count = $query->execute()->rowCount();
437 $this->assertEqual(count($this->hierarchy), $row_count);
438
439 // Set settings on not expanded.
440 $edit = array(
441 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
442 'taxonomy_menu[options][expanded]' => FALSE,
443 );
444 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
445
446 // Assert that menu links are not expanded anymore.
447 $query = $base_query->condition('ml.expanded', FALSE);
448 $row_count = $query->execute()->rowCount();
449 $this->assertEqual(0, $row_count);
450 }
451
452 /**
453 * Tests Taxonommy Menu "Term description" option.
454 */
455 function testTaxonomyMenuTermDescriptionOption() {
456 $vocab = $this->vocabulary->machine_name;
457
458 // Set settings on expanded.
459 $edit = array(
460 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
461 'taxonomy_menu[options][term_item_description]' => FALSE,
462 'taxonomy_menu[options][display_title_attr]' => TRUE,
463 );
464 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
465
466 // Assert that menu links does not have the term description.
467 $term = $this->hierarchy[3];
468 $mlid = _taxonomy_menu_get_mlid($term->tid, $this->vocabulary->vid);
469 $menu_link = menu_link_load($mlid);
470 $menu_link_title = $menu_link['options']['attributes']['title'];
471 $this->assertEqual($menu_link_title, '');
472
473 // Assert that menu links does have the term description, when the option is
474 // checked.
475 $edit = array(
476 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
477 'taxonomy_menu[options][term_item_description]' => TRUE,
478 'taxonomy_menu[options][display_title_attr]' => TRUE,
479 );
480 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
481 $menu_link = menu_link_load($mlid);
482 $menu_link_title = $menu_link['options']['attributes']['title'];
483 $this->assertEqual($menu_link_title, $term->description);
484
485 // Assert that menu links does not have the term description, when the option
486 // for displaying a description is on but the display title option is off.
487 $edit = array(
488 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
489 'taxonomy_menu[options][term_item_description]' => TRUE,
490 'taxonomy_menu[options][display_title_attr]' => FALSE,
491 );
492 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
493 $menu_link = menu_link_load($mlid);
494 $menu_link_title = $menu_link['options']['attributes']['title'];
495 $this->assertEqual($menu_link_title, '');
496 }
497
498 /**
499 * Tests Taxonommy Menu "Flatten" option.
500 */
501 function testTaxonomyMenuFlattenOption() {
502 $vocab = $this->vocabulary->machine_name;
503
504 // Set settings.
505 $edit = array(
506 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
507 'taxonomy_menu[options][flat]' => TRUE,
508 );
509 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
510
511 // Assert that all of the menu links have no children with the root being
512 // the menu.
513 $query = db_select('menu_links', 'ml');
514 $query->join('taxonomy_menu', 'tm', 'ml.mlid = tm.mlid');
515 $query->fields('ml');
516 $query
517 ->condition('tm.vid', $this->vocabulary->vid)
518 ->condition('ml.menu_name', 'main-menu')
519 ->condition('ml.module', 'taxonomy_menu')
520 ->condition('ml.has_children', 0)
521 ->condition('ml.plid', 0);
522 $row_count = $query->execute()->rowCount();
523 $this->assertEqual(count($this->hierarchy), $row_count);
524
525 // Assert that all of the menu links have no children with the root being
526 // a menu item.
527 $item = array(
528 'menu_name' => 'main-menu',
529 'weight' => 0,
530 'link_title' => 'test',
531 'link_path' => '<front>',
532 'module' => 'taxonomy_menu',
533 );
534 $mlid = menu_link_save($item);
535 menu_cache_clear('main-menu');
536
537 $edit = array(
538 'taxonomy_menu[vocab_parent]' => 'main-menu:' . $mlid,
539 'taxonomy_menu[options][flat]' => TRUE,
540 );
541 $this->drupalPost('admin/structure/taxonomy/' . $vocab . '/edit', $edit, t('Save'));
542
543 $query = db_select('menu_links', 'ml');
544 $query->join('taxonomy_menu', 'tm', 'ml.mlid = tm.mlid');
545 $query->fields('ml');
546 $query
547 ->condition('tm.vid', $this->vocabulary->vid)
548 ->condition('ml.menu_name', 'main-menu')
549 ->condition('ml.module', 'taxonomy_menu')
550 ->condition('ml.has_children', 0)
551 ->condition('ml.plid', $mlid);
552 $row_count = $query->execute()->rowCount();
553 $this->assertEqual(count($this->hierarchy), $row_count);
554 }
555
556 /**
557 * Tests Taxonommy Menu "Hide Empty terms" option.
558 */
559 function testTaxonomyMenuHideEmptyTerms() {
560 $voc_machine_name = $this->vocabulary->machine_name;
561
562 // Create several nodes and attach them to different terms of our hierarchy
563 // in order to match the following scheme.
564 /** terms[1] | depth: 0 | 0 node -> displayed
565 * -- terms[2] | depth: 1 | 0 node -> hidden
566 * -- terms[3] | depth: 1 | 2 nodes -> displayed
567 * ---- terms[4] | depth: 2 | 0 node -> hidden
568 * -- terms[5] | depth: 1 | 1 node -> displayed
569 * terms[6] | depth: 0 | 0 node -> hidden
570 * -- terms[7] | depth: 1 | 0 node -> hidden */
571
572 // Add a taxonomy reference to the "Article" content type.
573 $field = array(
574 'field_name' => 'taxonomy_' . $voc_machine_name,
575 'type' => 'taxonomy_term_reference',
576 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
577 'settings' => array(
578 'allowed_values' => array(
579 array(
580 'vocabulary' => $voc_machine_name,
581 'parent' => 0,
582 ),
583 ),
584 ),
585 );
586 field_create_field($field);
587 $this->instance = array(
588 'field_name' => 'taxonomy_' . $voc_machine_name,
589 'bundle' => 'article',
590 'entity_type' => 'node',
591 'widget' => array(
592 'type' => 'options_select',
593 ),
594 'display' => array(
595 'default' => array(
596 'type' => 'taxonomy_term_reference_link',
597 ),
598 ),
599 );
600 field_create_instance($this->instance);
601
602 // Create 3 articles respecting the previous scheme.
603 $terms_index = array(3, 3, 5);
604 foreach ($terms_index as $index) {
605 $edit = array();
606 $langcode = LANGUAGE_NONE;
607 $edit["title"] = $this->randomName();
608 $edit["body[$langcode][0][value]"] = $this->randomName();
609 $edit[$this->instance['field_name'] . '[' . $langcode . '][]'] = $this->hierarchy[$index]->tid;
610 $this->drupalPost('node/add/article', $edit, t('Save'));
611 }
612
613 // Set settings and save.
614 $edit = array(
615 'taxonomy_menu[vocab_parent]' => 'main-menu:0',
616 'taxonomy_menu[options][hide_empty_terms]' => TRUE,
617 );
618 $this->drupalPost('admin/structure/taxonomy/' . $voc_machine_name . '/edit', $edit, t('Save'));
619
620 // Assert that the hidden property of the taxonomy menu's menu links are
621 // set according to the scheme.
622 $visible_terms_index = array(1, 3, 5);
623 $hidden_item = array('hidden' => TRUE);
624 $visible_item = array('hidden' => FALSE);
625 $index = 1;
626 foreach ($this->hierarchy as $term) {
627 $mlid = _taxonomy_menu_get_mlid($term->tid, $this->vocabulary->vid);
628 if ($mlid) {
629 if (in_array($index, $visible_terms_index)) {
630 $this->assertMenuLink($mlid, $visible_item);
631 }
632 else {
633 $this->assertMenuLink($mlid, $hidden_item);
634 }
635 }
636 else {
637 $this->fail('No mlid could be found for the term ' . $term->tid);
638 }
639 $index++;
640 }
641 }
642 }