metatag_hreflang.with_entity_translation.test 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Tests for hreflang handle when Entity Translation is used.
  4. */
  5. class MetatagHreflangWithEntityTranslationTest extends MetatagTestHelper {
  6. /**
  7. * {@inheritdoc}
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Metatag tests for hreflang with Entity Translation',
  12. 'description' => 'Test Metatag:hreflang with the Entity Translation module.',
  13. 'group' => 'Metatag',
  14. 'dependencies' => array('devel', 'entity_translation'),
  15. );
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. function setUp(array $modules = array()) {
  21. // Used for debugging some token values.
  22. $modules[] = 'devel';
  23. // Need Locale for the multiple languages.
  24. $modules[] = 'locale';
  25. // Need Entity Translation for the tokens to work.
  26. $modules[] = 'entity_translation';
  27. // This module.
  28. $modules[] = 'metatag_hreflang';
  29. // Enable all of the modules & install the site.
  30. parent::setUp($modules);
  31. // Add some new languages.
  32. $this->setupLocales($this->supportedLocales());
  33. // The content that will be used.
  34. $content_type = 'page';
  35. // Create an admin user that can also manage locales.
  36. $perms = array(
  37. // For Locale, so languages can be added.
  38. 'administer languages',
  39. // For Entity Translation, so content can be translated.
  40. 'translate any entity',
  41. // For Devel, for access to the node's "devel" tab.
  42. 'access devel information',
  43. // For Field UI, so field settings can be changed.
  44. 'administer fields',
  45. // For Node, so content type settings can be changed.
  46. 'administer content types',
  47. // For Node, so content can be created and edited.
  48. 'create ' . $content_type . ' content',
  49. 'edit any ' . $content_type . ' content',
  50. );
  51. $this->adminUser = $this->createAdminUser($perms);
  52. // Log in the admin user.
  53. $this->drupalLogin($this->adminUser);
  54. // Enable translation support for the content type.
  55. variable_set('language_content_type_' . $content_type, ENTITY_TRANSLATION_ENABLED);
  56. // Allow the body field to be translated.
  57. $this->drupalGet('admin/structure/types/manage/' . $content_type . '/fields/body');
  58. $this->assertResponse(200);
  59. $this->assertFieldByName('field[translatable]');
  60. $edit = array(
  61. 'field[translatable]' => TRUE,
  62. );
  63. $this->drupalPost(NULL, $edit, t('Save settings'));
  64. // Clear all the caches so that all of the various hooks are activated and
  65. // the appropriate tokens, fields, meta tags, etc are available.
  66. drupal_flush_all_caches();
  67. }
  68. /**
  69. * The list of locales that are being tested.
  70. *
  71. * @return array
  72. * A simple list of language codes.
  73. */
  74. private function supportedLocales() {
  75. return array(
  76. 'de',
  77. 'fr',
  78. 'es',
  79. 'en',
  80. );
  81. }
  82. /**
  83. * Assert that the appropriate hreflang meta tag fields are present.
  84. *
  85. * @param string $form_langcode
  86. * The langcode of the current form. Defaults to LANGUAGE_NONE, which is
  87. * what is used on an empty node/add form.
  88. */
  89. private function assertHreflangFields($form_langcode = LANGUAGE_NONE) {
  90. // The x-default field has a specific default.
  91. $this->assertFieldByName("metatags[{$form_langcode}][hreflang_xdefault][value]", "[node:url-original]", 'Found the hreflang=x-default meta tag and it has the correct default value.');
  92. // Confirm each of the support locales has its own field and the appropriate
  93. // default value.
  94. foreach ($this->supportedLocales() as $langcode) {
  95. $this->assertFieldByName("metatags[{$form_langcode}][hreflang_{$langcode}][value]", "[node:url-{$langcode}]", format_string('Found the hreflang field for the "%lang" locale and it has the correct default value.', array('%lang' => $langcode)));
  96. }
  97. }
  98. /**
  99. * Confirm that each locale has a field added and shows the appropriate
  100. * default value.
  101. */
  102. function testFormFields() {
  103. $this->drupalGet('node/add/page');
  104. $this->assertResponse(200);
  105. // Confirm the fields exist.
  106. $this->assertHreflangFields();
  107. }
  108. /**
  109. * Confirm that the meta tags output are correct.
  110. */
  111. function testOutput() {
  112. // All of the locales we're supporting in these tests. The languages have
  113. // been enabled already, so this gets a list of language objects.
  114. $languages = language_list('enabled');
  115. $locales = $languages[1];
  116. // Identify the site's default language.
  117. $default_language = language_default('language');
  118. // Create an English node so it can be translated.
  119. $args = array(
  120. 'language' => $default_language,
  121. );
  122. $node = $this->drupalCreateNode($args);
  123. $this->verbose($node);
  124. // Load the translation page.
  125. $this->drupalGet('node/' . $node->nid . '/translate');
  126. $this->assertResponse(200);
  127. $this->assertText(t('Not translated'));
  128. // Confirm that there are links to translate the node.
  129. $urls = array();
  130. foreach ($locales as $langcode => $locale) {
  131. // There won't be a link to translate to English, that's the default
  132. // language for thos node.
  133. if ($langcode == $default_language) {
  134. continue;
  135. }
  136. // Confirm that a link to translate the node into each locale exists.
  137. $url = 'node/' . $node->nid . '/edit/add/' . $node->language . '/' . $langcode;
  138. $urls[$langcode] = $url;
  139. // @todo This fails in testbot.
  140. // $this->assertLinkbyHref(url($url));
  141. }
  142. // Check each of the 'translate' pages loads properly.
  143. foreach ($urls as $langcode => $url) {
  144. // Confirm the 'translate' page loads.
  145. $this->drupalGet($url);
  146. $this->assertResponse(200);
  147. // Confirm all of the hreflang fields exist.
  148. $this->assertHreflangFields($langcode);
  149. // Save the translation.
  150. $edit = array(
  151. // Add a custom title.
  152. "metatags[{$langcode}][title][value]" => "Tranlation for {$langcode}",
  153. );
  154. $this->drupalPost(NULL, $edit, t('Save'));
  155. }
  156. // Load the translation page again to confirm everything was translated.
  157. $this->drupalGet('node/' . $node->nid . '/translate');
  158. $this->assertResponse(200);
  159. $this->assertNoText(t('Not translated'));
  160. // Load the node's devel page to see the translations data.
  161. $this->drupalGet('node/' . $node->nid . '/devel');
  162. $this->assertResponse(200);
  163. // Load the node's devel page and confirm each of the tokens is available.
  164. $this->drupalGet('node/' . $node->nid . '/devel/token');
  165. $this->assertResponse(200);
  166. foreach ($locales as $langcode => $locale) {
  167. $this->assertText("[node:url-{$langcode}]");
  168. }
  169. // Load the node page again, confirm each hreflang meta tag.
  170. $this->drupalGet('node/' . $node->nid);
  171. $this->assertResponse(200);
  172. $xpath = $this->xpath("//link[@rel='alternate']");
  173. $this->verbose($xpath);
  174. $this->assertEqual(count($xpath), count($locales), 'The correct number of hreflang meta tags was found');
  175. // Try to find the position of the xdefault value in the $xpath structure.
  176. $xdefault_pos = NULL;
  177. // This is slightly messy logic as the sort order of $locales may be
  178. // different to the meta tags.
  179. foreach ($locales as $langcode => $locale) {
  180. $found = FALSE;
  181. foreach ($xpath as $ctr => $item) {
  182. if ($item['hreflang'] == 'x-default') {
  183. $xdefault_pos = $ctr;
  184. }
  185. elseif ($item['hreflang'] == $langcode) {
  186. $found = TRUE;
  187. $this->assertEqual($xpath[$ctr]['hreflang'], $langcode);
  188. // @todo Fix this. Not sure why, but the url() call returns the URL
  189. // without the language prefix.
  190. // $url_options = array(
  191. // 'language' => $locale,
  192. // 'absolute' => TRUE,
  193. // );
  194. // $this->assertEqual($xpath[$ctr]['href'], url('node/' . $node->nid, $url_options));
  195. }
  196. }
  197. // The node's default language should not have been found, it should have
  198. // been turned into an xdefault.
  199. if ($langcode == $node->language) {
  200. $this->assertFalse((bool)$found, format_string("A regular hreflang meta tag for the node's default language (%lang) was not found.", array('%lang' => $langcode)));
  201. }
  202. // Translations should have been found.
  203. else {
  204. $this->assertTrue((bool)$found, format_string('The hreflang meta tag for %lang was found.', array('%lang' => $langcode)));
  205. }
  206. }
  207. // Confirm the hreflang=xdefault meta tag was found.
  208. $this->assertNotNull($xdefault_pos, 'The hreflang=xdefault meta tag was found.');
  209. if (!is_null($xdefault_pos)) {
  210. $this->assertEqual($xpath[$xdefault_pos]['href'], url('node/' . $node->nid, array('absolute' => TRUE)), 'Found the x-default value.');
  211. }
  212. // Enable the xdefault-dupe option.
  213. variable_set('metatag_hreflang_allow_dupe', TRUE);
  214. metatag_config_cache_clear();
  215. // Load the node page again.
  216. $this->drupalGet('node/' . $node->nid);
  217. $this->assertResponse(200);
  218. // Confirm there are now more meta tags.
  219. $xpath = $this->xpath("//link[@rel='alternate']");
  220. $this->verbose($xpath);
  221. $this->assertEqual(count($xpath), count($locales) + 1, 'The correct number of hreflang meta tags was found.');
  222. $found = FALSE;
  223. foreach ($xpath as $ctr => $item) {
  224. if ($item['hreflang'] == $node->language) {
  225. $found = $ctr;
  226. }
  227. }
  228. $this->assertTrue((bool)$found, "Found an hreflang meta tag for the node's default locale.");
  229. if ($found) {
  230. $this->assertEqual($xpath[$found]['hreflang'], $node->language);
  231. }
  232. }
  233. }