You are here

menu_link_weight.test in Menu Link Weight 8.2

Same filename and directory in other branches
  1. 8 menu_link_weight.test
  2. 7 menu_link_weight.test

Web tests for Menu Link Weight.

File

menu_link_weight.test
View source
<?php

/**
 * @file
 * Web tests for Menu Link Weight.
 */

/**
 * Test class for Menu Link Weight.
 */
class MenuLinkWeightTest extends DrupalWebTestCase {

  /**
   * Module info.
   */
  public static function getInfo() {
    return array(
      'name' => 'Menu Link Weight',
      'description' => 'Web tests for Menu Link Weight',
      'group' => 'Menu Link Weight',
    );
  }

  /**
   * Set up.
   */
  protected function setUp() {
    parent::setUp('menu_link_weight');
    $permissions = array(
      'access administration pages',
      'administer content types',
      'administer blocks',
      'administer menu',
      'administer taxonomy',
      'create page content',
      'edit any page content',
      'delete any page content',
      'create article content',
    );

    // Create user.
    $this->user = $this
      ->drupalCreateUser($permissions);

    // Log in user.
    $this
      ->drupalLogin($this->user);

    // Create a new menu.
    $title = $this
      ->randomName(16);
    $menu_name = substr(hash('sha256', $this
      ->randomName(16)), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
    $edit = array(
      'menu_name' => $menu_name,
      'description' => '',
      'title' => $title,
    );

    // Drupal prepends the menu name with "menu-".
    $this->menu_name = 'menu-' . $menu_name;
    $this
      ->drupalPost('admin/structure/menu/add', $edit, t('Save'));
    $this
      ->drupalGet('admin/structure/menu');
    $this
      ->assertText($title, 'Menu created');

    // Enable the custom menu block.
    $edit = array();
    $edit['blocks[menu_' . $this->menu_name . '][region]'] = 'sidebar_first';
    $this
      ->drupalPost('admin/structure/block', $edit, t('Save blocks'));
    $this
      ->assertResponse(200);
    $this
      ->assertText(t('The block settings have been updated.'), 'Custom menu block was enabled');
  }

  /**
   * Test creating, editing, deleting menu links via node form widget.
   */
  public function testMenuFunctionality() {

    // Enable the Navigation menu as available menu.
    $edit = array(
      'menu_options[navigation]' => 1,
    );
    $this
      ->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));

