Skip to content

Commit

Permalink
Merge branch 'main' into feat/behat-definitions-commerce
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm authored Nov 6, 2024
2 parents f09d9db + a031e40 commit b6c3027
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/UserContext.php
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);
}

}

0 comments on commit b6c3027

Please sign in to comment.