You are here

function MenuNodeAPIHookTestCase::testMenuNodeAPIHooks in Menu Node API 7

Run tests against the menu_node_test.module.

That module fires hook which increment a simple counter for each hook, providing that the data passed to the hook is well-formed. We simply check that the count is what we expect.

File

tests/menu_node.test, line 223
Simpletest for Menu Node API.

Class

MenuNodeAPIHookTestCase
Inherit methods from the parent class and test API module.

Code

function testMenuNodeAPIHooks() {

  // Create some nodes.
  $this
    ->menuNodeAPICreateNodes(3, 3);

  // Check that we have the expected record count.
  $count = db_query("SELECT COUNT(*) FROM {menu_node}")
    ->fetchField();
  $this
    ->assertTrue($count == 3, t('Found the correct row count in {menu_node}.'));

  // Check that our data is loaded onto the node.
  $query = db_select('node', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->range(0, 1);
  $query
    ->join('menu_node', 'mn', 'n.nid = mn.nid');
  $result = $query
    ->execute();
  foreach ($result as $data) {
    $node = node_load($data->nid);
  }
  $this
    ->assertTrue(isset($node->menu_node_links), t('menu_node_node_load() fired correctly'));

  // Check that hook_menu_node_insert() ran three times.
  $this
    ->assertTrue(menu_node_test_get() == 'insert2', t('hook_menu_node_insert() fired properly.'));

  // Check that the insert hook does not run on non-node menu items.
  menu_node_test_set('no-insert');
  $this
    ->menuNodeAPICreateNodes(1, 0);
  $this
    ->assertTrue(menu_node_test_get() == 'no-insert', t('hook_menu_node_insert() did not fire for a non-node link.'));

  // Check that we have the expected record count.
  $count = db_query("SELECT COUNT(*) FROM {menu_node}")
    ->fetchField();
  $this
    ->assertTrue($count == 3, t('Found the correct row count in {menu_node}.'));

  // Check that the delete hook runs once on node delete.
  $this
    ->menuNodeAPIDeleteNode(TRUE);
  $this
    ->assertTrue(menu_node_test_get() == 'delete', t('hook_menu_node_delete() fired properly on node deletion.'));

  // Check that we have the expected record count.
  $count = db_query("SELECT COUNT(*) FROM {menu_node}")
    ->fetchField();
  $this
    ->assertTrue($count == 2, t('Found the correct row count in {menu_node}.'));

  // Check that the delete hook runs once on node link delete.
  menu_node_test_set('no-insert');
  $this
    ->menuNodeAPIDeleteLink(TRUE);
  $this
    ->assertTrue(menu_node_test_get() == 'delete1', t('hook_menu_node_delete() fired properly on node link deletion.'));

  // Check that we have the expected record count.
  $count = db_query("SELECT COUNT(*) FROM {menu_node}")
    ->fetchField();
  $this
    ->assertTrue($count == 1, t('Found the correct row count in {menu_node}.'));
  menu_node_test_set('no-delete');

  // Check that the delete hook does not run on non-node menu items.
  $this
    ->menuNodeAPIDeleteLink(FALSE);
  $this
    ->assertTrue(menu_node_test_get() == 'no-delete', t('hook_menu_node_delete() did not fire for a non-node link.'));

  // Check that we have the expected record count.
  $count = db_query("SELECT COUNT(*) FROM {menu_node}")
    ->fetchField();
  $this
    ->assertTrue($count == 1, t('Found the correct row count in {menu_node}.'));

  // Update an existing node and check that the hooks fire.
  $query = db_select('node', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->range(0, 1);
  $query
    ->join('menu_node', 'mn', 'n.nid = mn.nid');
  $result = $query
    ->execute();
  foreach ($result as $data) {
    $node = node_load($data->nid, NULL, TRUE);
  }

  // Check that our data is loaded onto the node.
  $this
    ->assertTrue(isset($node->menu_node_links), t('menu_node_node_load() fired correctly'));

  // Update the title.
  $node->title = $this
    ->randomName(12);
  $menu = current(menu_node_get_links($node->nid));

  // Similate a node save, with menu item.
  $node->menu['enabled'] = TRUE;
  $node->menu['mlid'] = $menu->mlid;
  $node->menu['link_path'] = $menu->link_path;
  $node->menu['description'] = '';
  $node->menu['link_title'] = $node->title;
  node_save($node);

  // Check that the update ran once.
  $this
    ->assertTrue(menu_node_test_get() == 'update', t('hook_menu_node_update() fired properly on node save with menu link.'));

  // Simulate a node save, removing the menu item.
  $node->menu['enabled'] = FALSE;
  node_save($node);

  // Check that the delete hook ran once.
  $this
    ->assertTrue(menu_node_test_get() == 'delete2', t('hook_menu_node_delete() fired properly on node save with menu link removed.'));

  // Add two menu items to the same node.
  $node->menu['enabled'] = TRUE;
  $node->menu['mlid'] = $menu->mlid;
  $node->menu['link_path'] = $menu->link_path;
  $node->menu['description'] = '';
  $node->menu['link_title'] = $node->title;
  node_save($node);
  $item = array(
    'menu_name' => 'management',
    'link_path' => "node/{$node->nid}",
    'link_title' => $node->title,
    'description' => '',
    'enabled' => 1,
    'expanded' => 0,
    'parent' => 'management' . ':' . 0,
    'weight' => '0',
  );
  menu_link_save($item);

  // Verify that we have two items.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertTrue(count($node->menu_node_links) == 2, t('Saved a node with two separate menu links.'));

  // Update the node and check that the hook runs twice.
  node_save($node);
  $this
    ->assertTrue(menu_node_test_get() == 'update2', t('hook_menu_node_update() fired properly on node save with multiple menu links.'));

  // Delete the node and check that the hook runs twice.
  node_delete($node->nid);
  $this
    ->assertTrue(menu_node_test_get() == 'delete4', t('hook_menu_node_delete() fired properly on node delete with multiple menu links.'));
}