metatag_context.i18n.test 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Tests the Metatag:Context module for i18n integration.
  4. */
  5. class MetatagContextI18nTest extends MetatagTestHelper {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Metatag:Context tests, with i18n',
  12. 'description' => 'Test Metatag integration with the Context and i18n modules.',
  13. 'group' => 'Metatag',
  14. 'dependencies' => array('ctools', 'token', 'context', 'i18n'),
  15. );
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. function setUp(array $modules = array()) {
  21. $modules[] = 'context';
  22. $modules[] = 'metatag_context';
  23. // Enable the hidden submodule to manage some default configs.
  24. $modules[] = 'metatag_context_tests';
  25. // Needed for translations.
  26. $modules[] = 'locale';
  27. $modules[] = 'i18n';
  28. $modules[] = 'i18n_string';
  29. // Enable all of the modules that are needed.
  30. parent::setUp($modules);
  31. // Add more locales.
  32. $this->setupLocales();
  33. // Set up the locales.
  34. $perms = array(
  35. 'translate admin strings',
  36. 'translate user-defined strings',
  37. // Needed for the content type.
  38. 'administer languages',
  39. 'translate interface',
  40. 'bypass node access',
  41. );
  42. // This replaces the one from MetatagContextTest().
  43. $this->adminUser = $this->createAdminUser($perms);
  44. // Log in the admin user.
  45. $this->drupalLogin($this->adminUser);
  46. // Reload the translations.
  47. drupal_flush_all_caches();
  48. module_load_include('admin.inc', 'i18n_string');
  49. i18n_string_refresh_group('metatag');
  50. }
  51. /**
  52. * Verify that strings are added to the translation system.
  53. */
  54. public function testContextI18n() {
  55. // Generate a test object with English strings.
  56. $object_en = $this->createTestObject('frontpage_metatags', '<front>');
  57. // Generate a copy of that object only designed for loading on the French
  58. // front page.
  59. $object_fr = $this->createTestObject('frontpage_metatags', 'fr');
  60. foreach (array('title', 'description', 'abstract', 'keywords') as $tag) {
  61. $object_fr->$tag = $object_fr->$tag . ' in French';
  62. }
  63. // Create the English test object and check their content.
  64. $this->generateByPathConfig($object_en);
  65. $this->editByPathConfig($object_en);
  66. $this->checkByPathConfig($object_en);
  67. // Check each of the meta tags that were transmitted.
  68. foreach (array('title', 'description', 'abstract', 'keywords') as $tag) {
  69. $i18n_context = 'metatag_context:' . $object_en->name . ':' . $tag;
  70. $lid = $this->getTranslationLidByContext($i18n_context);
  71. $this->assertNotEqual($lid, 0, "Found the {locales_source} record for the {$tag} tag.");
  72. // Save a translation for this tag.
  73. $this->saveTranslationString($lid, $i18n_context, 'fr', $object_en->$tag, $object_fr->$tag);
  74. }
  75. // Confirm the configuration still works.
  76. $this->checkByPathConfig($object_en);
  77. // Confirm the French configuration works too.
  78. $this->checkByPathConfig($object_fr);
  79. }
  80. /**
  81. * Test the Metatag:Context translations for an exported configuration.
  82. */
  83. public function testExportedContext() {
  84. // Plan out the different translation string tests.
  85. $string_en = 'Metatag:Context test description tag.';
  86. $string_fr = 'French page description';
  87. $config_name = 'metatag_context:metatag_context_test:description';
  88. $path = 'metatag-context-test';
  89. // Confirm the string is present as it has been grabbed by the string-
  90. // refresh triggered in $this->setUp().
  91. $this->searchTranslationPage($string_en, $config_name);
  92. // Get the translation string lid for the generator tag.
  93. $lid = $this->getTranslationLidByContext($config_name);
  94. $this->assertNotEqual($lid, 0, 'Found the locales_source string for the description tag.');
  95. // Save the translation string.
  96. $this->saveTranslationString($lid, $config_name, 'fr', $string_en, $string_fr);
  97. // Load the English page again.
  98. $this->drupalGet($path);
  99. $this->assertResponse(200, 'Loaded the default test page again.');
  100. // Confirm the page's description is what we set it to.
  101. $xpath = $this->xpath("//meta[@name='description']");
  102. $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
  103. $this->assertEqual($xpath[0]['content'], $string_en);
  104. $this->assertNotEqual($xpath[0]['content'], $string_fr);
  105. // Load the French page.
  106. $this->drupalGet('fr/' . $path);
  107. $this->assertResponse(200, 'Loaded the French test page.');
  108. // Confirm the generator string was translated.
  109. $xpath = $this->xpath("//meta[@name='description']");
  110. $this->assertEqual(count($xpath), 1, 'Exactly one description meta tag found.');
  111. $this->assertEqual($xpath[0]['content'], $string_fr);
  112. $this->assertNotEqual($xpath[0]['content'], $string_en);
  113. }
  114. }