From 14ebd1639041720faf24c634d34fe8c7be9641e5 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Thu, 9 Jan 2014 18:51:07 +0800 Subject: [PATCH] MDL-43584 behat: Removing DB calls from step definitions as much as possible Old methods deprecated as the expected arguments have changed. --- cohort/tests/behat/behat_cohort.php | 52 +++++++++------- group/tests/behat/behat_groups.php | 10 ++- lib/tests/behat/behat_deprecated.php | 89 ++++++++++++++++++++++++--- message/tests/behat/behat_message.php | 22 ++----- 4 files changed, 120 insertions(+), 53 deletions(-) diff --git a/cohort/tests/behat/behat_cohort.php b/cohort/tests/behat/behat_cohort.php index 887533b8a1f..fbc78881a1c 100644 --- a/cohort/tests/behat/behat_cohort.php +++ b/cohort/tests/behat/behat_cohort.php @@ -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 to "(?P(?:[^"]|\\")*)" cohort$/ - * @param string $username + * @Given /^I add "(?P(?:[^"]|\\")*)" user to "(?P(?:[^"]|\\")*)" 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; diff --git a/group/tests/behat/behat_groups.php b/group/tests/behat/behat_groups.php index f32ce79957a..625a9a1668d 100644 --- a/group/tests/behat/behat_groups.php +++ b/group/tests/behat/behat_groups.php @@ -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(?:[^"]|\\")*)" user to "(?P(?:[^"]|\\")*)" group$/ + * @Given /^I add "(?P(?:[^"]|\\")*)" user to "(?P(?:[^"]|\\")*)" 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); diff --git a/lib/tests/behat/behat_deprecated.php b/lib/tests/behat/behat_deprecated.php index 2203702d626..ab6b291a5cc 100644 --- a/lib/tests/behat/behat_deprecated.php +++ b/lib/tests/behat/behat_deprecated.php @@ -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 to "(?P(?:[^"]|\\")*)"$/ + * @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 to "(?P(?:[^"]|\\")*)" 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(?:[^"]|\\")*)" user to "(?P(?:[^"]|\\")*)" 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. * diff --git a/message/tests/behat/behat_message.php b/message/tests/behat/behat_message.php index ba5f6a52afd..02fbba66717 100644 --- a/message/tests/behat/behat_message.php +++ b/message/tests/behat/behat_message.php @@ -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 to "(?P(?:[^"]|\\")*)"$/ - * @throws ElementNotFoundException + * @Given /^I send "(?P(?:[^"]|\\")*)" message to "(?P(?:[^"]|\\")*)" 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') . '"');