You are here

public function MenuLinkWeightTest::testMenuFunctionality in Menu Link Weight 8

Same name and namespace in other branches
  1. 8.2 menu_link_weight.test \MenuLinkWeightTest::testMenuFunctionality()
  2. 7 menu_link_weight.test \MenuLinkWeightTest::testMenuFunctionality()

Test creating, editing, deleting menu links via node form widget.

File

./menu_link_weight.test, line 72
Web tests for Menu Link Weight.

Class

MenuLinkWeightTest
Test class for Menu Link Weight.

Code

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,
  ));
}