diff --git a/src/Codeception/Module/DrupalUser.php b/src/Codeception/Module/DrupalUser.php index 9aac7c4..a54c234 100644 --- a/src/Codeception/Module/DrupalUser.php +++ b/src/Codeception/Module/DrupalUser.php @@ -6,8 +6,8 @@ use Drupal\Core\Config\StorageInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\user\Entity\User; -use Faker\Factory; use Codeception\Util\Drush; +use Faker\Factory; /** * Class DrupalUser. @@ -17,8 +17,6 @@ * modules: * - DrupalUser: * default_role: 'authenticated' - * driver: 'PhpBrowser' - * drush: './vendor/bin/drush' * cleanup_entities: * - media * - file @@ -46,6 +44,13 @@ class DrupalUser extends Module { */ protected $users; + /** + * Flag to note whether the CLI should be used for user actions. + * + * @var bool + */ + protected $useCli = FALSE; + /** * Default module configuration. * @@ -53,9 +58,9 @@ class DrupalUser extends Module { */ protected $config = [ 'alias' => '', - 'default_role' => 'authenticated', - 'driver' => 'WebDriver', + 'driver' => NULL, 'drush' => 'drush', + 'default_role' => 'authenticated', 'cleanup_entities' => [], 'cleanup_test' => TRUE, 'cleanup_failed' => TRUE, @@ -68,10 +73,11 @@ class DrupalUser extends Module { public function _beforeSuite($settings = []) { // @codingStandardsIgnoreLine $this->driver = null; if (!$this->hasModule($this->_getConfig('driver'))) { - $this->fail('User driver module not found.'); + $this->useCli = TRUE; + } + else { + $this->driver = $this->getModule($this->_getConfig('driver')); } - - $this->driver = $this->getModule($this->_getConfig('driver')); } /** @@ -128,7 +134,8 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) { $this->users[] = $user->id(); } catch (\Exception $e) { - $this->fail('Could not create user with roles' . implode(', ', $roles)); + $message = sprintf('Could not create user with roles: %s. Error: %s', implode(', ', $roles), $e->getMessage()); + $this->fail($message); } return $user; @@ -141,11 +148,30 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) { * User id. */ public function logInAs($username) { - $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); + /** @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); + } } /**