d593a69d385d7a262389a6b4f4d0cea2d45409bd
[yaffs-website] / web / core / modules / config_translation / src / Tests / ConfigTranslationOverviewTest.php
1 <?php
2
3 namespace Drupal\config_translation\Tests;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\simpletest\WebTestBase;
8
9 /**
10  * Translate settings and entities to various languages.
11  *
12  * @group config_translation
13  */
14 class ConfigTranslationOverviewTest extends WebTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'block',
23     'config_test',
24     'config_translation',
25     'config_translation_test',
26     'contact',
27     'contextual',
28     'entity_test_operation',
29     'views',
30     'views_ui',
31   ];
32
33   /**
34    * Languages to enable.
35    *
36    * @var array
37    */
38   protected $langcodes = ['fr', 'ta'];
39
40   /**
41    * String translation storage object.
42    *
43    * @var \Drupal\locale\StringStorageInterface
44    */
45   protected $localeStorage;
46
47   protected function setUp() {
48     parent::setUp();
49     $permissions = [
50       'translate configuration',
51       'administer languages',
52       'administer site configuration',
53       'administer contact forms',
54       'access site-wide contact form',
55       'access contextual links',
56       'administer views',
57     ];
58     // Create and log in user.
59     $this->drupalLogin($this->drupalCreateUser($permissions));
60
61     // Add languages.
62     foreach ($this->langcodes as $langcode) {
63       ConfigurableLanguage::createFromLangcode($langcode)->save();
64     }
65     $this->localeStorage = $this->container->get('locale.storage');
66     $this->drupalPlaceBlock('local_tasks_block');
67     $this->drupalPlaceBlock('page_title_block');
68   }
69
70   /**
71    * Tests the config translation mapper page.
72    */
73   public function testMapperListPage() {
74     $this->drupalGet('admin/config/regional/config-translation');
75     $this->assertLinkByHref('admin/config/regional/config-translation/config_test');
76     $this->assertLinkByHref('admin/config/people/accounts/translate');
77     // Make sure there is only a single operation for each dropbutton, either
78     // 'List' or 'Translate'.
79     foreach ($this->cssSelect('ul.dropbutton') as $i => $dropbutton) {
80       $this->assertIdentical(1, $dropbutton->count());
81       foreach ($dropbutton->li as $link) {
82         $this->assertTrue(((string) $link->a === 'Translate') || ((string) $link->a === 'List'));
83       }
84     }
85
86     $labels = [
87       '&$nxd~i0',
88       'some "label" with quotes',
89       $this->randomString(),
90     ];
91
92     foreach ($labels as $label) {
93       $test_entity = entity_create('config_test', [
94         'id' => $this->randomMachineName(),
95         'label' => $label,
96       ]);
97       $test_entity->save();
98
99       $base_url = 'admin/structure/config_test/manage/' . $test_entity->id();
100       $this->drupalGet('admin/config/regional/config-translation/config_test');
101       $this->assertLinkByHref($base_url . '/translate');
102       $this->assertEscaped($test_entity->label());
103
104       // Make sure there is only a single 'Translate' operation for each
105       // dropbutton.
106       foreach ($this->cssSelect('ul.dropbutton') as $i => $dropbutton) {
107         $this->assertIdentical(1, $dropbutton->count());
108         foreach ($dropbutton->li as $link) {
109           $this->assertIdentical('Translate', (string) $link->a);
110         }
111       }
112
113       $entity_type = \Drupal::entityManager()->getDefinition($test_entity->getEntityTypeId());
114       $this->drupalGet($base_url . '/translate');
115
116       $title = $test_entity->label() . ' ' . $entity_type->getLowercaseLabel();
117       $title = 'Translations for <em class="placeholder">' . Html::escape($title) . '</em>';
118       $this->assertRaw($title);
119       $this->assertRaw('<th>' . t('Language') . '</th>');
120
121       $this->drupalGet($base_url);
122       $this->assertLink(t('Translate @title', ['@title' => $entity_type->getLowercaseLabel()]));
123     }
124   }
125
126   /**
127    * Tests availability of hidden entities in the translation overview.
128    */
129   public function testHiddenEntities() {
130     // Hidden languages are only available to translate through the
131     // configuration translation listings.
132     $this->drupalGet('admin/config/regional/config-translation/configurable_language');
133     $this->assertText('Not applicable');
134     $this->assertLinkByHref('admin/config/regional/language/edit/zxx/translate');
135     $this->assertText('Not specified');
136     $this->assertLinkByHref('admin/config/regional/language/edit/und/translate');
137
138     // Hidden date formats are only available to translate through the
139     // configuration translation listings. Test a couple of them.
140     $this->drupalGet('admin/config/regional/config-translation/date_format');
141     $this->assertText('HTML Date');
142     $this->assertLinkByHref('admin/config/regional/date-time/formats/manage/html_date/translate');
143     $this->assertText('HTML Year');
144     $this->assertLinkByHref('admin/config/regional/date-time/formats/manage/html_year/translate');
145   }
146
147   /**
148    * Tests that overrides do not affect listing screens.
149    */
150   public function testListingPageWithOverrides() {
151     $original_label = 'Default';
152     $overridden_label = 'Overridden label';
153
154     $config_test_storage = $this->container->get('entity.manager')->getStorage('config_test');
155
156     // Set up an override.
157     $settings['config']['config_test.dynamic.dotted.default']['label'] = (object) [
158       'value' => $overridden_label,
159       'required' => TRUE,
160     ];
161     $this->writeSettings($settings);
162
163     // Test that the overridden label is loaded with the entity.
164     $this->assertEqual($config_test_storage->load('dotted.default')->label(), $overridden_label);
165
166     // Test that the original label on the listing page is intact.
167     $this->drupalGet('admin/config/regional/config-translation/config_test');
168     $this->assertText($original_label);
169     $this->assertNoText($overridden_label);
170   }
171
172 }