    // Change default parent item to Navigation menu, so we can assert more
    // easily.
    $edit = array(
      'menu_parent' => 'navigation:0',
    );
    $this
      ->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));

    // Create a node.
    $node_title = $this
      ->randomName();
    $language = LANGUAGE_NONE;
    $edit = array(
      "title" => $node_title,
      "body[{$language}][0][value]" => $this
        ->randomString(),
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $node = $this
      ->drupalGetNodeByTitle($node_title);

    // Assert that there is no link for the node.
    $this
      ->drupalGet('');
    $this
      ->assertNoLink($node_title);

    // Edit the node, enable the menu link setting, but skip the link title.
    $edit = array(
      'menu[enabled]' => 1,
    );
    $this
      ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));

    // Assert that there is no link for the node.
    $this
      ->drupalGet('');
    $this
      ->assertNoLink($node_title);

    // Get the Menu link ID's for the sibling links of the to be created link.
    $links = menu_load_links('navigation');
    foreach ($links as $link) {
      if ($link['link_path'] == 'node/add') {
        $mlid_add_content = $link['mlid'];
      }
      elseif ($link['link_path'] == 'filter/tips') {
        $mlid_filter_tips = $link['mlid'];
      }
    }

    // Edit the node and create a menu link.
    $edit = array(
      'menu[enabled]' => 1,
      'menu[link_title]' => $node_title,
      'menu[menu_link_weight][' . $mlid_add_content . '][weight]' => -50,
      'menu[menu_link_weight][link_current][weight]' => -49,
      'menu[menu_link_weight][' . $mlid_filter_tips . '][weight]' => -48,
      // Test the hidden fields validation. This should pass:
      'menu[db_weights][' . $mlid_add_content . ']' => -49,
      'menu[db_weights][' . $mlid_filter_tips . ']' => -48,
    );
    $this
      ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
    $this
      ->assertNoText(t('The menu link weights have been changed by another user, please try again.'));

    // Assert that the link exists.
    $this
      ->drupalGet('');
    $this
      ->assertLink($node_title);
    $item = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(
      ':title' => $node_title,
    ))
      ->fetchAssoc();

    // Assert that the reordering was successful.
    $this
      ->assertMenuLink($mlid_add_content, array(
      'weight' => -50,
    ));
    $this
      ->assertMenuLink($item['mlid'], array(
      'weight' => -49,
    ));
    $this
      ->assertMenuLink($mlid_filter_tips, array(
      'weight' => -48,
    ));
    $this
      ->drupalGet('node/' . $node->nid . '/edit');
    $this
      ->assertText('(' . t('provided menu link') . ')');
    $this
      ->assertText('Change the weight of the links within the Navigation menu');
    $this
      ->assertOptionSelected('edit-menu-menu-link-weight-link-current-weight', -49, 'Menu weight correct in edit form');

    // Test AJAX functionality.
    $edit = array(
      'menu[enabled]' => TRUE,
      'menu[parent]' => 'navigation:' . $mlid_add_content,
    );
    $this
      ->drupalPostAJAX('node/' . $node->nid . '/edit', $edit, 'menu[parent]');
    $this
      ->assertText('Change the weight of the links within the Add content menu');

    // Test graceful degradation.
    $this
      ->drupalPost('node/' . $node->nid . '/edit', $edit, menu_link_weight_get_button_text());
    $this
      ->assertText('Change the weight of the links within the Add content menu');

    // Test "editing lock".
    $edit = array(
      'menu[enabled]' => 1,
      'menu[link_title]' => $this
        ->randomName(),
      // Test the hidden fields validation. This should not pass:
      'menu[db_weights][' . $mlid_add_content . ']' => -50,
      'menu[db_weights][' . $mlid_filter_tips . ']' => -49,
    );
    $this
      ->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
    $this
      ->assertText(t('The menu link weights have been changed by another user, please try again.'));

    // Test the "add new node" form.
    // Create a node.
    $node2_title = $this
      ->randomName();
    $language = LANGUAGE_NONE;
    $edit = array(
      "title" => $node2_title,
      "body[{$language}][0][value]" => $this
        ->randomString(),
      'menu[enabled]' => 1,
      'menu[link_title]' => $node2_title,
      'menu[menu_link_weight][' . $item['mlid'] . '][weight]' => -50,
      'menu[menu_link_weight][' . $mlid_filter_tips . '][weight]' => -49,
      'menu[menu_link_weight][link_current][weight]' => -48,
      'menu[menu_link_weight][' . $mlid_add_content . '][weight]' => -47,
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));

    // Assert that the link exists.
    $this
      ->drupalGet('');
    $this
      ->assertLink($node2_title);
    $item2 = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(
      ':title' => $node2_title,
    ))
      ->fetchAssoc();

    // Assert that the reordering was successful.
    $this
      ->assertMenuLink($item['mlid'], array(
      'weight' => -50,
    ));
    $this
      ->assertMenuLink($mlid_filter_tips, array(
      'weight' => -49,
    ));
    $this
      ->assertMenuLink($item2['mlid'], array(
      'weight' => -48,
    ));
    $this
      ->assertMenuLink($mlid_add_content, array(
      'weight' => -47,
    ));
    $node2 = $this
      ->drupalGetNodeByTitle($node2_title);
    $this
      ->drupalGet('node/' . $node2->nid . '/edit');
    $this
      ->assertText('(' . t('provided menu link') . ')');
    $this
      ->assertOptionSelected('edit-menu-menu-link-weight-link-current-weight', -48, 'Menu weight correct in edit form');

    // Assert that the item is placed on top of the list if no other options
    // are selected.
    $node3_title = $this
      ->randomName();
    $language = LANGUAGE_NONE;
    $edit = array(
      "title" => $node3_title,
      "body[{$language}][0][value]" => $this
        ->randomString(),
      'menu[enabled]' => 1,
      'menu[link_title]' => $node3_title,
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));

    // Assert that the link exists.
    $this
      ->drupalGet('');
    $this
      ->assertLink($node3_title);
    $item3 = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(
      ':title' => $node3_title,
    ))
      ->fetchAssoc();

    // Assert that the reordering was successful.
    $this
      ->assertMenuLink($item3['mlid'], array(
      'weight' => -50,
    ));
    $this
      ->assertMenuLink($item['mlid'], array(
      'weight' => -49,
    ));
    $this
      ->assertMenuLink($mlid_filter_tips, array(
      'weight' => -48,
    ));
    $this
      ->assertMenuLink($item2['mlid'], array(
      'weight' => -47,
    ));
    $this
      ->assertMenuLink($mlid_add_content, array(
      'weight' => -46,
    ));

    // Test the custom tree reordering functionality:
    module_enable(array(
      'menu_link_weight_test',
    ));

    // Insert the new link above item 2:
    variable_set('menu_link_weight_test_parent_value', 'navigation:0');
    variable_set('menu_link_weight_test_relative_position', 'above_' . $item2['mlid']);
    $node4_title = $this
      ->randomName();
    $language = LANGUAGE_NONE;
    $edit = array(
      "title" => $node4_title,
      "body[{$language}][0][value]" => $this
        ->randomString(),
      'menu[enabled]' => 1,
      'menu[link_title]' => $node4_title,
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));

    // Assert that the link exists.
    $this
      ->drupalGet('');
    $this
      ->assertLink($node4_title);
    $item4 = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(
      ':title' => $node4_title,
    ))
      ->fetchAssoc();

    // Assert that the reordering was successful.
    $this
      ->assertMenuLink($item3['mlid'], array(
      'weight' => -50,
    ));
    $this
      ->assertMenuLink($item['mlid'], array(
      'weight' => -49,
    ));
    $this
      ->assertMenuLink($mlid_filter_tips, array(
      'weight' => -48,
    ));
    $this
      ->assertMenuLink($item4['mlid'], array(
      'weight' => -47,
    ));
    $this
      ->assertMenuLink($item2['mlid'], array(
      'weight' => -46,
    ));
    $this
      ->assertMenuLink($mlid_add_content, array(
      'weight' => -45,
    ));
    variable_set('menu_link_weight_test_relative_position', 'below_' . $item2['mlid']);
    $node5_title = $this
      ->randomName();
    $language = LANGUAGE_NONE;
    $edit = array(
      "title" => $node5_title,
      "body[{$language}][0][value]" => $this
        ->randomString(),
      'menu[enabled]' => 1,
      'menu[link_title]' => $node5_title,
    );
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $item5 = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(
      ':title' => $node5_title,
    ))
      ->fetchAssoc();

    // Assert that the reordering was successful.
    $this
      ->assertMenuLink($item3['mlid'], array(
      'weight' => -50,
    ));
    $this
      ->assertMenuLink($item['mlid'], array(
      'weight' => -49,
    ));
    $this
      ->assertMenuLink($mlid_filter_tips, array(
      'weight' => -48,
    ));
    $this
      ->assertMenuLink($item4['mlid'], array(
      'weight' => -47,
    ));
    $this
      ->assertMenuLink($item2['mlid'], array(
      'weight' => -46,
    ));
    $this
      ->assertMenuLink($item5['mlid'], array(
      'weight' => -45,
    ));
    $this
      ->assertMenuLink($mlid_add_content, array(
      'weight' => -44,
    ));
  }

  /**
   * Fetch the menu item from the database and compare it to the given array.
   *
   * @param int $mlid
   *   Menu item id.
   * @param array $expected_item
   *   Array containing properties to verify.
   */
  protected function assertMenuLink($mlid, array $expected_item) {

    // Retrieve menu link.
    $item = db_query('SELECT * FROM {menu_links} WHERE mlid = :mlid', array(
      ':mlid' => $mlid,
    ))
      ->fetchAssoc();
    $options = unserialize($item['options']);
    if (!empty($options['query'])) {
      $item['link_path'] .= '?' . drupal_http_build_query($options['query']);
    }
    if (!empty($options['fragment'])) {
      $item['link_path'] .= '#' . $options['fragment'];
    }
    foreach ($expected_item as $key => $value) {
      $this
        ->assertEqual($item[$key], $value, format_string('Parameter %key had expected value.', array(
        '%key' => $key,
      )));
    }
  }

}

Classes

Namesort descending Description
MenuLinkWeightTest Test class for Menu Link Weight.