Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
59
core/modules/toolbar/src/Controller/ToolbarController.php
Normal file
59
core/modules/toolbar/src/Controller/ToolbarController.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\Controller\ToolbarController.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\Controller;
|
||||
|
||||
use Drupal\Core\Access\AccessResult;
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
/**
|
||||
* Defines a controller for the toolbar module.
|
||||
*/
|
||||
class ToolbarController extends ControllerBase {
|
||||
|
||||
/**
|
||||
* Returns the rendered subtree of each top-level toolbar link.
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
||||
*/
|
||||
public function subtreesJsonp() {
|
||||
$subtrees = toolbar_get_rendered_subtrees();
|
||||
$response = new JsonResponse($subtrees);
|
||||
$response->setCallback('Drupal.toolbar.setSubtrees.resolve');
|
||||
|
||||
// The Expires HTTP header is the heart of the client-side HTTP caching. The
|
||||
// additional server-side page cache only takes effect when the client
|
||||
// accesses the callback URL again (e.g., after clearing the browser cache
|
||||
// or when force-reloading a Drupal page).
|
||||
$max_age = 365 * 24 * 60 * 60;
|
||||
$response->setPrivate();
|
||||
$response->setMaxAge($max_age);
|
||||
|
||||
$expires = new \DateTime();
|
||||
$expires->setTimestamp(REQUEST_TIME + $max_age);
|
||||
$response->setExpires($expires);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks access for the subtree controller.
|
||||
*
|
||||
* @param string $hash
|
||||
* The hash of the toolbar subtrees.
|
||||
* @param string $langcode
|
||||
* The langcode of the requested site, NULL if none given.
|
||||
*
|
||||
* @return \Drupal\Core\Access\AccessResultInterface
|
||||
* The access result.
|
||||
*/
|
||||
public function checkSubTreeAccess($hash, $langcode) {
|
||||
return AccessResult::allowedIf($this->currentUser()->hasPermission('access toolbar') && $hash == _toolbar_get_subtrees_hash($langcode))->cachePerPermissions();
|
||||
}
|
||||
|
||||
}
|
124
core/modules/toolbar/src/Element/Toolbar.php
Normal file
124
core/modules/toolbar/src/Element/Toolbar.php
Normal file
|
@ -0,0 +1,124 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\Element\Toolbar.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\Element;
|
||||
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Render\Element\RenderElement;
|
||||
use Drupal\Core\Render\Element;
|
||||
|
||||
/**
|
||||
* Provides a render element for the default Drupal toolbar.
|
||||
*
|
||||
* @RenderElement("toolbar")
|
||||
*/
|
||||
class Toolbar extends RenderElement {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getInfo() {
|
||||
$class = get_class($this);
|
||||
return array(
|
||||
'#pre_render' => array(
|
||||
array($class, 'preRenderToolbar'),
|
||||
),
|
||||
'#theme' => 'toolbar',
|
||||
'#attached' => array(
|
||||
'library' => array(
|
||||
'toolbar/toolbar',
|
||||
),
|
||||
),
|
||||
// Metadata for the toolbar wrapping element.
|
||||
'#attributes' => array(
|
||||
// The id cannot be simply "toolbar" or it will clash with the
|
||||
// simpletest tests listing which produces a checkbox with attribute
|
||||
// id="toolbar".
|
||||
'id' => 'toolbar-administration',
|
||||
'role' => 'group',
|
||||
'aria-label' => $this->t('Site administration toolbar'),
|
||||
),
|
||||
// Metadata for the administration bar.
|
||||
'#bar' => array(
|
||||
'#heading' => $this->t('Toolbar items'),
|
||||
'#attributes' => array(
|
||||
'id' => 'toolbar-bar',
|
||||
'role' => 'navigation',
|
||||
'aria-label' => $this->t('Toolbar items'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the Toolbar as a structured array ready for drupal_render().
|
||||
*
|
||||
* Since building the toolbar takes some time, it is done just prior to
|
||||
* rendering to ensure that it is built only if it will be displayed.
|
||||
*
|
||||
* @param array $element
|
||||
* A renderable array.
|
||||
*
|
||||
* @return array
|
||||
* A renderable array.
|
||||
*
|
||||
* @see toolbar_page_top().
|
||||
*/
|
||||
public static function preRenderToolbar($element) {
|
||||
// Get the configured breakpoints to switch from vertical to horizontal
|
||||
// toolbar presentation.
|
||||
$breakpoints = static::breakpointManager()->getBreakpointsByGroup('toolbar');
|
||||
if (!empty($breakpoints)) {
|
||||
$media_queries = array();
|
||||
foreach ($breakpoints as $id => $breakpoint) {
|
||||
$media_queries[$id] = $breakpoint->getMediaQuery();
|
||||
}
|
||||
|
||||
$element['#attached']['drupalSettings']['toolbar']['breakpoints'] = $media_queries;
|
||||
}
|
||||
|
||||
$module_handler = static::moduleHandler();
|
||||
// Get toolbar items from all modules that implement hook_toolbar().
|
||||
$items = $module_handler->invokeAll('toolbar');
|
||||
// Allow for altering of hook_toolbar().
|
||||
$module_handler->alter('toolbar', $items);
|
||||
// Sort the children.
|
||||
uasort($items, array('\Drupal\Component\Utility\SortArray', 'sortByWeightProperty'));
|
||||
|
||||
// Merge in the original toolbar values.
|
||||
$element = array_merge($element, $items);
|
||||
|
||||
// Assign each item a unique ID, based on its key.
|
||||
foreach (Element::children($element) as $key) {
|
||||
$element[$key]['#id'] = Html::getId('toolbar-item-' . $key);
|
||||
}
|
||||
|
||||
// Render the children.
|
||||
$element['#children'] = drupal_render_children($element);
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the breakpoint manager.
|
||||
*
|
||||
* @return \Drupal\breakpoint\BreakpointManagerInterface
|
||||
*/
|
||||
protected static function breakpointManager() {
|
||||
return \Drupal::service('breakpoint.manager');
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the module handler.
|
||||
*
|
||||
* @return \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected static function moduleHandler() {
|
||||
return \Drupal::moduleHandler();
|
||||
}
|
||||
|
||||
}
|
92
core/modules/toolbar/src/Element/ToolbarItem.php
Normal file
92
core/modules/toolbar/src/Element/ToolbarItem.php
Normal file
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\Element\ToolbarItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\Element;
|
||||
|
||||
use Drupal\Core\Render\Element\RenderElement;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Provides a toolbar item that is wrapped in markup for common styling.
|
||||
*
|
||||
* The 'tray' property contains a renderable array.
|
||||
*
|
||||
* @RenderElement("toolbar_item")
|
||||
*/
|
||||
class ToolbarItem extends RenderElement {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getInfo() {
|
||||
$class = get_class($this);
|
||||
return array(
|
||||
'#pre_render' => array(
|
||||
array($class, 'preRenderToolbarItem'),
|
||||
),
|
||||
'tab' => array(
|
||||
'#type' => 'link',
|
||||
'#title' => NULL,
|
||||
'#url' => Url::fromRoute('<front>'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides markup for associating a tray trigger with a tray element.
|
||||
*
|
||||
* A tray is a responsive container that wraps renderable content. Trays
|
||||
* present content well on small and large screens alike.
|
||||
*
|
||||
* @param array $element
|
||||
* A renderable array.
|
||||
*
|
||||
* @return array
|
||||
* A renderable array.
|
||||
*/
|
||||
public static function preRenderToolbarItem($element) {
|
||||
$id = $element['#id'];
|
||||
|
||||
// Provide attributes for a toolbar item.
|
||||
$attributes = array(
|
||||
'id' => $id,
|
||||
);
|
||||
|
||||
// If tray content is present, markup the tray and its associated trigger.
|
||||
if (!empty($element['tray'])) {
|
||||
// Provide attributes necessary for trays.
|
||||
$attributes += array(
|
||||
'data-toolbar-tray' => $id . '-tray',
|
||||
'aria-owns' => $id . '-tray',
|
||||
'role' => 'button',
|
||||
'aria-pressed' => 'false',
|
||||
);
|
||||
|
||||
// Merge in module-provided attributes.
|
||||
$element['tab'] += array('#attributes' => array());
|
||||
$element['tab']['#attributes'] += $attributes;
|
||||
$element['tab']['#attributes']['class'][] = 'trigger';
|
||||
|
||||
// Provide attributes for the tray theme wrapper.
|
||||
$attributes = array(
|
||||
'id' => $id . '-tray',
|
||||
'data-toolbar-tray' => $id . '-tray',
|
||||
);
|
||||
// Merge in module-provided attributes.
|
||||
if (!isset($element['tray']['#wrapper_attributes'])) {
|
||||
$element['tray']['#wrapper_attributes'] = array();
|
||||
}
|
||||
$element['tray']['#wrapper_attributes'] += $attributes;
|
||||
$element['tray']['#wrapper_attributes']['class'][] = 'toolbar-tray';
|
||||
}
|
||||
|
||||
$element['tab']['#attributes']['class'][] = 'toolbar-item';
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
}
|
41
core/modules/toolbar/src/Menu/ToolbarMenuLinkTree.php
Normal file
41
core/modules/toolbar/src/Menu/ToolbarMenuLinkTree.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\Menu\ToolbarMenuLinkTree.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\Menu;
|
||||
|
||||
use Drupal\Core\Menu\MenuLinkTree;
|
||||
|
||||
/**
|
||||
* Extends MenuLinkTree to add specific theme suggestions for the toolbar.
|
||||
*/
|
||||
class ToolbarMenuLinkTree extends MenuLinkTree {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build(array $tree, $level = 0) {
|
||||
if ($level == 0) {
|
||||
if (!$tree) {
|
||||
return array();
|
||||
}
|
||||
$build = parent::build($tree, $level);
|
||||
|
||||
/** @var \Drupal\Core\Menu\MenuLinkInterface $link */
|
||||
$first_link = reset($tree)->link;
|
||||
// Get the menu name of the first link.
|
||||
$menu_name = $first_link->getMenuName();
|
||||
// Add a more specific theme suggestion to differentiate this rendered
|
||||
// menu from others.
|
||||
$build['#theme'] = 'menu__toolbar__' . strtr($menu_name, '-', '_');
|
||||
return $build;
|
||||
}
|
||||
else {
|
||||
return parent::build($tree, $level);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
32
core/modules/toolbar/src/PageCache/AllowToolbarPath.php
Normal file
32
core/modules/toolbar/src/PageCache/AllowToolbarPath.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\PageCache\AllowToolbarPath.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\PageCache;
|
||||
|
||||
use Drupal\Core\PageCache\RequestPolicyInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Cache policy for the toolbar page cache service.
|
||||
*
|
||||
* This policy allows caching of requests directed to /toolbar/subtrees/{hash}
|
||||
* even for authenticated users.
|
||||
*/
|
||||
class AllowToolbarPath implements RequestPolicyInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(Request $request) {
|
||||
// Note that this regular expression matches the end of pathinfo in order to
|
||||
// support multilingual sites using path prefixes.
|
||||
if (preg_match('#/toolbar/subtrees/[^/]+(/[^/]+)?$#', $request->getPathInfo())) {
|
||||
return static::ALLOW;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
529
core/modules/toolbar/src/Tests/ToolbarAdminMenuTest.php
Normal file
529
core/modules/toolbar/src/Tests/ToolbarAdminMenuTest.php
Normal file
|
@ -0,0 +1,529 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\Tests\ToolbarAdminMenuTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\Tests;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests the caching of the admin menu subtree items.
|
||||
*
|
||||
* The cache of the admin menu subtree items will be invalidated if the
|
||||
* following hooks are invoked.
|
||||
*
|
||||
* toolbar_modules_enabled()
|
||||
* toolbar_modules_disabled()
|
||||
* toolbar_menu_link_update()
|
||||
* toolbar_user_update()
|
||||
* toolbar_user_role_update()
|
||||
*
|
||||
* Each hook invocation is simulated and then the previous hash of the admin
|
||||
* menu subtrees is compared to the new hash.
|
||||
*
|
||||
* @group toolbar
|
||||
*/
|
||||
class ToolbarAdminMenuTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* A user with permission to access the administrative toolbar.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* A second user with permission to access the administrative toolbar.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser2;
|
||||
|
||||
/**
|
||||
* The current admin menu subtrees hash for adminUser.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $hash;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
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',
|
||||
);
|
||||
|
||||
// Create an administrative user and log it in.
|
||||
$this->adminUser = $this->drupalCreateUser($perms);
|
||||
$this->adminUser2 = $this->drupalCreateUser($perms);
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Assert that the toolbar is present in the HTML.
|
||||
$this->assertRaw('id="toolbar-administration"');
|
||||
|
||||
// Store the adminUser admin menu subtrees hash for comparison later.
|
||||
$this->hash = $this->getSubtreesHash();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the toolbar_modules_installed() and toolbar_modules_uninstalled() hook
|
||||
* implementations.
|
||||
*/
|
||||
function testModuleStatusChangeSubtreesHashCacheClear() {
|
||||
// Uninstall a module.
|
||||
$edit = array();
|
||||
$edit['uninstall[taxonomy]'] = TRUE;
|
||||
$this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
|
||||
// Confirm the uninstall form.
|
||||
$this->drupalPostForm(NULL, array(), t('Uninstall'));
|
||||
$this->rebuildContainer();
|
||||
|
||||
// Assert that the subtrees hash has been altered because the subtrees
|
||||
// structure changed.
|
||||
$this->assertDifferentHash();
|
||||
|
||||
// Enable a module.
|
||||
$edit = array();
|
||||
$edit['modules[Core][taxonomy][enable]'] = TRUE;
|
||||
$this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
|
||||
$this->rebuildContainer();
|
||||
|
||||
// Assert that the subtrees hash has been altered because the subtrees
|
||||
// structure changed.
|
||||
$this->assertDifferentHash();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests toolbar cache tags implementation.
|
||||
*/
|
||||
function testMenuLinkUpdateSubtreesHashCacheClear() {
|
||||
// The ID of a (any) admin menu link.
|
||||
$admin_menu_link_id = 'system.admin_config_development';
|
||||
|
||||
// Disable the link.
|
||||
$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.');
|
||||
|
||||
// Assert that the subtrees hash has been altered because the subtrees
|
||||
// structure changed.
|
||||
$this->assertDifferentHash();
|
||||
}
|
||||
|
||||
/**
|
||||
* Exercises the toolbar_user_role_update() and toolbar_user_update() hook
|
||||
* implementations.
|
||||
*/
|
||||
function testUserRoleUpdateSubtreesHashCacheClear() {
|
||||
// Find the new role ID.
|
||||
$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'));
|
||||
|
||||
// Assert that the subtrees hash has been altered because the subtrees
|
||||
// structure changed.
|
||||
$this->assertDifferentHash();
|
||||
|
||||
// Test that assigning a user an extra role only affects that single user.
|
||||
// Get the hash for a second user.
|
||||
$this->drupalLogin($this->adminUser2);
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Assert that the toolbar is present in the HTML.
|
||||
$this->assertRaw('id="toolbar-administration"');
|
||||
|
||||
$admin_user_2_hash = $this->getSubtreesHash();
|
||||
|
||||
// Log in the first admin user again.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Assert that the toolbar is present in the HTML.
|
||||
$this->assertRaw('id="toolbar-administration"');
|
||||
|
||||
$this->hash = $this->getSubtreesHash();
|
||||
|
||||
$rid = $this->drupalCreateRole(array('administer content types',));
|
||||
|
||||
// Assign the role to the user.
|
||||
$this->drupalPostForm('user/' . $this->adminUser->id() . '/edit', array("roles[$rid]" => $rid), t('Save'));
|
||||
$this->assertText(t('The changes have been saved.'));
|
||||
|
||||
// Assert that the subtrees hash has been altered because the subtrees
|
||||
// structure changed.
|
||||
$this->assertDifferentHash();
|
||||
|
||||
// Log in the second user again and assert that their subtrees hash did not
|
||||
// change.
|
||||
$this->drupalLogin($this->adminUser2);
|
||||
|
||||
// Request a new page to refresh the drupalSettings object.
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
$new_subtree_hash = $this->getSubtreesHash();
|
||||
|
||||
// Assert that the old admin menu subtree hash and the new admin menu
|
||||
// subtree hash are the same.
|
||||
$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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that all toolbar cache entries for a user are cleared with a cache
|
||||
* tag for that user, i.e. cache entries for all languages for that user.
|
||||
*/
|
||||
function testCacheClearByCacheTag() {
|
||||
// Test that the toolbar admin menu subtrees cache is invalidated for a user
|
||||
// across multiple languages.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$toolbarCache = $this->container->get('cache.toolbar');
|
||||
$admin_user_id = $this->adminUser->id();
|
||||
$admin_user_2_id = $this->adminUser2->id();
|
||||
|
||||
// Assert that a cache tag in the toolbar cache under the key "user" exists
|
||||
// for adminUser against the language "en".
|
||||
$cache = $toolbarCache->get('toolbar_' . $admin_user_id . ':' . 'en');
|
||||
$this->assertEqual(in_array('user:' . $admin_user_id, $cache->tags), 'A cache tag in the toolbar cache under the key "user" exists for admin_user against the language "en".');
|
||||
|
||||
// Assert that no toolbar cache exists for adminUser against the
|
||||
// language "fr".
|
||||
$cache = $toolbarCache->get('toolbar_' . $admin_user_id . ':' . 'fr');
|
||||
$this->assertFalse($cache, 'No toolbar cache exists for admin_user against the language "fr".');
|
||||
|
||||
// Install a second language.
|
||||
$edit = array(
|
||||
'predefined_langcode' => 'fr',
|
||||
);
|
||||
$this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
|
||||
|
||||
// Request a page in 'fr' to update the cache.
|
||||
$this->drupalGet('fr/test-page');
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Assert that a cache tag in the toolbar cache under the key "user" exists
|
||||
// for adminUser against the language "fr".
|
||||
$cache = $toolbarCache->get('toolbar_' . $admin_user_id . ':' . 'fr');
|
||||
$this->assertEqual(in_array('user:' . $admin_user_id, $cache->tags), 'A cache tag in the toolbar cache under the key "user" exists for admin_user against the language "fr".');
|
||||
|
||||
// Log in the adminUser2 user. We will use this user as a control to
|
||||
// verify that clearing a cache tag for adminUser does not clear the cache
|
||||
// for adminUser2.
|
||||
$this->drupalLogin($this->adminUser2);
|
||||
|
||||
// Request a page in 'en' to create the cache.
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
// Assert that a cache tag in the toolbar cache under the key "user" exists
|
||||
// for adminUser2 against the language "en".
|
||||
$cache = $toolbarCache->get('toolbar_' . $admin_user_2_id . ':' . 'en');
|
||||
$this->assertEqual(in_array('user:' . $admin_user_2_id, $cache->tags), 'A cache tag in the toolbar cache under the key "user" exists for admin_user_2 against the language "en".');
|
||||
|
||||
// Request a page in 'fr' to create the cache.
|
||||
$this->drupalGet('fr/test-page');
|
||||
$this->assertResponse(200);
|
||||
// Assert that a cache tag in the toolbar cache under the key "user" exists
|
||||
// for adminUser against the language "fr".
|
||||
$cache = $toolbarCache->get('toolbar_' . $admin_user_2_id . ':' . 'fr');
|
||||
$this->assertEqual(in_array('user:' . $admin_user_2_id, $cache->tags), 'A cache tag in the toolbar cache under the key "user" exists for admin_user_2 against the language "fr".');
|
||||
|
||||
// Log in the admin user and clear the caches for this user using a tag.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
Cache::invalidateTags(array('user:' . $admin_user_id));
|
||||
|
||||
// Assert that no toolbar cache exists for adminUser against the
|
||||
// language "en".
|
||||
$cache = $toolbarCache->get($admin_user_id . ':' . 'en');
|
||||
$this->assertFalse($cache, 'No toolbar cache exists for admin_user against the language "en".');
|
||||
|
||||
// Assert that no toolbar cache exists for adminUser against the
|
||||
// language "fr".
|
||||
$cache = $toolbarCache->get($admin_user_id . ':' . 'fr');
|
||||
$this->assertFalse($cache, 'No toolbar cache exists for admin_user against the language "fr".');
|
||||
|
||||
// Log in adminUser2 and verify that this user's caches still exist.
|
||||
$this->drupalLogin($this->adminUser2);
|
||||
|
||||
// Assert that a cache tag in the toolbar cache under the key "user" exists
|
||||
// for adminUser2 against the language "en".
|
||||
$cache = $toolbarCache->get('toolbar_' . $admin_user_2_id . ':' . 'en');
|
||||
$this->assertEqual(in_array('user:' . $admin_user_2_id, $cache->tags), 'A cache tag in the toolbar cache under the key "user" exists for admin_user_2 against the language "en".');
|
||||
|
||||
// Assert that a cache tag in the toolbar cache under the key "user" exists
|
||||
// for adminUser2 against the language "fr".
|
||||
$cache = $toolbarCache->get('toolbar_' . $admin_user_2_id . ':' . 'fr');
|
||||
$this->assertEqual(in_array('user:' . $admin_user_2_id, $cache->tags), 'A cache tag in the toolbar cache under the key "user" exists for admin_user_2 against the language "fr".');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that changes to a user account by another user clears the changed
|
||||
* account's toolbar cached, not the user's who took the action.
|
||||
*/
|
||||
function testNonCurrentUserAccountUpdates() {
|
||||
$toolbarCache = $this->container->get('cache.toolbar');
|
||||
$admin_user_id = $this->adminUser->id();
|
||||
$admin_user_2_id = $this->adminUser2->id();
|
||||
$this->hash = $this->getSubtreesHash();
|
||||
|
||||
// adminUser2 will add a role to adminUser.
|
||||
$this->drupalLogin($this->adminUser2);
|
||||
$rid = $this->drupalCreateRole(array('administer content types',));
|
||||
|
||||
// Get the subtree hash for adminUser2 to check later that it has not
|
||||
// changed. Request a new page to refresh the drupalSettings object.
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
$admin_user_2_hash = $this->getSubtreesHash();
|
||||
|
||||
// Assign the role to the user.
|
||||
$this->drupalPostForm('user/' . $admin_user_id . '/edit', array("roles[$rid]" => $rid), t('Save'));
|
||||
$this->assertText(t('The changes have been saved.'));
|
||||
|
||||
// Log in adminUser and assert that the subtrees hash has changed.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->assertDifferentHash();
|
||||
|
||||
// Log in adminUser2 to check that its subtrees hash has not changed.
|
||||
$this->drupalLogin($this->adminUser2);
|
||||
$new_subtree_hash = $this->getSubtreesHash();
|
||||
|
||||
// Assert that the old adminUser subtree hash and the new adminUser
|
||||
// subtree hash are the same.
|
||||
$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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that toolbar cache is cleared when string translations are made.
|
||||
*/
|
||||
function testLocaleTranslationSubtreesHashCacheClear() {
|
||||
$toolbarCache = $this->container->get('cache.toolbar');
|
||||
$admin_user = $this->adminUser;
|
||||
$admin_user_id = $this->adminUser->id();
|
||||
// User to translate and delete string.
|
||||
$translate_user = $this->drupalCreateUser(array('translate interface', 'access administration pages'));
|
||||
|
||||
// Create a new language with the langcode 'xx'.
|
||||
$langcode = 'xx';
|
||||
// The English name for the language. This will be translated.
|
||||
$name = $this->randomMachineName(16);
|
||||
// This is the language indicator on the translation search screen for
|
||||
// untranslated strings.
|
||||
$language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
|
||||
// This will be the translation of $name.
|
||||
$translation = $this->randomMachineName(16);
|
||||
$translation_to_en = $this->randomMachineName(16);
|
||||
|
||||
// Add custom language.
|
||||
$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));
|
||||
// Reset locale cache.
|
||||
$this->container->get('string_translation')->reset();
|
||||
$this->assertRaw('"edit-languages-' . $langcode .'-weight"', 'Language code found.');
|
||||
$this->assertText(t($name), 'Test language added.');
|
||||
|
||||
// Have the adminUser request a page in the new language.
|
||||
$this->drupalGet($langcode . '/test-page');
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Assert that a cache tag in the toolbar cache under the key "user" exists
|
||||
// for adminUser against the language "xx".
|
||||
$cache = $toolbarCache->get('toolbar_' . $admin_user_id . ':' . $langcode);
|
||||
$this->assertEqual(in_array('user:' . $admin_user_id, $cache->tags), 'A cache tag in the toolbar cache under the key "user" exists for admin_user against the language "xx".');
|
||||
|
||||
// Get a baseline hash for the admin menu subtrees before translating one
|
||||
// of the menu link items.
|
||||
$original_subtree_hash = $this->getSubtreesHash();
|
||||
$this->assertTrue($original_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Translate the string 'Menus' in the xx language. This string appears in
|
||||
// a link in the admin menu subtrees. Changing the string should create a
|
||||
// new menu hash if the toolbar subtrees cache is properly cleared.
|
||||
$this->drupalLogin($translate_user);
|
||||
$search = array(
|
||||
'string' => 'Menus',
|
||||
'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.');
|
||||
|
||||
// Assume this is the only result.
|
||||
// Translate the string to a random string.
|
||||
$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();
|
||||
|
||||
// Log in the adminUser. Check the admin menu subtrees hash now that one
|
||||
// of the link items in the Structure tree (Menus) has had its text
|
||||
// translated.
|
||||
$this->drupalLogin($admin_user);
|
||||
// Have the adminUser request a page in the new language.
|
||||
$this->drupalGet($langcode . '/test-page');
|
||||
$this->assertResponse(200);
|
||||
$new_subtree_hash = $this->getSubtreesHash();
|
||||
|
||||
// Assert that the old admin menu subtrees hash and the new admin menu
|
||||
// subtrees hash are different.
|
||||
$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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the 'toolbar/subtrees/{hash}' is reachable.
|
||||
*/
|
||||
function testSubtreesJsonRequest() {
|
||||
$admin_user = $this->adminUser;
|
||||
$this->drupalLogin($admin_user);
|
||||
// Request a new page to refresh the drupalSettings object.
|
||||
$subtrees_hash = $this->getSubtreesHash();
|
||||
|
||||
$this->drupalGetJSON('toolbar/subtrees/' . $subtrees_hash);
|
||||
$this->assertResponse('200');
|
||||
|
||||
// Test that the subtrees hash changes with a different language code and
|
||||
// that JSON is returned when a language code is specified.
|
||||
// Create a new language with the langcode 'xx'.
|
||||
$langcode = 'xx';
|
||||
// The English name for the language. This will be translated.
|
||||
$name = $this->randomMachineName(16);
|
||||
$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'));
|
||||
|
||||
// Get a page with the new language langcode in the URL.
|
||||
$this->drupalGet('xx/test-page');
|
||||
// Request a new page to refresh the drupalSettings object.
|
||||
$subtrees_hash = $this->getSubtreesHash();
|
||||
|
||||
$this->drupalGetJSON('toolbar/subtrees/' . $subtrees_hash . '/' . $langcode);
|
||||
$this->assertResponse('200');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that subtrees hashes vary by the language of the page.
|
||||
*/
|
||||
function testLanguageSwitching() {
|
||||
// Create a new language with the langcode 'xx'.
|
||||
$langcode = 'xx';
|
||||
$language = ConfigurableLanguage::createFromLangcode($langcode);
|
||||
$language->save();
|
||||
// The language path processor is just registered for more than one
|
||||
// configured language, so rebuild the container now that we are
|
||||
// multilingual.
|
||||
$this->rebuildContainer();
|
||||
|
||||
// Get a page with the new language langcode in the URL.
|
||||
$this->drupalGet('test-page', array('language' => $language));
|
||||
// Assert different hash.
|
||||
$new_subtree_hash = $this->getSubtreesHash();
|
||||
|
||||
// Assert that the old admin menu subtree hash and the new admin menu
|
||||
// subtree hash are different.
|
||||
$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.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that back to site link exists on admin pages, not on content pages.
|
||||
*/
|
||||
public function testBackToSiteLink() {
|
||||
// Back to site link should exist in the markup.
|
||||
$this->drupalGet('test-page');
|
||||
$back_link = $this->cssSelect('.home-toolbar-tab');
|
||||
$this->assertTrue($back_link);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hash value from the admin menu subtrees route path.
|
||||
*
|
||||
* @return string
|
||||
* The hash value from the admin menu subtrees route path.
|
||||
*/
|
||||
private function getSubtreesHash() {
|
||||
$settings = $this->getDrupalSettings();
|
||||
// The toolbar module defines a route '/toolbar/subtrees/{hash}' that
|
||||
// returns JSON for the rendered subtrees. This hash is provided to the
|
||||
// client in drupalSettings.
|
||||
return $settings['toolbar']['subtreesHash'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts the subtrees hash on a fresh page GET is different from the hash
|
||||
* from the previous page GET.
|
||||
*/
|
||||
private function assertDifferentHash() {
|
||||
// Request a new page to refresh the drupalSettings object.
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
$new_subtree_hash = $this->getSubtreesHash();
|
||||
|
||||
// Assert that the old admin menu subtree hash and the new admin menu
|
||||
// subtree hash are different.
|
||||
$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.');
|
||||
|
||||
// Save the new subtree hash as the original.
|
||||
$this->hash = $new_subtree_hash;
|
||||
}
|
||||
|
||||
}
|
150
core/modules/toolbar/src/Tests/ToolbarCacheContextsTest.php
Normal file
150
core/modules/toolbar/src/Tests/ToolbarCacheContextsTest.php
Normal file
|
@ -0,0 +1,150 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\Tests\ToolbarCacheContextsTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\Tests;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
|
||||
|
||||
|
||||
/**
|
||||
* Tests the cache contexts for toolbar.
|
||||
*
|
||||
* @group toolbar
|
||||
*/
|
||||
class ToolbarCacheContextsTest extends WebTestBase {
|
||||
|
||||
use AssertPageCacheContextsAndTagsTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['toolbar', 'test_page_test'];
|
||||
|
||||
/**
|
||||
* An authenticated user to use for testing.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* An authenticated user to use for testing.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser2;
|
||||
|
||||
/**
|
||||
* A list of default permissions for test users.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $perms = [
|
||||
'access toolbar',
|
||||
'access administration pages',
|
||||
'administer site configuration',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->adminUser = $this->drupalCreateUser($this->perms);
|
||||
$this->adminUser2 = $this->drupalCreateUser($this->perms);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests toolbar cache contexts.
|
||||
*/
|
||||
public function testToolbarCacheContextsCaller() {
|
||||
// Test with default combination and permission to see toolbar.
|
||||
$this->assertToolbarCacheContexts(['user'], 'Expected cache contexts found for default combination and permission to see toolbar.');
|
||||
|
||||
// Test without user toolbar tab. User module is a required module so we have to
|
||||
// manually remove the user toolbar tab.
|
||||
$this->installExtraModules(['toolbar_disable_user_toolbar']);
|
||||
$this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found without user toolbar tab.');
|
||||
|
||||
// Test with the toolbar and contextual enabled.
|
||||
$this->installExtraModules(['contextual']);
|
||||
$this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access contextual links']));
|
||||
$this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with contextual module enabled.');
|
||||
\Drupal::service('module_installer')->uninstall(['contextual']);
|
||||
|
||||
// Test with the tour module enabled.
|
||||
$this->installExtraModules(['tour']);
|
||||
$this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access tour']));
|
||||
$this->assertToolbarCacheContexts(['user.permissions'], 'Expected cache contexts found with tour module enabled.');
|
||||
\Drupal::service('module_installer')->uninstall(['tour']);
|
||||
|
||||
// Test with shortcut module enabled.
|
||||
$this->installExtraModules(['shortcut']);
|
||||
$this->adminUser2 = $this->drupalCreateUser(array_merge($this->perms, ['access shortcuts', 'administer shortcuts']));
|
||||
$this->assertToolbarCacheContexts(['user'], 'Expected cache contexts found with shortcut module enabled.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that cache contexts are applied for both users.
|
||||
*
|
||||
* @param string[] $cache_contexts
|
||||
* Expected cache contexts for both users.
|
||||
* @param string $message
|
||||
* (optional) A verbose message to output.
|
||||
*
|
||||
* @return
|
||||
* TRUE if the assertion succeeded, FALSE otherwise.
|
||||
*/
|
||||
protected function assertToolbarCacheContexts(array $cache_contexts, $message = NULL) {
|
||||
// Default cache contexts that should exist on all test cases.
|
||||
$default_cache_contexts = [
|
||||
'languages:language_interface',
|
||||
'theme',
|
||||
];
|
||||
$cache_contexts = Cache::mergeContexts($default_cache_contexts, $cache_contexts);
|
||||
|
||||
// Assert contexts for user1 which has only default permissions.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('test-page');
|
||||
$return = $this->assertCacheContexts($cache_contexts);
|
||||
$this->drupalLogout();
|
||||
|
||||
// Assert contexts for user2 which has some additional permissions.
|
||||
$this->drupalLogin($this->adminUser2);
|
||||
$this->drupalGet('test-page');
|
||||
$return = $return && $this->assertCacheContexts($cache_contexts);
|
||||
|
||||
if ($return) {
|
||||
$this->pass($message);
|
||||
}
|
||||
else {
|
||||
$this->fail($message);
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs a given list of modules and rebuilds the cache.
|
||||
*
|
||||
* @param string[] $module_list
|
||||
* An array of module names.
|
||||
*/
|
||||
protected function installExtraModules(array $module_list) {
|
||||
\Drupal::service('module_installer')->install($module_list);
|
||||
|
||||
// Installing modules updates the container and needs a router rebuild.
|
||||
$this->container = \Drupal::getContainer();
|
||||
$this->container->get('router.builder')->rebuildIfNeeded();
|
||||
}
|
||||
|
||||
|
||||
}
|
64
core/modules/toolbar/src/Tests/ToolbarHookToolbarTest.php
Normal file
64
core/modules/toolbar/src/Tests/ToolbarHookToolbarTest.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\Tests\ToolbarHookToolbarTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests the implementation of hook_toolbar() by a module.
|
||||
*
|
||||
* @group toolbar
|
||||
*/
|
||||
class ToolbarHookToolbarTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* A user with permission to access the administrative toolbar.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('toolbar', 'toolbar_test', 'test_page_test');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create an administrative user and log it in.
|
||||
$this->adminUser = $this->drupalCreateUser(array('access toolbar'));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for a tab and tray provided by a module implementing hook_toolbar().
|
||||
*/
|
||||
function testHookToolbar() {
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertResponse(200);
|
||||
|
||||
// Assert that the toolbar is present in the HTML.
|
||||
$this->assertRaw('id="toolbar-administration"');
|
||||
|
||||
// Assert that the tab registered by toolbar_test is present.
|
||||
$this->assertRaw('id="toolbar-tab-testing"');
|
||||
|
||||
// Assert that the tab item descriptions are present.
|
||||
$this->assertRaw('title="Test tab"');
|
||||
|
||||
// Assert that the tray registered by toolbar_test is present.
|
||||
$this->assertRaw('id="toolbar-tray-testing"');
|
||||
|
||||
// Assert that tray item descriptions are present.
|
||||
$this->assertRaw('title="Test link 1 title"');
|
||||
}
|
||||
|
||||
}
|
101
core/modules/toolbar/src/Tests/ToolbarMenuTranslationTest.php
Normal file
101
core/modules/toolbar/src/Tests/ToolbarMenuTranslationTest.php
Normal file
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\toolbar\Tests\ToolbarMenuTranslationTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\toolbar\Tests;
|
||||
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests that the toolbar icon class remains for translated menu items.
|
||||
*
|
||||
* @group toolbar
|
||||
*/
|
||||
class ToolbarMenuTranslationTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* A user with permission to access the administrative toolbar.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('toolbar', 'toolbar_test', 'locale');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create an administrative user and log it in.
|
||||
$this->adminUser = $this->drupalCreateUser(array('access toolbar', 'translate interface', 'administer languages', 'access administration pages'));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that toolbar classes don't change when adding a translation.
|
||||
*/
|
||||
function testToolbarClasses() {
|
||||
$langcode = 'es';
|
||||
|
||||
// Add Spanish.
|
||||
$edit['predefined_langcode'] = $langcode;
|
||||
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
|
||||
|
||||
// The menu item 'Structure' in the toolbar will be translated.
|
||||
$menu_item = 'Structure';
|
||||
|
||||
// Visit a page that has the string on it so it can be translated.
|
||||
$this->drupalGet($langcode . '/admin/structure');
|
||||
|
||||
// Search for the menu item.
|
||||
$search = array(
|
||||
'string' => $menu_item,
|
||||
'langcode' => $langcode,
|
||||
'translation' => 'untranslated',
|
||||
);
|
||||
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
|
||||
// Make sure will be able to translate the menu item.
|
||||
$this->assertNoText('No strings available.', 'Search found the menu item as untranslated.');
|
||||
|
||||
// Check that the class is on the item before we translate it.
|
||||
$xpath = $this->xpath('//a[contains(@class, "icon-system-admin-structure")]');
|
||||
$this->assertEqual(count($xpath), 1, 'The menu item class ok before translation.');
|
||||
|
||||
// Translate the menu item.
|
||||
$menu_item_translated = $this->randomMachineName();
|
||||
$textarea = current($this->xpath('//textarea'));
|
||||
$lid = (string) $textarea[0]['name'];
|
||||
$edit = array(
|
||||
$lid => $menu_item_translated,
|
||||
);
|
||||
$this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
|
||||
|
||||
// Search for the translated menu item.
|
||||
$search = array(
|
||||
'string' => $menu_item,
|
||||
'langcode' => $langcode,
|
||||
'translation' => 'translated',
|
||||
);
|
||||
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
|
||||
// Make sure the menu item string was translated.
|
||||
$this->assertText($menu_item_translated, 'Search found the menu item as translated: ' . $menu_item_translated . '.');
|
||||
|
||||
// Go to another page in the custom language and make sure the menu item
|
||||
// was translated.
|
||||
$this->drupalGet($langcode . '/admin/structure');
|
||||
$this->assertText($menu_item_translated, 'Found the menu translated.');
|
||||
|
||||
// Toolbar icons are included based on the presence of a specific class on
|
||||
// the menu item. Ensure that class also exists for a translated menu item.
|
||||
$xpath = $this->xpath('//a[contains(@class, "icon-system-admin-structure")]');
|
||||
$this->assertEqual(count($xpath), 1, 'The menu item class is the same.');
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue