View source
<?php
namespace Drupal\taxonomy_menu\Tests;
class TaxonomyMenuConfigurationTest extends TaxonomyMenuWebTestCase {
public static function getInfo() {
return array(
'name' => 'Configuration options',
'description' => 'Test configuration options.',
'group' => 'Taxonomy menu',
);
}
function setUp() {
parent::setUp();
$admin_user = $this
->drupalCreateUser(array(
'administer taxonomy',
'administer menu',
'bypass node access',
));
$this
->drupalLogin($admin_user);
$this->vocabulary = $this
->createVocabulary();
$this->terms_hierarchy = $this
->createTermsHierarchy();
}
public function testTaxonomyMenuSyncOption() {
$vid = $this->vocabulary
->id();
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[sync]' => FALSE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$this
->assertResponse(200);
$new_name = $this
->randomName(12);
$test_term = $this->terms_hierarchy[3];
$test_term
->setName($new_name);
$test_term
->save();
$mlid = _taxonomy_menu_get_mlid($test_term
->id(), $this->vocabulary
->id());
$menu_link = menu_link_load($mlid);
$this
->assertNotEqual($new_name, $menu_link['link_title']);
$edit['taxonomy_menu[sync]'] = TRUE;
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$this
->assertResponse(200);
$mlid = _taxonomy_menu_get_mlid($test_term
->id(), $this->vocabulary
->id());
$menu_link = menu_link_load($mlid);
$this
->assertEqual($new_name, $menu_link['link_title']);
}
public function testTaxonomyMenuExpandedOption() {
$vid = $this->vocabulary
->id();
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[options_structure][expanded]' => TRUE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$base_query = db_select('menu_links', 'ml');
$base_query
->join('taxonomy_menu', 'tm', 'ml.mlid = tm.mlid');
$base_query
->fields('ml')
->condition('tm.vid', $this->vocabulary
->id())
->condition('ml.module', 'taxonomy_menu');
$query = $base_query
->condition('ml.expanded', TRUE);
$row_count = $query
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(count($this->terms_hierarchy), $row_count);
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[options_structure][expanded]' => FALSE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$query = $base_query
->condition('ml.expanded', FALSE);
$row_count = $query
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(0, $row_count);
}
public function testTaxonomyMenuTermDescriptionOption() {
$vid = $this->vocabulary
->id();
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[options_markup][term_item_description]' => FALSE,
'taxonomy_menu[options_markup][display_title_attr]' => TRUE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$term = $this->terms_hierarchy[3];
$mlid = _taxonomy_menu_get_mlid($term
->id(), $this->vocabulary
->id());
$menu_link = menu_link_load($mlid);
$menu_link_title = $menu_link['options']['attributes']['title'];
$this
->assertEqual($menu_link_title, '');
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[options_markup][term_item_description]' => TRUE,
'taxonomy_menu[options_markup][display_title_attr]' => TRUE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$menu_link = menu_link_load($mlid);
$menu_link_title = $menu_link['options']['attributes']['title'];
$this
->assertEqual($menu_link_title, trim($term
->getDescription()));
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[options_markup][term_item_description]' => TRUE,
'taxonomy_menu[options_markup][display_title_attr]' => FALSE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$menu_link = menu_link_load($mlid);
$menu_link_title = $menu_link['options']['attributes']['title'];
$this
->assertEqual($menu_link_title, '');
}
public function testTaxonomyMenuFlattenOption() {
$vid = $this->vocabulary
->id();
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[options_structure][flat]' => TRUE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$query = db_select('menu_links', 'ml');
$query
->join('taxonomy_menu', 'tm', 'ml.mlid = tm.mlid');
$query
->fields('ml');
$query
->condition('tm.vid', $this->vocabulary
->id())
->condition('ml.menu_name', 'main')
->condition('ml.module', 'taxonomy_menu')
->condition('ml.has_children', 0)
->condition('ml.plid', 0);
$row_count = $query
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(count($this->terms_hierarchy), $row_count);
$menu_link = entity_create('menu_link', array(
'menu_name' => 'main',
'weight' => 0,
'link_title' => 'test',
'link_path' => '<front>',
'module' => 'taxonomy_menu',
));
menu_link_save($menu_link);
$mlid = entity_load_by_uuid('menu_link', $menu_link->uuid)->mlid;
menu_cache_clear_all('main');
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:' . $mlid,
'taxonomy_menu[options_structure][flat]' => TRUE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$query = db_select('menu_links', 'ml');
$query
->join('taxonomy_menu', 'tm', 'ml.mlid = tm.mlid');
$query
->fields('ml');
$query
->condition('tm.vid', $this->vocabulary
->id())
->condition('ml.menu_name', 'main')
->condition('ml.module', 'taxonomy_menu')
->condition('ml.has_children', 0)
->condition('ml.plid', $mlid);
$row_count = $query
->countQuery()
->execute()
->fetchField();
$this
->assertEqual(count($this->terms_hierarchy), $row_count);
}
public function testTaxonomyMenuHideEmptyTerms() {
$vid = $this->vocabulary
->id();
$this
->setUpTermReferenceAndNodes('article', array(
3,
3,
5,
));
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[options_structure][hide_empty_terms]' => FALSE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
foreach ($this->terms_hierarchy as $term) {
$mlid = _taxonomy_menu_get_mlid($term
->id(), $vid);
if ($mlid) {
$this
->assertMenuLink($mlid, array(
'hidden' => FALSE,
));
}
else {
$this
->fail('No mlid could be found for the term ' . $term
->id());
}
}
$edit['taxonomy_menu[options_structure][hide_empty_terms]'] = TRUE;
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$visible_terms_index = array(
3,
5,
);
$index = 1;
foreach ($this->terms_hierarchy as $term) {
$mlid = _taxonomy_menu_get_mlid($term
->id(), $vid);
if ($mlid) {
if (in_array($index, $visible_terms_index)) {
$this
->assertMenuLink($mlid, array(
'hidden' => FALSE,
));
}
else {
$this
->assertMenuLink($mlid, array(
'hidden' => TRUE,
));
}
}
else {
$this
->fail('No mlid could be found for the term ' . $term->tid);
}
$index++;
}
}
public function testTaxonomyMenuCountNodes() {
$vid = $this->vocabulary
->id();
$this
->setUpTermReferenceAndNodes('article', array(
3,
3,
4,
4,
5,
6,
));
$edit = array(
'taxonomy_menu[vocab_parent]' => 'main:0',
'taxonomy_menu[options_structure][hide_empty_terms]' => FALSE,
'taxonomy_menu[options_markup][display_num]' => TRUE,
);
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$index = 1;
$positive_count_terms_index = array(
3,
4,
5,
6,
);
$visible_item = array(
'hidden' => FALSE,
);
foreach ($this->terms_hierarchy as $term) {
$mlid = _taxonomy_menu_get_mlid($term
->id(), $vid);
$menu_link = menu_link_load($mlid);
if ($mlid) {
switch ($index) {
case '3':
case '4':
$count = 2;
break;
case '5':
case '6':
$count = 1;
break;
default:
$count = 0;
break;
}
if (in_array($index, $positive_count_terms_index)) {
$this
->assertMenuLink($mlid, array(
'link_title' => $term
->getName() . ' (' . $count . ')',
));
}
else {
$this
->assertMenuLink($mlid, array(
'link_title' => $term
->getName(),
));
}
}
else {
$this
->fail('No mlid could be found for the term ' . $term
->id());
}
$index++;
}
}
}