Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / metatag / src / Tests / MetatagConfigTranslationTest.php
1 <?php
2
3 namespace Drupal\metatag\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Ensures that the Metatag config translations work correctly.
9  *
10  * @group metatag
11  */
12 class MetatagConfigTranslationTest extends WebTestBase {
13
14   /**
15    * Profile to use.
16    *
17    * @var string
18    */
19   protected $profile = 'testing';
20
21   /**
22    * Admin user.
23    *
24    * @var \Drupal\Core\Session\AccountInterface
25    */
26   protected $adminUser;
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = [
34     'metatag',
35     'language',
36     'config_translation',
37   ];
38
39   /**
40    * Permissions to grant admin user.
41    *
42    * @var array
43    */
44   protected $permissions = [
45     // From Metatag.
46     'administer meta tags',
47
48     // From system module, in order to access the /admin pages.
49     'access administration pages',
50
51     // From language module.
52     'administer languages',
53
54     // From config_translations module.
55     'translate configuration',
56   ];
57
58   /**
59    * Sets the test up.
60    */
61   protected function setUp() {
62     parent::setUp();
63     $this->adminUser = $this->drupalCreateUser($this->permissions);
64     $this->drupalLogin($this->adminUser);
65
66     // Enable the French language.
67     $this->drupalGet('admin/config/regional/language/add');
68     $this->assertResponse(200);
69     $edit = [
70       'predefined_langcode' => 'fr',
71     ];
72     $this->drupalPostForm(NULL, $edit, t('Add language'));
73     $this->assertRaw(t(
74       'The language %language has been created and can now be used.',
75       ['%language' => t('French')]
76     ));
77   }
78
79   /**
80    * Confirm the config defaults show on the translations page.
81    */
82   public function testConfigTranslationsExist() {
83     // Ensure the config shows on the admin form.
84     $this->drupalGet('admin/config/regional/config-translation');
85     $this->assertResponse(200);
86     $this->assertText(t('Metatag defaults'));
87
88     // Load the main metatag_defaults config translation page.
89     $this->drupalGet('admin/config/regional/config-translation/metatag_defaults');
90     $this->assertResponse(200);
91     // @todo Update this to confirm the H1 is loaded.
92     $this->assertRaw(t('Metatag defaults'));
93
94     // Load all of the Metatag defaults.
95     $defaults = \Drupal::configFactory()->listAll('metatag.metatag_defaults');
96
97     /** @var \Drupal\Core\Config\ConfigManagerInterface $config_manager */
98     $config_manager = \Drupal::service('config.manager');
99
100     // Confirm each of the configs is available on the translation form.
101     foreach ($defaults as $config_name) {
102       if ($config_entity = $config_manager->loadConfigEntityByName($config_name)) {
103         $this->assertText($config_entity->label());
104       }
105     }
106
107     // Confirm that each config translation page can be loaded.
108     foreach ($defaults as $config_name) {
109       if ($config_entity = $config_manager->loadConfigEntityByName($config_name)) {
110         $this->drupalGet('admin/config/search/metatag/' . $config_entity->id() . '/translate');
111         $this->assertResponse(200);
112       }
113       else {
114         $this->error('Unable to load a Metatag default config: ' . $config_name);
115       }
116     }
117   }
118
119   /**
120    * Confirm the global configs are translatable page.
121    */
122   public function testConfigTranslations() {
123     // Add something to the Global config.
124     $this->drupalGet('admin/config/search/metatag/global');
125     $this->assertResponse(200);
126     $edit = [
127       'title' => 'Test title',
128       'description' => 'Test description',
129     ];
130     $this->drupalPostForm(NULL, $edit, t('Save'));
131     $this->assertResponse(200);
132     $this->assertText(t('Saved the Global Metatag defaults.'));
133
134     // Confirm the config has languages available to translate into.
135     $this->drupalGet('admin/config/search/metatag/global/translate');
136     $this->assertResponse(200);
137
138     // Load the translation form.
139     $this->drupalGet('admin/config/search/metatag/global/translate/fr/add');
140     $this->assertResponse(200);
141
142     // Confirm the meta tag fields are shown on the form. Confirm the fields and
143     // values separately to make it easier to pinpoint where the problem is if
144     // one should fail.
145     $this->assertFieldByName('translation[config_names][metatag.metatag_defaults.global][tags][title]');
146     $this->assertFieldByName('translation[config_names][metatag.metatag_defaults.global][tags][title]', $edit['title']);
147     $this->assertFieldByName('translation[config_names][metatag.metatag_defaults.global][tags][description]');
148     $this->assertFieldByName('translation[config_names][metatag.metatag_defaults.global][tags][description]', $edit['description']);
149
150     // Confirm the form can be saved correctly.
151     $edit = [
152       'translation[config_names][metatag.metatag_defaults.global][tags][title]' => 'Le title',
153       'translation[config_names][metatag.metatag_defaults.global][tags][description]' => 'Le description',
154     ];
155     $this->drupalPostForm(NULL, $edit, t('Save translation'));
156     $this->assertResponse(200);
157     $this->assertText(t('Successfully saved French translation'));
158   }
159
160 }