Skip to content

Commit

Permalink
MDL-43584 behat: Removing DB calls from step definitions as much as p…
Browse files Browse the repository at this point in the history
…ossible

Old methods deprecated as the expected arguments
have changed.
  • Loading branch information
David Monllao committed Jan 10, 2014
1 parent 8a0667a commit 14ebd16
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 53 deletions.
52 changes: 31 additions & 21 deletions cohort/tests/behat/behat_cohort.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,49 @@
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 ([email protected])".
*
* @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;
Expand Down
10 changes: 4 additions & 6 deletions group/tests/behat/behat_groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@
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 ([email protected])".
*
* @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);
Expand Down
89 changes: 79 additions & 10 deletions lib/tests/behat/behat_deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,7 @@ public function i_click_on_in_the_table_row($element, $selectortype, $tablerowte
'" 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);
}

/**
Expand Down Expand Up @@ -233,6 +224,84 @@ public function i_delete_file_from_filepicker($name, $filepickerelement) {
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.
*
Expand Down
22 changes: 6 additions & 16 deletions message/tests/behat/behat_message.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,13 @@
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');
Expand All @@ -67,9 +57,9 @@ public function i_send_message_to_user($messagecontent, $tousername) {
}

$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') . '"');

Expand Down

0 comments on commit 14ebd16

Please sign in to comment.