link.multilingual.test 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @file
  4. * Tests that exercise the relative / absolute output of the link module.
  5. */
  6. /**
  7. * Testing that absolute / relative outputs match the expected format.
  8. */
  9. class LinkMultilingualTestCase extends LinkBaseTestClass {
  10. public function setUp() {
  11. $modules = func_get_args();
  12. $modules = (isset($modules[0]) && is_array($modules[0]) ? $modules[0] : $modules);
  13. $modules = array_merge($modules, array(
  14. 'locale',
  15. 'locale_test',
  16. 'translation',
  17. ));
  18. $this->permissions = array_merge($this->permissions, array(
  19. 'administer site configuration',
  20. 'administer languages',
  21. ));
  22. parent::setUp($modules);
  23. }
  24. /**
  25. * Enables and configured language related stuff.
  26. */
  27. public function setUpLanguage() {
  28. global $language_url;
  29. $this->drupalGet('admin/config/regional/language');
  30. // Enable the path prefix for the default language: this way any un-prefixed
  31. // URL must have a valid fallback value.
  32. $edit = array('prefix' => 'en');
  33. $this->drupalPost('admin/config/regional/language/edit/en', $edit, t('Save language'));
  34. $language_url->prefix = $language_url->language;
  35. // Add custom language - as we need more than 1 language to be multilingual.
  36. // Code for the language.
  37. $langcode = 'xx';
  38. // The English name for the language.
  39. $name = $this->randomName(16);
  40. // The native name for the language.
  41. $native = $this->randomName(16);
  42. $edit = array(
  43. 'langcode' => $langcode,
  44. 'name' => $name,
  45. 'native' => $native,
  46. 'prefix' => $langcode,
  47. 'direction' => '0',
  48. );
  49. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  50. variable_set('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX);
  51. // Enable URL language detection and selection.
  52. $edit = array('language[enabled][locale-url]' => 1);
  53. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  54. language_negotiation_set(LANGUAGE_TYPE_INTERFACE, array(LOCALE_LANGUAGE_NEGOTIATION_URL));
  55. // Reset static caching.
  56. drupal_static_reset('language_list');
  57. drupal_static_reset('locale_url_outbound_alter');
  58. drupal_static_reset('locale_language_url_rewrite_url');
  59. }
  60. }
  61. class LinkMultilingualPathTest extends LinkMultilingualTestCase {
  62. public static function getInfo() {
  63. return array(
  64. 'name' => 'Link language path prefix',
  65. 'description' => 'Tests that path properly work with language path prefixes.',
  66. 'group' => 'Link',
  67. );
  68. }
  69. /**
  70. * Creates a link field, fills it, then uses a loaded node to test paths.
  71. */
  72. public function testLanguagePrefixedPaths() {
  73. $this->setUpLanguage();
  74. // Create fields.
  75. // Field for absolute urls.
  76. $field_name_absolute = $this->createLinkField('page');
  77. // Field for relative urls.
  78. $settings = array(
  79. 'instance[settings][absolute_url]' => FALSE,
  80. );
  81. $field_name_relative = $this->createLinkField('page', $settings);
  82. // Check the node edit form.
  83. $this->drupalGet('node/add/page');
  84. $this->assertField($field_name_absolute . '[und][0][title]', 'Title absolute found');
  85. $this->assertField($field_name_absolute . '[und][0][url]', 'URL absolute found');
  86. $this->assertField($field_name_relative . '[und][0][title]', 'Title relative found');
  87. $this->assertField($field_name_relative . '[und][0][url]', 'URL relative found');
  88. // Create test content.
  89. $url_tests = array(
  90. 1 => array(
  91. 'href' => 'http://dummy.com/' . $this->randomName(),
  92. 'label' => $this->randomName(),
  93. ),
  94. 2 => array(
  95. 'href' => 'node/1',
  96. 'label' => $this->randomName(),
  97. ),
  98. 3 => array(
  99. 'href' => 'node/1?property=value',
  100. 'label' => $this->randomName(),
  101. 'query' => array('property' => 'value'),
  102. ),
  103. 4 => array(
  104. 'href' => 'node/1#position',
  105. 'label' => $this->randomName(),
  106. 'fragment' => 'position',
  107. ),
  108. 5 => array(
  109. 'href' => 'node/1?property=value2#lower',
  110. 'label' => $this->randomName(),
  111. 'fragment' => 'lower',
  112. 'query' => array('property' => 'value2'),
  113. ),
  114. );
  115. foreach ($url_tests as $index => &$input) {
  116. $this->drupalGet('node/add/page');
  117. $edit = array(
  118. 'title' => $input['label'],
  119. $field_name_absolute . '[und][0][title]' => $input['label'],
  120. $field_name_absolute . '[und][0][url]' => $input['href'],
  121. $field_name_relative . '[und][0][title]' => $input['label'],
  122. $field_name_relative . '[und][0][url]' => $input['href'],
  123. );
  124. $this->drupalPost(NULL, $edit, t('Save'));
  125. $url = $this->getUrl();
  126. $input['url'] = $url;
  127. }
  128. // Change to anonymous user.
  129. $this->drupalLogout();
  130. foreach (array_slice($url_tests, 1, NULL, TRUE) as $index => $input2) {
  131. $node = node_load($index);
  132. $this->assertNotEqual(NULL, $node, "Do we have a node?");
  133. $this->assertEqual($node->nid, $index, "Test that we have a node.");
  134. $this->drupalGet('node/' . $index);
  135. $relative_expected = url('node/1', array('absolute' => FALSE) + $input2);
  136. $absolute_expected = url('node/1', array('absolute' => TRUE) + $input2);
  137. $absolute_result = $this->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_absolute) . '")]/div/div/a/@href');
  138. $absolute_result = (string) reset($absolute_result);
  139. $this->assertEqual($absolute_result, $absolute_expected, "Absolute url output ('" . $absolute_result . "') looks as expected ('" . $absolute_expected . "')");
  140. $relative_result = $this->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_relative) . '")]/div/div/a/@href');
  141. $relative_result = (string) reset($relative_result);
  142. $this->assertEqual($relative_result, $relative_expected, "Relative url output ('" . $relative_result . "') looks as expected ('" . $relative_expected . "')");
  143. }
  144. // Check if this works with the alias too.
  145. // Add a path alias for node 1.
  146. $path = array(
  147. 'source' => 'node/1',
  148. 'alias' => $url_tests[1]['label'],
  149. );
  150. path_save($path);
  151. // Another iteration over the same nodes - this time they should use the
  152. // path alias.
  153. foreach (array_slice($url_tests, 1, NULL, TRUE) as $index => $input2) {
  154. $node = node_load($index);
  155. $this->assertNotEqual(NULL, $node, "Do we have a node?");
  156. $this->assertEqual($node->nid, $index, "Test that we have a node.");
  157. $this->drupalGet('node/' . $index);
  158. $relative_expected = url('node/1', array('absolute' => FALSE) + $input2);
  159. $absolute_expected = url('node/1', array('absolute' => TRUE) + $input2);
  160. $absolute_result = $this->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_absolute) . '")]/div/div/a/@href');
  161. $absolute_result = (string) reset($absolute_result);
  162. $this->assertEqual($absolute_result, $absolute_expected, "Absolute alias-url output ('" . $absolute_result . "') looks as expected ('" . $absolute_expected . "')");
  163. $relative_result = $this->xpath('//*[contains(@class, "field-name-' . drupal_clean_css_identifier($field_name_relative) . '")]/div/div/a/@href');
  164. $relative_result = (string) reset($relative_result);
  165. $this->assertEqual($relative_result, $relative_expected, "Relative alias-url output ('" . $relative_result . "') looks as expected ('" . $relative_expected . "')");
  166. }
  167. }
  168. }