View source
<?php
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\RoleInterface;
class ToolbarAdminMenuTest extends BrowserTestBase {
protected $adminUser;
protected $adminUser2;
protected $hash;
public static $modules = [
'node',
'block',
'menu_ui',
'user',
'taxonomy',
'toolbar',
'language',
'test_page_test',
'locale',
];
protected $defaultTheme = 'stark';
protected function setUp() {
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',
];
$this->adminUser = $this
->drupalCreateUser($perms);
$this->adminUser2 = $this
->drupalCreateUser($perms);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertRaw('id="toolbar-administration"');
$this->hash = $this
->getSubtreesHash();
}
public function testModuleStatusChangeSubtreesHashCacheClear() {
$edit = [];
$edit['uninstall[taxonomy]'] = TRUE;
$this
->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
$this
->drupalPostForm(NULL, [], t('Uninstall'));
$this
->rebuildContainer();
$this
->assertDifferentHash();
$edit = [];
$edit['modules[taxonomy][enable]'] = TRUE;
$this
->drupalPostForm('admin/modules', $edit, t('Install'));
$this
->rebuildContainer();
$this
->assertDifferentHash();
}
public function testMenuLinkUpdateSubtreesHashCacheClear() {
$admin_menu_link_id = 'system.admin_config_development';
$edit = [];
$edit['enabled'] = FALSE;
$this
->drupalPostForm("admin/structure/menu/link/" . $admin_menu_link_id . "/edit", $edit, t('Save'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertText('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
->drupalPostForm('admin/people/permissions', $edit, t('Save permissions'));
$this
->assertDifferentHash();
$this
->drupalLogin($this->adminUser2);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertRaw('id="toolbar-administration"');
$admin_user_2_hash = $this
->getSubtreesHash();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('test-page');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertRaw('id="toolbar-administration"');
$this->hash = $this
->getSubtreesHash();
$rid = $this
->drupalCreateRole([
'administer content types',
]);
$this
->drupalPostForm('user/' . $this->adminUser
->id() . '/edit', [
"roles[{$rid}]" => $rid,
], t('Save'));
$this
->assertText(t('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
->assertEqual($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
->drupalPostForm('user/' . $admin_user_id . '/edit', [
"roles[{$rid}]" => $rid,
], t('Save'));
$this
->assertText(t('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
->assertEqual($admin_user_2_hash, $new_subtree_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
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
t($name, [], [
'langcode' => $langcode,
]);
$this->container
->get('string_translation')
->reset();
$this
->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
$this
->assertText(t($name), 'Test language added.');
$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);
$search = [
'string' => 'Search and metadata',
'langcode' => $langcode,
'translation' => 'untranslated',
];
$this
->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$this
->assertNoText(t('No strings available'));
$this
->assertText($name, 'Search found the string as untranslated.');
$textarea = current($this
->xpath('//textarea'));
$lid = (string) $textarea
->getAttribute('name');
$edit = [
$lid => $translation,
];
$this
->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
$this
->assertText(t('The strings have been saved.'), 'The strings have been saved.');
$this
->assertUrl(Url::fromRoute('locale.translate_page', [], [
'absolute' => TRUE,
])
->toString(), [], 'Correct page redirection.');
$this
->drupalLogout();
$this
->drupalLogin($admin_user);
$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
->assertNotEqual($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
->assertEqual($ajax_result[0]['command'], 'setToolbarSubtrees', 'Subtrees response uses the correct command.');
$this
->assertEqual(array_keys($ajax_result[0]['subtrees']), [
'system-admin_content',
'system-admin_structure',
'system-themes_page',
'system-modules_list',
'system-admin_config',
'entity-user-collection',
'front',
], '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
->assertNotEqual($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
->drupalPostForm('admin/structure/menu/manage/admin/add', $edit, 'Save');
$this
->drupalGet('admin/structure/menu/manage/admin');
$this
->assertText('External URL');
$this
->drupalGet(Url::fromRoute('<front>'));
$this
->assertText('External URL');
$this
->assertRaw('title="External URL & escaped"');
}
private function getSubtreesHash() {
$settings = $this
->getDrupalSettings();
return $settings['toolbar']['subtreesHash'];
}
private function assertDifferentHash() {
$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
->assertNotEqual($this->hash, $new_subtree_hash, 'The user-specific subtree menu hash has been updated.');
$this->hash = $new_subtree_hash;
}
}