From a031e403bc47736d39ce234d10c4e381d0e944c3 Mon Sep 17 00:00:00 2001 From: adamsokolowski06 <37297805+adamsokolowski06@users.noreply.github.com> Date: Mon, 4 Nov 2024 14:51:25 +0100 Subject: [PATCH] Create user context (#10) * Create user context * Changed method visibility --------- Co-authored-by: Eirik Stanghelle Morland --- src/UserContext.php | 54 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/UserContext.php diff --git a/src/UserContext.php b/src/UserContext.php new file mode 100644 index 0000000..b6f3a01 --- /dev/null +++ b/src/UserContext.php @@ -0,0 +1,54 @@ +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); + } + +}