2008-09-02 19:23:02 +00:00
|
|
|
<?php
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Provides SimpleTests for menu.inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class MenuIncTestCase extends DrupalWebTestCase {
|
2009-03-31 01:49:55 +00:00
|
|
|
public static function getInfo() {
|
2008-09-02 19:23:02 +00:00
|
|
|
return array(
|
2009-07-13 21:51:42 +00:00
|
|
|
'name' => 'Hook menu tests',
|
|
|
|
'description' => 'Test menu hook functionality.',
|
|
|
|
'group' => 'Menu',
|
2008-09-02 19:23:02 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
// Enable dummy module that implements hook_menu.
|
2008-12-28 18:27:14 +00:00
|
|
|
parent::setUp('menu_test');
|
2008-09-02 19:23:02 +00:00
|
|
|
}
|
2008-09-15 20:48:10 +00:00
|
|
|
|
2009-02-05 01:05:17 +00:00
|
|
|
/**
|
|
|
|
* Test title callback set to FALSE.
|
|
|
|
*/
|
|
|
|
function testTitleCallbackFalse() {
|
|
|
|
$this->drupalGet('node');
|
|
|
|
$this->assertText('A title with @placeholder', t('Raw text found on the page'));
|
|
|
|
$this->assertNoText(t('A title with @placeholder', array('@placeholder' => 'some other text')), t('Text with placeholder substitutions not found.'));
|
|
|
|
}
|
|
|
|
|
2009-04-13 12:18:52 +00:00
|
|
|
/**
|
|
|
|
* Tests for menu_link_maintain().
|
|
|
|
*/
|
|
|
|
function testMenuLinkMaintain() {
|
|
|
|
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
|
|
|
$this->drupalLogin($admin_user);
|
|
|
|
|
|
|
|
// Create three menu items.
|
|
|
|
menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/1', 'Menu link #1');
|
|
|
|
menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/1', 'Menu link #1-1');
|
|
|
|
menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/2', 'Menu link #2');
|
|
|
|
|
|
|
|
// Move second link to the main-menu, to test caching later on.
|
|
|
|
db_update('menu_links')
|
|
|
|
->fields(array('menu_name' => 'main-menu'))
|
|
|
|
->condition('link_title', 'Menu link #1-1')
|
|
|
|
->condition('customized', 0)
|
|
|
|
->condition('module', 'menu_test')
|
|
|
|
->execute();
|
|
|
|
menu_cache_clear('main-menu');
|
|
|
|
|
|
|
|
// Load front page.
|
|
|
|
$this->drupalGet('node');
|
|
|
|
$this->assertLink(t('Menu link #1'), 0, 'Found menu link #1');
|
|
|
|
$this->assertLink(t('Menu link #1-1'), 0, 'Found menu link #1-1');
|
|
|
|
$this->assertLink(t('Menu link #2'), 0, 'Found menu link #2');
|
|
|
|
|
|
|
|
// Rename all links for the given path.
|
|
|
|
menu_link_maintain('menu_test', 'update', 'menu_test_maintain/1', 'Menu link updated');
|
|
|
|
// Load a different page to be sure that we have up to date information.
|
|
|
|
$this->drupalGet('menu_test_maintain/1');
|
|
|
|
$this->assertLink(t('Menu link updated'), 0, t('Found updated menu link'));
|
|
|
|
$this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1'));
|
|
|
|
$this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1-1'));
|
|
|
|
$this->assertLink(t('Menu link #2'), 0, t('Found menu link #2'));
|
|
|
|
|
|
|
|
// Delete all links for the given path.
|
|
|
|
menu_link_maintain('menu_test', 'delete', 'menu_test_maintain/1', '');
|
|
|
|
// Load a different page to be sure that we have up to date information.
|
|
|
|
$this->drupalGet('menu_test_maintain/2');
|
|
|
|
$this->assertNoLink(t('Menu link updated'), 0, t('Not found deleted menu link'));
|
|
|
|
$this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1'));
|
|
|
|
$this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1-1'));
|
|
|
|
$this->assertLink(t('Menu link #2'), 0, t('Found menu link #2'));
|
|
|
|
}
|
|
|
|
|
2009-09-09 21:33:00 +00:00
|
|
|
/**
|
|
|
|
* Test menu_get_names().
|
|
|
|
*/
|
|
|
|
function testMenuGetNames() {
|
|
|
|
$this->assertEqual(menu_get_names(), array('admin_shortcuts', 'main-menu', 'management', 'navigation', 'original', 'user-menu'), t('Expected menu names were returned.'));
|
|
|
|
}
|
|
|
|
|
2008-09-02 19:23:02 +00:00
|
|
|
/**
|
|
|
|
* Tests for menu_name parameter for hook_menu().
|
|
|
|
*/
|
|
|
|
function testMenuName() {
|
|
|
|
$admin_user = $this->drupalCreateUser(array('administer site configuration'));
|
|
|
|
$this->drupalLogin($admin_user);
|
|
|
|
|
|
|
|
$sql = "SELECT menu_name FROM {menu_links} WHERE router_path = 'menu_name_test'";
|
2009-05-30 11:17:32 +00:00
|
|
|
$name = db_query($sql)->fetchField();
|
2008-09-02 19:23:02 +00:00
|
|
|
$this->assertEqual($name, 'original', t('Menu name is "original".'));
|
|
|
|
|
2009-07-14 20:53:16 +00:00
|
|
|
// Change the menu_name parameter in menu_test.module, then force a menu
|
|
|
|
// rebuild.
|
|
|
|
menu_test_menu_name('changed');
|
|
|
|
menu_rebuild();
|
2008-09-02 19:23:02 +00:00
|
|
|
|
|
|
|
$sql = "SELECT menu_name FROM {menu_links} WHERE router_path = 'menu_name_test'";
|
2009-05-30 11:17:32 +00:00
|
|
|
$name = db_query($sql)->fetchField();
|
2008-09-02 19:23:02 +00:00
|
|
|
$this->assertEqual($name, 'changed', t('Menu name was successfully changed after rebuild.'));
|
|
|
|
}
|
2009-03-14 20:56:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for menu hiearchy.
|
|
|
|
*/
|
|
|
|
function testMenuHiearchy() {
|
2009-05-30 11:17:32 +00:00
|
|
|
$parent_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent'))->fetchAssoc();
|
|
|
|
$child_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent/child'))->fetchAssoc();
|
|
|
|
$unattached_child_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent/child2/child'))->fetchAssoc();
|
2009-03-14 20:56:06 +00:00
|
|
|
|
|
|
|
$this->assertEqual($child_link['plid'], $parent_link['mlid'], t('The parent of a directly attached child is correct.'));
|
|
|
|
$this->assertEqual($unattached_child_link['plid'], $parent_link['mlid'], t('The parent of a non-directly attached child is correct.'));
|
|
|
|
}
|
2009-05-17 07:49:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test menu_set_item().
|
|
|
|
*/
|
|
|
|
function testMenuSetItem() {
|
|
|
|
$item = menu_get_item('node');
|
|
|
|
|
|
|
|
$this->assertEqual($item['path'], 'node', t("Path from menu_get_item('node') is equal to 'node'"), 'menu');
|
|
|
|
|
|
|
|
// Modify the path for the item then save it.
|
|
|
|
$item['path'] = 'node_test';
|
|
|
|
$item['href'] = 'node_test';
|
|
|
|
|
|
|
|
menu_set_item('node', $item);
|
|
|
|
$compare_item = menu_get_item('node');
|
|
|
|
$this->assertEqual($compare_item, $item, t('Modified menu item is equal to newly retrieved menu item.'), 'menu');
|
|
|
|
}
|
|
|
|
|
2009-09-17 04:07:40 +00:00
|
|
|
/**
|
|
|
|
* Test menu maintainance hooks.
|
|
|
|
*/
|
|
|
|
function testMenuItemHooks() {
|
|
|
|
// Create an item.
|
|
|
|
menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/4', 'Menu link #4');
|
|
|
|
$this->assertEqual(menu_test_static_variable(), 'insert', t('hook_menu_link_insert() fired correctly'));
|
|
|
|
// Update the item.
|
|
|
|
menu_link_maintain('menu_test', 'update', 'menu_test_maintain/4', 'Menu link updated');
|
|
|
|
$this->assertEqual(menu_test_static_variable(), 'update', t('hook_menu_link_update() fired correctly'));
|
|
|
|
// Delete the item.
|
|
|
|
menu_link_maintain('menu_test', 'delete', 'menu_test_maintain/4', '');
|
|
|
|
$this->assertEqual(menu_test_static_variable(), 'delete', t('hook_menu_link_delete() fired correctly'));
|
|
|
|
}
|
|
|
|
|
2008-09-02 19:23:02 +00:00
|
|
|
}
|
2008-11-20 07:18:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests rebuilding the menu by setting 'menu_rebuild_needed.'
|
|
|
|
*/
|
|
|
|
class MenuRebuildTestCase extends DrupalWebTestCase {
|
2009-03-31 01:49:55 +00:00
|
|
|
public static function getInfo() {
|
2008-11-20 07:18:59 +00:00
|
|
|
return array(
|
2009-07-13 21:51:42 +00:00
|
|
|
'name' => 'Menu rebuild test',
|
|
|
|
'description' => 'Test rebuilding of menu.',
|
|
|
|
'group' => 'Menu',
|
2008-11-20 07:18:59 +00:00
|
|
|
);
|
|
|
|
}
|
2009-01-26 14:08:44 +00:00
|
|
|
|
2008-11-20 07:18:59 +00:00
|
|
|
/**
|
|
|
|
* Test if the 'menu_rebuild_needed' variable triggers a menu_rebuild() call.
|
|
|
|
*/
|
|
|
|
function testMenuRebuildByVariable() {
|
|
|
|
// Check if 'admin' path exists.
|
2009-05-30 11:17:32 +00:00
|
|
|
$admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
|
2008-11-20 07:18:59 +00:00
|
|
|
$this->assertEqual($admin_exists, 'admin', t("The path 'admin/' exists prior to deleting."));
|
|
|
|
|
|
|
|
// Delete the path item 'admin', and test that the path doesn't exist in the database.
|
|
|
|
$delete = db_delete('menu_router')
|
|
|
|
->condition('path', 'admin')
|
|
|
|
->execute();
|
2009-05-30 11:17:32 +00:00
|
|
|
$admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
|
2008-11-20 07:18:59 +00:00
|
|
|
$this->assertFalse($admin_exists, t("The path 'admin/' has been deleted and doesn't exist in the database."));
|
|
|
|
|
|
|
|
// Now we enable the rebuild variable and trigger menu_execute_active_handler()
|
|
|
|
// to rebuild the menu item. Now 'admin' should exist.
|
|
|
|
variable_set('menu_rebuild_needed', TRUE);
|
|
|
|
// menu_execute_active_handler() should trigger the rebuild.
|
|
|
|
$this->drupalGet('<front>');
|
2009-05-30 11:17:32 +00:00
|
|
|
$admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
|
2008-11-20 07:18:59 +00:00
|
|
|
$this->assertEqual($admin_exists, 'admin', t("The menu has been rebuilt, the path 'admin' now exists again."));
|
|
|
|
}
|
2009-01-26 14:08:44 +00:00
|
|
|
|
2008-11-20 07:18:59 +00:00
|
|
|
}
|
2009-09-11 01:28:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Menu tree data related tests.
|
|
|
|
*/
|
|
|
|
class MenuTreeDataTestCase extends DrupalUnitTestCase {
|
|
|
|
/**
|
|
|
|
* Dummy link structure acceptable for menu_tree_data().
|
|
|
|
*/
|
|
|
|
var $links = array(
|
|
|
|
1 => array('mlid' => 1, 'depth' => 1),
|
|
|
|
2 => array('mlid' => 2, 'depth' => 1),
|
|
|
|
3 => array('mlid' => 3, 'depth' => 2),
|
|
|
|
4 => array('mlid' => 4, 'depth' => 3),
|
|
|
|
5 => array('mlid' => 5, 'depth' => 1),
|
|
|
|
);
|
|
|
|
|
|
|
|
public static function getInfo() {
|
|
|
|
return array(
|
|
|
|
'name' => 'Menu tree generation',
|
|
|
|
'description' => 'Tests recursive menu tree generation functions.',
|
|
|
|
'group' => 'Menu',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the generation of a proper menu tree hierarchy.
|
|
|
|
*/
|
|
|
|
function testMenuTreeData() {
|
|
|
|
$tree = menu_tree_data($this->links);
|
|
|
|
|
|
|
|
// Validate that parent items #1, #2, and #5 exist on the root level.
|
|
|
|
$this->assertSameLink($this->links[1], $tree[1]['link'], t('Parent item #1 exists.'));
|
|
|
|
$this->assertSameLink($this->links[2], $tree[2]['link'], t('Parent item #2 exists.'));
|
|
|
|
$this->assertSameLink($this->links[5], $tree[5]['link'], t('Parent item #5 exists.'));
|
|
|
|
|
|
|
|
// Validate that child item #4 exists at the correct location in the hierarchy.
|
|
|
|
$this->assertSameLink($this->links[4], $tree[2]['below'][3]['below'][4]['link'], t('Child item #4 exists in the hierarchy.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that two menu links are the same by comparing the mlid.
|
|
|
|
*
|
|
|
|
* @param $link1
|
|
|
|
* A menu link item.
|
|
|
|
* @param $link2
|
|
|
|
* A menu link item.
|
|
|
|
* @param $message
|
|
|
|
* The message to display along with the assertion.
|
|
|
|
* @return
|
|
|
|
* TRUE if the assertion succeeded, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
protected function assertSameLink($link1, $link2, $message = '') {
|
|
|
|
return $this->assert($link1['mlid'] == $link2['mlid'], $message ? $message : t('First link is identical to second link'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|