Skip to content

Commit

Permalink
MDL-43738 behat: Normalization and major refactoring of getters and s…
Browse files Browse the repository at this point in the history
…etters

Every single step that sets or gets a value from a field
has been updated to follow the same behaviour both when
using it through a single step or through generic steps
like "I fill the moodle form with:", to resume all the
changes:
- Created a behat_form_group to re-guess the field type
  and act appropriately setting and getting it's value
- Normalize all getters and setters to use behat_form_field
  children
- Complete behat_form_checkbox to trigger the appropiate JS
  event needed to perform some JS scripts that are listening
- Refactor MDL-43713 multi-select changes and remove
  the two new steps introduced there as that behaviour can
  be managed from the generic getter
- Added a new step definition to check a capability permission
  as we changed the way radio buttons gets it's value
  • Loading branch information
David Monllao committed Feb 27, 2014
1 parent becb7e5 commit c3f1e95
Show file tree
Hide file tree
Showing 8 changed files with 380 additions and 181 deletions.
50 changes: 45 additions & 5 deletions lib/behat/form_field/behat_form_checkbox.php
Expand Up @@ -47,18 +47,37 @@ class behat_form_checkbox extends behat_form_field {
*/
public function set_value($value) {

if (!$this->running_javascript()) {
$this->field->check();
return;
}

if (!empty($value) && !$this->field->isChecked()) {

if (!$this->running_javascript()) {
$this->field->check();
return;
}

// Check it if it should be checked and it is not.
$this->field->click();

// Trigger the onchange event as triggered when 'checking' the checkbox.
$this->session->getDriver()->triggerSynScript(
$this->field->getXPath(),
"Syn.trigger('change', {}, {{ELEMENT}})"
);

} else if (empty($value) && $this->field->isChecked()) {

if (!$this->running_javascript()) {
$this->field->uncheck();
return;
}

// Uncheck if it is checked and shouldn't.
$this->field->click();

// Trigger the onchange event as triggered when 'checking' the checkbox.
$this->session->getDriver()->triggerSynScript(
$this->field->getXPath(),
"Syn.trigger('change', {}, {{ELEMENT}})"
);
}
}

Expand All @@ -70,4 +89,25 @@ public function set_value($value) {
public function get_value() {
return $this->field->isChecked();
}

/**
* Is it enabled?
*
* @param string $expectedvalue Anything !empty() is considered checked.
* @return bool
*/
public function matches($expectedvalue = false) {

$ischecked = $this->field->isChecked();

// Any non-empty value provided means that it should be checked.
if (!empty($expectedvalue) && $ischecked) {
return true;
} else if (empty($expectedvalue) && !$ischecked) {
return true;
}

return false;
}

}
9 changes: 4 additions & 5 deletions lib/behat/form_field/behat_form_date_selector.php
Expand Up @@ -25,14 +25,13 @@

// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.

require_once(__DIR__ . '/behat_form_select.php');
require_once(__DIR__ . '/behat_form_group.php');

/**
* Date form field.
*
* Simple extension of behat_form_select to allow date-type
* select fields to be filled like select elements instead of
* text elements.
* Simple extension of behat_form_group to allow the different
* date_selector fields to be filled according to it's type.
*
* This class will be refactored in case we are interested in
* creating more complex formats to fill date and date-time fields.
Expand All @@ -42,4 +41,4 @@
* @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_select {}
class behat_form_date_selector extends behat_form_group {}
10 changes: 3 additions & 7 deletions lib/behat/form_field/behat_form_date_time_selector.php
Expand Up @@ -25,21 +25,17 @@

// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.

require_once(__DIR__ . '/behat_form_select.php');
require_once(__DIR__ . '/behat_form_date_selector.php');

/**
* Date time form field.
*
* Simple extension of behat_form_select to allow datetime-type
* select fields to be filled like select elements instead of
* text elements.
*
* This class will be refactored in case we are interested in
* creating more complex formats to fill date and date-time fields.
* creating more complex formats to fill date-time fields.
*
* @package core_form
* @category test
* @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_select {}
class behat_form_date_time_selector extends behat_form_date_selector {}
24 changes: 24 additions & 0 deletions lib/behat/form_field/behat_form_field.php
Expand Up @@ -95,6 +95,30 @@ public function get_value() {
}
}

/**
* Generic match implementation
*
* Will work well with text-based fields, extension required
* for most of the other cases.
*
* @param string $expectedvalue
* @return bool The provided value matches the field value?
*/
public function matches($expectedvalue) {

// If we are not dealing with a text-based tag try to find the most appropiate
// behat_form_* class to deal with it.
if ($instance = $this->guess_type()) {
return $instance->matches($expectedvalue);
}

// Text-based comparison.
if (trim($expectedvalue) != trim($this->get_value())) {
return false;
}
return true;
}

/**
* Guesses the element type we are dealing with in case is not a text-based element.
*
Expand Down
41 changes: 41 additions & 0 deletions lib/behat/form_field/behat_form_group.php
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Generic group field class.
*
* @package core_form
* @category test
* @copyright 2014 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.

require_once(__DIR__ . '/behat_form_field.php');

/**
* Class to re-guess the field type as grouped fields can have different field types.
*
* When filling fields in a fgroup field element we don't know what kind
* of field are we dealing with, so we should re-guess it.
*
* @package core_form
* @category test
* @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 {}
42 changes: 40 additions & 2 deletions lib/behat/form_field/behat_form_radio.php
Expand Up @@ -31,7 +31,8 @@
* Radio input form field.
*
* Extends behat_form_checkbox as the set_value() behaviour
* is the same.
* is the same and it behaves closer to a checkbox than to
* a text field.
*
* This form field type can be added to forms as any other
* moodle form element, but it does not make sense without
Expand All @@ -51,9 +52,46 @@ class behat_form_radio extends behat_form_checkbox {
/**
* Returns the radio input value attribute.
*
* Here we can not extend behat_form_checkbox because
* isChecked() does internally a (bool)getValue() and
* it is not good for radio buttons.
*
* @return string The value attribute
*/
public function get_value() {
return $this->field->getValue();
return (bool)$this->field->getAttribute('checked');
}

/**
* Sets the value of a radio
*
* Partially overwriting behat_form_checkbox
* implementation as when JS is disabled we
* can not check() and we should use setValue()
*
* @param string $value
* @return void
*/
public function set_value($value) {

if ($this->running_javascript()) {
parent::set_value($value);
} else {
// Goutte does not accept a check nor a click in an input[type=radio].
$this->field->setValue($this->field->getAttribute('value'));
}
}

/**
* Returns whether the provided value matches the current value or not.
*
* @param string $expectedvalue
* @return bool
*/
public function matches($expectedvalue = false) {
if (trim($expectedvalue) != trim($this->get_value())) {
return false;
}
return true;
}
}

0 comments on commit c3f1e95

Please sign in to comment.