MenuExampleTest.php in Examples for Developers 8
File
menu_example/tests/src/Functional/MenuExampleTest.php
View source
<?php
namespace Drupal\Tests\menu_example\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Url;
class MenuExampleTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'menu_example',
];
protected $profile = 'minimal';
protected function setUp() {
parent::setUp();
$this
->placeBlock('system_menu_block:main');
}
public function testMenuExampleRoutes() {
$assert = $this
->assertSession();
$routes = [
'examples.menu_example' => 'This page is displayed by the simplest (and base) menu example.',
'examples.menu_example.permissioned' => 'A menu item that requires the "access protected menu example" permission',
'examples.menu_example.custom_access' => 'A menu item that requires the user to posess',
'examples.menu_example.custom_access_page' => 'This menu entry will not be visible and access will result in a 403',
'examples.menu_example.route_only' => 'A menu entry with no menu link is',
'examples.menu_example.use_url_arguments' => 'This page demonstrates using arguments in the url',
'examples.menu_example.title_callbacks' => 'The title of this page is dynamically changed by the title callback',
'examples.menu_example.placeholder_argument' => 'Demonstrate placeholders by visiting',
'example.menu_example.path_override' => 'This menu item was created strictly to allow the RouteSubscriber',
'examples.menu_example.alternate_menu' => 'This will be in the Main menu instead of the default Tools menu',
];
$this
->drupalLogin($this
->createUser());
$this
->drupalGet(Url::fromRoute('<front>'));
foreach (array_keys($routes) as $route) {
$assert
->linkByHrefExists(Url::fromRoute($route)
->getInternalPath());
}
$routes['examples.menu_example.route_only.callback'] = 'The route entry has no corresponding menu links entry';
foreach ($routes as $route => $content) {
$this
->drupalGet(Url::fromRoute($route));
$assert
->statusCodeEquals(200);
$assert
->pageTextContains($content);
}
$arg = 2377;
$this
->drupalGet(Url::fromRoute('examples.menu_example.placeholder_argument.display', [
'arg' => $arg,
]));
$assert
->statusCodeEquals(200);
$assert
->pageTextContains($arg);
$dynamic_routes = [
'examples.menu_example.tabs_second',
'examples.menu_example.tabs_third',
'examples.menu_example.tabs_fourth',
'examples.menu_example.tabs_default_second',
'examples.menu_example.tabs_default_third',
];
$this
->drupalGet(Url::fromRoute('examples.menu_example.tabs'));
foreach ($dynamic_routes as $route) {
$assert
->linkByHrefExists(Url::fromRoute($route)
->getInternalPath());
}
foreach ($dynamic_routes as $route) {
$this
->drupalGet(Url::fromRoute($route));
$assert
->statusCodeEquals(200);
}
$this
->drupalGet(Url::fromRoute('examples.menu_example.permissioned_controlled'));
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this
->createUser([
'access protected menu example',
]));
$this
->drupalGet(Url::fromRoute('examples.menu_example.permissioned_controlled'));
$assert
->statusCodeEquals(200);
$assert
->pageTextContains('This menu entry will not show and the page will not be accessible');
$this
->drupalLogout();
$this
->drupalGet(Url::fromRoute('examples.menu_example.custom_access_page'));
$assert
->statusCodeEquals(403);
}
}