|
3 | 3 | namespace Codeception\Module; |
4 | 4 |
|
5 | 5 | use Codeception\Module; |
| 6 | +use Drupal\Core\Config\StorageInterface; |
6 | 7 | use Drupal\Core\Entity\EntityStorageInterface; |
7 | 8 | use Drupal\user\Entity\User; |
| 9 | +use Codeception\Util\Drush; |
8 | 10 | use Faker\Factory; |
9 | 11 |
|
10 | 12 | /** |
@@ -42,19 +44,42 @@ class DrupalUser extends Module { |
42 | 44 | */ |
43 | 45 | protected $users; |
44 | 46 |
|
| 47 | + /** |
| 48 | + * Flag to note whether the CLI should be used for user actions. |
| 49 | + * |
| 50 | + * @var bool |
| 51 | + */ |
| 52 | + protected $useCli = FALSE; |
| 53 | + |
45 | 54 | /** |
46 | 55 | * Default module configuration. |
47 | 56 | * |
48 | 57 | * @var array |
49 | 58 | */ |
50 | 59 | protected $config = [ |
| 60 | + 'alias' => '', |
| 61 | + 'driver' => NULL, |
| 62 | + 'drush' => 'drush', |
51 | 63 | 'default_role' => 'authenticated', |
52 | 64 | 'cleanup_entities' => [], |
53 | 65 | 'cleanup_test' => TRUE, |
54 | 66 | 'cleanup_failed' => TRUE, |
55 | 67 | 'cleanup_suite' => TRUE, |
56 | 68 | ]; |
57 | 69 |
|
| 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 | + |
58 | 83 | /** |
59 | 84 | * {@inheritdoc} |
60 | 85 | */ |
@@ -125,15 +150,24 @@ public function createUserWithRoles(array $roles = [], $password = FALSE) { |
125 | 150 | public function logInAs($username) { |
126 | 151 | /** @var \Drupal\user\Entity\User $user */ |
127 | 152 | 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); |
130 | 156 |
|
131 | | - if (FALSE === $account ) { |
132 | | - throw new \Exception(); |
133 | | - } |
| 157 | + if (FALSE === $account ) { |
| 158 | + throw new \Exception(); |
| 159 | + } |
134 | 160 |
|
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 | + } |
137 | 171 | } |
138 | 172 | catch (\Exception $e) { |
139 | 173 | $this->fail('Coud not login with username ' . $username); |
|
0 commit comments