1754162-15-language-in-login-cancel-links.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. diff --git a/modules/user/user.module b/modules/user/user.module
  2. index 47ac642..c204f6d 100644
  3. --- a/modules/user/user.module
  4. +++ b/modules/user/user.module
  5. @@ -2286,14 +2286,26 @@ function user_external_login_register($name, $module) {
  6. *
  7. * @param object $account
  8. * An object containing the user account.
  9. + * @param array $options
  10. + * (optional) A keyed array of settings. Supported options are:
  11. + * - langcode: A language code to be used when generating locale-sensitive
  12. + * urls. If langcode is NULL the users preferred language is used.
  13. *
  14. * @return
  15. * A unique URL that provides a one-time log in for the user, from which
  16. * they can change their password.
  17. */
  18. -function user_pass_reset_url($account) {
  19. +function user_pass_reset_url($account, $options = array()) {
  20. $timestamp = REQUEST_TIME;
  21. - return url("user/reset/$account->uid/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login), array('absolute' => TRUE));
  22. + $url_options = array('absolute' => TRUE);
  23. + if (isset($options['langcode'])) {
  24. + $languages = language_list();
  25. + $url_options['language'] = $languages[$options['langcode']];
  26. + }
  27. + else {
  28. + $url_options['language'] = user_preferred_language($account);
  29. + }
  30. + return url("user/reset/$account->uid/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login), $url_options);
  31. }
  32. /**
  33. @@ -2305,6 +2317,10 @@ function user_pass_reset_url($account) {
  34. * - uid: The user uid number.
  35. * - pass: The hashed user password string.
  36. * - login: The user login name.
  37. + * @param array $options
  38. + * (optional) A keyed array of settings. Supported options are:
  39. + * - langcode: A language code to be used when generating locale-sensitive
  40. + * urls. If langcode is NULL the users preferred language is used.
  41. *
  42. * @return
  43. * A unique URL that may be used to confirm the cancellation of the user
  44. @@ -2313,9 +2329,17 @@ function user_pass_reset_url($account) {
  45. * @see user_mail_tokens()
  46. * @see user_cancel_confirm()
  47. */
  48. -function user_cancel_url($account) {
  49. +function user_cancel_url($account, $options = array()) {
  50. $timestamp = REQUEST_TIME;
  51. - return url("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login), array('absolute' => TRUE));
  52. + $url_options = array('absolute' => TRUE);
  53. + if (isset($options['langcode'])) {
  54. + $languages = language_list();
  55. + $url_options['language'] = $languages[$options['langcode']];
  56. + }
  57. + else {
  58. + $url_options['language'] = user_preferred_language($account);
  59. + }
  60. + return url("user/$account->uid/cancel/confirm/$timestamp/" . user_pass_rehash($account->pass, $timestamp, $account->login), $url_options);
  61. }
  62. /**
  63. @@ -2752,7 +2776,7 @@ Your account on [site:name] has been canceled.
  64. if ($replace) {
  65. // We do not sanitize the token replacement, since the output of this
  66. // replacement is intended for an e-mail message, not a web browser.
  67. - return token_replace($text, $variables, array('language' => $language, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
  68. + return token_replace($text, $variables, array('langcode' => $langcode, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
  69. }
  70. return $text;
  71. @@ -2779,8 +2803,8 @@ Your account on [site:name] has been canceled.
  72. */
  73. function user_mail_tokens(&$replacements, $data, $options) {
  74. if (isset($data['user'])) {
  75. - $replacements['[user:one-time-login-url]'] = user_pass_reset_url($data['user']);
  76. - $replacements['[user:cancel-url]'] = user_cancel_url($data['user']);
  77. + $replacements['[user:one-time-login-url]'] = user_pass_reset_url($data['user'], $options);
  78. + $replacements['[user:cancel-url]'] = user_cancel_url($data['user'], $options);
  79. }
  80. }
  81. diff --git a/modules/user/user.test b/modules/user/user.test
  82. index b53db07..0417642 100644
  83. --- a/modules/user/user.test
  84. +++ b/modules/user/user.test
  85. @@ -2012,6 +2012,26 @@ class UserTokenReplaceTestCase extends DrupalWebTestCase {
  86. );
  87. }
  88. + public function setUp() {
  89. + parent::setUp('locale');
  90. +
  91. + $account = $this->drupalCreateUser(array('access administration pages', 'administer languages'));
  92. + $this->drupalLogin($account);
  93. +
  94. + // Add language.
  95. + $edit = array('langcode' => 'de');
  96. + $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  97. +
  98. + // Enable URL language detection and selection.
  99. + $edit = array('language[enabled][locale-url]' => 1);
  100. + $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  101. +
  102. + // Reset static caching.
  103. + drupal_static_reset('language_list');
  104. + drupal_static_reset('locale_url_outbound_alter');
  105. + drupal_static_reset('locale_language_url_rewrite_url');
  106. + }
  107. +
  108. /**
  109. * Creates a user, then tests the tokens generated from it.
  110. */
  111. @@ -2062,6 +2082,39 @@ class UserTokenReplaceTestCase extends DrupalWebTestCase {
  112. $output = token_replace($input, array('user' => $account), array('language' => $language, 'sanitize' => FALSE));
  113. $this->assertEqual($output, $expected, t('Unsanitized user token %token replaced.', array('%token' => $input)));
  114. }
  115. +
  116. + $languages = language_list();
  117. +
  118. + // Generate login and cancel link.
  119. + $tests = array();
  120. + $tests['[user:one-time-login-url]'] = user_pass_reset_url($account);
  121. + $tests['[user:cancel-url]'] = user_cancel_url($account);
  122. +
  123. + // Generate tokens with interface language.
  124. + $link = url('user', array('absolute' => TRUE));
  125. + foreach ($tests as $input => $expected) {
  126. + $output = token_replace($input, array('user' => $account), array('langcode' => $language->language, 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
  127. + $this->assertTrue(strpos($output, $link) === 0, 'Generated URL is in interface language.');
  128. + }
  129. +
  130. + // Generate tokens with the user's preferred language.
  131. + $edit['language'] = 'de';
  132. + $account = user_save($account, $edit);
  133. + $link = url('user', array('language' => $languages[$account->language], 'absolute' => TRUE));
  134. + foreach ($tests as $input => $expected) {
  135. + $output = token_replace($input, array('user' => $account), array('callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
  136. + $this->assertTrue(strpos($output, $link) === 0, "Generated URL is in the user's preferred language.");
  137. + }
  138. +
  139. + // Generate tokens with one specific language.
  140. + $link = url('user', array('language' => $languages['de'], 'absolute' => TRUE));
  141. + foreach ($tests as $input => $expected) {
  142. + foreach (array($user1, $user2) as $account) {
  143. + $output = token_replace($input, array('user' => $account), array('langcode' => 'de', 'callback' => 'user_mail_tokens', 'sanitize' => FALSE, 'clear' => TRUE));
  144. + $this->assertTrue(strpos($output, $link) === 0, "Generated URL in in the requested language.");
  145. + }
  146. + }
  147. +
  148. }
  149. }