3 namespace Drupal\token\Tests;
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\Core\Session\AnonymousUserSession;
7 use Drupal\field\Entity\FieldStorageConfig;
14 class TokenUserTest extends TokenTestBase {
19 * @var \Drupal\Core\Session\AccountInterface
21 protected $account = NULL;
28 public static $modules = ['token_user_picture'];
33 public function setUp() {
36 $this->account = $this->drupalCreateUser([
38 'administer account settings',
41 $this->drupalLogin($this->account);
45 * Tests the user releated tokens.
47 public function testUserTokens() {
48 // Enable user pictures.
49 \Drupal::state()->set('user_pictures', 1);
50 \Drupal::state()->set('user_picture_file_size', '');
52 // Set up the pictures directory.
53 $picture_path = file_default_scheme() . '://' . \Drupal::state()->get('user_picture_path', 'pictures');
54 if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
55 $this->fail('Could not create directory ' . $picture_path . '.');
58 // Add a user picture to the account.
59 $image = current($this->drupalGetTestFiles('image'));
60 $edit = ['files[user_picture_0]' => \Drupal::service('file_system')->realpath($image->uri)];
61 $this->drupalPostForm('user/' . $this->account->id() . '/edit', $edit, t('Save'));
63 $storage = \Drupal::entityTypeManager()->getStorage('user');
65 // Load actual user data from database.
66 $storage->resetCache();
67 $this->account = $storage->load($this->account->id());
68 $this->assertTrue(!empty($this->account->user_picture->target_id), 'User picture uploaded.');
71 '#theme' => 'user_picture',
72 '#account' => $this->account,
74 /** @var \Drupal\Core\Render\RendererInterface $renderer */
75 $renderer = \Drupal::service('renderer');
77 'picture' => $renderer->renderPlain($picture),
78 'picture:fid' => $this->account->user_picture->target_id,
79 'picture:size-raw' => 125,
81 'roles' => implode(', ', $this->account->getRoles()),
83 $this->assertTokens('user', ['user' => $this->account], $user_tokens);
85 // Remove the simpletest-created user role.
86 $roles = $this->account->getRoles();
87 $this->account->removeRole(end($roles));
88 $this->account->save();
90 // Remove the user picture field and reload the user.
91 FieldStorageConfig::loadByName('user', 'user_picture')->delete();
92 $storage->resetCache();
93 $this->account = $storage->load($this->account->id());
97 'picture:fid' => NULL,
99 'roles' => 'authenticated',
100 'roles:keys' => AccountInterface::AUTHENTICATED_ROLE,
102 $this->assertTokens('user', ['user' => $this->account], $user_tokens);
104 // The ip address token should work for the current user token type.
106 'ip-address' => \Drupal::request()->getClientIp(),
108 $this->assertTokens('current-user', [], $tokens);
110 $anonymous = new AnonymousUserSession();
112 'roles' => 'anonymous',
113 'roles:keys' => AccountInterface::ANONYMOUS_ROLE,
115 $this->assertTokens('user', ['user' => $anonymous], $tokens);
119 * Test user account settings.
121 public function testUserAccountSettings() {
122 $this->drupalGet('admin/config/people/accounts');
123 $this->assertText('The list of available tokens that can be used in e-mails is provided below.');
124 $this->assertLink('Browse available tokens.');
125 $this->assertLinkByHref('token/tree');