You are here

public function ToolbarAdminMenuTest::testLocaleTranslationSubtreesHashCacheClear in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php \Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest::testLocaleTranslationSubtreesHashCacheClear()

Tests that toolbar cache is cleared when string translations are made.

File

core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php, line 262

Class

ToolbarAdminMenuTest
Tests the caching of the admin menu subtree items.

Namespace

Drupal\Tests\toolbar\Functional

Code

public function testLocaleTranslationSubtreesHashCacheClear() {
  $admin_user = $this->adminUser;

  // User to translate and delete string.
  $translate_user = $this
    ->drupalCreateUser([
    '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 will be the translation of $name.
  $translation = $this
    ->randomMachineName(16);

  // Add custom language.
  $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,
  ]);

  // 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
    ->assertSession()
    ->statusCodeEquals(200);

  // Get a baseline hash for the admin menu subtrees before translating one
  // of the menu link items.
  $original_subtree_hash = $this
    ->getSubtreesHash();
  $this
    ->assertNotEmpty($original_subtree_hash, 'A valid hash value for the admin menu subtrees was created.');
  $this
    ->drupalLogout();

  // Translate the string 'Search and metadata' 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 correctly
  // invalidated.
  $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.');

  // Assume this is the only result.
  // Translate the string to a random string.
  $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();

  // 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
    ->assertSession()
    ->statusCodeEquals(200);
  $new_subtree_hash = $this
    ->getSubtreesHash();

  // Assert that the old admin menu subtrees hash and the new admin menu
  // subtrees hash are different.
  $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.');
}