3 namespace Drupal\token\Tests;
5 use Drupal\Core\Session\AnonymousUserSession;
6 use Drupal\field\Entity\FieldStorageConfig;
13 class TokenUserTest extends TokenTestBase {
18 * @var \Drupal\Core\Session\AccountInterface
20 protected $account = NULL;
27 public static $modules = array('token_user_picture');
32 public function setUp() {
35 $this->account = $this->drupalCreateUser(['administer users', 'administer account settings', 'access content']);
36 $this->drupalLogin($this->account);
39 public function testUserTokens() {
40 // Enable user pictures.
41 \Drupal::state()->set('user_pictures', 1);
42 \Drupal::state()->set('user_picture_file_size', '');
44 // Set up the pictures directory.
45 $picture_path = file_default_scheme() . '://' . \Drupal::state()->get('user_picture_path', 'pictures');
46 if (!file_prepare_directory($picture_path, FILE_CREATE_DIRECTORY)) {
47 $this->fail('Could not create directory ' . $picture_path . '.');
50 // Add a user picture to the account.
51 $image = current($this->drupalGetTestFiles('image'));
52 $edit = array('files[user_picture_0]' => drupal_realpath($image->uri));
53 $this->drupalPostForm('user/' . $this->account->id() . '/edit', $edit, t('Save'));
55 $storage = \Drupal::entityTypeManager()->getStorage('user');
57 // Load actual user data from database.
58 $storage->resetCache();
59 $this->account = $storage->load($this->account->id());
60 $this->assertTrue(!empty($this->account->user_picture->target_id), 'User picture uploaded.');
63 '#theme' => 'user_picture',
64 '#account' => $this->account,
66 /** @var \Drupal\Core\Render\RendererInterface $renderer */
67 $renderer = \Drupal::service('renderer');
69 'picture' => $renderer->renderPlain($picture),
70 'picture:fid' => $this->account->user_picture->target_id,
71 'picture:size-raw' => 125,
73 'roles' => implode(', ', $this->account->getRoles()),
75 $this->assertTokens('user', array('user' => $this->account), $user_tokens);
77 // Remove the simpletest-created user role.
78 $roles = $this->account->getRoles();
79 $this->account->removeRole(end($roles));
80 $this->account->save();
82 // Remove the user picture field and reload the user.
83 FieldStorageConfig::loadByName('user', 'user_picture')->delete();
84 $storage->resetCache();
85 $this->account = $storage->load($this->account->id());
89 'picture:fid' => NULL,
91 'roles' => 'authenticated',
92 'roles:keys' => (string) DRUPAL_AUTHENTICATED_RID,
94 $this->assertTokens('user', array('user' => $this->account), $user_tokens);
96 // The ip address token should work for the current user token type.
98 'ip-address' => \Drupal::request()->getClientIp(),
100 $this->assertTokens('current-user', array(), $tokens);
102 $anonymous = new AnonymousUserSession();
104 'roles' => 'anonymous',
105 'roles:keys' => (string) DRUPAL_ANONYMOUS_RID,
107 $this->assertTokens('user', array('user' => $anonymous), $tokens);
110 public function testUserAccountSettings() {
111 $this->drupalGet('admin/config/people/accounts');
112 $this->assertText('The list of available tokens that can be used in e-mails is provided below.');
113 $this->assertLink('Browse available tokens.');
114 $this->assertLinkByHref('token/tree');