metatag.locale.test 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Metatag module to ensure Locale integration doesn't break.
  5. */
  6. /**
  7. * Tests for the Metatag module to ensure Locale integration doesn't break.
  8. */
  9. class MetatagCoreLocaleTest extends MetatagTestHelper {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Metatag core tests for Locale',
  16. 'description' => 'Test Metatag integration with the locale module.',
  17. 'group' => 'Metatag',
  18. 'dependencies' => array('ctools', 'token'),
  19. );
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. function setUp(array $modules = array()) {
  25. // Need Locale for the multiple languages.
  26. $modules[] = 'locale';
  27. parent::setUp($modules);
  28. }
  29. /**
  30. * Test that the node form meta tag fields are translated correctly.
  31. */
  32. function testNodeFormTranslations() {
  33. $content_type = 'metatag_test';
  34. $content_type_path = str_replace('_', '-', $content_type);
  35. $label = 'Test';
  36. // Create a content type.
  37. $this->createContentType($content_type, $label);
  38. // Add more locales.
  39. $this->setupLocales();
  40. // Create an admin user and log them in.
  41. $perms = array(
  42. // Needed for the content type.
  43. 'create ' . $content_type . ' content',
  44. 'delete any ' . $content_type . ' content',
  45. 'edit any ' . $content_type . ' content',
  46. // Locale items.
  47. 'administer languages',
  48. 'translate interface',
  49. );
  50. $this->adminUser = $this->createAdminUser($perms);
  51. // Log in the admin user.
  52. $this->drupalLogin($this->adminUser);
  53. // Load the node form in each locale, confirm it has the fields. This also
  54. // preloads the field labels into the locale system.
  55. foreach (array('French' => 'fr', 'English' => '') as $lang => $langcode) {
  56. $path = 'node/add/' . $content_type_path;
  57. if (!empty($langcode)) {
  58. $path = $langcode . '/' . $path;
  59. }
  60. // Load the node form in French to prep the translation strings.
  61. $this->drupalGet($path);
  62. $this->assertResponse(200);
  63. // Verify the page loaded correctly.
  64. // @todo Update this to extract the page title.
  65. $this->assertText(strip_tags(t('Create @name', array('@name' => $label))));
  66. // Confirm the Metatag fields are present.
  67. $this->assertField('edit-metatags-und-title-value', 'Found the page title meta tag');
  68. $xpath = $this->xpath("//label[@for='edit-metatags-und-title-value']");
  69. $this->assertEqual(count($xpath), 1, 'Exactly one page title field found.');
  70. // Need to trim() the xpath results because Field API adds a space after
  71. // every field's label.
  72. $this->assertEqual(trim((string) $xpath[0]), 'Page title');
  73. $this->assertField('edit-metatags-und-description-value', 'Found the description meta tag');
  74. $xpath = $this->xpath("//label[@for='edit-metatags-und-description-value']");
  75. $this->assertEqual(count($xpath), 1, 'Exactly one description field found.');
  76. // Need to trim() the xpath results because Field API adds a space after
  77. // every field's label.
  78. $this->assertEqual(trim((string) $xpath[0]), 'Description');
  79. }
  80. // Translate the page title and description fields.
  81. $textgroup = 'default';
  82. $context = '';
  83. $langcode = 'fr';
  84. $title_source = 'Page title';
  85. $title_target = 'Le page title';
  86. $lid = $this->getTranslationLidBySource($title_source, $textgroup);
  87. $this->assertNotEqual($lid, 0, 'Found the locales_source string for the page title tag.');
  88. $this->saveTranslationString($lid, $context, $langcode, $title_source, $title_target);
  89. $desc_source = 'Description';
  90. $desc_target = 'Le description';
  91. $lid = $this->getTranslationLidBySource($desc_source, $textgroup);
  92. $this->assertNotEqual($lid, 0, 'Found the locales_source string for the description tag.');
  93. $this->saveTranslationString($lid, $context, $langcode, $desc_source, $desc_target);
  94. // Clear the metatag_get_info() cache since translated strings have changed.
  95. metatag_config_cache_clear();
  96. // Load the French node form again.
  97. $this->drupalGet('fr/node/add/' . $content_type_path);
  98. $this->assertResponse(200);
  99. // Verify the title field's label was translated.
  100. $xpath = $this->xpath("//label[@for='edit-metatags-und-title-value']");
  101. $this->assertEqual(count($xpath), 1, 'Exactly one page title field found.');
  102. // Need to trim() the xpath results because Field API adds a space after
  103. // every field's label.
  104. $this->assertEqual(trim((string) $xpath[0]), $title_target);
  105. // Verify the description field's label was translated.
  106. $xpath = $this->xpath("//label[@for='edit-metatags-und-description-value']");
  107. $this->assertEqual(count($xpath), 1, 'Exactly one description field found.');
  108. // Need to trim() the xpath results because Field API adds a space after
  109. // every field's label.
  110. $this->assertEqual(trim((string) $xpath[0]), $desc_target);
  111. }
  112. /**
  113. * Verify language meta tags don't output LANGUAGE_NONE / 'und'.
  114. */
  115. function testLanguageNone() {
  116. // Set the global node defaults to set "content-language" to the node's
  117. // language.
  118. $config = metatag_config_load('node');
  119. $config->config['content-language']['value'] = '[node:language]';
  120. metatag_config_save($config);
  121. // Create a test node.
  122. $node = $this->drupalCreateNode();
  123. $this->assertEqual($node->language, LANGUAGE_NONE);
  124. // Load the node page, confirm that the "content-language" meta tag is not
  125. // output.
  126. $this->drupalGet('node/' . $node->nid);
  127. $xpath = $this->xpath("//meta[@http-equiv='content-language']");
  128. $this->assertEqual(count($xpath), 0, 'The content-language meta tag was not found.');
  129. }
  130. // @todo Make sure the meta tag fields are displayed in the correct locale.
  131. // @todo Make sure the node records are stored with the correct language.
  132. }