MenuNodeTest.php 6.08 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\menu_ui\Tests\MenuNodeTest.
6 7
 */

8
namespace Drupal\menu_ui\Tests;
9 10

use Drupal\simpletest\WebTestBase;
11
use Drupal\menu_link_content\Entity\MenuLinkContent;
12 13

/**
14 15 16
 * Add, edit, and delete a node with menu link.
 *
 * @group menu_ui
17 18
 */
class MenuNodeTest extends WebTestBase {
19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
25
  public static $modules = array('menu_ui', 'test_page_test', 'node');
26

27
  function setUp() {
28
    parent::setUp();
29
    $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

    $this->admin_user = $this->drupalCreateUser(array(
      'access administration pages',
      'administer content types',
      'administer menu',
      'create page content',
      'edit any page content',
      'delete any page content',
    ));
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Test creating, editing, deleting menu links via node form widget.
   */
  function testMenuNodeFormWidget() {
46
    // Disable the default main menu, so that no menus are enabled.
47
    $edit = array(
48
      'menu_options[main]' => FALSE,
49
    );
50
    $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
51 52 53 54 55

    // Verify that no menu settings are displayed and nodes can be created.
    $this->drupalGet('node/add/page');
    $this->assertText(t('Create Basic page'));
    $this->assertNoText(t('Menu settings'));
56
    $node_title = $this->randomMachineName();
57
    $edit = array(
58 59 60 61 62 63 64
      'title[0][value]' => $node_title,
      'body[0][value]' => $this->randomString(),
    );
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $node = $this->drupalGetNodeByTitle($node_title);
    $this->assertEqual($node->getTitle(), $edit['title[0][value]']);

65 66 67 68 69 70 71 72 73 74
    // Test that we cannot set a menu item from a menu that is not set as
    // available.
    $edit = array(
      'menu_options[tools]' => 1,
      'menu_parent' => 'main:',
    );
    $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
    $this->assertText(t('The selected menu item is not under one of the selected menus.'));
    $this->assertNoRaw(t('The content type %name has been updated.', array('%name' => 'Basic page')));

75 76 77 78
    // Enable Tools menu as available menu.
    $edit = array(
      'menu_options[main]' => 1,
      'menu_options[tools]' => 1,
79
      'menu_parent' => 'main:',
80
    );
81
    $this->drupalPostForm('admin/structure/types/manage/page', $edit, t('Save content type'));
82
    $this->assertRaw(t('The content type %name has been updated.', array('%name' => 'Basic page')));
83 84

    // Create a node.
85
    $node_title = $this->randomMachineName();
86
    $edit = array(
87
      'title[0][value]' => $node_title,
88
      'body[0][value]' => $this->randomString(),
89
    );
90
    $this->drupalPostForm('node/add/page', $edit, t('Save'));
91 92
    $node = $this->drupalGetNodeByTitle($node_title);
    // Assert that there is no link for the node.
93
    $this->drupalGet('test-page');
94 95 96 97 98 99
    $this->assertNoLink($node_title);

    // Edit the node, enable the menu link setting, but skip the link title.
    $edit = array(
      'menu[enabled]' => 1,
    );
100
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
101
    // Assert that there is no link for the node.
102
    $this->drupalGet('test-page');
103 104 105 106 107
    $this->assertNoLink($node_title);

    // Edit the node and create a menu link.
    $edit = array(
      'menu[enabled]' => 1,
108
      'menu[title]' => $node_title,
109 110
      'menu[weight]' => 17,
    );
111
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
112
    // Assert that the link exists.
113
    $this->drupalGet('test-page');
114 115
    $this->assertLink($node_title);

116
    $this->drupalGet('node/' . $node->id() . '/edit');
117
    $this->assertFieldById('edit-menu-weight', 17, 'Menu weight correct in edit form');
118 119 120 121 122

    // Edit the node and remove the menu link.
    $edit = array(
      'menu[enabled]' => FALSE,
    );
123
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
124
    // Assert that there is no link for the node.
125
    $this->drupalGet('test-page');
126 127
    $this->assertNoLink($node_title);

128
    // Add a menu link to the Administration menu.
129
    $item = entity_create('menu_link_content', array(
130
      'route_name' => 'entity.node.canonical',
131
      'route_parameters' => array('node' => $node->id()),
132
      'title' => $this->randomMachineName(16),
133
      'menu_name' => 'admin',
134
      'bundle' => 'menu_link_content',
135 136
    ));
    $item->save();
137

138 139
    // Assert that disabled Administration menu is not shown on the
    // node/$nid/edit page.
140
    $this->drupalGet('node/' . $node->id() . '/edit');
141
    $this->assertText('Provide a menu link', 'Link in not allowed menu not shown in node edit form');
142
    // Assert that the link is still in the Administration menu after save.
143
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
144
    $link = MenuLinkContent::load($item->id());
145
    $this->assertTrue($link, 'Link in not allowed menu still exists after saving node');
146

147
    // Move the menu link back to the Tools menu.
148 149
    $item->menu_name->value = 'tools';
    $item->save();
150 151 152
    // Create a second node.
    $child_node = $this->drupalCreateNode(array('type' => 'article'));
    // Assign a menu link to the second node, being a child of the first one.
153
    $child_item = entity_create('menu_link_content', array(
154
      'route_name' => 'entity.node.canonical',
155
      'route_parameters' => array('node' => $child_node->id()),
156
      'title' => $this->randomMachineName(16),
157 158 159
      'parent' => $item->getPluginId(),
      'menu_name' => $item->getMenuName(),
      'bundle' => 'menu_link_content',
160 161
    ));
    $child_item->save();
162
    // Edit the first node.
163
    $this->drupalGet('node/'. $node->id() .'/edit');
164
    // Assert that it is not possible to set the parent of the first node to itself or the second node.
165 166
    $this->assertNoOption('edit-menu-menu-parent', 'tools:'. $item->getPluginId());
    $this->assertNoOption('edit-menu-menu-parent', 'tools:'. $child_item->getPluginId());
167
    // Assert that unallowed Administration menu is not available in options.
168
    $this->assertNoOption('edit-menu-menu-parent', 'admin:');
169 170
  }
}