MDL-43738 behat: Refactoring the field type guesser

The previous behaviour delegates the responsability
of overwriting the 3 public methods to the child
classes, now the field type or moodleform field is
guessed earlier in the execution flow.

Other changes introduced here:
- Fix wrong moodleform detection when there is a form
  in the page but the field we are dealing with is
  not inside it.
- Updating the last week feature files to these new
  step definitions.
- Some coding style changes.
This commit is contained in:
David Monllao 2014-02-27 16:39:25 +08:00
parent decf1e14c8
commit 8aff0eec9d
13 changed files with 223 additions and 93 deletions

View file

@ -30,9 +30,6 @@ require_once(__DIR__ . '/behat_form_group.php');
/**
* Date form field.
*
* 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.
*

View file

@ -27,7 +27,7 @@
use Behat\Mink\Element\NodeElement as NodeElement;
require_once(__DIR__ . '/behat_form_field.php');
require_once(__DIR__ . '/behat_form_textarea.php');
/**
* Moodle editor field.
@ -38,7 +38,7 @@ require_once(__DIR__ . '/behat_form_field.php');
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_editor extends behat_form_field {
class behat_form_editor extends behat_form_textarea {
/**
* Sets the value to a field.

View file

@ -69,14 +69,11 @@ class behat_form_field {
* @return void
*/
public function set_value($value) {
// 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()) {
$instance->set_value($value);
} else {
$this->field->setValue($value);
}
// We delegate to the best guess, if we arrived here
// using the generic behat_form_field is because we are
// dealing with a fgroup element.
$instance = $this->guess_type();
return $instance->set_value($value);
}
/**
@ -85,14 +82,11 @@ class behat_form_field {
* @return string
*/
public function get_value() {
// 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->get_value();
} else {
return $this->field->getValue();
}
// We delegate to the best guess, if we arrived here
// using the generic behat_form_field is because we are
// dealing with a fgroup element.
$instance = $this->guess_type();
return $instance->get_value();
}
/**
@ -105,18 +99,11 @@ class behat_form_field {
* @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;
// We delegate to the best guess, if we arrived here
// using the generic behat_form_field is because we are
// dealing with a fgroup element.
$instance = $this->guess_type();
return $instance->matches($expectedvalue);
}
/**
@ -130,53 +117,17 @@ class behat_form_field {
* moodle form elements we will need to refactor this simple HTML elements
* guess method.
*
* @return mixed False if no need for an special behat_form_*, otherwise the behat_form_*
* @return behat_form_field
*/
private function guess_type() {
global $CFG;
// Textareas are considered text based elements.
$tagname = strtolower($this->field->getTagName());
if ($tagname == 'textarea') {
if (!$this->running_javascript()) {
return false;
}
// If there is an iframe with $id + _ifr there a TinyMCE editor loaded.
$xpath = '//iframe[@id="' . $this->field->getAttribute('id') . '_ifr"]';
if (!$this->session->getPage()->find('xpath', $xpath)) {
// Generic one if it is a normal textarea.
return false;
}
$classname = 'behat_form_editor';
} else if ($tagname == 'input') {
$type = $this->field->getAttribute('type');
switch ($type) {
case 'text':
return false;
case 'checkbox':
$classname = 'behat_form_checkbox';
break;
case 'radio':
$classname = 'behat_form_radio';
break;
default:
return false;
}
} else if ($tagname == 'select') {
// Select tag.
$classname = 'behat_form_select';
} else {
// We can not provide a closer field type.
return false;
// We default to the text-based field if nothing was detected.
if (!$type = behat_field_manager::guess_field_type($this->field, $this->session)) {
$type = 'text';
}
$classname = 'behat_form_' . $type;
$classpath = $CFG->dirroot . '/lib/behat/form_field/' . $classname . '.php';
require_once($classpath);
return new $classname($this->session, $this->field);
@ -205,10 +156,19 @@ class behat_form_field {
throw new coding_exception('You can only get an internal ID using the selenium driver.');
}
return $this->session->
getDriver()->
getWebDriverSession()->
element('xpath', $this->field->getXPath())->
getID();
return $this->session->getDriver()->getWebDriverSession()->element('xpath', $this->field->getXPath())->getID();
}
/**
* Checks if the provided text matches the field value.
*
* @param string $expectedvalue
* @return bool
*/
protected function text_matches($expectedvalue) {
if (trim($expectedvalue) != trim($this->get_value())) {
return false;
}
return true;
}
}

View file

@ -30,8 +30,9 @@ 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.
* When filling fields inside a fgroup field element we don't know what kind
* of field are we dealing with, so we should re-guess it as behat_form_field
* does.
*
* @package core_form
* @category test

View file

@ -38,4 +38,5 @@ require_once(__DIR__ . '/behat_form_select.php');
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_modvisible extends behat_form_select {}
class behat_form_modvisible extends behat_form_select {
}

View file

@ -89,9 +89,6 @@ class behat_form_radio extends behat_form_checkbox {
* @return bool
*/
public function matches($expectedvalue = false) {
if (trim($expectedvalue) != trim($this->get_value())) {
return false;
}
return true;
return $this->text_matches($expectedvalue);
}
}

View file

@ -204,7 +204,10 @@ class behat_form_select extends behat_form_field {
// Same implementation as the parent if it is a single select.
if (!$multiple) {
if (trim($expectedvalue) != trim($this->get_value())) {
$cleanexpectedvalue = trim($expectedvalue);
$selectedtext = trim($this->get_selected_options());
$selectedvalue = trim($this->get_selected_options(false));
if ($cleanexpectedvalue != $selectedvalue && $cleanexpectedvalue != $selectedtext) {
return false;
}
return true;

View file

@ -38,4 +38,5 @@ require_once(__DIR__ . '/behat_form_select.php');
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_selectyesno extends behat_form_select {}
class behat_form_selectyesno extends behat_form_select {
}

View file

@ -0,0 +1,69 @@
<?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/>.
/**
* Text 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 for test-based fields.
*
* @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_text extends behat_form_field {
/**
* Sets the value to a field.
*
* @param string $value
* @return void
*/
public function set_value($value) {
$this->field->setValue($value);
}
/**
* Returns the current value of the element.
*
* @return string
*/
public function get_value() {
return $this->field->getValue();
}
/**
* Matches the provided value against the current field value.
*
* @param string $expectedvalue
* @return bool The provided value matches the field value?
*/
public function matches($expectedvalue) {
return $this->text_matches($expectedvalue);
}
}

View file

@ -0,0 +1,39 @@
<?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/>.
/**
* Textarea 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_text.php');
/**
* Textarea field class.
*
* @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_textarea extends behat_form_text {
}