Skip to content

Commit 5501414

Browse files
authored
refactor(module): use driver when available (#2)
Fallback to CLI login if driver is not defined. Allows both acceptance and functional tests to login
1 parent 4eeac70 commit 5501414

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

src/Codeception/Module/DrupalUser.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Codeception\Module;
44

55
use Codeception\Module;
6+
use Drupal\Core\Config\StorageInterface;
67
use Drupal\Core\Entity\EntityStorageInterface;
78
use Drupal\user\Entity\User;
9+
use Codeception\Util\Drush;
810
use Faker\Factory;
911

1012
/**
@@ -42,19 +44,42 @@ class DrupalUser extends Module {
4244
*/
4345
protected $users;
4446

47+
/**
48+
* Flag to note whether the CLI should be used for user actions.
49+
*
50+
* @var bool
51+
*/
52+
protected $useCli = FALSE;
53+
4554
/**
4655
* Default module configuration.
4756
*
4857
* @var array
4958
*/
5059
protected $config = [
60+
'alias' => '',
61+
'driver' => NULL,
62+
'drush' => 'drush',
5163
'default_role' => 'authenticated',
5264
'cleanup_entities' => [],
5365
'cleanup_test' => TRUE,
5466
'cleanup_failed' => TRUE,
5567
'cleanup_suite' => TRUE,
5668
];
5769

70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function _beforeSuite($settings = []) { // @codingStandardsIgnoreLine
74+
$this->driver = null;
75+
if (!$this->hasModule($this->_getConfig('driver'))) {
76+
$this->useCli = TRUE;
77+
}
78+
else {
79+
$this->driver = $this->getModule($this->_getConfig('driver'));
80+
}
81+
}
82+
5883
/**
5984
* {@inheritdoc}
6085
*/
@@ -125,15 +150,24 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) {
125150
public function logInAs($username) {
126151
/** @var \Drupal\user\Entity\User $user */
127152
try {
128-
// Load the user.
129-
$account = user_load_by_name($username);
153+
if ($this->useCli) {
154+
// Load the user.
155+
$account = user_load_by_name($username);
130156

131-
if (FALSE === $account ) {
132-
throw new \Exception();
133-
}
157+
if (FALSE === $account ) {
158+
throw new \Exception();
159+
}
134160

135-
// Login with the user.
136-
user_login_finalize($account);
161+
// Login with the user.
162+
user_login_finalize($account);
163+
}
164+
else {
165+
$alias = $this->_getConfig('alias') ? $this->_getConfig('alias') . ' ' : '';
166+
$output = Drush::runDrush($alias. 'uli --name=' . $username, $this->_getConfig('drush'), $this->_getConfig('working_directory'));
167+
$gen_url = str_replace(PHP_EOL, '', $output);
168+
$url = substr($gen_url, strpos($gen_url, '/user/reset'));
169+
$this->driver->amOnPage($url);
170+
}
137171
}
138172
catch (\Exception $e) {
139173
$this->fail('Coud not login with username ' . $username);

0 commit comments

Comments
 (0)