comparison core/modules/user/tests/src/Functional/UserTokenReplaceTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\user\Functional; 3 namespace Drupal\Tests\user\Functional;
4 4
5 use Drupal\Core\Url;
5 use Drupal\Component\Render\FormattableMarkup; 6 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Render\BubbleableMetadata; 7 use Drupal\Core\Render\BubbleableMetadata;
7 use Drupal\language\Entity\ConfigurableLanguage; 8 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\Tests\BrowserTestBase; 9 use Drupal\Tests\BrowserTestBase;
9 use Drupal\user\Entity\User; 10 use Drupal\user\Entity\User;
53 $this->drupalLogin($user2); 54 $this->drupalLogin($user2);
54 55
55 $account = User::load($user1->id()); 56 $account = User::load($user1->id());
56 $global_account = User::load(\Drupal::currentUser()->id()); 57 $global_account = User::load(\Drupal::currentUser()->id());
57 58
59 /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
60 $date_formatter = $this->container->get('date.formatter');
61
58 // Generate and test tokens. 62 // Generate and test tokens.
59 $tests = []; 63 $tests = [];
60 $tests['[user:uid]'] = $account->id(); 64 $tests['[user:uid]'] = $account->id();
61 $tests['[user:name]'] = $account->getAccountName(); 65 $tests['[user:name]'] = $account->getAccountName();
62 $tests['[user:account-name]'] = $account->getAccountName(); 66 $tests['[user:account-name]'] = $account->getAccountName();
63 $tests['[user:display-name]'] = $account->getDisplayName(); 67 $tests['[user:display-name]'] = $account->getDisplayName();
64 $tests['[user:mail]'] = $account->getEmail(); 68 $tests['[user:mail]'] = $account->getEmail();
65 $tests['[user:url]'] = $account->url('canonical', $url_options); 69 $tests['[user:url]'] = $account->toUrl('canonical', $url_options)->toString();
66 $tests['[user:edit-url]'] = $account->url('edit-form', $url_options); 70 $tests['[user:edit-url]'] = $account->toUrl('edit-form', $url_options)->toString();
67 $tests['[user:last-login]'] = format_date($account->getLastLoginTime(), 'medium', '', NULL, $language_interface->getId()); 71 $tests['[user:last-login]'] = $date_formatter->format($account->getLastLoginTime(), 'medium', '', NULL, $language_interface->getId());
68 $tests['[user:last-login:short]'] = format_date($account->getLastLoginTime(), 'short', '', NULL, $language_interface->getId()); 72 $tests['[user:last-login:short]'] = $date_formatter->format($account->getLastLoginTime(), 'short', '', NULL, $language_interface->getId());
69 $tests['[user:created]'] = format_date($account->getCreatedTime(), 'medium', '', NULL, $language_interface->getId()); 73 $tests['[user:created]'] = $date_formatter->format($account->getCreatedTime(), 'medium', '', NULL, $language_interface->getId());
70 $tests['[user:created:short]'] = format_date($account->getCreatedTime(), 'short', '', NULL, $language_interface->getId()); 74 $tests['[user:created:short]'] = $date_formatter->format($account->getCreatedTime(), 'short', '', NULL, $language_interface->getId());
71 $tests['[current-user:name]'] = $global_account->getAccountName(); 75 $tests['[current-user:name]'] = $global_account->getAccountName();
72 $tests['[current-user:account-name]'] = $global_account->getAccountName(); 76 $tests['[current-user:account-name]'] = $global_account->getAccountName();
73 $tests['[current-user:display-name]'] = $global_account->getDisplayName(); 77 $tests['[current-user:display-name]'] = $global_account->getDisplayName();
74 78
75 $base_bubbleable_metadata = BubbleableMetadata::createFromObject($account); 79 $base_bubbleable_metadata = BubbleableMetadata::createFromObject($account);
130 $tests = []; 134 $tests = [];
131 $tests['[user:one-time-login-url]'] = user_pass_reset_url($account); 135 $tests['[user:one-time-login-url]'] = user_pass_reset_url($account);
132 $tests['[user:cancel-url]'] = user_cancel_url($account); 136 $tests['[user:cancel-url]'] = user_cancel_url($account);
133 137
134 // Generate tokens with interface language. 138 // Generate tokens with interface language.
135 $link = \Drupal::url('user.page', [], ['absolute' => TRUE]); 139 $link = Url::fromRoute('user.page', [], ['absolute' => TRUE])->toString();
136 foreach ($tests as $input => $expected) { 140 foreach ($tests as $input => $expected) {
137 $output = $token_service->replace($input, ['user' => $account], ['langcode' => $language_interface->getId(), 'callback' => 'user_mail_tokens', 'clear' => TRUE]); 141 $output = $token_service->replace($input, ['user' => $account], ['langcode' => $language_interface->getId(), 'callback' => 'user_mail_tokens', 'clear' => TRUE]);
138 $this->assertTrue(strpos($output, $link) === 0, 'Generated URL is in interface language.'); 142 $this->assertTrue(strpos($output, $link) === 0, 'Generated URL is in interface language.');
139 } 143 }
140 144
141 // Generate tokens with the user's preferred language. 145 // Generate tokens with the user's preferred language.
142 $account->preferred_langcode = 'de'; 146 $account->preferred_langcode = 'de';
143 $account->save(); 147 $account->save();
144 $link = \Drupal::url('user.page', [], ['language' => \Drupal::languageManager()->getLanguage($account->getPreferredLangcode()), 'absolute' => TRUE]); 148 $link = Url::fromRoute('user.page', [], ['language' => \Drupal::languageManager()->getLanguage($account->getPreferredLangcode()), 'absolute' => TRUE])->toString();
145 foreach ($tests as $input => $expected) { 149 foreach ($tests as $input => $expected) {
146 $output = $token_service->replace($input, ['user' => $account], ['callback' => 'user_mail_tokens', 'clear' => TRUE]); 150 $output = $token_service->replace($input, ['user' => $account], ['callback' => 'user_mail_tokens', 'clear' => TRUE]);
147 $this->assertTrue(strpos($output, $link) === 0, "Generated URL is in the user's preferred language."); 151 $this->assertTrue(strpos($output, $link) === 0, "Generated URL is in the user's preferred language.");
148 } 152 }
149 153
150 // Generate tokens with one specific language. 154 // Generate tokens with one specific language.
151 $link = \Drupal::url('user.page', [], ['language' => \Drupal::languageManager()->getLanguage('de'), 'absolute' => TRUE]); 155 $link = Url::fromRoute('user.page', [], ['language' => \Drupal::languageManager()->getLanguage('de'), 'absolute' => TRUE])->toString();
152 foreach ($tests as $input => $expected) { 156 foreach ($tests as $input => $expected) {
153 foreach ([$user1, $user2] as $account) { 157 foreach ([$user1, $user2] as $account) {
154 $output = $token_service->replace($input, ['user' => $account], ['langcode' => 'de', 'callback' => 'user_mail_tokens', 'clear' => TRUE]); 158 $output = $token_service->replace($input, ['user' => $account], ['langcode' => 'de', 'callback' => 'user_mail_tokens', 'clear' => TRUE]);
155 $this->assertTrue(strpos($output, $link) === 0, "Generated URL in the requested language."); 159 $this->assertTrue(strpos($output, $link) === 0, "Generated URL in the requested language.");
156 } 160 }