Commit 51afa681 authored by alexpott's avatar alexpott

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
......@@ -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);
}
......
......@@ -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 {
......
<?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
......
<?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');
......
<?php
namespace Drupal\Tests\menu_ui\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use Drupal\system\Entity\Menu;
use Drupal\Tests\contextual\FunctionalJavascript\ContextualLinkClickTrait;
use Drupal\Tests\menu_ui\Traits\MenuUiTrait;
/**
* Tests custom menu and menu links operations using the UI.
*
* @group menu_ui
*/
class MenuUiJavascriptTest extends JavascriptTestBase {
use ContextualLinkClickTrait;
use MenuUiTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'block',
'contextual',
'menu_link_content',
'menu_ui',
'test_page_test',
];
/**
* Tests the contextual links on a menu block.
*/
public function testBlockContextualLinks() {
$this->drupalLogin($this->drupalCreateUser([
'administer menu',
'access contextual links',
'administer blocks',
]));
$menu = $this->addCustomMenu();
$block = $this->drupalPlaceBlock('system_menu_block:' . $menu->id(), [
'label' => 'Custom menu',
'provider' => 'system'
]);
$this->addMenuLink('', '/', $menu->id());
$this->drupalGet('test-page');
// Click on 'Configure block' contextual link.
$this->clickContextualLink("#block-{$block->id()}", 'Configure block');
// Check that we're on block configuration form.
$this->assertNotEmpty($this->getSession()->getPage()->findLink('Remove block'));
$this->drupalGet('test-page');
// Click on 'Edit menu' contextual link.
$this->clickContextualLink("#block-{$block->id()}", 'Edit menu');
// Check that we're on block configuration form.
$this->assertSession()->pageTextContains("Machine name: {$menu->id()}");
}
/**
* Creates a custom menu.
*
* @return \Drupal\system\Entity\Menu
* The custom menu that has been created.
*/
protected function addCustomMenu() {
// Try adding a menu using a menu_name that is too long.
$label = $this->randomMachineName(16);
$menu_id = strtolower($this->randomMachineName(MENU_MAX_MENU_NAME_LENGTH_UI + 1));
$this->drupalGet('admin/structure/menu/add');
$page = $this->getSession()->getPage();
// Type the label to activate the machine name field and the edit button.
$page->fillField('Title', $label);
// Wait for the machine name widget to be activated.
$this->assertSession()->waitForElementVisible('css', 'button[type=button].link:contains(Edit)');
// Activate the machine name text field.
$page->pressButton('Edit');
// Try to fill a text longer than the allowed limit.
$page->fillField('Menu name', $menu_id);
$page->pressButton('Save');
// Check that the menu was saved with the ID truncated to the max length.
$menu = Menu::load(substr($menu_id, 0, MENU_MAX_MENU_NAME_LENGTH_UI));
$this->assertEquals($label, $menu->label());
// Check that the menu was added.
$this->drupalGet('admin/structure/menu');
$this->assertSession()->pageTextContains($label);
// Confirm that the custom menu block is available.
$this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default'));
$this->clickLink('Place block');
// Wait for the modal dialog to be loaded.
$this->assertSession()->waitForElement('css', "div[aria-describedby=drupal-modal]");
// Check that the block is available to be placed.
$this->assertSession()->pageTextContains($label);
return $menu;
}
/**
* Adds a menu link using the UI.
*
* @param string $parent
* Optional parent menu link id.
* @param string $path
* The path to enter on the form. Defaults to the front page.
* @param string $menu_id
* Menu ID. Defaults to 'tools'.
* @param bool $expanded
* Whether or not this menu link is expanded. Setting this to TRUE should
* test whether it works when we do the authenticatedUser tests. Defaults
* to FALSE.
* @param string $weight
* Menu weight. Defaults to 0.
*
* @return \Drupal\menu_link_content\Entity\MenuLinkContent
* A menu link entity.
*/
protected function addMenuLink($parent = '', $path = '/', $menu_id = 'tools', $expanded = FALSE, $weight = '0') {
// View add menu link page.
$this->drupalGet("admin/structure/menu/manage/$menu_id/add");
$this->assertSession()->statusCodeEquals(200);
$title = '!link_' . $this->randomMachineName(16);
$edit = [
'link[0][uri]' => $path,
'title[0][value]' => $title,
'description[0][value]' => '',
'enabled[value]' => 1,
'expanded[value]' => $expanded,
'menu_parent' => $menu_id . ':' . $parent,
'weight[0][value]' => $weight,
];
// Add menu link.
$this->drupalPostForm(NULL, $edit, 'Save');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('The menu link has been saved.');
$storage = $this->container->get('entity_type.manager')->getStorage('menu_link_content');
$menu_links = $storage->loadByProperties(['title' => $title]);
$menu_link = reset($menu_links);
// Check that the stored menu link meeting the expectations.
$this->assertNotNull($menu_link);
$this->assertMenuLink([
'menu_name' => $menu_id,
'children' => [],
'parent' => $parent
], $menu_link->getPluginId());
return $menu_link;
}
}
<?php
namespace Drupal\Tests\menu_ui\Traits;
/**
* Provides common methods for Menu UI module tests.
*/
trait MenuUiTrait {
/**
* Asserts that a menu fetched from the database matches an expected one.
*
* @param array $expected_item
* Array containing properties to check.
* @param int $menu_plugin_id
* Menu item id.
*/
protected function assertMenuLink(array $expected_item, $menu_plugin_id) {
// Retrieve the menu link.
/** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
$menu_link_manager = \Drupal::service('plugin.manager.menu.link');
$menu_link_manager->resetDefinitions();
// Reset the static load cache.
\Drupal::entityTypeManager()->getStorage('menu_link_content')->resetCache();
$definition = $menu_link_manager->getDefinition($menu_plugin_id);
$entity = NULL;
// Pull the path from the menu link content.
if (strpos($menu_plugin_id, 'menu_link_content') === 0) {
list(, $uuid) = explode(':', $menu_plugin_id, 2);
/** @var \Drupal\menu_link_content\Entity\MenuLinkContent $entity */
$entity = \Drupal::service('entity.repository')
->loadEntityByUuid('menu_link_content', $uuid);
}
if (isset($expected_item['children'])) {
$child_ids = array_values($menu_link_manager->getChildIds($menu_plugin_id));
sort($expected_item['children']);
if ($child_ids) {
sort($child_ids);
}
$this->assertSame($expected_item['children'], $child_ids);
unset($expected_item['children']);
}
if (isset($expected_item['parents'])) {
$parent_ids = array_values($menu_link_manager->getParentIds($menu_plugin_id));
$this->assertSame($expected_item['parents'], $parent_ids);
unset($expected_item['parents']);
}
if (isset($expected_item['langcode']) && $entity) {
$this->assertEquals($expected_item['langcode'], $entity->langcode->value);
unset($expected_item['langcode']);
}
if (isset($expected_item['enabled']) && $entity) {
$this->assertEquals($expected_item['enabled'], $entity->enabled->value);
unset($expected_item['enabled']);
}
foreach ($expected_item as $key => $value) {
$this->assertNotNull($definition[$key]);
$this->assertSame($value, $definition[$key]);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment