-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDrupalUser.php
More file actions
265 lines (242 loc) · 6.44 KB
/
DrupalUser.php
File metadata and controls
265 lines (242 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace Codeception\Module;
use Codeception\Module;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\user\Entity\User;
use Codeception\Util\Drush;
use Faker\Factory;
/**
* Class DrupalUser.
*
* ### Example
* #### Example (DrupalUser)
* modules:
* - DrupalUser:
* default_role: 'authenticated'
* cleanup_entities:
* - media
* - file
* - paragraph
* cleanup_test: false
* cleanup_failed: false
* cleanup_suite: true
* alias: @site.com
*
* @package Codeception\Module
*/
class DrupalUser extends Module {
/**
* Driver to use.
*
* @var WebDriver|PhpBrowser
*/
protected $driver;
/**
* A list of user ids created during test suite.
*
* @var array
*/
protected $users;
/**
* Flag to note whether the CLI should be used for user actions.
*
* @var bool
*/
protected $useCli = FALSE;
/**
* Default module configuration.
*
* @var array
*/
protected $config = [
'alias' => '',
'driver' => NULL,
'drush' => 'drush',
'default_role' => 'authenticated',
'cleanup_entities' => [],
'cleanup_test' => TRUE,
'cleanup_failed' => TRUE,
'cleanup_suite' => TRUE,
];
/**
* {@inheritdoc}
*/
public function _beforeSuite($settings = []) { // @codingStandardsIgnoreLine
$this->driver = null;
if (!$this->hasModule($this->_getConfig('driver'))) {
$this->useCli = TRUE;
}
else {
$this->driver = $this->getModule($this->_getConfig('driver'));
}
}
/**
* {@inheritdoc}
*/
public function _after(\Codeception\TestCase $test) { // @codingStandardsIgnoreLine
if ($this->_getConfig('cleanup_test')) {
$this->userCleanup();
}
}
/**
* {@inheritdoc}
*/
public function _failed(\Codeception\TestCase $test, $fail) { // @codingStandardsIgnoreLine
if ($this->_getConfig('cleanup_failed')) {
$this->userCleanup();
}
}
/**
* {@inheritdoc}
*/
public function _afterSuite() { // @codingStandardsIgnoreLine
if ($this->_getConfig('cleanup_suite')) {
$this->userCleanup();
}
}
/**
* Create test user with specified roles.
*
* @param array $roles
* List of user roles.
* @param mixed $password
* Password.
*
* @return \Drupal\user\Entity\User
* User object.
*/
public function createUserWithRoles(array $roles = [], $password = FALSE) {
$faker = Factory::create();
/** @var \Drupal\user\Entity\User $user */
try {
$user = \Drupal::entityTypeManager()->getStorage('user')->create([
'name' => $faker->userName,
'mail' => $faker->email,
'roles' => empty($roles) ? $this->_getConfig('default_role') : $roles,
'pass' => $password ? $password : $faker->password(12, 14),
'status' => 1,
]);
$user->save();
$this->users[] = $user->id();
}
catch (\Exception $e) {
$message = sprintf('Could not create user with roles: %s. Error: %s', implode(', ', $roles), $e->getMessage());
$this->fail($message);
}
return $user;
}
/**
* Log in user by username.
*
* @param string|int $username
* User id.
*/
public function logInAs($username) {
/** @var \Drupal\user\Entity\User $user */
try {
if ($this->useCli) {
// Load the user.
$account = user_load_by_name($username);
if (FALSE === $account ) {
throw new \Exception();
}
// Login with the user.
user_login_finalize($account);
}
else {
$alias = $this->_getConfig('alias') ? $this->_getConfig('alias') . ' ' : '';
$output = Drush::runDrush($alias. 'uli --name=' . $username, $this->_getConfig('drush'), $this->_getConfig('working_directory'));
$gen_url = str_replace(PHP_EOL, '', $output);
$url = substr($gen_url, strpos($gen_url, '/user/reset'));
$this->driver->amOnPage($url);
}
}
catch (\Exception $e) {
$this->fail('Coud not login with username ' . $username);
}
}
/**
* Create user with role and Log in.
*
* @param string $role
* Role.
*
* @return \Drupal\user\Entity\User
* User object.
*/
public function logInWithRole($role) {
$user = $this->createUserWithRoles([$role], Factory::create()->password(12, 14));
$this->logInAs($user->getAccountName());
return $user;
}
/**
* Delete stored entities.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
private function userCleanup() {
if (isset($this->users)) {
$users = User::loadMultiple($this->users);
/** @var \Drupal\user\Entity\User $user */
foreach ($users as $user) {
$this->deleteUsersContent($user->id());
try {
$user->delete();
} catch (\Exception $e) {
continue;
}
}
}
}
/**
* Delete user created entities.
*
* @param string|int $uid
* User id.
*/
private function deleteUsersContent($uid) {
$errors = [];
$cleanup_entities = $this->_getConfig('cleanup_entities');
if (is_array($cleanup_entities)) {
foreach ($cleanup_entities as $cleanup_entity) {
if (!is_string($cleanup_entity)) {
continue;
}
try {
/** @var EntityStorageInterface $storage */
$storage = \Drupal::entityTypeManager()->getStorage($cleanup_entity);
}
catch (\Exception $e) {
$errors[] = 'Could not load storage ' . $cleanup_entity;
continue;
}
try {
$bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($cleanup_entity);
foreach ($bundles as $bundle => $bundle_data) {
$all_bundle_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions($cleanup_entity, $bundle);
if (isset($all_bundle_fields['uid'])) {
$entities = $storage->loadByProperties(['uid' => $uid]);
}
}
}
catch (\Exception $e) {
$errors[] = 'Could not load entities of type ' . $cleanup_entity . ' by uid ' . $uid;
continue;
}
try {
foreach ($entities as $entity) {
$entity->delete();
}
}
catch (\Exception $e) {
$errors[] = $e->getMessage();
continue;
}
}
}
if ($errors) {
$this->fail(implode(PHP_EOL, $errors));
}
}
}