metatag_hreflang.with_entity_translation.test 9.5 KB

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