metatag.with_i18n_output.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Metatag for when i18n is enabled and actively being used.
  5. */
  6. /**
  7. * Tests for Metatag for when i18n is enabled and actively being used.
  8. */
  9. class MetatagCoreWithI18nOutputTest extends MetatagTestHelper {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Metatag core tests with i18n: output',
  16. 'description' => 'Test Metatag 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. );
  40. $this->adminUser = $this->createAdminUser($perms);
  41. // Log in the admin user.
  42. $this->drupalLogin($this->adminUser);
  43. }
  44. /**
  45. * Test translations of the output tags.
  46. */
  47. public function testI18nOutputTranslationsEnabled() {
  48. // Enable output translations.
  49. $this->toggleOutputTranslations(TRUE);
  50. // Plan out the different translation string tests.
  51. $string_en = 'Welcome to the homepage!';
  52. $string_fr = 'Bonjour pour le homepage!';
  53. $string_context = 'output:frontpage:title';
  54. $this->searchTranslationPage('', $string_context);
  55. // Confirm the string is not present yet.
  56. $this->searchTranslationPage($string_en, $string_context, FALSE);
  57. // Load the front page's meta tags.
  58. $instance = 'global:frontpage';
  59. $config = metatag_config_load($instance);
  60. // Set something specific as the homepage.
  61. $config->config['title']['value'] = $string_en;
  62. metatag_config_save($config);
  63. // Load the front page.
  64. $this->drupalGet('');
  65. $this->assertResponse(200, 'Loaded the homepage.');
  66. // Confirm the page's title is what we set it to.
  67. $this->assertTitle($string_en);
  68. // Confirm the string is translatable.
  69. $this->searchTranslationPage($string_en, $string_context);
  70. // Get the translation string lid for the front page's title.
  71. $lid = $this->getTranslationLidByContext($string_context);
  72. $this->assertNotEqual($lid, 0, 'Found the locales_source string for the front page output title tag.');
  73. // Save the translation string.
  74. $this->saveTranslationString($lid, $string_context, 'fr', $string_en, $string_fr);
  75. // Load the English front page again.
  76. $this->drupalGet('');
  77. $this->assertResponse(200, 'Loaded the default homepage.');
  78. // Confirm the page's title is what we set it to.
  79. $this->assertTitle($string_en);
  80. // Load the French front page.
  81. $this->drupalGet('fr');
  82. $this->assertResponse(200, 'Loaded the French homepage.');
  83. // Confirm the page's title is what we set it to.
  84. $this->assertTitle($string_fr);
  85. // Make doubly sure there are output translations.
  86. $lids = $this->getTranslationsByContext($string_context);
  87. $this->assertNotEqual($lids, array());
  88. }
  89. /**
  90. * Verify that no output translation records are generated when it's disabled.
  91. */
  92. public function testI18nOutputTranslationsDisabled() {
  93. // Make sure output translations are disabled.
  94. $this->toggleOutputTranslations(FALSE);
  95. // Plan out the different translation string tests.
  96. $string_en = 'Welcome to the homepage!';
  97. $string_fr = 'Bonjour pour le homepage!';
  98. $string_context = 'output:frontpage:title';
  99. $this->searchTranslationPage('', $string_context);
  100. // Confirm the string is not present yet.
  101. $this->searchTranslationPage($string_en, $string_context, FALSE);
  102. // Load the front page's meta tags.
  103. $instance = 'global:frontpage';
  104. $config = metatag_config_load($instance);
  105. // Set something specific as the homepage.
  106. $config->config['title']['value'] = $string_en;
  107. metatag_config_save($config);
  108. // Load the front page.
  109. $this->drupalGet('');
  110. $this->assertResponse(200, 'Loaded the homepage.');
  111. // Confirm the page's title is what we set it to.
  112. $this->assertTitle($string_en);
  113. // Confirm the string is still not translatable.
  114. $this->searchTranslationPage($string_en, $string_context, FALSE);
  115. // Make doubly sure there are no output translations.
  116. $lids = $this->getTranslationsByContext($string_context);
  117. $this->assertEqual($lids, array());
  118. }
  119. /**
  120. *
  121. */
  122. public function testMenuPaths() {
  123. $paths = metatag_test_menu();
  124. $test_string = 'read-more-books-every-day';
  125. foreach ($paths as $path => $menu_item) {
  126. if (substr($path, -1) == '%') {
  127. $path = substr($path, 0, -1) . $test_string;
  128. }
  129. $this->drupalGet($path);
  130. $this->assertResponse(200, 'Loaded a page: ' . $path);
  131. }
  132. }
  133. /**
  134. * Enable or disable output translations.
  135. *
  136. * @param bool $enable
  137. * Whether or not to enable translations; defaults to TRUE.
  138. */
  139. private function toggleOutputTranslations($enable = TRUE) {
  140. // Enable output translation.
  141. variable_set('metatag_i18n_translate_output', $enable);
  142. // Reload the translations.
  143. drupal_flush_all_caches();
  144. module_load_include('admin.inc', 'i18n_string');
  145. i18n_string_refresh_group('metatag');
  146. }
  147. }