Skip to content

Commit 2957457

Browse files
committed
User module cleanup.
1 parent 97a222f commit 2957457

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

src/Codeception/Module/DrupalUser.php

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class DrupalUser extends Module {
3737
'default_role' => 'authenticated',
3838
'driver' => 'WebDriver',
3939
'drush' => 'drush',
40+
'cleanup_after_test' => TRUE,
41+
'cleanup_after_suite' => TRUE,
4042
];
4143

4244
/**
@@ -53,33 +55,28 @@ class DrupalUser extends Module {
5355
public function createUserWithRoles(array $roles = [], $password = FALSE) {
5456
$faker = Factory::create();
5557
/** @var \Drupal\user\Entity\User $user */
56-
$user = User::create([
58+
$user = \Drupal::entityTypeManager()->getStorage('user')->create([
5759
'name' => $faker->userName,
5860
'mail' => $faker->email,
5961
'roles' => empty($roles) ? $this->config['default_role'] : $roles,
6062
'pass' => $password ? $password : $faker->password(12, 14),
6163
'status' => 1,
6264
]);
6365

64-
try {
65-
$user->save();
66-
}
67-
catch (\Exception $e) {
68-
$this->fail($e);
69-
}
66+
$user->save();
7067
$this->users[] = $user->id();
7168

7269
return $user;
7370
}
7471

7572
/**
76-
* Log in user by id.
73+
* Log in user by username.
7774
*
7875
* @param string|int $uid
7976
* User id.
8077
*/
81-
public function logInWithUid($uid) {
82-
$output = Drush::runDrush('uli --uid=' . $uid, $this->config['drush'], $this->_getConfig('working_directory'));
78+
public function logInAs($username) {
79+
$output = Drush::runDrush('uli --name=' . $username, $this->config['drush'], $this->_getConfig('working_directory'));
8380
$gen_url = str_replace(PHP_EOL, '', $output);
8481
$url = substr($gen_url, strpos($gen_url, '/user/reset'));
8582
$this->driver->amOnPage($url);
@@ -92,33 +89,89 @@ public function logInWithUid($uid) {
9289
* Role.
9390
*/
9491
public function logInWithRole($role) {
95-
$user = $this->createUserWithRoles([$role]);
92+
$user = $this->createUserWithRoles([$role], Factory::create()->password(12, 14));
9693

97-
$this->logInWithRole($user->id());
94+
$this->logInAs($user->getUsername());
9895
}
9996

10097
/**
10198
* {@inheritdoc}
10299
*/
103100
public function _beforeSuite($settings = []) { // @codingStandardsIgnoreLine
104101
$this->driver = null;
105-
if (!$this->hasModule($this->config['module'])) {
102+
if (!$this->hasModule($this->_getConfig('driver'))) {
106103
$this->fail('User driver module not found.');
107104
}
108105
$this->driver = $this->getModule($this->config['driver']);
109106
}
110107

108+
/**
109+
* {@inheritdoc}
110+
*/
111+
public function _after(\Codeception\TestCase $test) { // @codingStandardsIgnoreLine
112+
if ($this->config['cleanup_after_test']) {
113+
$this->userCleanup();
114+
}
115+
}
116+
117+
/**
118+
* {@inheritdoc}
119+
*/
120+
public function _failed(\Codeception\TestCase $test) { // @codingStandardsIgnoreLine
121+
$this->_after($test);
122+
}
123+
111124
/**
112125
* {@inheritdoc}
113126
*/
114127
public function _afterSuite() { // @codingStandardsIgnoreLine
128+
if ($this->config['cleanup_after_suite']) {
129+
$this->userCleanup();
130+
}
131+
}
132+
133+
/**
134+
* Delete stored entities.
135+
*
136+
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
137+
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
138+
* @throws \Drupal\Core\Entity\EntityStorageException
139+
*/
140+
public function userCleanup() {
115141
if (isset($this->users)) {
116142
$users = User::loadMultiple($this->users);
117143
/** @var \Drupal\user\Entity\User $user */
118144
foreach ($users as $user) {
145+
$this->deleteUsersContent($user->id());
119146
$user->delete();
120147
}
121148
}
122149
}
123150

151+
/**
152+
* Delete user created entities.
153+
*
154+
* @param $uid
155+
* User id.
156+
*
157+
* @throws \Drupal\Core\Entity\EntityStorageException
158+
*/
159+
private function deleteUsersContent($uid) {
160+
$cleanup_entities = $this->_getConfig('clenup_entities');
161+
if (is_array($cleanup_entities)) {
162+
foreach ($cleanup_entities as $cleanup_entity) {
163+
try {
164+
$storage = \Drupal::entityTypeManager()->getStorage($cleanup_entity);
165+
}
166+
catch (\Exception $e) {
167+
continue;
168+
}
169+
$entities = $storage->loadByProperties(['uid' => $uid]);
170+
foreach ($entities as $entity) {
171+
$entity->delete();
172+
}
173+
}
174+
}
175+
}
176+
124177
}

src/Codeception/Module/DrupalWatchdog.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Codeception\Module;
66
use Drupal\Core\Logger\RfcLogLevel;
7+
use Drupal\Component\Utility\Xss;
78

89
/**
910
* Class DrupalWatchdog.

0 commit comments

Comments
 (0)