function ToolbarAdminMenuTest::testLocaleTranslationSubtreesHashCacheClear

Same name and namespace in other branches
  1. 9 core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php \Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest::testLocaleTranslationSubtreesHashCacheClear()
  2. 8.9.x core/modules/toolbar/tests/src/Functional/ToolbarAdminMenuTest.php \Drupal\Tests\toolbar\Functional\ToolbarAdminMenuTest::testLocaleTranslationSubtreesHashCacheClear()
  3. 10 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 289

Class

ToolbarAdminMenuTest
Tests the caching of the admin menu subtree items.

Namespace

Drupal\Tests\toolbar\Functional

Code

public function testLocaleTranslationSubtreesHashCacheClear() : void {
    $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->drupalGet('admin/config/regional/language/add');
    $this->submitForm($edit, 'Add custom language');
    t($name, [], [
        'langcode' => $langcode,
    ]);
    // Reset locale cache.
    $this->container
        ->get('string_translation')
        ->reset();
    $this->assertSession()
        ->responseContains('"edit-languages-' . $langcode . '-weight"');
    // Verify that the test language was added.
    $this->assertSession()
        ->pageTextContains($name);
    // 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);
    // We need to visit the page to get the string to be translated.
    $this->drupalGet($langcode . '/admin/config');
    $search = [
        'string' => 'Search and metadata',
        'langcode' => $langcode,
        'translation' => 'untranslated',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $this->assertSession()
        ->pageTextNotContains('No strings available');
    // Verify that search found the string as untranslated.
    $this->assertSession()
        ->pageTextContains($name);
    // Assume this is the only result.
    // Translate the string to a random string.
    $textarea = $this->assertSession()
        ->elementExists('xpath', '//textarea');
    $lid = (string) $textarea->getAttribute('name');
    $edit = [
        $lid => $translation,
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($edit, 'Save translations');
    $this->assertSession()
        ->pageTextContains('The strings have been saved.');
    // Verify that the user is redirected to the correct page.
    $this->assertSession()
        ->addressEquals(Url::fromRoute('locale.translate_page'));
    $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);
    $this->drupalGet('admin/config');
    // 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->assertNotEquals($original_subtree_hash, $new_subtree_hash, 'The user-specific subtree menu hash has been updated.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.