-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/behat-definitions-commerce
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Frontkom\DrupalBehatDefinitions; | ||
|
||
use Drupal\Core\Url; | ||
use Drupal\DrupalExtension\Context\RawDrupalContext; | ||
|
||
/** | ||
* Class UserContext. | ||
* | ||
* Provide Behat step-definitions for user related operations. | ||
*/ | ||
class UserContext extends RawDrupalContext { | ||
|
||
/** | ||
* Helper to edit user. | ||
* | ||
* @When I edit user with email :mail | ||
*/ | ||
public function iEditUser($mail) { | ||
$id = $this->getUserIdByMail($mail); | ||
$this->visitPath('user/' . $id . '/edit'); | ||
} | ||
|
||
/** | ||
* Helper to visit a route with user param. | ||
* | ||
* @When I visit route :route with user parameter having mail :mail | ||
*/ | ||
public function iVisitRouteForUser($route, $mail) { | ||
$id = $this->getUserIdByMail($mail); | ||
$url = Url::fromRoute($route, ['user' => $id]); | ||
$this->visitPath($url->toString()); | ||
} | ||
|
||
/** | ||
* Helper to get user ID. | ||
*/ | ||
public function getUserIdByMail($mail) { | ||
$users = \Drupal::entityTypeManager()->getStorage('user') | ||
->getQuery() | ||
->condition('mail', $mail) | ||
->accessCheck() | ||
->execute(); | ||
if (empty($users)) { | ||
throw new \Exception('No users found with email ' . $mail); | ||
} | ||
if (count($users) > 1) { | ||
throw new \Exception('More than 1 user found with email ' . $mail); | ||
} | ||
return reset($users); | ||
} | ||
|
||
} |