Skip to content

Instantly share code, notes, and snippets.

@swichers
Last active August 29, 2015 14:23
Show Gist options
  • Save swichers/dae460d69d0b099e4afc to your computer and use it in GitHub Desktop.
Save swichers/dae460d69d0b099e4afc to your computer and use it in GitHub Desktop.
iLoginAs Behat 2 method
<?php
/**
* Allows your feature to login as the specified user without specifying a password
*/
/**
* @Given /^I log in as "(?P<name>[^"]*)"$/
*/
public function iLogInAs($name) {
$base_url = rtrim($this->locatePath('/'), '/');
$login_link = $this->getMainContext()->getDriver('drush')->drush('uli', array(
"'${name}'",
'--browser=0',
"--uri=${base_url}",
));
// Trim EOL characters. Required or else visiting the link won't work.
$login_link = trim($login_link);
$this->getSession()->visit($this->locatePath($login_link));
if (!$this->loggedIn()) {
throw new \Exception('Unable to login as user ' . $name);
}
$user_info = $this->getDriver('drush')->drush('user-information', array(
"'${name}'",
'--pipe',
));
list(, $uid) = explode(',', $user_info, 5);
if (empty($uid)) {
throw new \Exception('Unable to get user ID for ' . $name);
}
$user = user_load($uid);
if (empty($user->uid)) {
throw new \Exception('Unable to load user ' . $uid);
}
$this->user = $user;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment