View source
<?php
namespace Drupal\toolbar\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
use Drupal\user\RoleInterface;
class ToolbarAdminMenuTest extends WebTestBase {
protected $adminUser;
protected $adminUser2;
protected $hash;
public static $modules = array(
'node',
'block',
'menu_ui',
'user',
'taxonomy',
'toolbar',
'language',
'test_page_test',
'locale',
);
protected function setUp() {
parent::setUp();
$perms = array(
'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
->assertResponse(200);
$this
->assertRaw('id="toolbar-administration"');
$this->hash = $this
->getSubtreesHash();
}
function testModuleStatusChangeSubtreesHashCacheClear() {
$edit = array();
$edit['uninstall[taxonomy]'] = TRUE;
$this
->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
$this
->drupalPostForm(NULL, array(), t('Uninstall'));
$this
->rebuildContainer();
$this
->assertDifferentHash();
$edit = array();
$edit['modules[Core][taxonomy][enable]'] = TRUE;
$this
->drupalPostForm('admin/modules', $edit, t('Install'));
$this
->rebuildContainer();
$this
->assertDifferentHash();
}
function testMenuLinkUpdateSubtreesHashCacheClear() {
$admin_menu_link_id = 'system.admin_config_development';
$edit = array();
$edit['enabled'] = FALSE;
$this
->drupalPostForm("admin/structure/menu/link/" . $admin_menu_link_id . "/edit", $edit, t('Save'));
$this
->assertResponse(200);
$this
->assertText('The menu link has been saved.');
$this
->assertDifferentHash();
}
function testUserRoleUpdateSubtreesHashCacheClear() {
$all_rids = $this->adminUser
->getRoles();
unset($all_rids[array_search(RoleInterface::AUTHENTICATED_ID, $all_rids)]);
$rid = reset($all_rids);
$edit = array();
$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
->assertResponse(200);
$this
->assertRaw('id="toolbar-administration"');
$admin_user_2_hash = $this
->getSubtreesHash();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('test-page');
$this
->assertResponse(200);
$this
->assertRaw('id="toolbar-administration"');
$this->hash = $this
->getSubtreesHash();
$rid = $this
->drupalCreateRole(array(
'administer content types',
));
$this
->drupalPostForm('user/' . $this->adminUser
->id() . '/edit', array(
"roles[{$rid}]" => $rid,
), t('Save'));
$this
->assertText(t('The changes have been saved.'));
$this
->assertDifferentHash();
$this
->drupalLogin($this->adminUser2);
$this
->drupalGet('test-page');
$this
->assertResponse(200);
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertTrue($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.');
}
function testNonCurrentUserAccountUpdates() {
$admin_user_id = $this->adminUser
->id();
$this->hash = $this
->getSubtreesHash();
$this
->drupalLogin($this->adminUser2);
$rid = $this
->drupalCreateRole(array(
'administer content types',
));
$this
->drupalGet('test-page');
$this
->assertResponse(200);
$admin_user_2_hash = $this
->getSubtreesHash();
$this
->drupalPostForm('user/' . $admin_user_id . '/edit', array(
"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
->assertTrue($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.');
}
function testLocaleTranslationSubtreesHashCacheClear() {
$admin_user = $this->adminUser;
$translate_user = $this
->drupalCreateUser(array(
'translate interface',
'access administration pages',
));
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$translation = $this
->randomMachineName(16);
$this
->drupalLogin($admin_user);
$edit = array(
'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, array(), array(
'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
->assertResponse(200);
$original_subtree_hash = $this
->getSubtreesHash();
$this
->assertTrue($original_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
$this
->drupalLogout();
$this
->drupalLogin($translate_user);
$search = array(
'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[0]['name'];
$edit = array(
$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(\Drupal::url('locale.translate_page', [], [
'absolute' => TRUE,
]), [], 'Correct page redirection.');
$this
->drupalLogout();
$this
->drupalLogin($admin_user);
$this
->drupalGet($langcode . '/test-page');
$this
->assertResponse(200);
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertTrue($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.');
}
function testSubtreesJsonRequest() {
$admin_user = $this->adminUser;
$this
->drupalLogin($admin_user);
$subtrees_hash = $this
->getSubtreesHash();
$ajax_result = $this
->drupalGetAjax('toolbar/subtrees/' . $subtrees_hash);
$this
->assertResponse('200');
$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.');
}
function testLanguageSwitching() {
$langcode = 'xx';
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language
->save();
$this
->rebuildContainer();
$this
->drupalGet('test-page', array(
'language' => $language,
));
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertTrue($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
->assertTrue($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
->assertResponse(200);
$new_subtree_hash = $this
->getSubtreesHash();
$this
->assertTrue($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;
}
}