|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\user\Entity\Role; |
|
6
|
|
|
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Tests the driver's handling of user entities. |
|
10
|
|
|
* |
|
11
|
|
|
* @group driver |
|
12
|
|
|
*/ |
|
13
|
|
|
class UserTest extends DriverEntityKernelTestBase { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Machine name of the entity type being tested. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $entityType = 'user'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function setUp() { |
|
26
|
|
|
parent::setUp(); |
|
27
|
|
|
$this->installSchema('user', 'users_data'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Test that a user can be created and deleted. |
|
32
|
|
|
*/ |
|
33
|
|
View Code Duplication |
public function testUserCreateDelete() { |
|
|
|
|
|
|
34
|
|
|
$name = $this->randomString(); |
|
35
|
|
|
$user = (object) [ |
|
36
|
|
|
'name' => $name, |
|
37
|
|
|
]; |
|
38
|
|
|
$user = $this->driver->userCreate($user); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
41
|
|
|
$this->assertEquals(1, count($entities)); |
|
42
|
|
|
|
|
43
|
|
|
// Status should be set to 1 by default. |
|
44
|
|
|
$entity = reset($entities); |
|
45
|
|
|
$this->assertEquals(1, $entity->status->value); |
|
46
|
|
|
|
|
47
|
|
|
// Looks like we forget to return the user object from userCreate, |
|
48
|
|
|
// so none of the code below works. But then how does userDelete ever work? |
|
49
|
|
|
/* // Check the id of the new user has been added to the returned object. |
|
50
|
|
|
$entity = reset($entities); |
|
51
|
|
|
$this->assertEquals($entity->id(), $user->uid); |
|
52
|
|
|
|
|
53
|
|
|
// Check the node can be deleted. |
|
54
|
|
|
$this->driver->userDelete($user); |
|
55
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
56
|
|
|
$this->assertEquals(0, count($entities));*/ |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Test that a blocked user can be created. |
|
61
|
|
|
*/ |
|
62
|
|
View Code Duplication |
public function testUserCreateBlocked() { |
|
|
|
|
|
|
63
|
|
|
$name = $this->randomString(); |
|
64
|
|
|
$user = (object) [ |
|
65
|
|
|
'name' => $name, |
|
66
|
|
|
'status' => 0, |
|
67
|
|
|
]; |
|
68
|
|
|
$user = $this->driver->userCreate($user); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
71
|
|
|
$this->assertEquals(1, count($entities)); |
|
72
|
|
|
|
|
73
|
|
|
// Status should be set to 0 as explicitly specified. |
|
74
|
|
|
$entity = reset($entities); |
|
75
|
|
|
$this->assertEquals(0, $entity->status->value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Test that a user can be given a role, using role label or machine name. |
|
80
|
|
|
*/ |
|
81
|
|
|
public function testUserAddRole() { |
|
82
|
|
|
$role1Id = $this->randomMachineName(); |
|
83
|
|
|
$role1Label = $this->randomString(); |
|
84
|
|
|
$role2Id = $this->randomMachineName(); |
|
85
|
|
|
$role2Label = $this->randomString(); |
|
86
|
|
|
$role3Id = $this->randomMachineName(); |
|
87
|
|
|
$role3Label = $this->randomString(); |
|
88
|
|
|
|
|
89
|
|
|
$role1 = Role::create(['id' => $role1Id, 'label' => $role1Label]); |
|
90
|
|
|
$role2 = Role::create(['id' => $role2Id, 'label' => $role2Label]); |
|
91
|
|
|
$role3 = Role::create(['id' => $role3Id, 'label' => $role3Label]); |
|
92
|
|
|
$role1->save(); |
|
93
|
|
|
$role2->save(); |
|
94
|
|
|
$role3->save(); |
|
95
|
|
|
|
|
96
|
|
|
$user = $this->createUser(); |
|
97
|
|
|
$userSimplified = (object) [ |
|
98
|
|
|
'uid' => $user->id(), |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
$this->driver->userAddRole($userSimplified, $role1Id); |
|
102
|
|
|
$this->driver->userAddRole($userSimplified, $role2Label); |
|
103
|
|
|
$user = $this->reloadEntity($user); |
|
104
|
|
|
|
|
105
|
|
|
// Check role detection is working. |
|
106
|
|
|
$this->assertFalse($user->hasRole($role3Id)); |
|
107
|
|
|
|
|
108
|
|
|
// Check user roles whether specified by machine name or label. |
|
109
|
|
|
$this->assertTrue($user->hasRole($role1Id)); |
|
110
|
|
|
$this->assertTrue($user->hasRole($role2Id)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Test that a user can be created and deleted. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function testUserCreateDeleteByWrapper() { |
|
117
|
|
|
$name = $this->randomString(); |
|
118
|
|
|
$fields = [ |
|
119
|
|
|
'name' => $name, |
|
120
|
|
|
]; |
|
121
|
|
|
$user = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
122
|
|
|
|
|
123
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
124
|
|
|
$this->assertEquals(1, count($entities)); |
|
125
|
|
|
|
|
126
|
|
|
// Status should be set to 1 by default. |
|
127
|
|
|
$entity = reset($entities); |
|
128
|
|
|
$this->assertEquals(1, $entity->status->value); |
|
129
|
|
|
|
|
130
|
|
|
// Check the id of the new user has been added to the returned object. |
|
131
|
|
|
$entity = reset($entities); |
|
132
|
|
|
$this->assertEquals($entity->id(), $user->uid); |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
// Check the node can be deleted. |
|
135
|
|
|
$user->delete(); |
|
136
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
137
|
|
|
$this->assertEquals(0, count($entities)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Test that a blocked user can be created. |
|
142
|
|
|
*/ |
|
143
|
|
View Code Duplication |
public function testUserCreateBlockedByWrapper() { |
|
|
|
|
|
|
144
|
|
|
$name = $this->randomString(); |
|
145
|
|
|
$fields = [ |
|
146
|
|
|
'name' => $name, |
|
147
|
|
|
'status' => 0, |
|
148
|
|
|
]; |
|
149
|
|
|
$user = DriverEntityDrupal8::create($fields, $this->entityType)->save(); |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
$entities = $this->storage->loadByProperties(['name' => $name]); |
|
152
|
|
|
$this->assertEquals(1, count($entities)); |
|
153
|
|
|
|
|
154
|
|
|
// Status should be set to 0 as explicitly specified. |
|
155
|
|
|
$entity = reset($entities); |
|
156
|
|
|
$this->assertEquals(0, $entity->status->value); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Test that a user can be given a role, using role label or machine name. |
|
161
|
|
|
*/ |
|
162
|
|
|
public function testUserAddRoleByWrapper() { |
|
163
|
|
|
$role1Id = $this->randomMachineName(); |
|
164
|
|
|
$role1Label = $this->randomString(); |
|
165
|
|
|
$role2Id = $this->randomMachineName(); |
|
166
|
|
|
$role2Label = $this->randomString(); |
|
167
|
|
|
$role3Id = $this->randomMachineName(); |
|
168
|
|
|
$role3Label = $this->randomString(); |
|
169
|
|
|
|
|
170
|
|
|
$role1 = Role::create(['id' => $role1Id, 'label' => $role1Label]); |
|
171
|
|
|
$role2 = Role::create(['id' => $role2Id, 'label' => $role2Label]); |
|
172
|
|
|
$role3 = Role::create(['id' => $role3Id, 'label' => $role3Label]); |
|
173
|
|
|
$role1->save(); |
|
174
|
|
|
$role2->save(); |
|
175
|
|
|
$role3->save(); |
|
176
|
|
|
|
|
177
|
|
|
$user = $this->createUser(); |
|
178
|
|
|
$userWrapped = new DriverEntityDrupal8('user'); |
|
179
|
|
|
$userWrapped->load($user->id()); |
|
180
|
|
|
$userWrapped->addRole($role1Id); |
|
|
|
|
|
|
181
|
|
|
$userWrapped->addRole($role2Label); |
|
|
|
|
|
|
182
|
|
|
$userWrapped->save(); |
|
183
|
|
|
$user = $this->reloadEntity($user); |
|
184
|
|
|
|
|
185
|
|
|
// Check role detection is working. |
|
186
|
|
|
$this->assertFalse($user->hasRole($role3Id)); |
|
187
|
|
|
|
|
188
|
|
|
// Check user roles whether specified by machine name or label. |
|
189
|
|
|
$this->assertTrue($user->hasRole($role1Id)); |
|
190
|
|
|
$this->assertTrue($user->hasRole($role2Id)); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.