AdminTest.php in Drupal 10
File
core/modules/system/tests/src/Functional/System/AdminTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Tests\BrowserTestBase;
class AdminTest extends BrowserTestBase {
protected $adminUser;
protected $webUser;
protected static $modules = [
'locale',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser(array_keys(\Drupal::service('user.permissions')
->getPermissions()));
$this->webUser = $this
->drupalCreateUser([
'access administration pages',
'translate interface',
]);
$this
->drupalLogin($this->adminUser);
}
public function testAdminPages() {
$this
->drupalGet('admin');
foreach ($this
->getTopLevelMenuLinks() as $item) {
$this
->assertSession()
->linkExists($item
->getTitle());
$this
->assertSession()
->linkByHrefExists($item
->getUrlObject()
->toString());
$this
->assertSession()
->pageTextContains($item
->getDescription());
}
$admin_list_pages = [
'admin/index',
'admin/config',
'admin/config/regional',
];
foreach ($admin_list_pages as $page) {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($page);
$this
->assertSession()
->linkByHrefExists('admin/config');
$this
->assertSession()
->linkByHrefExists('admin/config/regional/settings');
$this
->assertSession()
->linkByHrefExists('admin/config/regional/date-time');
$this
->assertSession()
->linkByHrefExists('admin/config/regional/language');
$this
->assertSession()
->linkByHrefNotExists('admin/config/regional/language/detection/session');
$this
->assertSession()
->linkByHrefNotExists('admin/config/regional/language/detection/url');
$this
->assertSession()
->linkByHrefExists('admin/config/regional/translate');
if ($page == 'admin/index') {
$this
->assertSession()
->linkByHrefExists("admin/people/permissions/module/locale");
}
$this
->drupalLogin($this->webUser);
$this
->drupalGet($page);
$this
->assertSession()
->linkByHrefExists('admin/config');
$this
->assertSession()
->linkByHrefNotExists('admin/config/regional/settings');
$this
->assertSession()
->linkByHrefNotExists('admin/config/regional/date-time');
$this
->assertSession()
->linkByHrefNotExists('admin/config/regional/language');
$this
->assertSession()
->linkByHrefNotExists('admin/config/regional/language/detection/session');
$this
->assertSession()
->linkByHrefNotExists('admin/config/regional/language/detection/url');
$this
->assertSession()
->linkByHrefExists('admin/config/regional/translate');
if ($page == 'admin/index') {
$this
->assertSession()
->linkByHrefNotExists("admin/people/permissions#module-locale");
}
}
}
protected function getTopLevelMenuLinks() {
$menu_tree = \Drupal::menuTree();
$parameters = new MenuTreeParameters();
$parameters
->setRoot('system.admin')
->excludeRoot()
->setTopLevelOnly()
->onlyEnabledLinks();
$tree = $menu_tree
->load(NULL, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:flatten',
],
];
$tree = $menu_tree
->transform($tree, $manipulators);
$menu_links = [];
foreach ($tree as $element) {
$menu_links[] = $element->link;
}
return $menu_links;
}
public function testCompactMode() {
$session = $this
->getSession();
$frontpage_url = 'user/' . $this->adminUser
->id();
$this
->drupalGet('admin/compact/on');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($frontpage_url);
$this
->assertEquals('1', $session
->getCookie('Drupal.visitor.admin_compact_mode'), 'Compact mode turns on.');
$this
->drupalGet('admin/compact/on');
$this
->assertEquals('1', $session
->getCookie('Drupal.visitor.admin_compact_mode'), 'Compact mode remains on after a repeat call.');
$this
->drupalGet('');
$this
->assertEquals('1', $session
->getCookie('Drupal.visitor.admin_compact_mode'), 'Compact mode persists on new requests.');
$this
->drupalGet('admin/compact/off');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->addressEquals($frontpage_url);
$this
->assertNull($session
->getCookie('Drupal.visitor.admin_compact_mode'), 'Compact mode turns off.');
$this
->drupalGet('admin/compact/off');
$this
->assertNull($session
->getCookie('Drupal.visitor.admin_compact_mode'), 'Compact mode remains off after a repeat call.');
$this
->drupalGet('');
$this
->assertNull($session
->getCookie('Drupal.visitor.admin_compact_mode'), 'Compact mode persists off new requests.');
}
}
Classes
Name |
Description |
AdminTest |
Tests output on administrative pages and compact mode functionality. |