Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / src / Tests / TokenUserTest.php
1 <?php
2
3 namespace Drupal\token\Tests;
4
5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\Core\Session\AnonymousUserSession;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10  * Tests user tokens.
11  *
12  * @group token
13  */
14 class TokenUserTest extends TokenTestBase {
15
16   /**
17    * The user account.
18    *
19    * @var \Drupal\Core\Session\AccountInterface
20    */
21   protected $account = NULL;
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = ['token_user_picture'];
29
30   /**
31    * {@inheritdoc}
32    */
33   public function setUp() {
34     parent::setUp();
35
36     $this->account = $this->drupalCreateUser([
37       'administer users',
38       'administer account settings',
39       'access content',
40     ]);
41     $this->drupalLogin($this->account);
42   }
43
44   /**
45    * Tests the user releated tokens.
46    */
47   public function testUserTokens() {
48     // Enable user pictures.
49     \Drupal::state()->set('user_pictures', 1);
50     \Drupal::state()->set('user_picture_file_size', '');
51
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 . '.');
56     }
57
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'));
62
63     $storage = \Drupal::entityTypeManager()->getStorage('user');
64
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.');
69
70     $picture = [
71       '#theme' => 'user_picture',
72       '#account' => $this->account,
73     ];
74     /** @var \Drupal\Core\Render\RendererInterface $renderer */
75     $renderer = \Drupal::service('renderer');
76     $user_tokens = [
77       'picture' => $renderer->renderPlain($picture),
78       'picture:fid' => $this->account->user_picture->target_id,
79       'picture:size-raw' => 125,
80       'ip-address' => NULL,
81       'roles' => implode(', ', $this->account->getRoles()),
82     ];
83     $this->assertTokens('user', ['user' => $this->account], $user_tokens);
84
85     // Remove the simpletest-created user role.
86     $roles = $this->account->getRoles();
87     $this->account->removeRole(end($roles));
88     $this->account->save();
89
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());
94
95     $user_tokens = [
96       'picture' => NULL,
97       'picture:fid' => NULL,
98       'ip-address' => NULL,
99       'roles' => 'authenticated',
100       'roles:keys' => AccountInterface::AUTHENTICATED_ROLE,
101     ];
102     $this->assertTokens('user', ['user' => $this->account], $user_tokens);
103
104     // The ip address token should work for the current user token type.
105     $tokens = [
106       'ip-address' => \Drupal::request()->getClientIp(),
107     ];
108     $this->assertTokens('current-user', [], $tokens);
109
110     $anonymous = new AnonymousUserSession();
111     $tokens = [
112       'roles' => 'anonymous',
113       'roles:keys' => AccountInterface::ANONYMOUS_ROLE,
114     ];
115     $this->assertTokens('user', ['user' => $anonymous], $tokens);
116   }
117
118   /**
119    * Test user account settings.
120    */
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');
126   }
127 }