metatag.locale.test 5.7 KB

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