diff --git a/src/Drupal/DrupalExtension/Context/MessageContext.php b/src/Drupal/DrupalExtension/Context/MessageContext.php index 025bda3d..0d5e7dda 100644 --- a/src/Drupal/DrupalExtension/Context/MessageContext.php +++ b/src/Drupal/DrupalExtension/Context/MessageContext.php @@ -239,20 +239,6 @@ public function assertNotMessage($message) { ); } - /** - * Returns a specific css selector. - * - * @param $name - * string CSS selector name - */ - public function getDrupalSelector($name) { - $text = $this->getDrupalParameter('selectors'); - if (!isset($text[$name])) { - throw new \Exception(sprintf('No such selector configured: %s', $name)); - } - return $text[$name]; - } - /** * Internal callback to check for a specific message in a given context. * diff --git a/src/Drupal/DrupalExtension/Context/RawDrupalContext.php b/src/Drupal/DrupalExtension/Context/RawDrupalContext.php index 66ed408c..f4ad6809 100644 --- a/src/Drupal/DrupalExtension/Context/RawDrupalContext.php +++ b/src/Drupal/DrupalExtension/Context/RawDrupalContext.php @@ -3,6 +3,7 @@ namespace Drupal\DrupalExtension\Context; use Behat\MinkExtension\Context\RawMinkContext; +use Behat\Mink\Exception\DriverException; use Behat\Testwork\Hook\HookDispatcher; use Drupal\DrupalDriverManager; @@ -145,6 +146,20 @@ public function getDrupalText($name) { return $text[$name]; } + /** + * Returns a specific css selector. + * + * @param $name + * string CSS selector name + */ + public function getDrupalSelector($name) { + $text = $this->getDrupalParameter('selectors'); + if (!isset($text[$name])) { + throw new \Exception(sprintf('No such selector configured: %s', $name)); + } + return $text[$name]; + } + /** * Get active Drupal Driver. */ @@ -479,12 +494,31 @@ public function logout() { */ public function loggedIn() { $session = $this->getSession(); + $page = $session->getPage(); + + // Look for a css selector to determine if a user is logged in. + // Default is the logged-in class on the body tag. + // Which should work with almost any theme. + try { + if ($page->has('css', $this->getDrupalSelector('logged_in_selector'))) { + return TRUE; + } + } catch (DriverException $e) { + // This test may fail if the driver did not load any site yet. + } + + // Some themes do not add that class to the body, so lets check if the + // login form is displayed on /user/login. + $session->visit($this->locatePath('/user/login')); + if (!$page->has('css', $this->getDrupalSelector('login_form_selector'))) { + return TRUE; + } + $session->visit($this->locatePath('/')); - // If a logout link is found, we are logged in. While not perfect, this is - // how Drupal SimpleTests currently work as well. - $element = $session->getPage(); - return $element->findLink($this->getDrupalText('log_out')); + // As a last resort, if a logout link is found, we are logged in. While not + // perfect, this is how Drupal SimpleTests currently work as well. + return $page->findLink($this->getDrupalText('log_out')); } /** diff --git a/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php b/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php index 982a9c35..9d532bf5 100644 --- a/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php +++ b/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php @@ -132,11 +132,18 @@ public function configure(ArrayNodeDefinition $builder) { end()-> end()-> arrayNode('selectors')-> + addDefaultsIfNotSet()-> children()-> scalarNode('message_selector')->end()-> scalarNode('error_message_selector')->end()-> scalarNode('success_message_selector')->end()-> scalarNode('warning_message_selector')->end()-> + scalarNode('login_form_selector')-> + defaultValue('form#user-login')-> + end()-> + scalarNode('logged_in_selector')-> + defaultValue('body.logged-in,body.user-logged-in')-> + end()-> end()-> end()-> // Drupal drivers.