View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Core\Url;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
class UserTokenReplaceTest extends BrowserTestBase {
protected static $modules = [
'language',
'user_hooks_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
ConfigurableLanguage::createFromLangcode('de')
->save();
}
public function testUserTokenReplacement() {
$token_service = \Drupal::token();
$language_interface = \Drupal::languageManager()
->getCurrentLanguage();
$url_options = [
'absolute' => TRUE,
'language' => $language_interface,
];
\Drupal::state()
->set('user_hooks_test_user_format_name_alter', TRUE);
\Drupal::state()
->set('user_hooks_test_user_format_name_alter_safe', TRUE);
$user1 = $this
->drupalCreateUser([]);
$user2 = $this
->drupalCreateUser([]);
$this
->drupalLogin($user1);
$this
->drupalLogout();
$this
->drupalLogin($user2);
$account = User::load($user1
->id());
$global_account = User::load(\Drupal::currentUser()
->id());
$date_formatter = $this->container
->get('date.formatter');
$tests = [];
$tests['[user:uid]'] = $account
->id();
$tests['[user:name]'] = $account
->getAccountName();
$tests['[user:account-name]'] = $account
->getAccountName();
$tests['[user:display-name]'] = $account
->getDisplayName();
$tests['[user:mail]'] = $account
->getEmail();
$tests['[user:url]'] = $account
->toUrl('canonical', $url_options)
->toString();
$tests['[user:edit-url]'] = $account
->toUrl('edit-form', $url_options)
->toString();
$tests['[user:last-login]'] = $date_formatter
->format($account
->getLastLoginTime(), 'medium', '', NULL, $language_interface
->getId());
$tests['[user:last-login:short]'] = $date_formatter
->format($account
->getLastLoginTime(), 'short', '', NULL, $language_interface
->getId());
$tests['[user:created]'] = $date_formatter
->format($account
->getCreatedTime(), 'medium', '', NULL, $language_interface
->getId());
$tests['[user:created:short]'] = $date_formatter
->format($account
->getCreatedTime(), 'short', '', NULL, $language_interface
->getId());
$tests['[current-user:name]'] = $global_account
->getAccountName();
$tests['[current-user:account-name]'] = $global_account
->getAccountName();
$tests['[current-user:display-name]'] = $global_account
->getDisplayName();
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($account);
$metadata_tests = [];
$metadata_tests['[user:uid]'] = $base_bubbleable_metadata;
$metadata_tests['[user:name]'] = $base_bubbleable_metadata;
$metadata_tests['[user:account-name]'] = $base_bubbleable_metadata;
$metadata_tests['[user:display-name]'] = $base_bubbleable_metadata;
$metadata_tests['[user:mail]'] = $base_bubbleable_metadata;
$metadata_tests['[user:url]'] = $base_bubbleable_metadata;
$metadata_tests['[user:edit-url]'] = $base_bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$bubbleable_metadata
->addCacheContexts([
'languages:language_interface',
]);
$metadata_tests['[user:last-login]'] = $bubbleable_metadata
->addCacheTags([
'rendered',
]);
$metadata_tests['[user:last-login:short]'] = $bubbleable_metadata;
$metadata_tests['[user:created]'] = $bubbleable_metadata;
$metadata_tests['[user:created:short]'] = $bubbleable_metadata;
$metadata_tests['[current-user:name]'] = $base_bubbleable_metadata
->merge(BubbleableMetadata::createFromObject($global_account)
->addCacheContexts([
'user',
]));
$metadata_tests['[current-user:account-name]'] = $base_bubbleable_metadata
->merge(BubbleableMetadata::createFromObject($global_account)
->addCacheContexts([
'user',
]));
$metadata_tests['[current-user:display-name]'] = $base_bubbleable_metadata
->merge(BubbleableMetadata::createFromObject($global_account)
->addCacheContexts([
'user',
]));
$this
->assertNotContains(0, array_map('strlen', $tests), 'No empty tokens generated.');
foreach ($tests as $input => $expected) {
$bubbleable_metadata = new BubbleableMetadata();
$output = $token_service
->replace($input, [
'user' => $account,
], [
'langcode' => $language_interface
->getId(),
], $bubbleable_metadata);
$this
->assertEquals($expected, $output, new FormattableMarkup('User token %token replaced.', [
'%token' => $input,
]));
$this
->assertEquals($metadata_tests[$input], $bubbleable_metadata);
}
$anonymous_user = User::load(0);
$tests = [];
$tests['[user:uid]'] = 'not yet assigned';
$tests['[user:display-name]'] = $anonymous_user
->getDisplayName();
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($anonymous_user);
$metadata_tests = [];
$metadata_tests['[user:uid]'] = $base_bubbleable_metadata;
$bubbleable_metadata = clone $base_bubbleable_metadata;
$bubbleable_metadata
->addCacheableDependency(\Drupal::config('user.settings'));
$metadata_tests['[user:display-name]'] = $bubbleable_metadata;
foreach ($tests as $input => $expected) {
$bubbleable_metadata = new BubbleableMetadata();
$output = $token_service
->replace($input, [
'user' => $anonymous_user,
], [
'langcode' => $language_interface
->getId(),
], $bubbleable_metadata);
$this
->assertEquals($expected, $output, new FormattableMarkup('Sanitized user token %token replaced.', [
'%token' => $input,
]));
$this
->assertEquals($metadata_tests[$input], $bubbleable_metadata);
}
$tests = [];
$tests['[user:one-time-login-url]'] = user_pass_reset_url($account);
$tests['[user:cancel-url]'] = user_cancel_url($account);
$link = Url::fromRoute('user.page', [], [
'absolute' => TRUE,
])
->toString();
foreach ($tests as $input => $expected) {
$output = $token_service
->replace($input, [
'user' => $account,
], [
'langcode' => $language_interface
->getId(),
'callback' => 'user_mail_tokens',
'clear' => TRUE,
]);
$this
->assertStringStartsWith($link, $output, 'Generated URL is in interface language.');
}
$account->preferred_langcode = 'de';
$account
->save();
$link = Url::fromRoute('user.page', [], [
'language' => \Drupal::languageManager()
->getLanguage($account
->getPreferredLangcode()),
'absolute' => TRUE,
])
->toString();
foreach ($tests as $input => $expected) {
$output = $token_service
->replace($input, [
'user' => $account,
], [
'callback' => 'user_mail_tokens',
'clear' => TRUE,
]);
$this
->assertStringStartsWith($link, $output, "Generated URL is in the user's preferred language.");
}
$link = Url::fromRoute('user.page', [], [
'language' => \Drupal::languageManager()
->getLanguage('de'),
'absolute' => TRUE,
])
->toString();
foreach ($tests as $input => $expected) {
foreach ([
$user1,
$user2,
] as $account) {
$output = $token_service
->replace($input, [
'user' => $account,
], [
'langcode' => 'de',
'callback' => 'user_mail_tokens',
'clear' => TRUE,
]);
$this
->assertStringStartsWith($link, $output, "Generated URL in the requested language.");
}
}
\Drupal::state()
->set('user_hooks_test_user_format_name_alter_safe', TRUE);
$input = '[user:display-name] [current-user:display-name]';
$expected = "<em>{$user1->id()}</em> <em>{$user2->id()}</em>";
$output = $token_service
->replace($input, [
'user' => $user1,
]);
$this
->assertEquals($expected, $output, new FormattableMarkup('User token %token does not escape safe markup.', [
'%token' => 'display-name',
]));
}
}