Skip to content

Commit

Permalink
Merge pull request #237 from LionsAd/better-loggedin-support
Browse files Browse the repository at this point in the history
Use better technique to determine whether user is logged in #131
  • Loading branch information
jhedstrom committed Dec 14, 2015
2 parents a69a009 + 9893065 commit 9dc38c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
14 changes: 0 additions & 14 deletions src/Drupal/DrupalExtension/Context/MessageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
42 changes: 38 additions & 4 deletions src/Drupal/DrupalExtension/Context/RawDrupalContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9dc38c5

Please sign in to comment.