You are here

menu_module.test in SimpleTest 6

File

tests/menu_module.test
View source
<?php

class MenuModuleTestCase extends DrupalTestCase {

  /**
   * Implementation of get_info() for information
   */
  function get_info() {
    return array(
      'name' => t('Menu link creation/deletion'),
      'desc' => t('Create two links in the Navigation menu, check their data, and delete them using the menu module UI.'),
      'group' => 'Menu Module Tests',
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalModuleEnable('menu');
  }
  function testCreateCheckDelete() {
    $web_user = $this
      ->drupalCreateUserRolePerm(array(
      'access content',
      'administer menu',
      'access administration pages',
    ));
    $this
      ->drupalLoginUser($web_user);
    $mlid1 = $this
      ->uiCreateLink();
    $mlid2 = $this
      ->uiCreateLink($mlid1);
    $link1 = menu_link_load($mlid1);
    $this
      ->assertTrue((bool) $link1, '1st link created and loaded');
    $link2 = menu_link_load($mlid2);
    $this
      ->assertTrue((bool) $link2, '2nd link created as child and loaded');

    // Check the structure in the DB of the two links.
    // In general, if $n = $link['depth'] then $link['p'. $n] == $link['mlid'] and $link['p'. ($n - 1)] == $link['plid'] (unless depth == 0).
    // All $link['p'. $n] for $n > depth must be 0.
    // We know link1 is at the top level, so $link1['deptj'] == 1 and $link1['plid'] == 0.
    // We know that the parent of link2 is link1, so $link2['plid'] == $link1['mlid'].
    // Both links were created in the avigation menu.
    $this
      ->assertTrue($link1['p2'] == 0 && $link1['p1'] == $mlid1 && $link1['plid'] == 0 && $link1['depth'] == 1 && $link1['has_children'], '1st link has correct data');
    $this
      ->assertTrue($link2['menu_name'] == 'navigation' && $link2['p2'] == $mlid2 && $link2['p1'] == $mlid1 && $link2['plid'] == $mlid1 && $link2['depth'] == 2, '2nd link has correct data');
    $this
      ->uiDeleteLink($mlid1);
    $this
      ->assertFalse(menu_link_load($mlid1), '1st link deleted');
    $link2 = menu_link_load($mlid2);
    $this
      ->assertTrue($link2['plid'] == 0, '2nd link re-parented');
    $this
      ->uiDeleteLink($mlid2);
    $this
      ->assertFalse(menu_link_load($mlid2), '2nd link link deleted');
  }

  /**
   * Delete a menu link using the menu module UI.
   */
  function uiDeleteLink($mlid) {
    $this
      ->drupalPost("admin/build/menu/item/" . $mlid . "/delete", array(), "Confirm");
  }

  /**
   * Create a menu link using the menu module UI.
   */
  function uiCreateLink($plid = 0, $menu_name = 'navigation') {
    $this
      ->drupalGet("admin/build/menu-customize/{$menu_name}/add");
    $this
      ->assertResponse(200);
    $title = '!link_' . $this
      ->randomName(16);
    $edit = array(
      'menu[link_path]' => '<front>',
      'menu[link_title]' => $title,
      'menu[description]' => '',
      'menu[parent]' => $menu_name . ':' . $plid,
      'menu[weight]' => '0',
    );
    $this
      ->drupalPost("admin/build/menu-customize/" . $menu_name . "/add", $edit, "Save");
    $out = $this
      ->drupalGet("admin/build/menu-customize/{$menu_name}");
    $this
      ->assertText($title, 'Link created');
    $mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_title = '%s'", $title));
    return $mlid;
  }

}
class MenuModuleCustomMenuTest extends MenuModuleTestCase {

  /**
   * Implementation of get_info() for information
   */
  function get_info() {
    return array(
      'name' => t('Custom menu creation/deletion'),
      'desc' => t('Create a custom menu, add a link to it, and delete it using the menu module UI.'),
      'group' => 'Menu Module Tests',
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalModuleEnable('menu');
  }
  function tearDown() {
    parent::tearDown();
  }
  function testCreateCheckDelete() {
    $web_user = $this
      ->drupalCreateUserRolePerm(array(
      'access content',
      'administer menu',
      'access administration pages',
    ));
    $this
      ->drupalLoginUser($web_user);
    $this
      ->drupalGet('admin/build/menu/add');
    $name = substr(md5($this
      ->randomName(16)), 0, 20);
    $title = $this
      ->randomName(16);
    $edit = array(
      'menu_name' => $name,
      'description' => '',
      'title' => $title,
    );
    $this
      ->drupalPost("admin/build/menu/add", $edit, "Save");
    $name = 'menu-' . $name;
    $this
      ->drupalGet('admin/build/menu');
    $this
      ->assertText($title, 'Menu created');
    $mlid1 = $this
      ->uiCreateLink(0, $name);
    $link1 = menu_link_load($mlid1);
    $this
      ->assertTrue((bool) $link1, '1st link created and loaded');
    $this
      ->drupalPost("admin/build/menu-customize/" . $name . "/delete", array(), "Delete");
    $this
      ->assertFalse(menu_load($name), 'Custom menu deleted');
    $this
      ->assertFalse(menu_link_load($mlid1), '1st link deleted with menu');
  }

}
class MenuModuleEnable extends DrupalTestCase {

