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