You are here

public function MultipleNodeMenuTestCase::testNodeFormWidget in Multiple Node Menu 8

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

File

tests/src/Functional/MultipleNodeMenuTestCase.php, line 64

Class

MultipleNodeMenuTestCase
Test adding, editing and deleting multiple menu links attached to nodes.

Namespace

Drupal\Tests\multiple_node_menu\Functional

Code

public function testNodeFormWidget() {

  // Enable Navigation menu as available menu.
  $this
    ->drupalGet('admin/structure/types/manage/page');
  $edit = [
    'menu_options[main]' => 1,
  ];
  $this
    ->submitForm($edit, 'Save content type');

  // Change default parent item to Navigation menu, so we can assert more
  // easily.
  $this
    ->drupalGet('admin/structure/types/manage/page');
  $edit = [
    'menu_parent' => 'main:',
  ];
  $this
    ->submitForm($edit, 'Save content type');

  // Create a node.
  $this
    ->drupalGet('node/add/page');
  $node_title = $this
    ->randomString();
  $edit = [
    'title' => $node_title,
    'body[0][value]' => $this
      ->randomString(),
    'multiple_node_menu[enabled]' => TRUE,
    'multiple_node_menu[add_link][link_title]' => $node_title,
    'multiple_node_menu[add_link][weight]' => 5,
  ];
  $this
    ->submitForm($edit, 'Save');
  $node = $this
    ->drupalGetNodeByTitle($node_title);

  // Assert that the link exists.
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->linkExists($node_title, 0, 'Menu link is present.');

  // Check if weight was set correctly.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Menu weight correct in edit form.
  $option = $this
    ->assertSession()
    ->optionExists('edit-multiple-node-menu-current-links-0-weight', 5);
  static::assertTrue($option
    ->isChecked());

  // Add another item via Ajax.
  $new_link_title = $this
    ->randomString();
  $edit = [
    'multiple_node_menu[add_link][link_title]' => $new_link_title,
    'multiple_node_menu[add_link][weight]' => -5,
  ];
  $this
    ->submitForm($edit, 'Add new menu link');
  $this
    ->submitForm([], 'Save');

  // Assert that the new link exists.
  $this
    ->drupalGet('');

  // Menu link is present.
  $this
    ->assertLink($new_link_title, 0);

  // Disable first menu item.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $edit = [
    'multiple_node_menu[current_links][0][enabled]' => FALSE,
  ];
  $this
    ->submitForm($edit, 'Save');

  // Assert that the first link has been hidden.
  $this
    ->drupalGet('');

  // Menu link was disabled.
  $this
    ->assertNoLink($node_title);

  // Edit the node and remove the menu link.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm([], 'Delete');
  $this
    ->submitForm([], 'Save');

  // Assert that there are no links to display for the node.
  $this
    ->drupalGet('');

  // No enabled menu links.
  $this
    ->assertNoLink($node_title);

  // No enabled menu links.
  $this
    ->assertNoLink($new_link_title);

  // Edit the node and re-enable the menu link.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $edit = [
    'multiple_node_menu[enabled]' => TRUE,
    'multiple_node_menu[current_links][0][enabled]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save');

  // Assert that the link exists.
  $this
    ->drupalGet('');

  // Menu link is present.
  $this
    ->assertLink($node_title, 0);

  // Test disabling menu links when the 'Provide menu link' checkbox is
  // unchecked.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $edit = [
    'multiple_node_menu[enabled]' => FALSE,
  ];
  $this
    ->submitForm($edit, 'Save');

  // Assert that there are no links to display for the node.
  $this
    ->drupalGet('');

  // No enabled menu links.
  $this
    ->assertNoLink($node_title);
}