2013-02-28 10:53:26 +08:00
|
|
|
<?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/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form fields helper.
|
|
|
|
*
|
|
|
|
* @package core
|
|
|
|
* @category test
|
|
|
|
* @copyright 2013 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.
|
|
|
|
|
|
|
|
use Behat\Mink\Session as Session,
|
2014-04-02 17:18:46 +08:00
|
|
|
Behat\Mink\Element\NodeElement as NodeElement,
|
|
|
|
Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException,
|
|
|
|
Behat\MinkExtension\Context\RawMinkContext as RawMinkContext;
|
2013-02-28 10:53:26 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to interact with form fields.
|
|
|
|
*
|
|
|
|
* @package core
|
|
|
|
* @category test
|
|
|
|
* @copyright 2013 David Monllaó
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
class behat_field_manager {
|
|
|
|
|
2014-04-02 17:18:46 +08:00
|
|
|
/**
|
|
|
|
* Gets an instance of the form field from it's label
|
|
|
|
*
|
|
|
|
* @param string $label
|
|
|
|
* @param RawMinkContext $context
|
|
|
|
* @return behat_form_field
|
|
|
|
*/
|
|
|
|
public static function get_form_field_from_label($label, RawMinkContext $context) {
|
|
|
|
// There are moodle form elements that are not directly related with
|
|
|
|
// a basic HTML form field, we should also take care of them.
|
2016-08-25 10:55:25 +08:00
|
|
|
// The DOM node.
|
|
|
|
$fieldnode = $context->find_field($label);
|
2014-04-02 17:18:46 +08:00
|
|
|
|
|
|
|
// The behat field manager.
|
2021-09-16 16:13:27 +08:00
|
|
|
$field = self::get_form_field($fieldnode, $context->getSession());
|
|
|
|
return $field;
|
2014-04-02 17:18:46 +08:00
|
|
|
}
|
|
|
|
|
2013-02-28 10:53:26 +08:00
|
|
|
/**
|
|
|
|
* Gets an instance of the form field.
|
|
|
|
*
|
|
|
|
* Not all the fields are part of a moodle form, in this
|
|
|
|
* cases it fallsback to the generic form field. Also note
|
|
|
|
* that this generic field type is using a generic setValue()
|
|
|
|
* method from the Behat API, which is not always good to set
|
|
|
|
* the value of form elements.
|
|
|
|
*
|
|
|
|
* @param NodeElement $fieldnode
|
|
|
|
* @param Session $session The behat browser session
|
|
|
|
* @return behat_form_field
|
|
|
|
*/
|
2013-06-14 15:11:36 +08:00
|
|
|
public static function get_form_field(NodeElement $fieldnode, Session $session) {
|
2013-02-28 10:53:26 +08:00
|
|
|
|
|
|
|
// Get the field type if is part of a moodleform.
|
|
|
|
if (self::is_moodleform_field($fieldnode)) {
|
2021-09-16 16:13:27 +08:00
|
|
|
$type = self::get_field_node_type($fieldnode, $session);
|
2013-02-28 10:53:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If is not a moodleforms field use the base field type.
|
|
|
|
if (empty($type)) {
|
|
|
|
$type = 'field';
|
|
|
|
}
|
|
|
|
|
2013-03-30 16:17:14 +08:00
|
|
|
return self::get_field_instance($type, $fieldnode, $session);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the appropiate behat_form_field according to the provided type.
|
|
|
|
*
|
|
|
|
* It defaults to behat_form_field.
|
|
|
|
*
|
|
|
|
* @param string $type The field type (checkbox, date_selector, text...)
|
|
|
|
* @param NodeElement $fieldnode
|
|
|
|
* @param Session $session The behat session
|
|
|
|
* @return behat_form_field
|
|
|
|
*/
|
|
|
|
public static function get_field_instance($type, NodeElement $fieldnode, Session $session) {
|
|
|
|
global $CFG;
|
|
|
|
|
2014-02-27 16:39:25 +08:00
|
|
|
// If the field is not part of a moodleform, we should still try to find out
|
|
|
|
// which field type are we dealing with.
|
2021-09-16 16:13:27 +08:00
|
|
|
if ($type == 'field' && $guessedtype = self::guess_field_type($fieldnode, $session)) {
|
2014-02-27 16:39:25 +08:00
|
|
|
$type = $guessedtype;
|
|
|
|
}
|
|
|
|
|
2013-02-28 10:53:26 +08:00
|
|
|
$classname = 'behat_form_' . $type;
|
|
|
|
|
2014-02-27 16:39:25 +08:00
|
|
|
// Fallsback on the type guesser if nothing specific exists.
|
2013-02-28 10:53:26 +08:00
|
|
|
$classpath = $CFG->libdir . '/behat/form_field/' . $classname . '.php';
|
|
|
|
if (!file_exists($classpath)) {
|
|
|
|
$classname = 'behat_form_field';
|
|
|
|
$classpath = $CFG->libdir . '/behat/form_field/' . $classname . '.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the instance.
|
|
|
|
require_once($classpath);
|
|
|
|
return new $classname($session, $fieldnode);
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:39:25 +08:00
|
|
|
/**
|
|
|
|
* Guesses a basic field type and returns it.
|
|
|
|
*
|
|
|
|
* This method is intended to detect HTML form fields when no
|
|
|
|
* moodleform-specific elements have been detected.
|
|
|
|
*
|
|
|
|
* @param NodeElement $fieldnode
|
|
|
|
* @param Session $session
|
|
|
|
* @return string|bool The field type or false.
|
|
|
|
*/
|
|
|
|
public static function guess_field_type(NodeElement $fieldnode, Session $session) {
|
2021-09-16 16:13:27 +08:00
|
|
|
[
|
|
|
|
'document' => $document,
|
|
|
|
'node' => $node,
|
|
|
|
] = self::get_dom_elements_for_node($fieldnode, $session);
|
2014-02-27 16:39:25 +08:00
|
|
|
|
2021-02-11 11:38:00 +08:00
|
|
|
// If the type is explicitly set on the element pointed to by the label - use it.
|
2021-09-16 16:13:27 +08:00
|
|
|
if ($fieldtype = $node->getAttribute('data-fieldtype')) {
|
2021-02-11 11:38:00 +08:00
|
|
|
return self::normalise_fieldtype($fieldtype);
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:39:25 +08:00
|
|
|
// Textareas are considered text based elements.
|
2021-09-16 16:13:27 +08:00
|
|
|
$tagname = strtolower($node->nodeName);
|
2014-02-27 16:39:25 +08:00
|
|
|
if ($tagname == 'textarea') {
|
2021-09-16 16:13:27 +08:00
|
|
|
$xpath = new \DOMXPath($document);
|
2014-02-27 16:39:25 +08:00
|
|
|
|
|
|
|
// If there is an iframe with $id + _ifr there a TinyMCE editor loaded.
|
2021-09-16 16:13:27 +08:00
|
|
|
if ($xpath->query('//div[@id="' . $node->getAttribute('id') . 'editable"]')->count() !== 0) {
|
2014-02-27 16:39:25 +08:00
|
|
|
return 'editor';
|
|
|
|
}
|
|
|
|
return 'textarea';
|
|
|
|
|
2021-09-16 16:13:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($tagname == 'input') {
|
|
|
|
switch ($node->getAttribute('type')) {
|
2014-02-27 16:39:25 +08:00
|
|
|
case 'text':
|
|
|
|
case 'password':
|
|
|
|
case 'email':
|
|
|
|
case 'file':
|
|
|
|
return 'text';
|
|
|
|
case 'checkbox':
|
|
|
|
return 'checkbox';
|
|
|
|
break;
|
|
|
|
case 'radio':
|
|
|
|
return 'radio';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Here we return false because all text-based
|
|
|
|
// fields should be included in the first switch case.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-16 16:13:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($tagname == 'select') {
|
2014-02-27 16:39:25 +08:00
|
|
|
// Select tag.
|
|
|
|
return 'select';
|
2021-09-16 16:13:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($tagname == 'span') {
|
|
|
|
if ($node->hasAttribute('data-inplaceeditable') && $node->getAttribute('data-inplaceeditable')) {
|
2021-11-08 12:05:07 +00:00
|
|
|
// Determine appropriate editable type of this field (text or select).
|
|
|
|
if ($node->getAttribute('data-type') == 'select') {
|
|
|
|
return 'inplaceeditable_select';
|
|
|
|
} else {
|
|
|
|
return 'inplaceeditable';
|
|
|
|
}
|
2020-06-17 13:45:15 +08:00
|
|
|
}
|
2014-02-27 16:39:25 +08:00
|
|
|
}
|
|
|
|
|
2022-08-19 12:24:30 +10:00
|
|
|
if ($tagname == 'div') {
|
|
|
|
if ($node->getAttribute('role') == 'combobox') {
|
|
|
|
return 'select_menu';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 16:39:25 +08:00
|
|
|
// We can not provide a closer field type.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-28 10:53:26 +08:00
|
|
|
/**
|
|
|
|
* Detects when the field is a moodleform field type.
|
|
|
|
*
|
|
|
|
* Note that there are fields inside moodleforms that are not
|
|
|
|
* moodleform element; this method can not detect this, this will
|
2013-06-14 15:11:36 +08:00
|
|
|
* be managed by get_field_node_type, after failing to find the form
|
2013-02-28 10:53:26 +08:00
|
|
|
* element element type.
|
|
|
|
*
|
|
|
|
* @param NodeElement $fieldnode
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected static function is_moodleform_field(NodeElement $fieldnode) {
|
|
|
|
|
|
|
|
// We already waited when getting the NodeElement and we don't want an exception if it's not part of a moodleform.
|
2014-02-27 16:39:25 +08:00
|
|
|
$parentformfound = $fieldnode->find('xpath',
|
|
|
|
"/ancestor::form[contains(concat(' ', normalize-space(@class), ' '), ' mform ')]"
|
|
|
|
);
|
2013-02-28 10:53:26 +08:00
|
|
|
|
|
|
|
return ($parentformfound != false);
|
|
|
|
}
|
|
|
|
|
2021-09-16 16:13:27 +08:00
|
|
|
/**
|
|
|
|
* Get the DOMDocument and DOMElement for a NodeElement.
|
|
|
|
*
|
|
|
|
* @param NodeElement $fieldnode
|
|
|
|
* @param Session $session
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected static function get_dom_elements_for_node(NodeElement $fieldnode, Session $session): array {
|
|
|
|
$html = $session->getPage()->getContent();
|
|
|
|
|
|
|
|
$document = new \DOMDocument();
|
|
|
|
|
|
|
|
$previousinternalerrors = libxml_use_internal_errors(true);
|
|
|
|
$document->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_BIGLINES);
|
|
|
|
libxml_clear_errors();
|
|
|
|
libxml_use_internal_errors($previousinternalerrors);
|
|
|
|
|
|
|
|
$xpath = new \DOMXPath($document);
|
|
|
|
$node = $xpath->query($fieldnode->getXpath())->item(0);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'document' => $document,
|
|
|
|
'node' => $node,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2013-02-28 10:53:26 +08:00
|
|
|
/**
|
|
|
|
* Recursive method to find the field type.
|
|
|
|
*
|
2013-06-14 15:11:36 +08:00
|
|
|
* Depending on the field the felement class node is in a level or in another. We
|
2013-02-28 10:53:26 +08:00
|
|
|
* look recursively for a parent node with a 'felement' class to find the field type.
|
|
|
|
*
|
|
|
|
* @param NodeElement $fieldnode The current node.
|
|
|
|
* @param Session $session The behat browser session
|
2021-09-16 16:13:27 +08:00
|
|
|
* @return null|string A text description of the node type, or null if one could not be accurately determined
|
2013-02-28 10:53:26 +08:00
|
|
|
*/
|
2021-09-16 16:13:27 +08:00
|
|
|
protected static function get_field_node_type(NodeElement $fieldnode, Session $session): ?string {
|
|
|
|
[
|
|
|
|
'document' => $document,
|
|
|
|
'node' => $node,
|
|
|
|
] = self::get_dom_elements_for_node($fieldnode, $session);
|
|
|
|
|
|
|
|
return self::get_field_type($document, $node, $session);
|
|
|
|
}
|
2013-02-28 10:53:26 +08:00
|
|
|
|
2021-09-16 16:13:27 +08:00
|
|
|
/**
|
|
|
|
* Get the field type from the specified DOMElement.
|
|
|
|
*
|
|
|
|
* @param \DOMDocument $document
|
|
|
|
* @param \DOMElement $node
|
|
|
|
* @param Session $session
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
protected static function get_field_type(\DOMDocument $document, \DOMElement $node, Session $session): ?string {
|
|
|
|
$xpath = new \DOMXPath($document);
|
|
|
|
|
|
|
|
if ($node->getAttribute('name') === 'availabilityconditionsjson') {
|
|
|
|
// Special handling for availability field which requires custom JavaScript.
|
2014-09-01 13:35:54 +01:00
|
|
|
return 'availability';
|
|
|
|
}
|
|
|
|
|
2021-09-16 16:13:27 +08:00
|
|
|
if ($node->nodeName == 'html') {
|
|
|
|
// The top of the document has been reached.
|
|
|
|
return null;
|
2016-07-21 11:06:46 +08:00
|
|
|
}
|
|
|
|
|
2016-08-29 14:57:46 +08:00
|
|
|
// If the type is explictly set on the element pointed to by the label - use it.
|
2021-09-16 16:13:27 +08:00
|
|
|
$fieldtype = $node->getAttribute('data-fieldtype');
|
2020-03-04 13:38:49 +08:00
|
|
|
if ($fieldtype) {
|
2020-03-05 14:16:18 +08:00
|
|
|
return self::normalise_fieldtype($fieldtype);
|
2016-08-29 14:57:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-16 16:13:27 +08:00
|
|
|
if ($xpath->query('/ancestor::*[@data-passwordunmaskid]', $node)->count() !== 0) {
|
|
|
|
// This element has a passwordunmaskid as a parent.
|
2016-08-22 07:03:51 +08:00
|
|
|
return 'passwordunmask';
|
|
|
|
}
|
|
|
|
|
2020-03-04 13:38:49 +08:00
|
|
|
// Fetch the parentnode only once.
|
2021-09-16 16:13:27 +08:00
|
|
|
$parentnode = $node->parentNode;
|
|
|
|
if ($parentnode instanceof \DOMDocument) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-02-28 10:53:26 +08:00
|
|
|
|
2020-03-05 14:16:18 +08:00
|
|
|
// Check the parent fieldtype before we check classes.
|
|
|
|
$fieldtype = $parentnode->getAttribute('data-fieldtype');
|
|
|
|
if ($fieldtype) {
|
|
|
|
return self::normalise_fieldtype($fieldtype);
|
|
|
|
}
|
|
|
|
|
2020-03-04 13:38:49 +08:00
|
|
|
// We look for a parent node with 'felement' class.
|
|
|
|
if ($class = $parentnode->getAttribute('class')) {
|
2013-02-28 10:53:26 +08:00
|
|
|
if (strstr($class, 'felement') != false) {
|
|
|
|
// Remove 'felement f' from class value.
|
|
|
|
return substr($class, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop propagation through the DOM, if it does not have a felement is not part of a moodle form.
|
|
|
|
if (strstr($class, 'fcontainer') != false) {
|
2021-09-16 16:13:27 +08:00
|
|
|
return null;
|
2013-02-28 10:53:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 16:13:27 +08:00
|
|
|
// Move up the tree.
|
|
|
|
return self::get_field_type($document, $parentnode, $session);
|
2013-06-14 15:11:36 +08:00
|
|
|
}
|
|
|
|
|
2020-03-05 14:16:18 +08:00
|
|
|
/**
|
|
|
|
* Normalise the field type.
|
|
|
|
*
|
|
|
|
* @param string $fieldtype
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function normalise_fieldtype(string $fieldtype): string {
|
|
|
|
if ($fieldtype === 'tags') {
|
|
|
|
return 'autocomplete';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fieldtype;
|
|
|
|
}
|
2013-02-28 10:53:26 +08:00
|
|
|
}
|