metatag.with_i18n_config.test 11 KB

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