link.multilingual.test 7.4 KB

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