995d49ee386991a9ced937ac1cf64e007c679e07
[yaffs-website] / web / core / modules / toolbar / tests / src / Functional / ToolbarMenuTranslationTest.php
1 <?php
2
3 namespace Drupal\Tests\toolbar\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests that the toolbar icon class remains for translated menu items.
9  *
10  * @group toolbar
11  */
12 class ToolbarMenuTranslationTest extends BrowserTestBase {
13
14   /**
15    * A user with permission to access the administrative toolbar.
16    *
17    * @var \Drupal\user\UserInterface
18    */
19   protected $adminUser;
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = ['toolbar', 'toolbar_test', 'locale', 'locale_test'];
27
28   protected function setUp() {
29     parent::setUp();
30
31     // Create an administrative user and log it in.
32     $this->adminUser = $this->drupalCreateUser(['access toolbar', 'translate interface', 'administer languages', 'access administration pages']);
33     $this->drupalLogin($this->adminUser);
34   }
35
36   /**
37    * Tests that toolbar classes don't change when adding a translation.
38    */
39   public function testToolbarClasses() {
40     $langcode = 'es';
41
42     // Add Spanish.
43     $edit['predefined_langcode'] = $langcode;
44     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
45
46     // The menu item 'Structure' in the toolbar will be translated.
47     $menu_item = 'Structure';
48
49     // Visit a page that has the string on it so it can be translated.
50     $this->drupalGet($langcode . '/admin/structure');
51
52     // Search for the menu item.
53     $search = [
54       'string' => $menu_item,
55       'langcode' => $langcode,
56       'translation' => 'untranslated',
57     ];
58     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
59     // Make sure will be able to translate the menu item.
60     $this->assertNoText('No strings available.', 'Search found the menu item as untranslated.');
61
62     // Check that the class is on the item before we translate it.
63     $xpath = $this->xpath('//a[contains(@class, "icon-system-admin-structure")]');
64     $this->assertEqual(count($xpath), 1, 'The menu item class ok before translation.');
65
66     // Translate the menu item.
67     $menu_item_translated = $this->randomMachineName();
68     $textarea = current($this->xpath('//textarea'));
69     $lid = (string) $textarea->getAttribute('name');
70     $edit = [
71       $lid => $menu_item_translated,
72     ];
73     $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
74
75     // Search for the translated menu item.
76     $search = [
77       'string' => $menu_item,
78       'langcode' => $langcode,
79       'translation' => 'translated',
80     ];
81     $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
82     // Make sure the menu item string was translated.
83     $this->assertText($menu_item_translated, 'Search found the menu item as translated: ' . $menu_item_translated . '.');
84
85     // Go to another page in the custom language and make sure the menu item
86     // was translated.
87     $this->drupalGet($langcode . '/admin/structure');
88     $this->assertText($menu_item_translated, 'Found the menu translated.');
89
90     // Toolbar icons are included based on the presence of a specific class on
91     // the menu item. Ensure that class also exists for a translated menu item.
92     $xpath = $this->xpath('//a[contains(@class, "icon-system-admin-structure")]');
93     $this->assertEqual(count($xpath), 1, 'The menu item class is the same.');
94   }
95
96 }