Unverified Commit 51afa681 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2870452 by andypost, vaplas, claudiu.cristea, Ujin, Jo Fitzgerald,...

Issue #2870452 by andypost, vaplas, claudiu.cristea, Ujin, Jo Fitzgerald, michielnugter, Lendude: Convert web tests to browser tests for menu_ui module
parent 4022cc19
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -18,6 +18,11 @@ trait ContextualLinkClickTrait {
   *   If true then the button will be forced to visible so it can be clicked.
   */
  protected function clickContextualLink($selector, $link_locator, $force_visible = TRUE) {
    $page = $this->getSession()->getPage();
    $page->waitFor(10, function () use ($page, $selector) {
      return $page->find('css', "$selector .contextual-links");
    });

    if ($force_visible) {
      $this->toggleContextualTriggerVisibility($selector);
    }
+7 −0
Original line number Diff line number Diff line
@@ -2,10 +2,17 @@

namespace Drupal\menu_ui\Tests;

@trigger_error(__NAMESPACE__ . '\MenuWebTestBase is deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.0. Use the \Drupal\Tests\BrowserTestBase base class and the \Drupal\Tests\menu_ui\Traits\MenuUiTrait trait instead. See https://www.drupal.org/node/2917910.', E_USER_DEPRECATED);

use Drupal\simpletest\WebTestBase;

/**
 * Base class for menu web tests.
 *
 * @deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.x. Use
 *   \Drupal\Tests\menu_ui\Traits\MenuUiTrait methods, instead.
 *
 * @see https://www.drupal.org/node/2917910
 */
abstract class MenuWebTestBase extends WebTestBase {

+17 −9
Original line number Diff line number Diff line
<?php

namespace Drupal\menu_ui\Tests;
namespace Drupal\Tests\menu_ui\Functional;

use Drupal\Component\Utility\Unicode;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\menu_ui\Traits\MenuUiTrait;

/**
 * Tests for menu_ui language settings.
@@ -14,14 +16,20 @@
 *
 * @group menu_ui
 */
class MenuLanguageTest extends MenuWebTestBase {
class MenuUiLanguageTest extends BrowserTestBase {

  use MenuUiTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['language'];
  protected static $modules = [
    'language',
    'menu_link_content',
    'menu_ui',
  ];

  protected function setUp() {
    parent::setUp();
@@ -73,11 +81,11 @@ public function testMenuLanguage() {
    // Check the link was added with the correct menu link default language.
    $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]);
    $menu_link = reset($menu_links);
    $this->assertMenuLink($menu_link->getPluginId(), [
    $this->assertMenuLink([
      'menu_name' => $menu_name,
      'route_name' => '<front>',
      'langcode' => 'bb',
    ]);
    ], $menu_link->getPluginId());

    // Edit menu link default, changing it to cc.
    ContentLanguageSettings::loadByEntityTypeBundle('menu_link_content', 'menu_link_content')
@@ -95,22 +103,22 @@ public function testMenuLanguage() {
    // Check the link was added with the correct new menu link default language.
    $menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]);
    $menu_link = reset($menu_links);
    $this->assertMenuLink($menu_link->getPluginId(), [
    $this->assertMenuLink([
      'menu_name' => $menu_name,
      'route_name' => '<front>',
      'langcode' => 'cc',
    ]);
    ], $menu_link->getPluginId());

    // Now change the language of the new link to 'bb'.
    $edit = [
      'langcode[0][value]' => 'bb',
    ];
    $this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save'));
    $this->assertMenuLink($menu_link->getPluginId(), [
    $this->assertMenuLink([
      'menu_name' => $menu_name,
      'route_name' => '<front>',
      'langcode' => 'bb',
    ]);
    ], $menu_link->getPluginId());

    // Saving menu link items ends up on the edit menu page. To check the menu
    // link has the correct language default on edit, go to the menu link edit
+4 −4
Original line number Diff line number Diff line
<?php

namespace Drupal\menu_ui\Tests;
namespace Drupal\Tests\menu_ui\Functional;

use Drupal\simpletest\WebTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;

/**
 * Add, edit, and delete a node with menu link.
 *
 * @group menu_ui
 */
class MenuNodeTest extends WebTestBase {
class MenuUiNodeTest extends BrowserTestBase {

  /**
   * An editor user.
@@ -60,7 +60,7 @@ public function testMenuNodeFormWidget() {
    // item" options in menu_ui_form_node_type_form_alter(). The "log out" link
    // adds the "user.roles:authenticated" cache context.
    $this->drupalGet('admin/structure/types/manage/page');
    $this->assertCacheContext('user.roles:authenticated');
    $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.roles:authenticated');

    // Verify that the menu link title has the correct maxlength.
    $max_length = \Drupal::entityManager()->getBaseFieldDefinitions('menu_link_content')['title']->getSetting('max_length');
+59 −71
Original line number Diff line number Diff line
<?php

namespace Drupal\menu_ui\Tests;
namespace Drupal\Tests\menu_ui\Functional;

use Drupal\block\Entity\Block;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Menu\MenuLinkInterface;
use Drupal\Core\Url;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\system\Entity\Menu;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\menu_ui\Traits\MenuUiTrait;

/**
 * Add a custom menu, add menu links to the custom menu and Tools menu, check
@@ -18,14 +20,25 @@
 *
 * @group menu_ui
 */
class MenuTest extends MenuWebTestBase {
class MenuUiTest extends BrowserTestBase {

  use MenuUiTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['node', 'block', 'contextual', 'help', 'path', 'test_page_test'];
  protected static $modules = [
    'block',
    'contextual',
    'help',
    'menu_link_content',
    'menu_ui',
    'node',
    'path',
    'test_page_test',
  ];

  /**
   * A user with administration rights.
@@ -216,7 +229,7 @@ public function addCustomMenu() {

    // Confirm that the custom menu block is available.
    $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
    $this->clickLinkPartialName('Place block');
    $this->clickLink('Place block');
    $this->assertText($label);

    // Enable the block.
@@ -310,25 +323,25 @@ public function doMenuTests() {
    // -- item2
    // --- item3

    $this->assertMenuLink($item1->getPluginId(), [
    $this->assertMenuLink([
      'children' => [$item2->getPluginId(), $item3->getPluginId()],
      'parents' => [$item1->getPluginId()],
      // We assert the language code here to make sure that the language
      // selection element degrades gracefully without the Language module.
      'langcode' => 'en',
    ]);
    $this->assertMenuLink($item2->getPluginId(), [
    ], $item1->getPluginId());
    $this->assertMenuLink([
      'children' => [$item3->getPluginId()],
      'parents' => [$item2->getPluginId(), $item1->getPluginId()],
      // See above.
      'langcode' => 'en',
    ]);
    $this->assertMenuLink($item3->getPluginId(), [
    ], $item2->getPluginId());
    $this->assertMenuLink([
      'children' => [],
      'parents' => [$item3->getPluginId(), $item2->getPluginId(), $item1->getPluginId()],
      // See above.
      'langcode' => 'en',
    ]);
    ], $item3->getPluginId());

    // Verify menu links.
    $this->verifyMenuLink($item1, $node1);
@@ -350,18 +363,18 @@ public function doMenuTests() {
    // -- item5
    // -- item6

    $this->assertMenuLink($item4->getPluginId(), [
    $this->assertMenuLink([
      'children' => [$item5->getPluginId(), $item6->getPluginId()],
      'parents' => [$item4->getPluginId()],
      // See above.
      'langcode' => 'en',
    ]);
    $this->assertMenuLink($item5->getPluginId(), [
    ], $item4->getPluginId());
    $this->assertMenuLink([
      'children' => [],
      'parents' => [$item5->getPluginId(), $item4->getPluginId()],
      'langcode' => 'en',
    ]);
    $this->assertMenuLink($item6->getPluginId(), [
    ], $item5->getPluginId());
    $this->assertMenuLink([
      'children' => [],
      'parents' => [$item6->getPluginId(), $item4->getPluginId()],
      'route_name' => 'entity.node.canonical',
@@ -369,7 +382,7 @@ public function doMenuTests() {
      'url' => '',
      // See above.
      'langcode' => 'en',
    ]);
    ], $item6->getPluginId());

    // Modify menu links.
    $this->modifyMenuLink($item1);
@@ -390,37 +403,37 @@ public function doMenuTests() {
    // ---- item3
    // -- item6

    $this->assertMenuLink($item1->getPluginId(), [
    $this->assertMenuLink([
      'children' => [],
      'parents' => [$item1->getPluginId()],
      // See above.
      'langcode' => 'en',
    ]);
    $this->assertMenuLink($item4->getPluginId(), [
    ], $item1->getPluginId());
    $this->assertMenuLink([
      'children' => [$item5->getPluginId(), $item6->getPluginId(), $item2->getPluginId(), $item3->getPluginId()],
      'parents' => [$item4->getPluginId()],
      // See above.
      'langcode' => 'en',
    ]);
    ], $item4->getPluginId());

    $this->assertMenuLink($item5->getPluginId(), [
    $this->assertMenuLink([
      'children' => [$item2->getPluginId(), $item3->getPluginId()],
      'parents' => [$item5->getPluginId(), $item4->getPluginId()],
      // See above.
      'langcode' => 'en',
    ]);
    $this->assertMenuLink($item2->getPluginId(), [
    ], $item5->getPluginId());
    $this->assertMenuLink([
      'children' => [$item3->getPluginId()],
      'parents' => [$item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()],
      // See above.
      'langcode' => 'en',
    ]);
    $this->assertMenuLink($item3->getPluginId(), [
    ], $item2->getPluginId());
    $this->assertMenuLink([
      'children' => [],
      'parents' => [$item3->getPluginId(), $item2->getPluginId(), $item5->getPluginId(), $item4->getPluginId()],
      // See above.
      'langcode' => 'en',
    ]);
    ], $item3->getPluginId());

    // Add 102 menu links with increasing weights, then make sure the last-added
    // item's weight doesn't get changed because of the old hardcoded delta=50.
@@ -428,7 +441,7 @@ public function doMenuTests() {
    for ($i = -50; $i <= 51; $i++) {
      $items[$i] = $this->addMenuLink('', '/node/' . $node1->id(), $menu_name, TRUE, strval($i));
    }
    $this->assertMenuLink($items[51]->getPluginId(), ['weight' => '51']);
    $this->assertMenuLink(['weight' => '51'], $items[51]->getPluginId());

    // Disable a link and then re-enable the link via the overview form.
    $this->disableMenuLink($item1);
@@ -446,15 +459,15 @@ public function doMenuTests() {
    $item5->save();

    // Verify in the database.
    $this->assertMenuLink($item1->getPluginId(), ['enabled' => 1]);
    $this->assertMenuLink(['enabled' => 1], $item1->getPluginId());

    // Add an external link.
    $item7 = $this->addMenuLink('', 'https://www.drupal.org', $menu_name);
    $this->assertMenuLink($item7->getPluginId(), ['url' => 'https://www.drupal.org']);
    $this->assertMenuLink(['url' => 'https://www.drupal.org'], $item7->getPluginId());

    // Add <front> menu item.
    $item8 = $this->addMenuLink('', '/', $menu_name);
    $this->assertMenuLink($item8->getPluginId(), ['route_name' => '<front>']);
    $this->assertMenuLink(['route_name' => '<front>'], $item8->getPluginId());
    $this->drupalGet('');
    $this->assertResponse(200);
    // Make sure we get routed correctly.
@@ -531,7 +544,7 @@ public function testSystemMenuRename() {
    // Make sure menu shows up with new name in block addition.
    $default_theme = $this->config('system.theme')->get('default');
    $this->drupalget('admin/structure/block/list/' . $default_theme);
    $this->clickLinkPartialName('Place block');
    $this->clickLink('Place block');
    $this->assertText($edit['label']);
  }

@@ -557,30 +570,7 @@ public function testUnpublishedNodeMenuItem() {
    $this->assertNoText($item->getTitle(), "Menu link pointing to unpublished node is only visible to users with 'bypass node access' permission");
    // The cache contexts associated with the (in)accessible menu links are
    // bubbled. See DefaultMenuLinkTreeManipulators::menuLinkCheckAccess().
    $this->assertCacheContext('user.permissions');
  }

  /**
   * Tests the contextual links on a menu block.
   */
  public function testBlockContextualLinks() {
    $this->drupalLogin($this->drupalCreateUser(['administer menu', 'access contextual links', 'administer blocks']));
    $custom_menu = $this->addCustomMenu();
    $this->addMenuLink('', '/', $custom_menu->id());
    $block = $this->drupalPlaceBlock('system_menu_block:' . $custom_menu->id(), ['label' => 'Custom menu', 'provider' => 'system']);
    $this->drupalGet('test-page');

    $id = 'block:block=' . $block->id() . ':langcode=en|menu:menu=' . $custom_menu->id() . ':langcode=en';
    // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:assertContextualLinkPlaceHolder()
    $this->assertRaw('<div data-contextual-id="' . $id . '"></div>', format_string('Contextual link placeholder with id @id exists.', ['@id' => $id]));

    // Get server-rendered contextual links.
    // @see \Drupal\contextual\Tests\ContextualDynamicContextTest:renderContextualLinks()
    $post = ['ids[0]' => $id];
    $response = $this->drupalPost('contextual/render', 'application/json', $post, ['query' => ['destination' => 'test-page']]);
    $this->assertResponse(200);
    $json = Json::decode($response);
    $this->assertIdentical($json[$id], '<ul class="contextual-links"><li class="block-configure"><a href="' . base_path() . 'admin/structure/block/manage/' . $block->id() . '">Configure block</a></li><li class="entitymenuedit-form"><a href="' . base_path() . 'admin/structure/menu/manage/' . $custom_menu->id() . '">Edit menu</a></li></ul>');
    $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Contexts', 'user.permissions');
  }

  /**
@@ -627,7 +617,7 @@ public function addMenuLink($parent = '', $path = '/', $menu_name = 'tools', $ex

    $menu_link = reset($menu_links);
    $this->assertTrue($menu_link, 'Menu link was found in database.');
    $this->assertMenuLink($menu_link->getPluginId(), ['menu_name' => $menu_name, 'children' => [], 'parent' => $parent]);
    $this->assertMenuLink(['menu_name' => $menu_name, 'children' => [], 'parent' => $parent], $menu_link->getPluginId());

    return $menu_link;
  }
@@ -841,7 +831,7 @@ public function disableMenuLink(MenuLinkContent $item) {

    // Unlike most other modules, there is no confirmation message displayed.
    // Verify in the database.
    $this->assertMenuLink($item->getPluginId(), ['enabled' => 0]);
    $this->assertMenuLink(['enabled' => 0], $item->getPluginId());
  }

  /**
@@ -856,25 +846,23 @@ public function enableMenuLink(MenuLinkContent $item) {
    $this->drupalPostForm("admin/structure/menu/item/$mlid/edit", $edit, t('Save'));

    // Verify in the database.
    $this->assertMenuLink($item->getPluginId(), ['enabled' => 1]);
    $this->assertMenuLink(['enabled' => 1], $item->getPluginId());
  }

  /**
   * Tests if administrative users other than user 1 can access the menu parents
   * AJAX callback.
   * Tests if admin users, other than UID1, can access parents AJAX callback.
   */
  public function testMenuParentsJsAccess() {
    $admin = $this->drupalCreateUser(['administer menu']);
    $this->drupalLogin($admin);
    $this->drupalLogin($this->drupalCreateUser(['administer menu']));
    // Just check access to the callback overall, the POST data is irrelevant.
    $this->drupalGetAjax('admin/structure/menu/parents');
    $this->assertResponse(200);

    // Do standard user tests.
    // Log in the user.
    $this->drupalLogin($this->authenticatedUser);
    $this->drupalGetAjax('admin/structure/menu/parents');
    $this->assertResponse(403);
    $this->drupalGet('admin/structure/menu/parents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With: XMLHttpRequest']);
    $this->assertSession()->statusCodeEquals(200);

    // Log in as authenticated user.
    $this->drupalLogin($this->drupalCreateUser());
    // Check that a simple user is not able to access the menu.
    $this->drupalGet('admin/structure/menu/parents', ['query' => [MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax']], ['X-Requested-With: XMLHttpRequest']);
    $this->assertSession()->statusCodeEquals(403);
  }

  /**
Loading