Created
April 17, 2024 13:58
-
-
Save nickopris/82812ed29c1f14dd817da4f88925111f to your computer and use it in GitHub Desktop.
Behat get current user
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
<?php | |
declare(strict_types = 1); | |
use Drupal\DrupalExtension\Context\RawDrupalContext; | |
/** | |
* Defines step definitions that are generally useful for the project. | |
*/ | |
class FeatureContext extends RawDrupalContext { | |
/** | |
* @return \Drupal\Core\Entity\EntityInterface|null | |
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException | |
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException | |
*/ | |
public function getCurrentUser() { | |
$this->getSession()->visit($this->locatePath($this->getDrupalText('login_url'))); | |
$user_url = $this->getSession()->getCurrentUrl(); | |
preg_match('/(?<=\/)\d+/', $user_url, $matches); | |
if ($matches) { | |
$user_id = reset($matches); | |
$user = \Drupal::entityTypeManager() | |
->getStorage('user')->load($user_id); | |
if ($user) { | |
return $user; | |
} | |
} | |
return NULL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment