Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use better technique to determine whether user is logged in #131 #237

Merged
merged 4 commits into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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