View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\toolbar\Functional;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class ToolbarAdminMenuTest extends BrowserTestBase {
protected $adminUser;
protected $adminUser2;
protected $hash;
protected static $modules = [
'node',
'block',
'menu_ui',
'user',
'taxonomy',
'toolbar',
'language',
'test_page_test',
'locale',
'search',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$perms = [
'access toolbar',
'access administration pages',
'administer site configuration',
'bypass node access',
'administer themes',
'administer nodes',
'access content overview',
'administer blocks',
'administer menu',
'administer modules',
'administer permissions',
'administer users',
'access user profiles',
'administer taxonomy',
'administer languages',
'translate interface',
'administer search',
];
$this->adminUser = $this
->drupalCreateUser($perms);
$this->adminUser2 = $this
->drupalCreateUser($perms);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('id="toolbar-administration"');
$this->hash = $this
->getSubtreesHash();
}
public function testModuleStatusChangeSubtreesHashCacheClear() {
$role = Role::load($this
->createRole([]));
$role
->setIsAdmin(TRUE);
$role
->save();
$this->adminUser
->addRole($role
->id());
$this->adminUser
->save();
$edit = [];
$edit['uninstall[taxonomy]'] = TRUE;
$this
->drupalGet('admin/modules/uninstall');
$this
->submitForm($edit, 'Uninstall');
$this
->submitForm([], 'Uninstall');
$this
->rebuildContainer();
$this
->assertDifferentHash();
$edit = [];
$edit['modules[taxonomy][enable]'] = TRUE;
$this
->drupalGet('admin/modules');
$this
->submitForm($edit, 'Install');
$this
->rebuildContainer();
$this
->assertDifferentHash();
}
public function testMenuLinkUpdateSubtreesHashCacheClear() {
$admin_menu_link_id = 'system.admin_config_development';
$edit = [];
$edit['enabled'] = FALSE;
$this
->drupalGet("admin/structure/menu/link/" . $admin_menu_link_id . "/edit");
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('The menu link has been saved.');
$this
->assertDifferentHash();
}
public function testUserRoleUpdateSubtreesHashCacheClear() {
$all_rids = $this->adminUser
->getRoles();
unset($all_rids[array_search(RoleInterface::AUTHENTICATED_ID, $all_rids)]);
$rid = reset($all_rids);
$edit = [];
$edit[$rid . '[administer taxonomy]'] = FALSE;
$this
->drupalGet('admin/people/permissions');
$this
->submitForm($edit, 'Save permissions');
$this
->assertDifferentHash();
$this
->drupalLogin($this->adminUser2);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('id="toolbar-administration"');
$admin_user_2_hash = $this
->getSubtreesHash();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('id="toolbar-administration"');
$this->hash = $this
->getSubtreesHash();
$rid = $this
->drupalCreateRole([
'administer content types',
]);
$this
->drupalGet('user/' . $this->adminUser
->id() . '/edit');
$this
->submitForm([
"roles[{$rid}]" => $rid,
], 'Save');
$this
->assertSession()
->pageTextContains('The changes have been saved.');
$this
->assertDifferentHash();
$this
->drupalLogin($this->adminUser2);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertNotEmpty($new_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
$this
->assertEquals($admin_user_2_hash, $new_subtree_hash, 'The user-specific subtree menu hash has not been updated.');
}
public function testNonCurrentUserAccountUpdates() {
$admin_user_id = $this->adminUser
->id();
$this->hash = $this
->getSubtreesHash();
$this
->drupalLogin($this->adminUser2);
$rid = $this
->drupalCreateRole([
'administer content types',
]);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$admin_user_2_hash = $this
->getSubtreesHash();
$this
->drupalGet('user/' . $admin_user_id . '/edit');
$this
->submitForm([
"roles[{$rid}]" => $rid,
], 'Save');
$this
->assertSession()
->pageTextContains('The changes have been saved.');
$this
->drupalLogin($this->adminUser);
$this
->assertDifferentHash();
$this
->drupalLogin($this->adminUser2);
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertNotEmpty($new_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
$this
->assertEquals($new_subtree_hash, $admin_user_2_hash, 'The user-specific subtree menu hash has not been updated.');
}
public function testLocaleTranslationSubtreesHashCacheClear() {
$admin_user = $this->adminUser;
$translate_user = $this
->drupalCreateUser([
'translate interface',
'access administration pages',
]);
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$translation = $this
->randomMachineName(16);
$this
->drupalLogin($admin_user);
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add custom language');
t($name, [], [
'langcode' => $langcode,
]);
$this->container
->get('string_translation')
->reset();
$this
->assertSession()
->responseContains('"edit-languages-' . $langcode . '-weight"');
$this
->assertSession()
->pageTextContains($name);
$this
->drupalGet($langcode . '/test-page');
$this
->assertSession()
->statusCodeEquals(200);
$original_subtree_hash = $this
->getSubtreesHash();
$this
->assertNotEmpty($original_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$this
->drupalGet($langcode . '/admin/config');
$search = [
'string' => 'Search and metadata',
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($search, 'Filter');
$this
->assertSession()
->pageTextNotContains('No strings available');
$this
->assertSession()
->pageTextContains($name);
$textarea = $this
->assertSession()
->elementExists('xpath', '//textarea');
$lid = (string) $textarea
->getAttribute('name');
$edit = [
$lid => $translation,
];
$this
->drupalGet('admin/config/regional/translate');
$this
->submitForm($edit, 'Save translations');
$this
->assertSession()
->pageTextContains('The strings have been saved.');
$this
->assertSession()
->addressEquals(Url::fromRoute('locale.translate_page'));
$this
->drupalLogout();
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config');
$this
->drupalGet($langcode . '/test-page');
$this
->assertSession()
->statusCodeEquals(200);
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertNotEmpty($new_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
$this
->assertNotEquals($original_subtree_hash, $new_subtree_hash, 'The user-specific subtree menu hash has been updated.');
}
public function testSubtreesJsonRequest() {
$admin_user = $this->adminUser;
$this
->drupalLogin($admin_user);
$subtrees_hash = $this
->getSubtreesHash();
$this
->drupalGet('toolbar/subtrees/' . $subtrees_hash, [
'query' => [
MainContentViewSubscriber::WRAPPER_FORMAT => 'drupal_ajax',
],
], [
'X-Requested-With: XMLHttpRequest',
]);
$ajax_result = json_decode($this
->getSession()
->getPage()
->getContent(), TRUE);
$this
->assertEquals('setToolbarSubtrees', $ajax_result[0]['command'], 'Subtrees response uses the correct command.');
$this
->assertEquals([
'system-admin_content',
'system-admin_structure',
'system-themes_page',
'system-modules_list',
'system-admin_config',
'entity-user-collection',
'front',
], array_keys($ajax_result[0]['subtrees']), 'Correct subtrees returned.');
}
public function testLanguageSwitching() {
$langcode = 'xx';
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language
->save();
$this
->rebuildContainer();
$this
->drupalGet('test-page', [
'language' => $language,
]);
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertNotEmpty($new_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
$this
->assertNotEquals($this->hash, $new_subtree_hash, 'The user-specific subtree menu hash has been updated.');
}
public function testBackToSiteLink() {
$this
->drupalGet('test-page');
$back_link = $this
->cssSelect('.home-toolbar-tab');
$this
->assertNotEmpty($back_link);
}
public function testExternalLink() {
$edit = [
'title[0][value]' => 'External URL',
'link[0][uri]' => 'http://example.org',
'menu_parent' => 'admin:system.admin',
'description[0][value]' => 'External URL & escaped',
];
$this
->drupalGet('admin/structure/menu/manage/admin/add');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('admin/structure/menu/manage/admin');
$this
->assertSession()
->pageTextContains('External URL');
$this
->drupalGet(Url::fromRoute('<front>'));
$this
->assertSession()
->pageTextContains('External URL');
$this
->assertSession()
->responseContains('title="External URL & escaped"');
}
private function getSubtreesHash() {
$settings = $this
->getDrupalSettings();
return $settings['toolbar']['subtreesHash'];
}
private function assertDifferentHash() : void {
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertNotEmpty($new_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
$this
->assertNotEquals($this->hash, $new_subtree_hash, 'The user-specific subtree menu hash has been updated.');
$this->hash = $new_subtree_hash;
}
}