Commit 297c35eb authored by catch's avatar catch

Issue #2887099 by naveenvalecha, Lendude, Manuel Garcia: Menu: convert system...

Issue #2887099 by naveenvalecha, Lendude, Manuel Garcia: Menu: convert system functional tests to PHPUnit
parent ff6eea71
......@@ -2,11 +2,16 @@
namespace Drupal\system\Tests\Menu;
@trigger_error(__NAMESPACE__ . '\AssertBreadcrumbTrait is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait', E_USER_DEPRECATED);
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
/**
* Provides test assertions for verifying breadcrumbs.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait instead.
*/
trait AssertBreadcrumbTrait {
......
......@@ -2,10 +2,15 @@
namespace Drupal\system\Tests\Menu;
@trigger_error(__NAMESPACE__ . '\AssertMenuActiveTrailTrait is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\system\Functional\Menu\AssertMenuActiveTrailTrait', E_USER_DEPRECATED);
use Drupal\Core\Url;
/**
* Provides test assertions for verifying the active menu trail.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use \Drupal\Tests\system\Functional\Menu\AssertMenuActiveTrailTrait instead.
*/
trait AssertMenuActiveTrailTrait {
......
......@@ -2,8 +2,16 @@
namespace Drupal\system\Tests\Menu;
@trigger_error(__NAMESPACE__ . '\MenuTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\BrowserTestBase', E_USER_DEPRECATED);
use Drupal\simpletest\WebTestBase;
/**
* Base class for Menu tests.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use \Drupal\Tests\BrowserTestBase instead.
*/
abstract class MenuTestBase extends WebTestBase {
use AssertBreadcrumbTrait;
......
<?php
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
/**
* Provides test assertions for verifying breadcrumbs.
*/
trait AssertBreadcrumbTrait {
use AssertMenuActiveTrailTrait;
/**
* Assert that a given path shows certain breadcrumb links.
*
* @param \Drupal\Core\Url|string $goto
* (optional) A path or URL to pass to
* Drupal\simpletest\WebTestBase::drupalGet().
* @param array $trail
* An associative array whose keys are expected breadcrumb link paths and
* whose values are expected breadcrumb link texts (not sanitized).
* @param string $page_title
* (optional) A page title to additionally assert via
* Drupal\simpletest\WebTestBase::assertTitle(). Without site name suffix.
* @param array $tree
* (optional) An associative array whose keys are link paths and whose
* values are link titles (not sanitized) of an expected active trail in a
* menu tree output on the page.
* @param $last_active
* (optional) Whether the last link in $tree is expected to be active (TRUE)
* or just to be in the active trail (FALSE).
*/
protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = [], $last_active = TRUE) {
if (isset($goto)) {
$this->drupalGet($goto);
}
$this->assertBreadcrumbParts($trail);
// Additionally assert page title, if given.
if (isset($page_title)) {
$this->assertTitle(strtr('@title | Drupal', ['@title' => $page_title]));
}
// Additionally assert active trail in a menu tree output, if given.
if ($tree) {
$this->assertMenuActiveTrail($tree, $last_active);
}
}
/**
* Assert that a trail exists in the internal browser.
*
* @param array $trail
* An associative array whose keys are expected breadcrumb link paths and
* whose values are expected breadcrumb link texts (not sanitized).
*/
protected function assertBreadcrumbParts($trail) {
// Compare paths with actual breadcrumb.
$parts = $this->getBreadcrumbParts();
$pass = TRUE;
// There may be more than one breadcrumb on the page. If $trail is empty
// this test would go into an infinite loop, so we need to check that too.
while ($trail && !empty($parts)) {
foreach ($trail as $path => $title) {
// If the path is empty, generate the path from the <front> route. If
// the path does not start with a leading slash, then run it through
// Url::fromUri('base:')->toString() to get the correct base
// prepended.
if ($path == '') {
$url = Url::fromRoute('<front>')->toString();
}
elseif ($path[0] != '/') {
$url = Url::fromUri('base:' . $path)->toString();
}
else {
$url = $path;
}
$part = array_shift($parts);
$pass = ($pass && $part['href'] === $url && $part['text'] === Html::escape($title));
}
}
// No parts must be left, or an expected "Home" will always pass.
$pass = ($pass && empty($parts));
$this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', [
'%parts' => implode(' » ', $trail),
'@path' => $this->getUrl(),
]));
}
/**
* Returns the breadcrumb contents of the current page in the internal browser.
*/
protected function getBreadcrumbParts() {
$parts = [];
$elements = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
if (!empty($elements)) {
foreach ($elements as $element) {
$parts[] = [
'text' => $element->getText(),
'href' => $element->getAttribute('href'),
'title' => $element->getAttribute('title'),
];
}
}
return $parts;
}
}
<?php
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Core\Url;
/**
* Provides test assertions for verifying the active menu trail.
*/
trait AssertMenuActiveTrailTrait {
/**
* Assert that active trail exists in a menu tree output.
*
* @param array $tree
* An associative array whose keys are link paths and whose
* values are link titles (not sanitized) of an expected active trail in a
* menu tree output on the page.
* @param bool $last_active
* Whether the last link in $tree is expected to be active (TRUE)
* or just to be in the active trail (FALSE).
*/
protected function assertMenuActiveTrail($tree, $last_active) {
end($tree);
$active_link_path = key($tree);
$active_link_title = array_pop($tree);
$xpath = '';
if ($tree) {
$i = 0;
foreach ($tree as $link_path => $link_title) {
$part_xpath = (!$i ? '//' : '/following-sibling::ul/descendant::');
$part_xpath .= 'li[contains(@class, :class)]/a[contains(@href, :href) and contains(text(), :title)]';
$part_args = [
':class' => 'menu-item--active-trail',
':href' => Url::fromUri('base:' . $link_path)->toString(),
':title' => $link_title,
];
$xpath .= $this->buildXPathQuery($part_xpath, $part_args);
$i++;
}
$elements = $this->xpath($xpath);
$this->assertTrue(!empty($elements), 'Active trail to current page was found in menu tree.');
// Append prefix for active link asserted below.
$xpath .= '/following-sibling::ul/descendant::';
}
else {
$xpath .= '//';
}
$xpath_last_active = ($last_active ? 'and contains(@class, :class-active)' : '');
$xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]';
$args = [
':class-trail' => 'menu-item--active-trail',
':class-active' => 'is-active',
':href' => Url::fromUri('base:' . $active_link_path)->toString(),
':title' => $active_link_title,
];
$elements = $this->xpath($xpath, $args);
$this->assertTrue(!empty($elements), format_string('Active link %title was found in menu tree, including active trail links %tree.', [
'%title' => $active_link_title,
'%tree' => implode(' » ', $tree),
]));
}
}
<?php
namespace Drupal\system\Tests\Menu;
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Core\Url;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\RoleInterface;
/**
......@@ -11,7 +12,9 @@
*
* @group Menu
*/
class BreadcrumbTest extends MenuTestBase {
class BreadcrumbTest extends BrowserTestBase {
use AssertBreadcrumbTrait;
/**
* Modules to enable.
......@@ -358,17 +361,17 @@ public function testBreadCrumbs() {
// user is not able to access "Administer".
$trail = $home;
$this->assertBreadcrumb('admin', $trail, t('Access denied'));
$this->assertResponse(403);
$this->assertSession()->statusCodeEquals(403);
// Since the 'admin' path is not accessible, we still expect only the Home
// link.
$this->assertBreadcrumb('admin/reports', $trail, t('Reports'));
$this->assertNoResponse(403);
$this->assertSession()->statusCodeNotEquals(403);
// Since the Reports page is accessible, that will show.
$trail += ['admin/reports' => t('Reports')];
$this->assertBreadcrumb('admin/reports/dblog', $trail, t('Recent log messages'));
$this->assertNoResponse(403);
$this->assertSession()->statusCodeNotEquals(403);
// Ensure that the breadcrumb is safe against XSS.
$this->drupalGet('menu-test/breadcrumb1/breadcrumb2/breadcrumb3');
......
<?php
namespace Drupal\system\Tests\Menu;
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Tests local actions derived from router and added/altered via hooks.
*
* @group Menu
*/
class LocalActionTest extends WebTestBase {
class LocalActionTest extends BrowserTestBase {
/**
* Modules to enable.
......@@ -80,7 +80,7 @@ protected function assertLocalAction(array $actions) {
// This behaviour is a bug in libxml, see
// https://bugs.php.net/bug.php?id=49437.
$this->assertPattern('@<a [^>]*class="[^"]*button-action[^"]*"[^>]*>' . preg_quote($title, '@') . '</@');
$this->assertEqual($elements[$index]['href'], $url->toString());
$this->assertEqual($elements[$index]->getAttribute('href'), $url->toString());
$index++;
}
}
......
<?php
namespace Drupal\system\Tests\Menu;
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Tests local tasks derived from router and added/altered via hooks.
*
* @group Menu
*/
class LocalTasksTest extends WebTestBase {
class LocalTasksTest extends BrowserTestBase {
/**
* Modules to enable.
......@@ -55,10 +55,10 @@ protected function assertLocalTasks(array $routes, $level = 0) {
foreach ($routes as $index => $route_info) {
list($route_name, $route_parameters) = $route_info;
$expected = Url::fromRoute($route_name, $route_parameters)->toString();
$method = ($elements[$index]['href'] == $expected ? 'pass' : 'fail');
$method = ($elements[$index]->getAttribute('href') == $expected ? 'pass' : 'fail');
$this->{$method}(format_string('Task @number href @value equals @expected.', [
'@number' => $index + 1,
'@value' => (string) $elements[$index]['href'],
'@value' => $elements[$index]->getAttribute('href'),
'@expected' => $expected,
]));
}
......@@ -129,7 +129,7 @@ public function testPluginLocalTask() {
// Ensure the view tab is active.
$result = $this->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]/a');
$this->assertEqual(1, count($result), 'There is just a single active tab.');
$this->assertEqual('View', (string) $result[0], 'The view tab is active.');
$this->assertEqual('View(active tab)', $result[0]->getText(), 'The view tab is active.');
// Verify that local tasks in the second level appear.
$sub_tasks = [
......@@ -144,15 +144,15 @@ public function testPluginLocalTask() {
$result = $this->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]/a');
$this->assertEqual(1, count($result), 'There is just a single active tab.');
$this->assertEqual('Settings', (string) $result[0], 'The settings tab is active.');
$this->assertEqual('Settings(active tab)', $result[0]->getText(), 'The settings tab is active.');
$this->drupalGet(Url::fromRoute('menu_test.local_task_test_tasks_settings_sub1'));
$this->assertLocalTasks($sub_tasks, 1);
$result = $this->xpath('//ul[contains(@class, "tabs")]//a[contains(@class, "active")]');
$this->assertEqual(2, count($result), 'There are tabs active on both levels.');
$this->assertEqual('Settings', (string) $result[0], 'The settings tab is active.');
$this->assertEqual('Dynamic title for TestTasksSettingsSub1', (string) $result[1], 'The sub1 tab is active.');
$this->assertEqual('Settings(active tab)', $result[0]->getText(), 'The settings tab is active.');
$this->assertEqual('Dynamic title for TestTasksSettingsSub1(active tab)', $result[1]->getText(), 'The sub1 tab is active.');
$this->assertCacheTag('kittens:ragdoll');
$this->assertCacheTag('kittens:dwarf-cat');
......@@ -162,8 +162,8 @@ public function testPluginLocalTask() {
$result = $this->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]');
$this->assertEqual(2, count($result), 'There are tabs active on both levels.');
$this->assertEqual('Settings', (string) $result[0]->a, 'The settings tab is active.');
$this->assertEqual('Derive 1', (string) $result[1]->a, 'The derive1 tab is active.');
$this->assertEqual('Settings(active tab)', $result[0]->getText(), 'The settings tab is active.');
$this->assertEqual('Derive 1(active tab)', $result[1]->getText(), 'The derive1 tab is active.');
// Ensures that the local tasks contains the proper 'provider key'
$definitions = $this->container->get('plugin.manager.menu.local_task')->getDefinitions();
......@@ -190,7 +190,7 @@ public function testPluginLocalTask() {
$result = $this->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]');
$this->assertEqual(1, count($result), 'There is one active tab.');
$this->assertEqual('upcasting sub1', (string) $result[0]->a, 'The "upcasting sub1" tab is active.');
$this->assertEqual('upcasting sub1(active tab)', $result[0]->getText(), 'The "upcasting sub1" tab is active.');
$this->drupalGet(Url::fromRoute('menu_test.local_task_test_upcasting_sub2', ['entity_test' => '1']));
......@@ -202,7 +202,7 @@ public function testPluginLocalTask() {
$result = $this->xpath('//ul[contains(@class, "tabs")]//li[contains(@class, "active")]');
$this->assertEqual(1, count($result), 'There is one active tab.');
$this->assertEqual('upcasting sub2', (string) $result[0]->a, 'The "upcasting sub2" tab is active.');
$this->assertEqual('upcasting sub2(active tab)', $result[0]->getText(), 'The "upcasting sub2" tab is active.');
}
/**
......
<?php
namespace Drupal\system\Tests\Menu;
namespace Drupal\Tests\system\Functional\Menu;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
use Drupal\Tests\BrowserTestBase;
/**
* Tests menu router and default menu link functionality.
*
* @group Menu
*/
class MenuRouterTest extends WebTestBase {
class MenuRouterTest extends BrowserTestBase {
/**
* Modules to enable.
......
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