From 9bff3fd08358d34562f1a94514ced0a573f3cbec Mon Sep 17 00:00:00 2001 From: Fabian Franz Date: Sun, 13 Dec 2015 09:16:32 -0800 Subject: [PATCH 1/4] Use better technique to determine whether user is logged in. --- .../Context/RawDrupalContext.php | 42 +++++++++++++++++-- .../ServiceContainer/DrupalExtension.php | 7 ++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Drupal/DrupalExtension/Context/RawDrupalContext.php b/src/Drupal/DrupalExtension/Context/RawDrupalContext.php index 66ed408c..c31d0300 100644 --- a/src/Drupal/DrupalExtension/Context/RawDrupalContext.php +++ b/src/Drupal/DrupalExtension/Context/RawDrupalContext.php @@ -145,6 +145,21 @@ 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])) { + var_dump($text); + 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 (\Behat\Mink\Exception\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..a458e831 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')-> + end()-> end()-> end()-> // Drupal drivers. From 346b8e10405592d38096651e6c977e7d0dea2dd3 Mon Sep 17 00:00:00 2001 From: Fabian Franz Date: Sun, 13 Dec 2015 09:19:55 -0800 Subject: [PATCH 2/4] Code review fixes. --- src/Drupal/DrupalExtension/Context/RawDrupalContext.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Drupal/DrupalExtension/Context/RawDrupalContext.php b/src/Drupal/DrupalExtension/Context/RawDrupalContext.php index c31d0300..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; @@ -154,7 +155,6 @@ public function getDrupalText($name) { public function getDrupalSelector($name) { $text = $this->getDrupalParameter('selectors'); if (!isset($text[$name])) { - var_dump($text); throw new \Exception(sprintf('No such selector configured: %s', $name)); } return $text[$name]; @@ -503,7 +503,7 @@ public function loggedIn() { if ($page->has('css', $this->getDrupalSelector('logged_in_selector'))) { return TRUE; } - } catch (\Behat\Mink\Exception\DriverException $e) { + } catch (DriverException $e) { // This test may fail if the driver did not load any site yet. } From ad5df0814d125dc2ad3681461ec47bae41faddb3 Mon Sep 17 00:00:00 2001 From: Fabian Franz Date: Sun, 13 Dec 2015 09:30:37 -0800 Subject: [PATCH 3/4] Tailor for Drupal 7 and 8 in the default selector. --- src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php b/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php index a458e831..9d532bf5 100644 --- a/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php +++ b/src/Drupal/DrupalExtension/ServiceContainer/DrupalExtension.php @@ -142,7 +142,7 @@ public function configure(ArrayNodeDefinition $builder) { defaultValue('form#user-login')-> end()-> scalarNode('logged_in_selector')-> - defaultValue('body.logged-in')-> + defaultValue('body.logged-in,body.user-logged-in')-> end()-> end()-> end()-> From 98930653c85ab25c34ade37dcf00d613f3181871 Mon Sep 17 00:00:00 2001 From: Fabian Franz Date: Sun, 13 Dec 2015 09:37:59 -0800 Subject: [PATCH 4/4] Remove duplicated method, which is now present in parent class. --- .../DrupalExtension/Context/MessageContext.php | 14 -------------- 1 file changed, 14 deletions(-) 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. *