From a525414125e003a7a9fb8388d7efbc34f916c258 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Thu, 30 Jan 2014 14:48:20 +0800 Subject: [PATCH] MDL-43738 behat: Updating steps definitions and deprecating methods --- admin/tool/behat/upgrade.txt | 6 +- .../form_field/behat_form_date_selector.php | 3 +- .../behat_form_date_time_selector.php | 3 +- lib/behat/form_field/behat_form_group.php | 3 +- lib/behat/form_field/behat_form_select.php | 10 +- lib/tests/behat/behat_data_generators.php | 10 +- lib/tests/behat/behat_deprecated.php | 167 +++++++++++++++++- lib/tests/behat/behat_forms.php | 124 +++---------- lib/tests/behat/behat_permissions.php | 43 ++++- message/tests/behat/behat_message.php | 3 +- 10 files changed, 254 insertions(+), 118 deletions(-) diff --git a/admin/tool/behat/upgrade.txt b/admin/tool/behat/upgrade.txt index e677b90dc4a..525aa94f379 100644 --- a/admin/tool/behat/upgrade.txt +++ b/admin/tool/behat/upgrade.txt @@ -1,6 +1,8 @@ -This files describes API changes in the assign code. +This files describes API changes in the tool_behat code. === 2.7 === - +* Constants behat_base::cap_allow, behat_base::cap_prevent and + behat_base::cap_prohibit have been removed in favour of the + lang/en/role.php language strings 'allow', 'prevent' and 'prohibit'. * @_only_local tag used in .feature files replaced by @_file_upload tag * @_alerts tag used in .feature files replaced by @_alert tag diff --git a/lib/behat/form_field/behat_form_date_selector.php b/lib/behat/form_field/behat_form_date_selector.php index 86e00645461..4fc8e47646b 100644 --- a/lib/behat/form_field/behat_form_date_selector.php +++ b/lib/behat/form_field/behat_form_date_selector.php @@ -41,4 +41,5 @@ require_once(__DIR__ . '/behat_form_group.php'); * @copyright 2013 David Monllaó * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class behat_form_date_selector extends behat_form_group {} +class behat_form_date_selector extends behat_form_group { +} diff --git a/lib/behat/form_field/behat_form_date_time_selector.php b/lib/behat/form_field/behat_form_date_time_selector.php index da277b40ed5..19181cf6b40 100644 --- a/lib/behat/form_field/behat_form_date_time_selector.php +++ b/lib/behat/form_field/behat_form_date_time_selector.php @@ -38,4 +38,5 @@ require_once(__DIR__ . '/behat_form_date_selector.php'); * @copyright 2013 David Monllaó * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class behat_form_date_time_selector extends behat_form_date_selector {} +class behat_form_date_time_selector extends behat_form_date_selector { +} diff --git a/lib/behat/form_field/behat_form_group.php b/lib/behat/form_field/behat_form_group.php index e61450947b0..533a787cd7e 100644 --- a/lib/behat/form_field/behat_form_group.php +++ b/lib/behat/form_field/behat_form_group.php @@ -38,4 +38,5 @@ require_once(__DIR__ . '/behat_form_field.php'); * @copyright 2014 David Monllaó * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class behat_form_group extends behat_form_field {} +class behat_form_group extends behat_form_field { +} diff --git a/lib/behat/form_field/behat_form_select.php b/lib/behat/form_field/behat_form_select.php index 74c216008c1..86d6b781734 100644 --- a/lib/behat/form_field/behat_form_select.php +++ b/lib/behat/form_field/behat_form_select.php @@ -124,7 +124,7 @@ class behat_form_select extends behat_form_field { // Single select sometimes needs an extra click in the option. if (!$multiple) { - // $options only contains 1 option. + // Var $options only contains 1 option. $optionxpath = $this->get_option_xpath(end($options), $selectxpath); // Using the driver direcly because Element methods are messy when dealing @@ -297,14 +297,14 @@ class behat_form_select extends behat_form_field { // If the select is multiple, text commas must be encoded. $selectedoptions[] = trim(str_replace(',', '\,', $option->{$method}())); } else { - $selectedoptions[] = trim($option->{$method}()); + $selectedoptions[] = trim($option->{$method}()); } } } - // Goutte does not keep the 'selected' attribute updated, but its getValue() returns - // the selected elements correctly, also those having commas within them. } else { + // Goutte does not keep the 'selected' attribute updated, but its getValue() returns + // the selected elements correctly, also those having commas within them. // Goutte returns the values as an array or as a string depending // on whether multiple options are selected or not. @@ -322,7 +322,7 @@ class behat_form_select extends behat_form_field { // If the select is multiple, text commas must be encoded. $selectedoptions[] = trim(str_replace(',', '\,', $option->{$method}())); } else { - $selectedoptions[] = trim($option->{$method}()); + $selectedoptions[] = trim($option->{$method}()); } } } diff --git a/lib/tests/behat/behat_data_generators.php b/lib/tests/behat/behat_data_generators.php index 749770bb287..7df9ff6c927 100644 --- a/lib/tests/behat/behat_data_generators.php +++ b/lib/tests/behat/behat_data_generators.php @@ -50,10 +50,6 @@ use Behat\Behat\Exception\PendingException as PendingException; */ class behat_data_generators extends behat_base { - const cap_allow = 'Allow'; - const cap_prevent = 'Prevent'; - const cap_prohibit = 'Prohibit'; - /** * @var testing_data_generator */ @@ -303,13 +299,13 @@ class behat_data_generators extends behat_base { $context = $this->get_context($data['contextlevel'], $data['reference']); switch ($data['permission']) { - case self::cap_allow: + case get_string('allow', 'role'): $permission = CAP_ALLOW; break; - case self::cap_prevent: + case get_string('prevent', 'role'): $permission = CAP_PREVENT; break; - case self::cap_prohibit: + case get_string('prohibit', 'role'): $permission = CAP_PROHIBIT; break; default: diff --git a/lib/tests/behat/behat_deprecated.php b/lib/tests/behat/behat_deprecated.php index ab6b291a5cc..7b99ce6d48d 100644 --- a/lib/tests/behat/behat_deprecated.php +++ b/lib/tests/behat/behat_deprecated.php @@ -28,7 +28,8 @@ require_once(__DIR__ . '/../../../lib/behat/behat_base.php'); use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException, - Behat\Behat\Context\Step\Given as Given; + Behat\Behat\Context\Step\Given as Given, + Behat\Behat\Context\Step\Then as Then; /** * Deprecated behat step definitions. @@ -302,6 +303,170 @@ class behat_deprecated extends behat_base { return new Given($alternative); } + /** + * Fills in form text field with specified id|name|label|value. It works with text-based fields. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::i_set_the_field_to() + * + * @When /^I fill in "(?P(?:[^"]|\\")*)" with "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $field + * @param string $value + */ + public function fill_field($field, $value) { + $alternative = 'I set the field "' . $this->escape($field) . '" to "' . $this->escape($value) . '"'; + $this->deprecated_message($alternative); + + return new Given($alternative); + } + + /** + * Selects option in select field with specified id|name|label|value. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::i_set_the_field_to() + * + * @When /^I select "(?P(?:[^"]|\\")*)" from "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $option + * @param string $select + */ + public function select_option($option, $select) { + $alternative = 'I set the field "' . $this->escape($select) . '" to "' . $this->escape($option) . '"'; + $this->deprecated_message($alternative); + + return new Given($alternative); + } + + /** + * Selects the specified id|name|label from the specified radio button. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::i_set_the_field_to() + * + * @When /^I select "(?P(?:[^"]|\\")*)" radio button$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $radio The radio button id, name or label value + */ + public function select_radio($radio) { + $alternative = 'I set the field "' . $this->escape($radio) . '" to "1"'; + $this->deprecated_message($alternative); + + return new Given($alternative); + } + + /** + * Checks checkbox with specified id|name|label|value. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::i_set_the_field_to() + * + * @When /^I check "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $option + */ + public function check_option($option) { + $alternative = 'I set the field "' . $this->escape($option) . '" to "1"'; + $this->deprecated_message($alternative); + + return new Given($alternative); + } + + /** + * Unchecks checkbox with specified id|name|label|value. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::i_set_the_field_to() + * + * @When /^I uncheck "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $option + */ + public function uncheck_option($option) { + $alternative = 'I set the field "' . $this->escape($option) . '" to ""'; + $this->deprecated_message($alternative); + + return new Given($alternative); + } + + /** + * Checks that the field matches the specified value. When using multi-select fields use commas to separate selected options. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::the_field_matches_value() + * + * @Then /^the "(?P(?:[^"]|\\")*)" field should match "(?P(?:[^"]|\\")*)" value$/ + * @throws ExpectationException + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $locator + * @param string $value + */ + public function the_field_should_match_value($locator, $value) { + $alternative = 'the field "' . $this->escape($locator) . '" matches value "' . $this->escape($value) . '"'; + $this->deprecated_message($alternative); + + return new Then($alternative); + } + + /** + * Checks, that checkbox with specified in|name|label|value is checked. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::the_field_matches_value() + * + * @Then /^the "(?P(?:[^"]|\\")*)" checkbox should be checked$/ + * @param string $checkbox + */ + public function assert_checkbox_checked($checkbox) { + $alternative = 'the field "' . $this->escape($checkbox) . '" matches value "1"'; + $this->deprecated_message($alternative); + + return new Then($alternative); + } + + /** + * Checks, that checkbox with specified in|name|label|value is unchecked. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::the_field_matches_value() + * + * @Then /^the "(?P(?:[^"]|\\")*)" checkbox should not be checked$/ + * @param string $checkbox + */ + public function assert_checkbox_not_checked($checkbox) { + $alternative = 'the field "' . $this->escape($checkbox) . '" matches value ""'; + $this->deprecated_message($alternative); + + return new Then($alternative); + } + + /** + * Fills a moodle form with field/value data. + * + * @deprecated since 2.7 + * @todo MDL-42862 This will be deleted in Moodle 2.9 + * @see behat_forms::i_set_the_following_fields_to_these_values() + * + * @Given /^I fill the moodle form with:$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param TableNode $data + */ + public function i_fill_the_moodle_form_with(TableNode $data) { + $alternative = 'I set the following fields to these values:'; + $this->deprecated_message($alternative); + + return new Given($alternative, $data); + } + /** * Throws an exception if $CFG->behat_usedeprecated is not allowed. * diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index 85fa85d51e9..4934b2bd8ff 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -61,13 +61,13 @@ class behat_forms extends behat_base { } /** - * Fills a moodle form with field/value data. + * Fills a form with field/value data. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. * - * @Given /^I fill the moodle form with:$/ + * @Given /^I set the following fields to these values:$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param TableNode $data */ - public function i_fill_the_moodle_form_with(TableNode $data) { + public function i_set_the_following_fields_to_these_values(TableNode $data) { // Expand all fields in case we have. $this->expand_all_fields(); @@ -76,15 +76,7 @@ class behat_forms extends behat_base { // The action depends on the field type. foreach ($datahash as $locator => $value) { - - // Getting the node element pointed by the label. - $fieldnode = $this->find_field($locator); - - // Gets the field type from a parent node. - $field = behat_field_manager::get_form_field($fieldnode, $this->getSession()); - - // Delegates to the field class. - $field->set_value($value); + $this->set_field_value($locator, $value); } } @@ -154,90 +146,46 @@ class behat_forms extends behat_base { } /** - * Fills in form text field with specified id|name|label|value. It works with text-based fields. + * Sets the specified value to the field. * - * @When /^I fill in "(?P(?:[^"]|\\")*)" with "(?P(?:[^"]|\\")*)"$/ + * @Given /^I set the field "(?P(?:[^"]|\\")*)" to "(?P(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find * @param string $field * @param string $value + * @return void */ - public function fill_field($field, $value) { + public function i_set_the_field_to($field, $value) { $this->set_field_value($field, $value); } /** - * Selects option in select field with specified id|name|label|value. + * Checks, the field matches the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. * - * @When /^I select "(?P(?:[^"]|\\")*)" from "(?P(?:[^"]|\\")*)"$/ + * @Then /^the field "(?P(?:[^"]|\\")*)" matches value "(?P(?:[^"]|\\")*)"$/ * @throws ElementNotFoundException Thrown by behat_base::find - * @param string $option - * @param string $select - */ - public function select_option($option, $select) { - $this->set_field_value($select, $option); - } - - /** - * Selects the specified id|name|label from the specified radio button. - * - * @When /^I select "(?P(?:[^"]|\\")*)" radio button$/ - * @throws ElementNotFoundException Thrown by behat_base::find - * @param string $radio The radio button id, name or label value - */ - public function select_radio($radio) { - $this->set_field_value($radio, 1); - } - - /** - * Checks checkbox with specified id|name|label|value. - * - * @When /^I check "(?P(?:[^"]|\\")*)"$/ - * @throws ElementNotFoundException Thrown by behat_base::find - * @param string $option - */ - public function check_option($option) { - $this->set_field_value($option, 1); - } - - /** - * Unchecks checkbox with specified id|name|label|value. - * - * @When /^I uncheck "(?P(?:[^"]|\\")*)"$/ - * @throws ElementNotFoundException Thrown by behat_base::find - * @param string $option - */ - public function uncheck_option($option) { - $this->set_field_value($option, ''); - } - - /** - * Checks that the form element field matches the specified value. When using multi-select fields use commas to separate the selected options. - * - * @Then /^the "(?P(?:[^"]|\\")*)" field should match "(?P(?:[^"]|\\")*)" value$/ - * @throws ExpectationException - * @throws ElementNotFoundException Thrown by behat_base::find - * @param string $locator + * @param string $field * @param string $value + * @return void */ - public function the_field_should_match_value($locator, $value) { + public function the_field_matches_value($field, $value) { - $fieldnode = $this->find_field($locator); + $fieldnode = $this->find_field($field); // Get the field. - $field = behat_field_manager::get_form_field($fieldnode, $this->getSession()); + $formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession()); // Checks if the provided value matches the current field value. - if (!$field->matches($value)) { - $fieldvalue = $field->get_value(); + if (!$formfield->matches($value)) { + $fieldvalue = $formfield->get_value(); throw new ExpectationException( - 'The \'' . $locator . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' , + 'The \'' . $field . '\' value is \'' . $fieldvalue . '\', \'' . $value . '\' expected' , $this->getSession() ); } } /** - * Checks that the form element field does not match the specified value. + * Checks, the field does not match the value. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. * * @Then /^the field "(?P(?:[^"]|\\")*)" does not match value "(?P(?:[^"]|\\")*)"$/ * @throws ExpectationException @@ -251,11 +199,11 @@ class behat_forms extends behat_base { $fieldnode = $this->find_field($field); // Get the field. - $field = behat_field_manager::get_form_field($fieldnode, $this->getSession()); + $formfield = behat_field_manager::get_form_field($fieldnode, $this->getSession()); // Checks if the provided value matches the current field value. - if ($field->matches($value)) { - $fieldvalue = $field->get_value(); + if ($formfield->matches($value)) { + $fieldvalue = $formfield->get_value(); throw new ExpectationException( 'The \'' . $field . '\' value matches \'' . $value . '\' and it should not match it' , $this->getSession() @@ -264,11 +212,11 @@ class behat_forms extends behat_base { } /** - * Checks if fields values matches the provided values. Provide a table with field/value data. + * Checks, the provided field/value matches. More info in http://docs.moodle.org/dev/Acceptance_testing#Providing_values_to_steps. * * @Then /^the following fields match these values:$/ * @throws ExpectationException - * @param TableNode $table Pairs of | field | value | + * @param TableNode $data Pairs of | field | value | */ public function the_following_fields_match_these_values(TableNode $data) { @@ -279,7 +227,7 @@ class behat_forms extends behat_base { // The action depends on the field type. foreach ($datahash as $locator => $value) { - $this->the_field_should_match_value($locator, $value); + $this->the_field_matches_value($locator, $value); } } @@ -288,7 +236,7 @@ class behat_forms extends behat_base { * * @Then /^the following fields do not match these values:$/ * @throws ExpectationException - * @param TableNode $table Pairs of | field | value | + * @param TableNode $data Pairs of | field | value | */ public function the_following_fields_do_not_match_these_values(TableNode $data) { @@ -303,26 +251,6 @@ class behat_forms extends behat_base { } } - /** - * Checks, that checkbox with specified in|name|label|value is checked. - * - * @Then /^the "(?P(?:[^"]|\\")*)" checkbox should be checked$/ - * @param string $checkbox - */ - public function assert_checkbox_checked($checkbox) { - $this->the_field_should_match_value($checkbox, 1); - } - - /** - * Checks, that checkbox with specified in|name|label|value is unchecked. - * - * @Then /^the "(?P(?:[^"]|\\")*)" checkbox should not be checked$/ - * @param string $checkbox - */ - public function assert_checkbox_not_checked($checkbox) { - $this->the_field_should_match_value($checkbox, ''); - } - /** * Checks, that given select box contains the specified option. * diff --git a/lib/tests/behat/behat_permissions.php b/lib/tests/behat/behat_permissions.php index 562be1a709f..1e6fd98b266 100644 --- a/lib/tests/behat/behat_permissions.php +++ b/lib/tests/behat/behat_permissions.php @@ -76,7 +76,8 @@ class behat_permissions extends behat_base { $roleoption = $this->find('xpath', '//select[@name="roleid"]/option[contains(.,"' . $this->escape($rolename) . '")]'); return array( - new Given('I select "' . $this->escape($roleoption->getText()) . '" from "' . get_string('advancedoverride', 'role') . '"'), + new Given('I set the field "' . get_string('advancedoverride', 'role') . + '" to "' . $this->escape($roleoption->getText()) . '"'), new Given('I fill the capabilities form with the following permissions:', $table), new Given('I press "' . get_string('savechanges') . '"') ); @@ -142,4 +143,44 @@ class behat_permissions extends behat_base { } } + /** + * Checks if the capability has the specified permission. Works in the role definition advanced page. + * + * @Then /^"(?P(?:[^"]|\\")*)" capability has "(?PNot set|Allow|Prevent|Prohibit)" permission$/ + * @throws ExpectationException + * @param string $capabilityname + * @param string $permission + * @return void + */ + public function capability_has_permission($capabilityname, $permission) { + + // We already know the name, so we just need the value. + $radioxpath = "//table[@class='rolecap']/descendant::input[@type='radio']" . + "[@name='" . $capabilityname . "'][@checked]"; + + $checkedradio = $this->find('xpath', $radioxpath); + + switch ($permission) { + case get_string('notset', 'role'): + $perm = CAP_INHERIT; + break; + case get_string('allow', 'role'): + $perm = CAP_ALLOW; + break; + case get_string('prevent', 'role'): + $perm = CAP_PREVENT; + break; + case get_string('prohibit', 'role'): + $perm = CAP_PROHIBIT; + break; + default: + throw new ExpectationException('"' . $permission . '" permission does not exist', $this->getSession()); + break; + } + + if ($checkedradio->getAttribute('value') != $perm) { + throw new ExpectationException('"' . $capabilityname . '" permission is not "' . $permission . '"', $this->getSession()); + } + } + } diff --git a/message/tests/behat/behat_message.php b/message/tests/behat/behat_message.php index 02fbba66717..eeb619bcba5 100644 --- a/message/tests/behat/behat_message.php +++ b/message/tests/behat/behat_message.php @@ -57,7 +57,8 @@ 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($userfullname) . '"'); + $steps[] = new Given('I set the field "' . get_string('searchcombined', 'message') . + '" to "' . $this->escape($userfullname) . '"'); $steps[] = new Given('I press "' . get_string('searchcombined', 'message') . '"'); $steps[] = new Given('I follow "' . $this->escape(get_string('sendmessageto', 'message', $userfullname)) . '"'); $steps[] = new Given('I fill in "id_message" with "' . $this->escape($messagecontent) . '"');