View source
<?php
namespace Drupal\taxonomy_menu\Tests;
class TaxonomyMenuUnitTest extends TaxonomyMenuWebTestCase {
public static $modules = array(
'taxonomy_menu',
);
public static function getInfo() {
return array(
'name' => 'CRUD functions',
'description' => 'Test CRUD functions',
'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 testTaxonomyMenuCRUD() {
$menu_name = 'main';
$vocabulary = $this->vocabulary;
$terms = $this->terms_hierarchy;
$hierarchy_term = $this->terms_hierarchy[3];
$vid = $this->vocabulary
->id();
$edit['taxonomy_menu[vocab_parent]'] = $menu_name . ':0';
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$this
->assertResponse(200);
$this
->assertEqualNumberTermsMenuLinks(count($terms), $vocabulary, $menu_name);
$new_name = $this
->randomName();
$hierarchy_term->name = $new_name;
$hierarchy_term
->save();
$this
->drupalGet('admin/structure/menu/manage/' . $menu_name);
$this
->assertLink($new_name);
$orig_mlid = _taxonomy_menu_get_mlid($hierarchy_term
->id(), $vid);
entity_load('taxonomy_term', $hierarchy_term
->id())
->delete();
$this
->assertEqualNumberTermsMenuLinks(count($terms) - 2, $vocabulary, $menu_name);
$menu_link = menu_link_load($orig_mlid);
$message = 'The menu link ' . $orig_mlid . ' associated to the term ' . $hierarchy_term
->id() . ' could not be found.';
$this
->assertFalse($menu_link, $message);
$mlid = _taxonomy_menu_get_mlid($hierarchy_term
->id(), $vid);
$message = 'The ( mlid = ' . $orig_mlid . ' / tid = ' . $hierarchy_term
->id() . ') association could not be found in {taxonomy_menu} table.';
$this
->assertFalse($mlid, $message);
$mlids = _taxonomy_menu_get_menu_items($vid);
$vocabulary
->delete();
$this
->assertEqualNumberTermsMenuLinks(0, $vocabulary, $menu_name);
}
public function testTaxonomyMenuTermsHierarchy() {
$vid = $this->vocabulary
->id();
$edit = array();
$edit['taxonomy_menu[vocab_parent]'] = 'main:0';
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
$this
->assertResponse(200);
foreach ($this->terms_hierarchy as $term) {
$mlid = _taxonomy_menu_get_mlid($term
->id(), $vid);
if ($mlid) {
$menu_link = menu_link_load($mlid);
$plid_from_mlid = $menu_link['plid'];
$parents = taxonomy_term_load_parents($term
->id());
if (!empty($parents)) {
foreach ($parents as $ptid => $parent) {
$plid_from_ptid = _taxonomy_menu_get_mlid($ptid, $vid);
$message = 'Parent mlids from taxonomy term ' . $term
->id() . ' are a match.';
$this
->assertEqual($plid_from_mlid, $plid_from_ptid, $message);
break;
}
}
else {
$this
->assertEqual($menu_link['plid'], 0);
}
}
else {
$this
->fail("mlid for taxonomy term " . $term
->id() . " could not be found.");
}
}
}
public function testTaxonomyMenuCustomMenu() {
$vocabulary = $this->vocabulary;
$terms = $this->terms_hierarchy;
$custom_menu_name = $this
->randomName(16);
$menu_machine_name = substr(hash('sha256', $custom_menu_name), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
$menu_edit = array(
'id' => $menu_machine_name,
'description' => '',
'label' => $custom_menu_name,
);
$this
->drupalPostForm('admin/structure/menu/add', $menu_edit, t('Save'));
$this
->assertResponse(200);
$vocab_edit = array();
$vocab_edit['taxonomy_menu[vocab_parent]'] = $menu_machine_name . ':0';
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $vocabulary
->id(), $vocab_edit, 'Save');
$this
->assertResponse(200);
$this
->assertEqualNumberTermsMenuLinks(count($terms), $vocabulary, $menu_machine_name);
}
}