Skip to content

Commit

Permalink
Create user context (#10)
Browse files Browse the repository at this point in the history
* Create user context

* Changed method visibility

---------

Co-authored-by: Eirik Stanghelle Morland <[email protected]>
  • Loading branch information
adamsokolowski06 and eiriksm authored Nov 4, 2024
1 parent e9b76e8 commit a031e40
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 a031e40

Please sign in to comment.