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