You are here

function ToolbarAdminMenuTest::testLocaleTranslationSubtreesHashCacheClear in Zircon Profile 8

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

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

File

core/modules/toolbar/src/Tests/ToolbarAdminMenuTest.php, line 251
Contains \Drupal\toolbar\Tests\ToolbarAdminMenuTest.

Class

ToolbarAdminMenuTest
Tests the caching of the admin menu subtree items.

Namespace

Drupal\toolbar\Tests

Code

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

  // 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 will be the translation of $name.
  $translation = $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);

  // 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 '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 = 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.');

  // 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.');
}