From 8f291dfe3ea74970fb1b3cf95b62e379b224e191 Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Wed, 23 Jun 2021 16:07:25 -0400 Subject: [PATCH 1/2] feat(user): allow login without a browser Use login methods from the user module Remove drush requirement --- src/Codeception/Module/DrupalUser.php | 39 +++++++++++---------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/Codeception/Module/DrupalUser.php b/src/Codeception/Module/DrupalUser.php index 9aac7c4..e81e384 100644 --- a/src/Codeception/Module/DrupalUser.php +++ b/src/Codeception/Module/DrupalUser.php @@ -3,11 +3,9 @@ namespace Codeception\Module; use Codeception\Module; -use Drupal\Core\Config\StorageInterface; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\user\Entity\User; use Faker\Factory; -use Codeception\Util\Drush; /** * Class DrupalUser. @@ -17,8 +15,6 @@ * modules: * - DrupalUser: * default_role: 'authenticated' - * driver: 'PhpBrowser' - * drush: './vendor/bin/drush' * cleanup_entities: * - media * - file @@ -52,28 +48,13 @@ class DrupalUser extends Module { * @var array */ protected $config = [ - 'alias' => '', 'default_role' => 'authenticated', - 'driver' => 'WebDriver', - 'drush' => 'drush', '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->fail('User driver module not found.'); - } - - $this->driver = $this->getModule($this->_getConfig('driver')); - } - /** * {@inheritdoc} */ @@ -141,11 +122,21 @@ 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 { + // Load the user. + $account = user_load_by_name($username); + + if (FALSE === $account ) { + throw new \Exception(); + } + + // Login with the user. + user_login_finalize($account); + } + catch (\Exception $e) { + $this->fail('Coud not login with username ' . $username); + } } /** From 42d5aa3e180ca4ff175130962a0c2468e6b5da68 Mon Sep 17 00:00:00 2001 From: Mathew Winstone Date: Mon, 12 Sep 2022 21:29:21 -0400 Subject: [PATCH 2/2] refactor(module): use driver when available Fallback to CLI login if driver is not defined. Allows both acceptance and functional tests to login --- src/Codeception/Module/DrupalUser.php | 51 ++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/Codeception/Module/DrupalUser.php b/src/Codeception/Module/DrupalUser.php index e81e384..a54c234 100644 --- a/src/Codeception/Module/DrupalUser.php +++ b/src/Codeception/Module/DrupalUser.php @@ -3,8 +3,10 @@ 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; /** @@ -42,12 +44,22 @@ 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. * * @var array */ protected $config = [ + 'alias' => '', + 'driver' => NULL, + 'drush' => 'drush', 'default_role' => 'authenticated', 'cleanup_entities' => [], 'cleanup_test' => TRUE, @@ -55,6 +67,19 @@ class DrupalUser extends Module { '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} */ @@ -109,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; @@ -124,15 +150,24 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) { public function logInAs($username) { /** @var \Drupal\user\Entity\User $user */ try { - // Load the user. - $account = user_load_by_name($username); + if ($this->useCli) { + // Load the user. + $account = user_load_by_name($username); - if (FALSE === $account ) { - throw new \Exception(); - } + if (FALSE === $account ) { + throw new \Exception(); + } - // Login with the user. - user_login_finalize($account); + // 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);