Skip to content

Commit

Permalink
Add user exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
guncha25 committed Sep 25, 2018
1 parent a1794ee commit 162bbcc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 62 deletions.
135 changes: 75 additions & 60 deletions src/Codeception/Module/DrupalUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,45 @@ class DrupalUser extends Module {
'cleanup_suite' => TRUE,
];

/**
* {@inheritdoc}
*/
public function _beforeSuite($settings = []) { // @codingStandardsIgnoreLine
$this->driver = null;
if (!$this->hasModule($this->_getConfig('driver'))) {
$this->fail('User driver module not found.');
}

$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.
*
Expand All @@ -68,24 +107,25 @@ class DrupalUser extends Module {
*
* @return \Drupal\user\Entity\User
* User object.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
public function createUserWithRoles(array $roles = [], $password = FALSE) {
$faker = Factory::create();
/** @var \Drupal\user\Entity\User $user */
$user = \Drupal::entityTypeManager()->getStorage('user')->create([
'name' => $faker->userName,
'mail' => $faker->email,
'roles' => empty($roles) ? $this->config['default_role'] : $roles,
'pass' => $password ? $password : $faker->password(12, 14),
'status' => 1,
]);

$user->save();
$this->users[] = $user->id();
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) {
$this->fail('Could not create user with roles' . implode(', ', $roles));
}

return $user;
}
Expand All @@ -97,7 +137,7 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) {
* User id.
*/
public function logInAs($username) {
$output = Drush::runDrush('uli --name=' . $username, $this->config['drush'], $this->_getConfig('working_directory'));
$output = Drush::runDrush('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);
Expand All @@ -109,52 +149,15 @@ public function logInAs($username) {
* @param string $role
* Role.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
* @return \Drupal\user\Entity\User
* User object.
*/
public function logInWithRole($role) {
$user = $this->createUserWithRoles([$role], Factory::create()->password(12, 14));

$this->logInAs($user->getUsername());
}

/**
* {@inheritdoc}
*/
public function _beforeSuite($settings = []) { // @codingStandardsIgnoreLine
$this->driver = null;
if (!$this->hasModule($this->_getConfig('driver'))) {
$this->fail('User driver module not found.');
}
$this->driver = $this->getModule($this->config['driver']);
}

/**
* {@inheritdoc}
*/
public function _after(\Codeception\TestCase $test) { // @codingStandardsIgnoreLine
if ($this->config['cleanup_test']) {
$this->userCleanup();
}
}

/**
* {@inheritdoc}
*/
public function _failed(\Codeception\TestCase $test, $fail) { // @codingStandardsIgnoreLine
if ($this->config['cleanup_failed']) {
$this->userCleanup();
}
}

/**
* {@inheritdoc}
*/
public function _afterSuite() { // @codingStandardsIgnoreLine
if ($this->config['cleanup_suite']) {
$this->userCleanup();
}
return $user;
}

/**
Expand All @@ -178,25 +181,37 @@ private function userCleanup() {
*
* @param string|int $uid
* User id.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
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 {
$storage = \Drupal::entityTypeManager()->getStorage($cleanup_entity);
}
catch (\Exception $e) {
$errors[] = 'Could not load storage ' . $cleanup_entity;
continue;
}
$entities = $storage->loadByProperties(['uid' => $uid]);
foreach ($entities as $entity) {
$entity->delete();
try {
$entities = $storage->loadByProperties(['uid' => $uid]);
foreach ($entities as $entity) {
$entity->delete();
}
}
catch (\Exception $e) {
$errors[] = 'Could not load and delete entities of type ' . $cleanup_entity . ' with uid ' . $uid;
continue;
}
}
}
if ($errors) {
$this->fail(implode(PHP_EOL, $errors));
}
}

}
4 changes: 2 additions & 2 deletions src/Codeception/Module/DrupalWatchdog.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ public function _afterSuite() { // @codingStandardsIgnoreLine
* Setting override.
*/
public function checkLogs(array $settings = []) {
$channels = isset($settings['channels']) ? $settings['channels'] :$this->_getConfig('channels');
$channels = isset($settings['channels']) ? $settings['channels'] : $this->_getConfig('channels');
if (!empty($channels) && is_array($channels)) {
foreach ($this->_getConfig('channels') as $channel => $level) {
if (is_string($level) && isset($this->logLevels[strtoupper($level)])) {
$this->processResult($this->getLogResults($this->logLevels[strtoupper($level)], $channel));
}
}
}
$level = isset($settings['level']) ? $settings['level'] :$this->_getConfig('level');
$level = isset($settings['level']) ? $settings['level'] : $this->_getConfig('level');
if (is_string($level) && isset($this->logLevels[strtoupper($level)])) {
$this->processResult($this->getLogResults($this->logLevels[strtoupper($level)]));
}
Expand Down

0 comments on commit 162bbcc

Please sign in to comment.