X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fdrupal%2Fdrupal-extension%2Fsrc%2FDrupal%2FDrupalExtension%2FContext%2FDrupalSubContextBase.php;fp=vendor%2Fdrupal%2Fdrupal-extension%2Fsrc%2FDrupal%2FDrupalExtension%2FContext%2FDrupalSubContextBase.php;h=8bdac8341bd3b4633941a1e9064854fbb95d41b0;hb=1270d9129ce8f27c9b28b10518e32132c58e0aca;hp=0000000000000000000000000000000000000000;hpb=c27c0f0cdaa3f354b1fe54a56ae7e854be6e3f68;p=yaffs-website diff --git a/vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/DrupalSubContextBase.php b/vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/DrupalSubContextBase.php new file mode 100644 index 000000000..8bdac8341 --- /dev/null +++ b/vendor/drupal/drupal-extension/src/Drupal/DrupalExtension/Context/DrupalSubContextBase.php @@ -0,0 +1,85 @@ +drupal = $drupal; + } + + /** + * Get the currently logged in user from DrupalContext. + */ + protected function getUser() { + /** @var DrupalContext $context */ + $context = $this->getContext('\Drupal\DrupalExtension\Context\DrupalContext'); + if (empty($context->user)) { + throw new \Exception('No user is logged in.'); + } + + return $context->user; + } + + /** + * Returns the Behat context that corresponds with the given class name. + * + * This is inspired by InitializedContextEnvironment::getContext() but also + * returns subclasses of the given class name. This allows us to retrieve for + * example DrupalContext even if it is overridden in a project. + * + * @param string $class + * A fully namespaced class name. + * + * @return \Behat\Behat\Context\Context|false + * The requested context, or FALSE if the context is not registered. + * + * @throws \Exception + * Thrown when the environment is not yet initialized, meaning that contexts + * cannot yet be retrieved. + */ + protected function getContext($class) { + /** @var InitializedContextEnvironment $environment */ + $environment = $this->drupal->getEnvironment(); + // Throw an exception if the environment is not yet initialized. To make + // sure state doesn't leak between test scenarios, the environment is + // reinitialized at the start of every scenario. If this code is executed + // before a test scenario starts (e.g. in a `@BeforeScenario` hook) then the + // contexts cannot yet be retrieved. + if (!$environment instanceof InitializedContextEnvironment) { + throw new \Exception('Cannot retrieve contexts when the environment is not yet initialized.'); + } + foreach ($environment->getContexts() as $context) { + if ($context instanceof $class) { + return $context; + } + } + + return FALSE; + } + +}