metatag.with_i18n_output.test 5.3 KB

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