MDL-43584 behat: Removing DB calls from step definitions as much as possible

Old methods deprecated as the expected arguments
have changed.
This commit is contained in:
David Monllao 2014-01-09 18:51:07 +08:00
parent 8a0667a994
commit 14ebd16390
4 changed files with 120 additions and 53 deletions

View File

@ -40,39 +40,49 @@ use Behat\Behat\Context\Step\Given as Given;
class behat_cohort extends behat_base {
/**
* Adds the user to the specified cohort.
* Adds the user to the specified cohort. The user should be specified like "Firstname Lastname (user@email.com)".
*
* @Given /^I add "(?P<user_username_string>(?:[^"]|\\")*)" user to "(?P<cohort_idnumber_string>(?:[^"]|\\")*)" cohort$/
* @param string $username
* @Given /^I add "(?P<user_fullname_string>(?:[^"]|\\")*)" user to "(?P<cohort_idnumber_string>(?:[^"]|\\")*)" cohort members$/
* @param string $user
* @param string $cohortidnumber
*/
public function i_add_user_to_cohort($username, $cohortidnumber) {
global $DB;
// The user was created by the data generator, executed by the same PHP process that is
// running this step, not by any Selenium action.
$userid = $DB->get_field('user', 'id', array('username' => $username));
public function i_add_user_to_cohort_members($user, $cohortidnumber) {
$steps = array(
new Given('I click on "' . get_string('assign', 'cohort') . '" "link" in the "' . $this->escape($cohortidnumber) . '" "table_row"'),
new Given('I select "' . $userid . '" from "' . get_string('potusers', 'cohort') . '"'),
new Given('I select "' . $this->escape($user) . '" from "' . get_string('potusers', 'cohort') . '"'),
new Given('I press "' . get_string('add') . '"'),
new Given('I press "' . get_string('backtocohorts', 'cohort') . '"')
);
// If we are not in the cohorts management we should move there before anything else.
if (!$this->getSession()->getPage()->find('css', 'input#cohort_search_q')) {
$steps = array_merge(
array(
new Given('I am on homepage'),
new Given('I collapse "' . get_string('frontpagesettings', 'admin') . '" node'),
new Given('I expand "' . get_string('administrationsite') . '" node'),
new Given('I expand "' . get_string('users', 'admin') . '" node'),
new Given('I expand "' . get_string('accounts', 'admin') . '" node'),
new Given('I follow "' . get_string('cohorts', 'cohort') . '"')
),
$steps
);
// With JS enabled we should expand a few tree nodes.
if ($this->running_javascript()) {
$steps = array_merge(
array(
new Given('I am on homepage'),
new Given('I collapse "' . get_string('frontpagesettings', 'admin') . '" node'),
new Given('I expand "' . get_string('administrationsite') . '" node'),
new Given('I expand "' . get_string('users', 'admin') . '" node'),
new Given('I expand "' . get_string('accounts', 'admin') . '" node'),
new Given('I follow "' . get_string('cohorts', 'cohort') . '"')
),
$steps
);
} else {
// JS disabled.
$steps = array_merge(
array(
new Given('I am on homepage'),
new Given('I follow "' . get_string('administrationsite') . '" node'),
new Given('I follow "' . get_string('cohorts', 'cohort') . '"')
),
$steps
);
}
}
return $steps;

View File

@ -40,18 +40,16 @@ use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
class behat_groups extends behat_base {
/**
* Add the specified user to the group. You should be in the groups page when running this step.
* Add the specified user to the group. You should be in the groups page when running this step. The user should be specified like "Firstname Lastname (user@email.com)".
*
* @Given /^I add "(?P<username_string>(?:[^"]|\\")*)" user to "(?P<group_name_string>(?:[^"]|\\")*)" group$/
* @Given /^I add "(?P<user_fullname_string>(?:[^"]|\\")*)" user to "(?P<group_name_string>(?:[^"]|\\")*)" group members$/
* @throws ElementNotFoundException Thrown by behat_base::find
* @param string $username
* @param string $groupname
*/
public function i_add_user_to_group($username, $groupname) {
global $DB;
public function i_add_user_to_group_members($userfullname, $groupname) {
$user = $DB->get_record('user', array('username' => $username));
$userfullname = $this->getSession()->getSelectorsHandler()->xpathLiteral(fullname($user));
$userfullname = $this->getSession()->getSelectorsHandler()->xpathLiteral($userfullname);
// Using a xpath liternal to avoid problems with quotes and double quotes.
$groupname = $this->getSession()->getSelectorsHandler()->xpathLiteral($groupname);

View File

@ -60,16 +60,7 @@ class behat_deprecated extends behat_base {
'" in the "' . $this->escape($tablerowtext) . '" "table_row"';
$this->deprecated_message($alternative);
// The table row container.
$nocontainerexception = new ElementNotFoundException($this->getSession(), '"' . $tablerowtext . '" row text ');
$tablerowtext = $this->getSession()->getSelectorsHandler()->xpathLiteral($tablerowtext);
$rownode = $this->find('xpath', "//tr[contains(., $tablerowtext)]", $nocontainerexception);
// Looking for the element DOM node inside the specified row.
list($selector, $locator) = $this->transform_selector($selectortype, $element);
$elementnode = $this->find($selector, $locator, false, $rownode);
$this->ensure_element_is_visible($elementnode);
$elementnode->click();
return new Given($alternative);
}
/**
@ -233,6 +224,84 @@ class behat_deprecated extends behat_base {
return array(new Given($alternative));
}
/**
* Sends a message to the specified user from the logged user.
*
* @deprecated since 2.7
* @todo MDL-42862 This will be deleted in Moodle 2.9
* @see behat_message::i_send_message_to_user()
*
* @Given /^I send "(?P<message_contents_string>(?:[^"]|\\")*)" message to "(?P<username_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException
* @param string $messagecontent
* @param string $tousername
*/
public function i_send_message_to_user($messagecontent, $tousername) {
global $DB;
// Runs by CLI, same PHP process that created the user.
$touser = $DB->get_record('user', array('username' => $tousername));
if (!$touser) {
throw new ElementNotFoundException($this->getSession(), '"' . $tousername . '" ');
}
$tofullname = fullname($touser);
$alternative = 'I send "' . $this->escape($messagecontent) . '" message to "' . $tofullname . '" user';
$this->deprecated_message($alternative);
return new Given($alternative);
}
/**
* Adds the user to the specified cohort.
*
* @deprecated since 2.7
* @todo MDL-42862 This will be deleted in Moodle 2.9
* @see behat_cohort::i_add_user_to_cohort_members()
*
* @Given /^I add "(?P<user_username_string>(?:[^"]|\\")*)" user to "(?P<cohort_idnumber_string>(?:[^"]|\\")*)" cohort$/
* @param string $username
* @param string $cohortidnumber
*/
public function i_add_user_to_cohort($username, $cohortidnumber) {
global $DB;
// The user was created by the data generator, executed by the same PHP process that is
// running this step, not by any Selenium action.
$user = $DB->get_record('user', array('username' => $username));
$userlocator = $user->firstname . ' ' . $user->lastname . ' (' . $user->email . ')';
$alternative = 'I add "' . $this->escape($userlocator) .
'" user to "' . $this->escape($cohortidnumber) . '" cohort members';
$this->deprecated_message($alternative);
return new Given($alternative);
}
/**
* Add the specified user to the group. You should be in the groups page when running this step.
*
* @deprecated since 2.7
* @todo MDL-42862 This will be deleted in Moodle 2.9
* @see behat_groups::i_add_user_to_group_members()
*
* @Given /^I add "(?P<username_string>(?:[^"]|\\")*)" user to "(?P<group_name_string>(?:[^"]|\\")*)" group$/
* @param string $username
* @param string $groupname
*/
public function i_add_user_to_group($username, $groupname) {
global $DB;
$user = $DB->get_record('user', array('username' => $username));
$userfullname = fullname($user);
$alternative = 'I add "' . $this->escape($userfullname) .
'" user to "' . $this->escape($groupname) . '" group members';
$this->deprecated_message($alternative);
return new Given($alternative);
}
/**
* Throws an exception if $CFG->behat_usedeprecated is not allowed.
*

View File

@ -41,23 +41,13 @@ use Behat\Behat\Context\Step\Given as Given,
class behat_message extends behat_base {
/**
* Sends a message to the specified user from the logged user.
* Sends a message to the specified user from the logged user. The user full name should contain the first and last names.
*
* @Given /^I send "(?P<message_contents_string>(?:[^"]|\\")*)" message to "(?P<username_string>(?:[^"]|\\")*)"$/
* @throws ElementNotFoundException
* @Given /^I send "(?P<message_contents_string>(?:[^"]|\\")*)" message to "(?P<user_full_name_string>(?:[^"]|\\")*)" user$/
* @param string $messagecontent
* @param string $tousername
* @param string $userfullname
*/
public function i_send_message_to_user($messagecontent, $tousername) {
global $DB;
// Runs by CLI, same PHP process that created the user.
$touser = $DB->get_record('user', array('username' => $tousername));
if (!$touser) {
throw new ElementNotFoundException($this->getSession(), '"' . $tousername . '" ');
}
$tofullname = fullname($touser);
public function i_send_message_to_user($messagecontent, $userfullname) {
$steps = array();
$steps[] = new Given('I am on homepage');
@ -67,9 +57,9 @@ class behat_message extends behat_base {
}
$steps[] = new Given('I follow "' . get_string('messages', 'message') . '"');
$steps[] = new Given('I fill in "' . get_string('searchcombined', 'message') . '" with "' . $this->escape($tofullname) . '"');
$steps[] = new Given('I fill in "' . get_string('searchcombined', 'message') . '" with "' . $this->escape($userfullname) . '"');
$steps[] = new Given('I press "' . get_string('searchcombined', 'message') . '"');
$steps[] = new Given('I follow "' . $this->escape(get_string('sendmessageto', 'message', $tofullname)) . '"');
$steps[] = new Given('I follow "' . $this->escape(get_string('sendmessageto', 'message', $userfullname)) . '"');
$steps[] = new Given('I fill in "id_message" with "' . $this->escape($messagecontent) . '"');
$steps[] = new Given('I press "' . get_string('sendmessage', 'message') . '"');