1 menu.test | MenuNodeTestCase::testMenuNodeFormWidget() |
Test creating, editing, deleting menu links via node form widget.
File
- core/
modules/ menu/ tests/ menu.test, line 683 - Tests for menu.module.
Class
- MenuNodeTestCase
- Test menu settings for nodes.
Code
function testMenuNodeFormWidget() {
// Enable Main menu as available menu.
$edit = array(
'menu_options[main-menu]' => 1,
);
$this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
// Change default parent item to Main menu, so we can assert more easily.
$edit = array(
'menu_parent' => 'main-menu:0',
);
$this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
// Verify that the menu link title on the node add form has the correct
// maxlength.
$this->backdropGet('node/add/page');
$this->assertPattern('/<input .* id="edit-menu-link-title" .* maxlength="255" .* \/>/', 'Menu link title field has correct maxlength in node add form.');
// Create a node with menu link disabled.
$node_title = $this->randomName();
$language = LANGUAGE_NONE;
$edit = array(
"title" => $node_title,
"body[$language][0][value]" => $this->randomString(),
"menu[enabled]" => 0,
);
$this->backdropPost('node/add/page', $edit, t('Save'));
$node = $this->backdropGetNodeByTitle($node_title);
// Assert that there is no link for the node.
$this->backdropGet('user');
$this->assertNoLink($node_title);
// Edit the node, enable the menu link setting, but skip the link title.
$edit = array(
'menu[enabled]' => 1,
);
$this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
// Assert that there is no link for the node.
$this->backdropGet('user');
$this->assertNoLink($node_title);
// Edit the node and create a menu link.
$edit = array(
'menu[enabled]' => 1,
'menu[link_title]' => $node_title,
'menu[weight]' => 17,
);
$this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
// Assert that the link exists.
$this->backdropGet('user');
$this->assertLink($node_title);
$this->backdropGet('node/' . $node->nid . '/edit');
$this->assertOptionSelected('edit-menu-weight', 17, 'Menu weight correct in edit form');
// Disable Main menu as available menu to ensure the link stays when saving
// the node while menu options are not on the node form.
$edit = array(
'menu_options[main-menu]' => 0,
);
$this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
$this->backdropPost('node/' . $node->nid . '/edit', array(), t('Save'));
// Check that the link is still present.
$this->backdropGet('user');
$this->assertLink($node_title);
// Re-enable Main menu as available menu, then remove the link.
$edit = array(
'menu_options[main-menu]' => 1,
);
$this->backdropPost('admin/structure/types/manage/page', $edit, t('Save content type'));
// Verify that the menu link title on the node edit form has the correct
// maxlength.
$this->backdropGet('node/' . $node->nid . '/edit');
$this->assertPattern('/<input .* id="edit-menu-link-title" .* maxlength="255" .* \/>/', 'Menu link title field has correct maxlength in node edit form.');
// Edit the node and remove the menu link.
$edit = array(
'menu[enabled]' => FALSE,
);
$this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
// Assert that there is no link for the node.
$this->backdropGet('user');
$this->assertNoLink($node_title);
// Add a menu link to the Administration menu.
$item = array(
'link_path' => 'node/' . $node->nid,
'link_title' => $this->randomName(16),
'menu_name' => 'management',
);
menu_link_save($item);
// Assert that disabled Administration menu is not shown on the
// node/$nid/edit page.
$this->backdropGet('node/' . $node->nid . '/edit');
$this->assertText('Provide a menu link', 'Link in not-allowed menu not shown in node edit form.');
// Assert that the link is still in the Administration menu after save.
$this->backdropPost('node/' . $node->nid . '/edit', $edit, t('Save'));
$link = menu_link_load($item['mlid']);
$this->assertTrue($link, 'Link in not-allowed menu still exists after saving node.');
// Move the menu link back to the Main menu.
$item['menu_name'] = 'main-menu';
menu_link_save($item);
// Create a second node.
$child_node = $this->backdropCreateNode(array('type' => 'post'));
// Assign a menu link to the second node, being a child of the first one.
$child_item = array(
'link_path' => 'node/' . $child_node->nid,
'link_title' => $this->randomName(16),
'plid' => $item['mlid'],
);
menu_link_save($child_item);
// Edit the first node.
$this->backdropGet('node/' . $node->nid . '/edit');
// Assert that it is not possible to set the parent of the first node to
// itself...
$this->assertNoOption('edit-menu-parent', 'main-menu:' . $item['mlid']);
// ...nor to the second node
$this->assertNoOption('edit-menu-parent', 'main-menu:' . $child_item['mlid']);
// ...nor to the not-allowed "Management" menu.
$this->assertNoOption('edit-menu-parent', 'management:0');
}