ToolbarAdminMenuTest.php 20.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\toolbar\Tests\ToolbarAdminMenuTest.
 */

namespace Drupal\toolbar\Tests;

10
use Drupal\Core\Cache\Cache;
11
use Drupal\Core\Language\LanguageInterface;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use Drupal\simpletest\WebTestBase;

/**
 * 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.
28 29
 *
 * @group toolbar
30 31 32 33 34 35
 */
class ToolbarAdminMenuTest extends WebTestBase {

  /**
   * A user with permission to access the administrative toolbar.
   *
36
   * @var \Drupal\user\UserInterface
37
   */
38
  protected $adminUser;
39 40 41 42

  /**
   * A second user with permission to access the administrative toolbar.
   *
43
   * @var \Drupal\user\UserInterface
44
   */
45
  protected $adminUser2;
46 47

  /**
48
   * The current admin menu subtrees hash for adminUser.
49 50 51 52 53 54 55 56 57 58
   *
   * @var string
   */
  protected $hash;

  /**
   * Modules to enable.
   *
   * @var array
   */
59
  public static $modules = array('node', 'block', 'menu_ui', 'user', 'taxonomy', 'toolbar', 'language', 'test_page_test', 'locale');
60

61
  protected function setUp() {
62 63 64 65 66 67 68 69 70
    parent::setUp();

    $perms = array(
      'access toolbar',
      'access administration pages',
      'administer site configuration',
      'bypass node access',
      'administer themes',
      'administer nodes',
71
      'access content overview',
72 73 74 75 76 77 78 79 80 81 82 83
      '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.
84 85
    $this->adminUser = $this->drupalCreateUser($perms);
    $this->adminUser2 = $this->drupalCreateUser($perms);
86

87
    $this->drupalLogin($this->adminUser);
88 89 90 91 92 93 94

    $this->drupalGet('test-page');
    $this->assertResponse(200);

    // Assert that the toolbar is present in the HTML.
    $this->assertRaw('id="toolbar-administration"');

95
    // Store the adminUser admin menu subtrees hash for comparison later.
96 97 98 99
    $this->hash = $this->getSubtreesHash();
  }

  /**
100
   * Tests the toolbar_modules_installed() and toolbar_modules_uninstalled() hook
101 102 103
   * implementations.
   */
  function testModuleStatusChangeSubtreesHashCacheClear() {
104
    // Uninstall a module.
105
    $edit = array();
106 107 108 109
    $edit['uninstall[taxonomy]'] = TRUE;
    $this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
    // Confirm the uninstall form.
    $this->drupalPostForm(NULL, array(), t('Uninstall'));
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    $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();
  }

  /**
128
   * Tests toolbar cache tags implementation.
129 130
   */
  function testMenuLinkUpdateSubtreesHashCacheClear() {
131 132
    // The ID of a (any) admin menu link.
    $admin_menu_link_id = 'system.admin_config_development';
133 134 135 136

    // Disable the link.
    $edit = array();
    $edit['enabled'] = FALSE;
137
    $this->drupalPostForm("admin/structure/menu/link/" . $admin_menu_link_id . "/edit", $edit, t('Save'));
138 139 140 141 142 143 144 145 146 147 148 149 150 151
    $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.
152
    $all_rids = $this->adminUser->getRoles();
153 154 155 156 157 158 159 160 161 162 163 164 165
    unset($all_rids[array_search(DRUPAL_AUTHENTICATED_RID, $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.
166
    $this->drupalLogin($this->adminUser2);
167 168 169 170 171 172 173 174 175
    $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.
176
    $this->drupalLogin($this->adminUser);
177 178 179 180 181 182 183 184 185 186 187
    $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.
188
    $this->drupalPostForm('user/' . $this->adminUser->id() . '/edit', array("roles[$rid]" => $rid), t('Save'));
189 190 191 192 193 194 195 196
    $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.
197
    $this->drupalLogin($this->adminUser2);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

    // 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.
217
    $this->drupalLogin($this->adminUser);
218
    $toolbarCache = $this->container->get('cache.toolbar');
219 220
    $admin_user_id = $this->adminUser->id();
    $admin_user_2_id = $this->adminUser2->id();
221 222

    // Assert that a cache tag in the toolbar cache under the key "user" exists
223
    // for adminUser against the language "en".
224
    $cache = $toolbarCache->get('toolbar_' . $admin_user_id . ':' . 'en');
225
    $this->assertEqual($cache->tags[2], 'user:' . $admin_user_id, 'A cache tag in the toolbar cache under the key "user" exists for admin_user against the language "en".');
226

227
    // Assert that no toolbar cache exists for adminUser against the
228
    // language "fr".
229
    $cache = $toolbarCache->get('toolbar_' . $admin_user_id . ':' . 'fr');
230 231 232 233 234 235 236 237 238 239 240 241 242
    $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
243
    // for adminUser against the language "fr".
244
    $cache = $toolbarCache->get('toolbar_' . $admin_user_id . ':' . 'fr');
245
    $this->assertEqual($cache->tags[2], 'user:' . $admin_user_id, 'A cache tag in the toolbar cache under the key "user" exists for admin_user against the language "fr".');
246

247 248 249 250
    // 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);
251 252 253 254 255

    // 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
256
    // for adminUser2 against the language "en".
257
    $cache = $toolbarCache->get('toolbar_' . $admin_user_2_id . ':' . 'en');
258
    $this->assertEqual($cache->tags[2], 'user:' . $admin_user_2_id, 'A cache tag in the toolbar cache under the key "user" exists for admin_user_2 against the language "en".');
259 260 261 262 263

    // 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
264
    // for adminUser against the language "fr".
265
    $cache = $toolbarCache->get('toolbar_' . $admin_user_2_id . ':' . 'fr');
266
    $this->assertEqual($cache->tags[2], 'user:' . $admin_user_2_id, 'A cache tag in the toolbar cache under the key "user" exists for admin_user_2 against the language "fr".');
267

268 269
    // Log in adminUser and clear the caches for this user using a tag.
    $this->drupalLogin($this->adminUser);
270
    Cache::deleteTags(array('user:' . $admin_user_id));
271

272
    // Assert that no toolbar cache exists for adminUser against the
273 274 275 276
    // language "en".
    $cache = $toolbarCache->get($admin_user_id . ':' . 'en');
    $this->assertFalse($cache, 'No toolbar cache exists for admin_user against the language "en".');

277
    // Assert that no toolbar cache exists for adminUser against the
278 279 280 281
    // language "fr".
    $cache = $toolbarCache->get($admin_user_id . ':' . 'fr');
    $this->assertFalse($cache, 'No toolbar cache exists for admin_user against the language "fr".');

282 283
    // Log in adminUser2 and verify that this user's caches still exist.
    $this->drupalLogin($this->adminUser2);
284 285

    // Assert that a cache tag in the toolbar cache under the key "user" exists
286
    // for adminUser2 against the language "en".
287
    $cache = $toolbarCache->get('toolbar_' . $admin_user_2_id . ':' . 'en');
288
    $this->assertEqual($cache->tags[2], 'user:' . $admin_user_2_id, 'A cache tag in the toolbar cache under the key "user" exists for admin_user_2 against the language "en".');
289 290

    // Assert that a cache tag in the toolbar cache under the key "user" exists
291
    // for adminUser2 against the language "fr".
292
    $cache = $toolbarCache->get('toolbar_' . $admin_user_2_id . ':' . 'fr');
293
    $this->assertEqual($cache->tags[2], 'user:' . $admin_user_2_id, 'A cache tag in the toolbar cache under the key "user" exists for admin_user_2 against the language "fr".');
294 295 296 297 298 299 300 301
  }

  /**
   * 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');
302 303
    $admin_user_id = $this->adminUser->id();
    $admin_user_2_id = $this->adminUser2->id();
304 305
    $this->hash = $this->getSubtreesHash();

306 307
    // adminUser2 will add a role to adminUser.
    $this->drupalLogin($this->adminUser2);
308 309
    $rid = $this->drupalCreateRole(array('administer content types',));

310
    // Get the subtree hash for adminUser2 to check later that it has not
311 312 313 314 315 316 317 318 319
    // 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.'));

320 321
    // Log in adminUser and assert that the subtrees hash has changed.
    $this->drupalLogin($this->adminUser);
322 323
    $this->assertDifferentHash();

324 325
    // Log in adminUser2 to check that its subtrees hash has not changed.
    $this->drupalLogin($this->adminUser2);
326 327
    $new_subtree_hash = $this->getSubtreesHash();

328
    // Assert that the old adminUser subtree hash and the new adminUser
329 330 331 332 333 334 335 336 337 338
    // 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');
339 340
    $admin_user = $this->adminUser;
    $admin_user_id = $this->adminUser->id();
341 342 343 344 345 346
    // 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.
347
    $name = $this->randomMachineName(16);
348 349 350 351
    // 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.
352 353
    $translation = $this->randomMachineName(16);
    $translation_to_en = $this->randomMachineName(16);
354 355 356 357 358 359

    // Add custom language.
    $this->drupalLogin($admin_user);
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
360
      'label' => $name,
361
      'direction' => LanguageInterface::DIRECTION_LTR,
362 363 364 365 366 367 368 369
    );
    $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.');

370
    // Have the adminUser request a page in the new language.
371 372 373 374
    $this->drupalGet($langcode . '/test-page');
    $this->assertResponse(200);

    // Assert that a cache tag in the toolbar cache under the key "user" exists
375
    // for adminUser against the language "xx".
376
    $cache = $toolbarCache->get('toolbar_' . $admin_user_id . ':' . $langcode);
377
    $this->assertEqual($cache->tags[2], 'user:' . $admin_user_id, 'A cache tag in the toolbar cache under the key "user" exists for admin_user against the language "xx".');
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

    // 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'));
395
    $this->assertNoText(t('No strings available'));
396 397 398 399 400 401 402 403 404 405 406
    $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.');
407
    $this->assertUrl(\Drupal::url('locale.translate_page', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
408 409
    $this->drupalLogout();

410
    // Log in the adminUser. Check the admin menu subtrees hash now that one
411 412 413
    // of the link items in the Structure tree (Menus) has had its text
    // translated.
    $this->drupalLogin($admin_user);
414
    // Have the adminUser request a page in the new language.
415 416 417 418 419 420 421 422 423 424
    $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.');
  }

425 426 427 428
  /**
   * Tests that the 'toolbar/subtrees/{hash}' is reachable.
   */
  function testSubtreesJsonRequest() {
429
    $admin_user = $this->adminUser;
430 431 432 433 434 435
    $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');
436 437 438 439 440 441

    // 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.
442
    $name = $this->randomMachineName(16);
443 444 445
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
446
      'label' => $name,
447
      'direction' => LanguageInterface::DIRECTION_LTR,
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    );
    $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';
    // The English name for the language. This will be translated.
467
    $name = $this->randomMachineName(16);
468 469 470
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
471
      'label' => $name,
472
      'direction' => LanguageInterface::DIRECTION_LTR,
473 474 475 476 477 478 479 480 481 482 483 484
    );
    $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');
    // 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.');
485 486
  }

487 488 489 490 491 492 493
  /**
   * 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() {
494
    $settings = $this->getDrupalSettings();
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    // 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;
  }

}