You are here

MultipleNodeMenuTestCase.php in Multiple Node Menu 8

File

tests/src/Functional/MultipleNodeMenuTestCase.php
View source
<?php

namespace Drupal\Tests\multiple_node_menu\Functional;

use Drupal\Tests\BrowserTestBase;

/**
 * Test adding, editing and deleting multiple menu links attached to nodes.
 *
 * @group multiple_node_menu
 */
class MultipleNodeMenuTestCase extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'menu_ui',
    'multiple_node_menu',
  ];

  /**
   * {@inheritdoc}
   */
  protected $profile = 'standard';

  /**
   * Admin user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * Get simpletest information.
   *
   * @return string[]
   *   Simpletest information.
   */
  public static function getInfo() {
    return [
      'name' => 'Multiple Node Menu',
      'description' => 'Test adding, editing and deleting multiple menu links attachd to nodes.',
      'group' => 'Multiple Node Menu',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    parent::setUp();
    $this->adminUser = $this
      ->drupalCreateUser([
      'access administration pages',
      'administer content types',
      'administer menu',
      'create page content',
      'edit any page content',
      'delete any page content',
    ]);
    $this
      ->drupalLogin($this->adminUser);
  }

  /**
   * Test creating, editing, deleting menu links via node form widget.
   */
  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);
  }

}

Classes

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