Backup of db before drupal security update
[yaffs-website] / web / core / modules / config_translation / tests / src / Functional / ConfigTranslationListUiTest.php
1 <?php
2
3 namespace Drupal\Tests\config_translation\Functional;
4
5 use Drupal\block_content\Entity\BlockContentType;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\language\Entity\ConfigurableLanguage;
10 use Drupal\Tests\BrowserTestBase;
11 use Drupal\shortcut\Entity\ShortcutSet;
12 use Drupal\contact\Entity\ContactForm;
13 use Drupal\filter\Entity\FilterFormat;
14 use Drupal\taxonomy\Entity\Vocabulary;
15
16 /**
17  * Visit all lists.
18  *
19  * @group config_translation
20  * @see \Drupal\config_translation\Tests\ConfigTranslationViewListUiTest
21  */
22 class ConfigTranslationListUiTest extends BrowserTestBase {
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = [
30     'block',
31     'config_translation',
32     'contact',
33     'block_content',
34     'field',
35     'field_ui',
36     'menu_ui',
37     'node',
38     'shortcut',
39     'taxonomy',
40     'image',
41     'responsive_image',
42     'toolbar',
43   ];
44
45   /**
46    * Admin user with all needed permissions.
47    *
48    * @var \Drupal\user\Entity\User
49    */
50   protected $adminUser;
51
52   protected function setUp() {
53     parent::setUp();
54
55     $permissions = [
56       'access site-wide contact form',
57       'administer blocks',
58       'administer contact forms',
59       'administer content types',
60       'administer block_content fields',
61       'administer filters',
62       'administer menu',
63       'administer node fields',
64       'administer permissions',
65       'administer shortcuts',
66       'administer site configuration',
67       'administer taxonomy',
68       'administer account settings',
69       'administer languages',
70       'administer image styles',
71       'administer responsive images',
72       'translate configuration',
73     ];
74
75     // Create and log in user.
76     $this->adminUser = $this->drupalCreateUser($permissions);
77     $this->drupalLogin($this->adminUser);
78
79     // Enable import of translations. By default this is disabled for automated
80     // tests.
81     $this->config('locale.settings')
82       ->set('translation.import_enabled', TRUE)
83       ->save();
84     $this->drupalPlaceBlock('local_tasks_block');
85   }
86
87   /**
88    * Tests the block listing for the translate operation.
89    *
90    * There are no blocks placed in the testing profile. Add one, then check
91    * for Translate operation.
92    */
93   protected function doBlockListTest() {
94     // Add a test block, any block will do.
95     // Set the machine name so the translate link can be built later.
96     $id = Unicode::strtolower($this->randomMachineName(16));
97     $this->drupalPlaceBlock('system_powered_by_block', ['id' => $id]);
98
99     // Get the Block listing.
100     $this->drupalGet('admin/structure/block');
101
102     $translate_link = 'admin/structure/block/manage/' . $id . '/translate';
103     // Test if the link to translate the block is on the page.
104     $this->assertLinkByHref($translate_link);
105
106     // Test if the link to translate actually goes to the translate page.
107     $this->drupalGet($translate_link);
108     $this->assertRaw('<th>' . t('Language') . '</th>');
109   }
110
111   /**
112    * Tests the menu listing for the translate operation.
113    */
114   protected function doMenuListTest() {
115     // Create a test menu to decouple looking for translate operations link so
116     // this does not test more than necessary.
117     $this->drupalGet('admin/structure/menu/add');
118     // Lowercase the machine name.
119     $menu_name = Unicode::strtolower($this->randomMachineName(16));
120     $label = $this->randomMachineName(16);
121     $edit = [
122       'id' => $menu_name,
123       'description' => '',
124       'label' => $label,
125     ];
126     // Create the menu by posting the form.
127     $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
128
129     // Get the Menu listing.
130     $this->drupalGet('admin/structure/menu');
131
132     $translate_link = 'admin/structure/menu/manage/' . $menu_name . '/translate';
133     // Test if the link to translate the menu is on the page.
134     $this->assertLinkByHref($translate_link);
135
136     // Check if the Link is not added if you are missing 'translate
137     // configuration' permission.
138     $permissions = [
139       'administer menu',
140     ];
141     $this->drupalLogin($this->drupalCreateUser($permissions));
142
143     // Get the Menu listing.
144     $this->drupalGet('admin/structure/menu');
145
146     $translate_link = 'admin/structure/menu/manage/' . $menu_name . '/translate';
147     // Test if the link to translate the menu is NOT on the page.
148     $this->assertNoLinkByHref($translate_link);
149
150     // Log in as Admin again otherwise the rest will fail.
151     $this->drupalLogin($this->adminUser);
152
153     // Test if the link to translate actually goes to the translate page.
154     $this->drupalGet($translate_link);
155     $this->assertRaw('<th>' . t('Language') . '</th>');
156   }
157
158   /**
159    * Tests the vocabulary listing for the translate operation.
160    */
161   protected function doVocabularyListTest() {
162     // Create a test vocabulary to decouple looking for translate operations
163     // link so this does not test more than necessary.
164     $vocabulary = Vocabulary::create([
165       'name' => $this->randomMachineName(),
166       'description' => $this->randomMachineName(),
167       'vid' => Unicode::strtolower($this->randomMachineName()),
168     ]);
169     $vocabulary->save();
170
171     // Get the Taxonomy listing.
172     $this->drupalGet('admin/structure/taxonomy');
173
174     $translate_link = 'admin/structure/taxonomy/manage/' . $vocabulary->id() . '/translate';
175     // Test if the link to translate the vocabulary is on the page.
176     $this->assertLinkByHref($translate_link);
177
178     // Test if the link to translate actually goes to the translate page.
179     $this->drupalGet($translate_link);
180     $this->assertRaw('<th>' . t('Language') . '</th>');
181   }
182
183   /**
184    * Tests the custom block listing for the translate operation.
185    */
186   public function doCustomContentTypeListTest() {
187     // Create a test custom block type to decouple looking for translate
188     // operations link so this does not test more than necessary.
189     $block_content_type = BlockContentType::create([
190       'id' => Unicode::strtolower($this->randomMachineName(16)),
191       'label' => $this->randomMachineName(),
192       'revision' => FALSE
193     ]);
194     $block_content_type->save();
195
196     // Get the custom block type listing.
197     $this->drupalGet('admin/structure/block/block-content/types');
198
199     $translate_link = 'admin/structure/block/block-content/manage/' . $block_content_type->id() . '/translate';
200     // Test if the link to translate the custom block type is on the page.
201     $this->assertLinkByHref($translate_link);
202
203     // Test if the link to translate actually goes to the translate page.
204     $this->drupalGet($translate_link);
205     $this->assertRaw('<th>' . t('Language') . '</th>');
206   }
207
208   /**
209    * Tests the contact forms listing for the translate operation.
210    */
211   public function doContactFormsListTest() {
212     // Create a test contact form to decouple looking for translate operations
213     // link so this does not test more than necessary.
214     $contact_form = ContactForm::create([
215       'id' => Unicode::strtolower($this->randomMachineName(16)),
216       'label' => $this->randomMachineName(),
217     ]);
218     $contact_form->save();
219
220     // Get the contact form listing.
221     $this->drupalGet('admin/structure/contact');
222
223     $translate_link = 'admin/structure/contact/manage/' . $contact_form->id() . '/translate';
224     // Test if the link to translate the contact form is on the page.
225     $this->assertLinkByHref($translate_link);
226
227     // Test if the link to translate actually goes to the translate page.
228     $this->drupalGet($translate_link);
229     $this->assertRaw('<th>' . t('Language') . '</th>');
230   }
231
232   /**
233    * Tests the content type listing for the translate operation.
234    */
235   public function doContentTypeListTest() {
236     // Create a test content type to decouple looking for translate operations
237     // link so this does not test more than necessary.
238     $content_type = $this->drupalCreateContentType([
239       'type' => Unicode::strtolower($this->randomMachineName(16)),
240       'name' => $this->randomMachineName(),
241     ]);
242
243     // Get the content type listing.
244     $this->drupalGet('admin/structure/types');
245
246     $translate_link = 'admin/structure/types/manage/' . $content_type->id() . '/translate';
247     // Test if the link to translate the content type is on the page.
248     $this->assertLinkByHref($translate_link);
249
250     // Test if the link to translate actually goes to the translate page.
251     $this->drupalGet($translate_link);
252     $this->assertRaw('<th>' . t('Language') . '</th>');
253   }
254
255   /**
256    * Tests the formats listing for the translate operation.
257    */
258   public function doFormatsListTest() {
259     // Create a test format to decouple looking for translate operations
260     // link so this does not test more than necessary.
261     $filter_format = FilterFormat::create([
262       'format' => Unicode::strtolower($this->randomMachineName(16)),
263       'name' => $this->randomMachineName(),
264     ]);
265     $filter_format->save();
266
267     // Get the format listing.
268     $this->drupalGet('admin/config/content/formats');
269
270     $translate_link = 'admin/config/content/formats/manage/' . $filter_format->id() . '/translate';
271     // Test if the link to translate the format is on the page.
272     $this->assertLinkByHref($translate_link);
273
274     // Test if the link to translate actually goes to the translate page.
275     $this->drupalGet($translate_link);
276     $this->assertRaw('<th>' . t('Language') . '</th>');
277   }
278
279   /**
280    * Tests the shortcut listing for the translate operation.
281    */
282   public function doShortcutListTest() {
283     // Create a test shortcut to decouple looking for translate operations
284     // link so this does not test more than necessary.
285     $shortcut = ShortcutSet::create([
286       'id' => Unicode::strtolower($this->randomMachineName(16)),
287       'label' => $this->randomString(),
288     ]);
289     $shortcut->save();
290
291     // Get the shortcut listing.
292     $this->drupalGet('admin/config/user-interface/shortcut');
293
294     $translate_link = 'admin/config/user-interface/shortcut/manage/' . $shortcut->id() . '/translate';
295     // Test if the link to translate the shortcut is on the page.
296     $this->assertLinkByHref($translate_link);
297
298     // Test if the link to translate actually goes to the translate page.
299     $this->drupalGet($translate_link);
300     $this->assertRaw('<th>' . t('Language') . '</th>');
301   }
302
303   /**
304    * Tests the role listing for the translate operation.
305    */
306   public function doUserRoleListTest() {
307     // Create a test role to decouple looking for translate operations
308     // link so this does not test more than necessary.
309     $role_id = Unicode::strtolower($this->randomMachineName(16));
310     $this->drupalCreateRole([], $role_id);
311
312     // Get the role listing.
313     $this->drupalGet('admin/people/roles');
314
315     $translate_link = 'admin/people/roles/manage/' . $role_id . '/translate';
316     // Test if the link to translate the role is on the page.
317     $this->assertLinkByHref($translate_link);
318
319     // Test if the link to translate actually goes to the translate page.
320     $this->drupalGet($translate_link);
321     $this->assertRaw('<th>' . t('Language') . '</th>');
322   }
323
324   /**
325    * Tests the language listing for the translate operation.
326    */
327   public function doLanguageListTest() {
328     // Create a test language to decouple looking for translate operations
329     // link so this does not test more than necessary.
330     ConfigurableLanguage::createFromLangcode('ga')->save();
331
332     // Get the language listing.
333     $this->drupalGet('admin/config/regional/language');
334
335     $translate_link = 'admin/config/regional/language/edit/ga/translate';
336     // Test if the link to translate the language is on the page.
337     $this->assertLinkByHref($translate_link);
338
339     // Test if the link to translate actually goes to the translate page.
340     $this->drupalGet($translate_link);
341     $this->assertRaw('<th>' . t('Language') . '</th>');
342   }
343
344   /**
345    * Tests the image style listing for the translate operation.
346    */
347   public function doImageStyleListTest() {
348     // Get the image style listing.
349     $this->drupalGet('admin/config/media/image-styles');
350
351     $translate_link = 'admin/config/media/image-styles/manage/medium/translate';
352     // Test if the link to translate the style is on the page.
353     $this->assertLinkByHref($translate_link);
354
355     // Test if the link to translate actually goes to the translate page.
356     $this->drupalGet($translate_link);
357     $this->assertRaw('<th>' . t('Language') . '</th>');
358   }
359
360   /**
361    * Tests the responsive image mapping listing for the translate operation.
362    */
363   public function doResponsiveImageListTest() {
364     $edit = [];
365     $edit['label'] = $this->randomMachineName();
366     $edit['id'] = strtolower($edit['label']);
367     $edit['fallback_image_style'] = 'thumbnail';
368
369     $this->drupalPostForm('admin/config/media/responsive-image-style/add', $edit, t('Save'));
370     $this->assertRaw(t('Responsive image style %label saved.', ['%label' => $edit['label']]));
371
372     // Get the responsive image style listing.
373     $this->drupalGet('admin/config/media/responsive-image-style');
374
375     $translate_link = 'admin/config/media/responsive-image-style/' . $edit['id'] . '/translate';
376     // Test if the link to translate the style is on the page.
377     $this->assertLinkByHref($translate_link);
378
379     // Test if the link to translate actually goes to the translate page.
380     $this->drupalGet($translate_link);
381     $this->assertRaw('<th>' . t('Language') . '</th>');
382   }
383
384   /**
385    * Tests the field listing for the translate operation.
386    */
387   public function doFieldListTest() {
388     // Create a base content type.
389     $content_type = $this->drupalCreateContentType([
390       'type' => Unicode::strtolower($this->randomMachineName(16)),
391       'name' => $this->randomMachineName(),
392     ]);
393
394     // Create a block content type.
395     $block_content_type = BlockContentType::create([
396       'id' => 'basic',
397       'label' => 'Basic',
398       'revision' => FALSE
399     ]);
400     $block_content_type->save();
401     $field = FieldConfig::create([
402       // The field storage is guaranteed to exist because it is supplied by the
403       // block_content module.
404       'field_storage' => FieldStorageConfig::loadByName('block_content', 'body'),
405       'bundle' => $block_content_type->id(),
406       'label' => 'Body',
407       'settings' => ['display_summary' => FALSE],
408     ]);
409     $field->save();
410
411     // Look at a few fields on a few entity types.
412     $pages = [
413       [
414         'list' => 'admin/structure/types/manage/' . $content_type->id() . '/fields',
415         'field' => 'node.' . $content_type->id() . '.body',
416       ],
417       [
418         'list' => 'admin/structure/block/block-content/manage/basic/fields',
419         'field' => 'block_content.basic.body',
420       ],
421     ];
422
423     foreach ($pages as $values) {
424       // Get fields listing.
425       $this->drupalGet($values['list']);
426
427       $translate_link = $values['list'] . '/' . $values['field'] . '/translate';
428       // Test if the link to translate the field is on the page.
429       $this->assertLinkByHref($translate_link);
430
431       // Test if the link to translate actually goes to the translate page.
432       $this->drupalGet($translate_link);
433       $this->assertRaw('<th>' . t('Language') . '</th>');
434     }
435   }
436
437   /**
438    * Tests the date format listing for the translate operation.
439    */
440   public function doDateFormatListTest() {
441     // Get the date format listing.
442     $this->drupalGet('admin/config/regional/date-time');
443
444     $translate_link = 'admin/config/regional/date-time/formats/manage/long/translate';
445     // Test if the link to translate the format is on the page.
446     $this->assertLinkByHref($translate_link);
447
448     // Test if the link to translate actually goes to the translate page.
449     $this->drupalGet($translate_link);
450     $this->assertRaw('<th>' . t('Language') . '</th>');
451   }
452
453   /**
454    * Tests a given settings page for the translate operation.
455    *
456    * @param string $link
457    *   URL of the settings page to test.
458    */
459   public function doSettingsPageTest($link) {
460     // Get the settings page.
461     $this->drupalGet($link);
462
463     $translate_link = $link . '/translate';
464     // Test if the link to translate the settings page is present.
465     $this->assertLinkByHref($translate_link);
466
467     // Test if the link to translate actually goes to the translate page.
468     $this->drupalGet($translate_link);
469     $this->assertRaw('<th>' . t('Language') . '</th>');
470   }
471
472   /**
473    * Tests if translate link is added to operations in all configuration lists.
474    */
475   public function testTranslateOperationInListUi() {
476     // All lists based on paths provided by the module.
477     $this->doBlockListTest();
478     $this->doMenuListTest();
479     $this->doVocabularyListTest();
480     $this->doCustomContentTypeListTest();
481     $this->doContactFormsListTest();
482     $this->doContentTypeListTest();
483     $this->doFormatsListTest();
484     $this->doShortcutListTest();
485     $this->doUserRoleListTest();
486     $this->doLanguageListTest();
487     $this->doImageStyleListTest();
488     $this->doResponsiveImageListTest();
489     $this->doDateFormatListTest();
490     $this->doFieldListTest();
491
492     // Views is tested in Drupal\config_translation\Tests\ConfigTranslationViewListUiTest
493
494     // Test the maintenance settings page.
495     $this->doSettingsPageTest('admin/config/development/maintenance');
496     // Test the site information settings page.
497     $this->doSettingsPageTest('admin/config/system/site-information');
498     // Test the account settings page.
499     $this->doSettingsPageTest('admin/config/people/accounts');
500     // Test the RSS settings page.
501     $this->doSettingsPageTest('admin/config/services/rss-publishing');
502   }
503
504 }