  /**
   * Implementation of get_info() for information
   */
  function get_info() {
    return array(
      'name' => t('Menu enable'),
      'desc' => 'Enable / disable a menu item',
      'group' => 'Menu Module Tests',
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalModuleEnable('menu');
  }
  function tearDown() {
    parent::tearDown();
  }
  function testMenuModuleEnable() {
    $web_user = $this
      ->drupalCreateUserRolePerm(array(
      'administer menu',
    ));
    $this
      ->drupalLoginUser($web_user);
    $this
      ->drupalGet('admin/build/menu-customize/navigation');
    $this
      ->clickLink('edit', 0);
    $url = $this
      ->getUrl();
    preg_match('/\\d+/', $url, $matches);
    $item = menu_link_load($matches[0]);
    $hidden = $item['hidden'];
    $edit['menu[enabled]'] = $hidden ? 1 : FALSE;
    $this
      ->assertTrue(TRUE, $hidden ? 'Disabled item found' : 'Enabled item found');
    $this
      ->drupalPost('admin/build/menu/item/' . $item['mlid'] . '/edit', $edit, 'Save');
    $item = menu_link_load($item['mlid']);
    $this
      ->assertTrue($item['hidden'] != $hidden, $item['hidden'] ? 'Item is now disabled' : 'Item is now enabled');
    $edit['menu[enabled]'] = $hidden ? FALSE : 1;
    $this
      ->drupalPost('admin/build/menu/item/' . $item['mlid'] . '/edit', $edit, 'Save');
    $item = menu_link_load($item['mlid']);
    $this
      ->assertTrue($item['hidden'] == $hidden, $item['hidden'] ? 'Item is disabled again' : 'Item is now enabled again');
  }

}
class MenuModuleReset extends DrupalTestCase {

  /**
   * Implementation of get_info() for information
   */
  function get_info() {
    return array(
      'name' => t('Menu reset'),
      'desc' => 'Edit and reset a menu item',
      'group' => 'Menu Module Tests',
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalModuleEnable('menu');
  }
  function tearDown() {
    parent::tearDown();
  }
  function testMenuModuleReset() {
    $web_user = $this
      ->drupalCreateUserRolePerm(array(
      'administer menu',
    ));
    $this
      ->drupalLoginUser($web_user);
    $form_state = array();
    $menu['menu_name'] = 'navigation';
    require_once drupal_get_path('module', 'menu') . '/menu.admin.inc';
    $form = drupal_retrieve_form('menu_overview_form', $form_state, $menu);
    $found = FALSE;
    foreach ($form as $mlid => $elements) {
      if (isset($elements['#item']) && $elements['#item']['module'] == 'system') {
        $found = TRUE;
        $mlid = substr($mlid, 5);
        break;
      }
    }
    $this
      ->assertTrue($found, 'System module item found');
    if ($found) {

      // We can't use menu API here because of localization issues.
      $item = db_fetch_array(db_query('SELECT * FROM {menu_links} WHERE mlid = %d', $mlid));
      $edit['menu[link_title]'] = $this
        ->randomName(16);
      $path = 'admin/build/menu/item/' . $mlid;
      $this
        ->drupalPost($path . '/edit', $edit, 'Save');
      $new_title = db_result(db_query('SELECT link_title FROM {menu_links} WHERE mlid = %d', $mlid));
      $this
        ->assertTrue($new_title == $edit['menu[link_title]'], 'Edit succesful');
      $this
        ->assertFalse($item['link_title'] == $new_title, 'Item changed');
      $reset_path = $path . '/reset';
      $this
        ->assertWantedRaw($reset_path, 'Reset link found');
      $this
        ->drupalPost($reset_path, array(), 'Reset');
      $reset_title = db_result(db_query('SELECT link_title FROM {menu_links} WHERE mlid = %d', $mlid));
      $this
        ->assertFalse($edit['menu[link_title]'] == $reset_title, 'Item reset');
      $this
        ->assertText(t('The menu item was reset to its default settings.'), 'Reset message');
      drupal_write_record('menu_links', $item, 'mlid');
      $restored_item = db_fetch_array(db_query('SELECT * FROM {menu_links} WHERE mlid = %d', $mlid));
      $this
        ->assertTrue($item == $restored_item, 'Item restored');
    }
  }

}
class MenuModuleInvalidPath extends DrupalTestCase {

  /**
   * Implementation of get_info() for information
   */
  function get_info() {
    return array(
      'name' => t('Menu invalid path'),
      'desc' => 'Try to create a menu item with an invalid / inaccesible path.',
      'group' => 'Menu Module Tests',
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalModuleEnable('menu');
  }
  function tearDown() {
    parent::tearDown();
  }
  function testMenuModuleInvalidPath() {
    $web_user = $this
      ->drupalCreateUserRolePerm(array(
      'administer menu',
    ));
    $this
      ->drupalLoginUser($web_user);
    foreach (array(
      '-&-',
      'admin/user/permissions',
    ) as $invalid_path) {
      $edit = array(
        'menu[link_path]' => $invalid_path,
        'menu[link_title]' => 'title',
      );
      $this
        ->drupalPost("admin/build/menu-customize/navigation/add", $edit, "Save");
      $this
        ->assertWantedRaw(t("The path '@path' is either invalid or you do not have access to it.", array(
        '@path' => $invalid_path,
      )), 'Invalid path failed');
    }
  }

}