Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(user): allow login without a browser #13

Open
wants to merge 2 commits into
base: 9.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 40 additions & 14 deletions src/Codeception/Module/DrupalUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,8 +17,6 @@
* modules:
* - DrupalUser:
* default_role: 'authenticated'
* driver: 'PhpBrowser'
* drush: './vendor/bin/drush'
* cleanup_entities:
* - media
* - file
Expand Down Expand Up @@ -46,16 +44,23 @@ 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' => '',
'default_role' => 'authenticated',
'driver' => 'WebDriver',
'driver' => NULL,
'drush' => 'drush',
'default_role' => 'authenticated',
'cleanup_entities' => [],
'cleanup_test' => TRUE,
'cleanup_failed' => TRUE,
Expand All @@ -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'));
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

/**
Expand Down