metatag.with_i18n_config.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Metatag's i18n integration for the configurations.
  5. */
  6. /**
  7. * Tests for Metatag's i18n integration for the configurations.
  8. */
  9. class MetatagCoreWithI18nConfigTest extends MetatagTestHelper {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Metatag core tests with i18n: configs',
  16. 'description' => 'Test Metatag configuration integration with the i18n module.',
  17. 'group' => 'Metatag',
  18. 'dependencies' => array('ctools', 'token', 'i18n'),
  19. );
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. function setUp(array $modules = array()) {
  25. // Need the i18n and i18n_strings modules.
  26. $modules[] = 'i18n';
  27. $modules[] = 'i18n_string';
  28. // Enable all of the modules that are needed.
  29. parent::setUp($modules);
  30. // Add more locales.
  31. $this->setupLocales();
  32. // Set up the locales.
  33. $perms = array(
  34. 'administer languages',
  35. 'translate interface',
  36. // From i18n.
  37. 'translate admin strings',
  38. 'translate user-defined strings',
  39. // Needed for content type management.
  40. 'bypass node access',
  41. 'administer content types',
  42. 'administer nodes',
  43. );
  44. $this->adminUser = $this->createAdminUser($perms);
  45. // Log in the admin user.
  46. $this->drupalLogin($this->adminUser);
  47. // Reload the translations.
  48. drupal_flush_all_caches();
  49. module_load_include('admin.inc', 'i18n_string');
  50. i18n_string_refresh_group('metatag');
  51. }
  52. /**
  53. * Test translation functionality with i18n on config defaults.
  54. */
  55. public function testI18nDefaultConfig() {
  56. // Plan out the different translation string tests.
  57. $string_en = 'Drupal 7 (https://www.drupal.org)';
  58. $string_fr = 'French Drupal';
  59. $config_name = 'metatag_config:global:generator';
  60. // Confirm the translation page exists and has some Metatag strings.
  61. $this->searchTranslationPage('', $config_name);
  62. // Load the front page.
  63. $this->drupalGet('');
  64. $this->assertResponse(200, 'Loaded the homepage.');
  65. // Confirm the page's title is what we set it to.
  66. $xpath = $this->xpath("//meta[@name='generator']");
  67. $this->assertEqual(count($xpath), 1, 'Exactly one generator meta tag found.');
  68. $this->assertEqual($xpath[0]['content'], $string_en);
  69. // Confirm the string is present.
  70. $this->searchTranslationPage($string_en, $config_name);
  71. // Get the translation string lid for the generator tag.
  72. $lid = $this->getTranslationLidByContext($config_name);
  73. $this->assertNotEqual($lid, 0, 'Found the locales_source string for the generator tag.');
  74. // Save the translation string.
  75. $this->saveTranslationString($lid, $config_name, 'fr', $string_en, $string_fr);
  76. // Load the English front page again.
  77. $this->drupalGet('');
  78. $this->assertResponse(200, 'Loaded the default homepage.');
  79. // Confirm the page's generator tag is what we set it to.
  80. $xpath = $this->xpath("//meta[@name='generator']");
  81. $this->assertEqual(count($xpath), 1, 'Exactly one generator meta tag found.');
  82. $this->assertEqual($xpath[0]['content'], $string_en);
  83. // Load the French front page.
  84. $this->drupalGet('fr');
  85. $this->assertResponse(200, 'Loaded the French homepage.');
  86. // Confirm the generator string was translated.
  87. $xpath = $this->xpath("//meta[@name='generator']");
  88. $this->assertEqual(count($xpath), 1, 'Exactly one generator meta tag found.');
  89. $this->assertEqual($xpath[0]['content'], $string_fr);
  90. }
  91. /**
  92. * Test translations of the custom configurations.
  93. */
  94. public function testI18nCustomConfig() {
  95. // Plan out the different translation string tests.
  96. $string_en = 'Welcome to the homepage!';
  97. $string_fr = 'Bonjour pour le homepage!';
  98. $config_name = 'metatag_config:global:frontpage:title';
  99. // Confirm the translation page exists and has some Metatag strings.
  100. $this->searchTranslationPage('', $config_name);
  101. // Confirm the string is not present yet.
  102. $this->searchTranslationPage($string_en, $config_name, FALSE);
  103. // Load the front page's meta tags.
  104. $instance = 'global:frontpage';
  105. $config = metatag_config_load($instance);
  106. // Set something specific as the homepage.
  107. $config->config['title']['value'] = $string_en;
  108. metatag_config_save($config);
  109. // Confirm the string is present now.
  110. $this->searchTranslationPage($string_en, $config_name);
  111. // Load the front page.
  112. $this->drupalGet('');
  113. $this->assertResponse(200, 'Loaded the homepage.');
  114. // Confirm the page's title is what we set it to.
  115. $this->assertTitle($string_en);
  116. // Get the translation string lid for the front page's title.
  117. $lid = $this->getTranslationLidByContext($config_name);
  118. $this->assertNotEqual($lid, 0, 'Found the locales_source string for the front page title tag.');
  119. // Save the translation string.
  120. $this->saveTranslationString($lid, $config_name, 'fr', $string_en, $string_fr);
  121. // Load the English front page again.
  122. $this->drupalGet('');
  123. $this->assertResponse(200, 'Loaded the default homepage.');
  124. // Confirm the page's title is what we set it to.
  125. $this->assertTitle($string_en);
  126. // Load the French front page.
  127. $this->drupalGet('fr');
  128. $this->assertResponse(200, 'Loaded the French homepage.');
  129. // Confirm the page's title is what we set it to.
  130. $this->assertTitle($string_fr);
  131. }
  132. /**
  133. * Confirm that translations will be deleted when configs are deleted.
  134. */
  135. public function testI18nDeleteConfig() {
  136. // Plan out the different translation string tests.
  137. $string_en = 'Hey, an article!';
  138. $string_fr = 'Alors! Un article!';
  139. $config_name = 'metatag_config:node:article:title';
  140. // Create a config for the 'article' content type's meta tags.
  141. $instance = 'node:article';
  142. $label = t('Node: Article');
  143. $config = new StdClass();
  144. $config->instance = $instance;
  145. $config->api_version = 1;
  146. $config->disabled = FALSE;
  147. $config->export_module = 'metatag';
  148. $config->config = array();
  149. // Set something specific as the page title.
  150. $config->config['title']['value'] = $string_en;
  151. metatag_config_save($config);
  152. // Confirm the string is present now.
  153. $this->searchTranslationPage($string_en, $config_name);
  154. // Load the 'delete' page.
  155. $this->drupalGet('admin/config/search/metatags/config/' . $instance . '/delete');
  156. $this->assertResponse(200);
  157. // Confirm this is the correct page.
  158. $this->assertText(t('Are you sure you want to delete the meta tag defaults for @label?', array('@label' => $label)));
  159. $this->assertText(t('This action cannot be undone.'));
  160. $this->assertFieldByName('op');
  161. $this->assertFieldByName('op', t('Confirm'));
  162. // Submit the form.
  163. $this->drupalPost(NULL, array(), t('Confirm'));
  164. $this->assertResponse(200);
  165. $this->assertText(t('The meta tag defaults for @label have been deleted.', array('@label' => $label)));
  166. // Confirm the string has been removed.
  167. $this->searchTranslationPage($string_en, $config_name, FALSE);
  168. }
  169. /**
  170. * Confirm that translations will be deleted when entity bundles are deleted.
  171. */
  172. public function testI18nDeleteEntityBundle() {
  173. // Plan out the different translation string tests.
  174. $string_en = 'Hey, an article!';
  175. $string_fr = 'Alors! Un article!';
  176. $config_name = 'metatag_config:node:article:title';
  177. // Create a config for the 'article' content type's meta tags.
  178. $content_type = 'article';
  179. $content_type_label = t('Article');
  180. $instance = 'node:article';
  181. $instance_label = t('Node: Article');
  182. $config = new StdClass();
  183. $config->instance = $instance;
  184. $config->api_version = 1;
  185. $config->disabled = FALSE;
  186. $config->export_module = 'metatag';
  187. $config->config = array();
  188. // Set something specific as the page title.
  189. $config->config['title']['value'] = $string_en;
  190. metatag_config_save($config);
  191. // Confirm the string is present now.
  192. $this->searchTranslationPage($string_en, $config_name);
  193. // Load the 'delete' page.
  194. $this->drupalGet('admin/structure/types/manage/' . $content_type);
  195. $this->assertResponse(200);
  196. // Confirm this is the correct page.
  197. $this->assertFieldByName('op', t('Delete content type'));
  198. // Submit the form.
  199. $this->drupalPost(NULL, array(), t('Delete content type'));
  200. $this->assertResponse(200);
  201. // Confirm this is the correct page.
  202. $this->assertText(t('Are you sure you want to delete the content type @label?', array('@label' => $content_type_label)));
  203. $this->assertText(t('This action cannot be undone.'));
  204. $this->assertFieldByName('op');
  205. $this->assertFieldByName('op', t('Delete'));
  206. // Submit the form.
  207. $this->drupalPost(NULL, array(), t('Delete'));
  208. $this->assertResponse(200);
  209. $this->assertText(t('The content type @label has been deleted.', array('@label' => $content_type_label)));
  210. // Confirm the config was deleted.
  211. $config = metatag_config_load($instance);
  212. $this->assertEqual($config, array());
  213. // Confirm the string has been removed.
  214. $this->searchTranslationPage($string_en, $config_name, FALSE);
  215. }
  216. /**
  217. * Test translations of the custom configurations.
  218. */
  219. public function testI18nDifferentSourceLanguage() {
  220. // Make French the default source locale.
  221. variable_set('i18n_string_source_language', 'fr');
  222. // Verify that the default language and source language are different.
  223. $this->assertNotEqual(i18n_string_source_language(), language_default('language'));
  224. // Plan out the different translation string tests.
  225. $string_en = 'Welcome to the homepage!';
  226. $string_fr = 'Bonjour pour le homepage!';
  227. $config_name = 'metatag_config:global:frontpage:title';
  228. // Confirm the translation page exists and has some Metatag strings.
  229. $this->searchTranslationPage('', $config_name);
  230. // Confirm the string is not present yet.
  231. $this->searchTranslationPage($string_en, $config_name, FALSE);
  232. // Load the front page's meta tags.
  233. $instance = 'global:frontpage';
  234. $config = metatag_config_load($instance);
  235. // Set something specific as the homepage.
  236. $config->config['title']['value'] = $string_fr;
  237. metatag_config_save($config);
  238. // Confirm the string is present now.
  239. $this->searchTranslationPage($string_fr, $config_name);
  240. // Load the front page.
  241. $this->drupalGet('fr');
  242. $this->assertResponse(200, 'Loaded the homepage.');
  243. // Confirm the page's title is what we set it to.
  244. $this->assertTitle($string_fr);
  245. // Get the translation string lid for the front page's title.
  246. $lid = $this->getTranslationLidByContext($config_name);
  247. $this->assertNotEqual($lid, 0, 'Found the locales_source string for the front page title tag.');
  248. // Save the translation string.
  249. $this->saveTranslationString($lid, $config_name, 'en', $string_fr, $string_en);
  250. // Load the English front page again.
  251. $this->drupalGet('');
  252. $this->assertResponse(200, 'Loaded the default homepage.');
  253. // Confirm the page's title is what we set it to.
  254. $this->assertTitle($string_en);
  255. // Load the French front page.
  256. $this->drupalGet('fr');
  257. $this->assertResponse(200, 'Loaded the French homepage.');
  258. // Confirm the page's title is what we set it to.
  259. $this->assertTitle($string_fr);
  260. }
  261. }