first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,122 @@
<?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/>.
/**
* Auto complete form field class.
*
* @package core_form
* @category test
* @copyright 2015 Damyon Wiese
* @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');
/**
* Auto complete form field.
*
* @package core_form
* @category test
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_autocomplete extends behat_form_text {
/**
* Sets the value to a field.
*
* @param string $value
* @return void
*/
public function set_value($value) {
if (!$this->running_javascript()) {
throw new coding_exception('Setting the value of an autocomplete field requires javascript.');
}
// Clear all current selections.
$rootnode = $this->field->getParent()->getParent();
$selections = $rootnode->findAll('css', '.form-autocomplete-selection [role=option]');
foreach (array_reverse($selections) as $selection) {
$selection->click();
$this->wait_for_pending_js();
}
$allowscreation = $this->field->hasAttribute('data-tags') && !empty($this->field->getAttribute('data-tags'));
$hasmultiple = $this->field->hasAttribute('data-multiple') && !empty($this->field->getAttribute('data-multiple'));
if ($hasmultiple && false !== strpos($value, ',')) {
// Commas have a special meaning as a value separator in 'multiple' autocomplete elements.
// To handle this we break the value up by comma, and enter it in chunks.
$values = explode(',', $value);
while ($value = array_shift($values)) {
$this->add_value(trim($value), $allowscreation);
}
} else {
$this->add_value(trim($value), $allowscreation);
}
}
/**
* Add a value to the autocomplete.
*
* @param string $value
* @param bool $allowscreation
*/
protected function add_value(string $value, bool $allowscreation): void {
$value = trim($value);
// Click into the field.
$this->field->click();
// Remove any existing text.
do {
behat_base::type_keys($this->session, [behat_keys::BACKSPACE, behat_keys::DELETE]);
} while (strlen($this->field->getValue()) > 0);
$this->wait_for_pending_js();
// Type in the new value.
behat_base::type_keys($this->session, str_split($value));
$this->wait_for_pending_js();
// If the autocomplete found suggestions, then it will have:
// 1) marked itself as expanded; and
// 2) have an aria-selected suggestion in the list.
$expanded = $this->field->getAttribute('aria-expanded');
$suggestion = $this->field->getParent()->getParent()->find('css', '.form-autocomplete-suggestions > [aria-selected="true"]');
if ($expanded && null !== $suggestion) {
// A suggestion was found.
// Click on the first item in the list.
$suggestion->click();
} else if ($allowscreation) {
// Press the return key to create a new entry.
behat_base::type_keys($this->session, [behat_keys::ENTER]);
} else {
throw new \InvalidArgumentException(
"Unable to find '{$value}' in the list of options, and unable to create a new option"
);
}
$this->wait_for_pending_js();
// Press the escape to close the autocomplete suggestions list.
behat_base::type_keys($this->session, [behat_keys::ESCAPE]);
$this->wait_for_pending_js();
}
}
@@ -0,0 +1,117 @@
<?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/>.
/**
* Availability form field class.
*
* @package core_form
* @category test
* @copyright 2014 The Open University
* @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_textarea.php');
/**
* Availability form field class.
*
* @package core_form
* @category test
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_availability extends behat_form_textarea {
/**
* Sets the value(s) of an availability element.
*
* At present this only supports the following value 'Grouping: xxx' where
* xxx is the name of a grouping. Additional value types can be added as
* necessary.
*
* @param string $value Value code
* @return void
*/
public function set_value($value) {
global $DB;
$driver = $this->session->getDriver();
// Check the availability condition is currently unset - we don't yet
// support changing an existing one.
$existing = $this->get_value();
if ($existing && $existing !== '{"op":"&","c":[],"showc":[]}') {
throw new Exception('Cannot automatically set availability when ' .
'there is existing setting - must clear manually');
}
// Check the value matches a supported format.
$matches = array();
if (!preg_match('~^\s*([^:]*):\s*(.*?)\s*$~', $value, $matches)) {
throw new Exception('Value for availability field does not match correct ' .
'format. Example: "Grouping: G1"');
}
$type = $matches[1];
$param = $matches[2];
if ($this->running_javascript()) {
switch (strtolower($type)) {
case 'grouping' :
// Set a grouping condition.
$driver->click('//div[@class="availability-button"]/button');
$driver->click('//button[@id="availability_addrestriction_grouping"]');
$escparam = behat_context_helper::escape($param);
$nodes = $driver->find(
'//span[contains(concat(" " , @class, " "), " availability_grouping ")]//' .
'option[normalize-space(.) = ' . $escparam . ']');
if (count($nodes) != 1) {
throw new Exception('Cannot find grouping in dropdown' . count($nodes));
}
$node = reset($nodes);
$value = $node->getValue();
$driver->selectOption(
'//span[contains(concat(" " , @class, " "), " availability_grouping ")]//' .
'select', $value);
break;
default:
// We don't support other types yet. The test author must write
// manual 'click on that button, etc' commands.
throw new Exception('The availability type "' . $type .
'" is currently not supported - must set manually');
}
} else {
$courseid = $driver->getValue('//input[@name="course"]');
switch (strtolower($type)) {
case 'grouping' :
// Define result with one grouping condition.
$groupingid = $DB->get_field('groupings', 'id',
array('courseid' => $courseid, 'name' => $param));
$json = \core_availability\tree::get_root_json(array(
\availability_grouping\condition::get_json($groupingid)));
break;
default:
// We don't support other types yet.
throw new Exception('The availability type "' . $type .
'" is currently not supported - must set with JavaScript');
}
$driver->setValue('//textarea[@name="availabilityconditionsjson"]',
json_encode($json));
}
}
}
@@ -0,0 +1,100 @@
<?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/>.
/**
* Single checkbox form element.
*
* @package core_form
* @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.
require_once(__DIR__ . '/behat_form_field.php');
/**
* Checkbox form field.
*
* @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_checkbox extends behat_form_field {
/**
* Sets the value of a checkbox.
*
* Anything !empty() is considered checked.
*
* @param string $value
* @return void
*/
public function set_value($value) {
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();
} 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();
}
}
/**
* Returns whether the field is checked or not.
*
* @return bool True if it is checked and false if it's not.
*/
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;
}
}
+118
View File
@@ -0,0 +1,118 @@
<?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/>.
/**
* Date form field class.
*
* @package core_form
* @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.
require_once(__DIR__ . '/behat_form_group.php');
use Behat\Mink\Exception\ExpectationException;
/**
* Date form field.
*
* This class will be refactored in case we are interested in
* creating more complex formats to fill date and 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 extends behat_form_group {
/**
* Sets the value to a date field.
*
* @param string $value The value to be assigned to the date selector field. The string value must be either
* parsable into a UNIX timestamp or equal to 'disabled' (if disabling the date selector).
* @return void
* @throws ExpectationException If the value is invalid.
*/
public function set_value($value) {
if ($value === 'disabled') {
// Disable the given date selector field.
$this->set_child_field_value('enabled', false);
} else if (is_numeric($value)) { // The value is numeric (unix timestamp).
// Assign the mapped values to each form element in the date selector field.
foreach ($this->get_mapped_fields($value) as $childname => $childvalue) {
$this->set_child_field_value($childname, $childvalue);
if ($childname === 'enabled') {
// As soon as the form is enabled, reset the day to an existing one (1st). Without that
// undesired modifications (JS) happens when changing of month and day if
// the interim combination doesn't exists (for example, 31 March => 01 April).
// Note that instead of always setting the day to 1, this could be a little more
// clever, for example only changing when the day > 28, or only when the
// months (current or changed) have less days that the other. But that would
// require more complex calculations than the simpler line below.
$this->set_child_field_value('day', 1);
}
}
} else { // Invalid value.
// Get the name of the field.
$fieldname = $this->field->find('css', 'legend')->getHtml();
throw new ExpectationException("Invalid value for '{$fieldname}'", $this->session);
}
}
/**
* Returns the date field identifiers and the values that should be assigned to them.
*
* @param int $timestamp The UNIX timestamp
* @return array
*/
protected function get_mapped_fields(int $timestamp): array {
// Order is important, first enable, and then year -> month -> day
// (other order can lead to some transitions not working as expected,
// for example, changing from 15 June to 31 August, Behat ends with
// date being 1 August if the modification order is day, then month).
// Note that the behaviour described above is 100% reproducible
// manually, with the form (JS) auto-fixing things in the middle and
// leading to undesired final dates.
return [
'enabled' => true,
'year' => date('Y', $timestamp),
'month' => date('n', $timestamp),
'day' => date('j', $timestamp),
];
}
/**
* Sets a value to a child element in the date form field.
*
* @param string $childname The name of the child field
* @param string|bool $childvalue The value
*/
private function set_child_field_value(string $childname, $childvalue) {
// Find the given child form element in the date selector field.
$childelement = $this->field->find('css', "*[name$='[{$childname}]']");
if ($childelement) {
// Get the field instance for the given child form element.
$childinstance = $this->get_field_instance_for_element($childelement);
// Set the value to the child form element.
$childinstance->set_value($childvalue);
}
}
}
@@ -0,0 +1,55 @@
<?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/>.
/**
* Date time form field class.
*
* @package core_form
* @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.
require_once(__DIR__ . '/behat_form_date.php');
/**
* Date time form field.
*
* This class will be refactored in case we are interested in
* 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 extends behat_form_date {
/**
* Returns the date field identifiers and the values that should be assigned to them.
*
* @param int $timestamp The UNIX timestamp
* @return array
*/
protected function get_mapped_fields(int $timestamp): array {
return array_merge(parent::get_mapped_fields($timestamp), [
'hour' => date('G', $timestamp),
'minute' => (int) date('i', $timestamp)
]);
}
}
+169
View File
@@ -0,0 +1,169 @@
<?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/>.
/**
* Moodle editor field.
*
* @package core_form
* @category test
* @copyright 2012 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\Element\NodeElement as NodeElement;
require_once(__DIR__ . '/behat_form_textarea.php');
/**
* Moodle editor field.
*
* @package core_form
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_editor extends behat_form_textarea {
/**
* Sets the value to a field.
*
* @param string $value
*/
public function set_value($value): void {
$editorid = $this->field->getAttribute('id');
if ($this->running_javascript()) {
$value = addslashes($value);
// This will be transported in JSON, which doesn't allow newlines in strings, so we must escape them.
$value = str_replace("\n", "\\n", $value);
behat_base::execute_in_matching_contexts('editor', 'set_editor_value', [
$editorid,
$value,
]);
} else {
parent::set_value($value);
}
}
/**
* Returns the current value of the select element.
*
* @return string
*/
public function get_value(): string {
if ($this->running_javascript()) {
// Give any listening editors a chance to persist the value to the textarea.
// Some editors only do this on form submission or similar events.
behat_base::execute_in_matching_contexts('editor', 'store_current_value', [
$this->field->getAttribute('id'),
]);
}
return parent::get_value();
}
/**
* Select all the text in the form field.
*
*/
public function select_text() {
// NodeElement.keyPress simply doesn't work.
if (!$this->running_javascript()) {
throw new coding_exception('Selecting text requires javascript.');
}
$editorid = $this->field->getAttribute('id');
$js = ' (function() {
var e = document.getElementById("'.$editorid.'editable"),
r = rangy.createRange(),
s = rangy.getSelection();
while ((e.firstChild !== null) && (e.firstChild.nodeType != document.TEXT_NODE)) {
e = e.firstChild;
}
e.focus();
r.selectNodeContents(e);
s.setSingleRange(r);
}()); ';
behat_base::execute_script_in_session($this->session, $js);
}
/**
* 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) {
// Fetch the actual value to save fetching it multiple times.
$actualvalue = $this->get_value();
if ($this->text_matches($expectedvalue, $actualvalue)) {
// The text is an exact match already.
return true;
}
if ($this->text_matches("<p>{$expectedvalue}</p>", $actualvalue)) {
// A text editor may silently wrap the content in p tags.
return true;
}
// Standardise both the expected value and the actual field value.
// We are likely dealing with HTML content, given this is an editor.
$expectedvalue = $this->standardise_html($expectedvalue);
$actualvalue = $this->standardise_html($actualvalue);
// Note: We don't need to worry about the floats here that we care about in text_matches.
// That condition isn't relevant to the content of an editor.
if ($expectedvalue === $actualvalue) {
return true;
}
return false;
}
/**
* Standardises the HTML content for comparison.
*
* @param string $html The HTML content to standardise
* @return string The standardised HTML content
*/
protected function standardise_html(string $html): string {
$document = new DOMDocument();
$errorstate = libxml_use_internal_errors(true);
// Format the whitespace nicely.
$document->preserveWhiteSpace = false;
$document->formatOutput = true;
// Wrap the content in a DIV element so that it is not parsed weirdly.
// Note: We must remove newlines too because DOMDocument does not do so, despite preserveWhiteSpace being false.
// Unfortunately this is slightly limited in that it will also remove newlines from <pre> content and similar.
$document->loadHTML(str_replace("\n", "", "<div>{$html}</div>"), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$document->normalizeDocument();
libxml_clear_errors();
libxml_use_internal_errors($errorstate);
// Save the content of the 'div' element, removing the <div> and </div> tags at the start and end.
return trim(substr(
$document->saveHTML($document->getElementsByTagName('div')->item(0)),
5,
-6
));
}
}
+329
View File
@@ -0,0 +1,329 @@
<?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 moodleforms field.
*
* @package core_form
* @category test
* @copyright 2012 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\Element\NodeElement;
use Behat\Mink\Session;
/**
* Representation of a form field.
*
* Basically an interface with Mink session.
*
* @package core_form
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_field implements behat_session_interface {
// All of the functionality of behat_base is shared with form fields via the behat_session_trait trait.
use behat_session_trait;
/**
* @var Session Behat session.
*/
protected $session;
/**
* @var NodeElement The field DOM node to interact with.
*/
protected $field;
/**
* @var string The field's locator.
*/
protected $fieldlocator = false;
/**
* Returns the Mink session.
*
* @param string|null $name name of the session OR active session will be used
* @return \Behat\Mink\Session
*/
public function getSession($name = null) {
return $this->session;
}
/**
* General constructor with the node and the session to interact with.
*
* @param Session $session Reference to Mink session to traverse/modify the page DOM.
* @param NodeElement $fieldnode The field DOM node
* @return void
*/
public function __construct(Session $session, NodeElement $fieldnode) {
$this->session = $session;
$this->field = $fieldnode;
}
/**
* Sets the value to a field.
*
* @param string $value
* @return void
*/
public function set_value($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);
}
/**
* Returns the current value of the select element.
*
* @return string
*/
public function get_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->get_value();
}
/**
* Presses specific keyboard key.
*
* @param mixed $char could be either char ('b') or char-code (98)
* @param string $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')
*/
public function key_press($char, $modifier = null) {
// 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();
$instance->field->keyDown($char, $modifier);
try {
$instance->field->keyPress($char, $modifier);
$instance->field->keyUp($char, $modifier);
} catch (\Facebook\WebDriver\Exception\WebDriverException $e) {
// If the JS handler attached to keydown or keypress destroys the element
// the later events may trigger errors because form element no longer exist
// or is not visible. Ignore such exceptions here.
} catch (\Behat\Mink\Exception\ElementNotFoundException $e) {
// Other Mink drivers can throw this for the same reason as above.
}
}
/**
* 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) {
// 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);
}
/**
* Get the value of an attribute set on this field.
*
* @param string $name The attribute name
* @return string The attribute value
*/
public function get_attribute($name) {
return $this->field->getAttribute($name);
}
/**
* Guesses the element type we are dealing with in case is not a text-based element.
*
* This class is the generic field type, behat_field_manager::get_form_field()
* should be able to find the appropiate class for the field type, but
* in cases like moodle form group elements we can not find the type of
* the field through the DOM so we also need to take care of the
* different field types from here. If we need to deal with more complex
* moodle form elements we will need to refactor this simple HTML elements
* guess method.
*
* @return behat_form_field
*/
private function guess_type() {
return $this->get_field_instance_for_element($this->field);
}
/**
* Returns the appropriate form field object for a given node element.
*
* @param NodeElement $element The node element
* @return behat_form_field
*/
protected function get_field_instance_for_element(NodeElement $element): behat_form_field {
global $CFG;
// We default to the text-based field if nothing was detected.
if (!$type = behat_field_manager::guess_field_type($element, $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, $element);
}
/**
* Returns whether the scenario is running in a browser that can run Javascript or not.
*
* @return bool
*/
protected function running_javascript() {
return get_class($this->session->getDriver()) !== 'Behat\Mink\Driver\BrowserKitDriver';
}
/**
* Waits for all the JS activity to be completed.
*
* @return bool Whether any JS is still pending completion.
*/
protected function wait_for_pending_js() {
if (!$this->running_javascript()) {
// JS is not available therefore there is nothing to wait for.
return false;
}
return behat_base::wait_for_pending_js_in_session($this->session);
}
/**
* Gets the field internal id used by selenium wire protocol.
*
* Only available when running_javascript().
*
* @throws coding_exception
* @return int
*/
protected function get_internal_field_id() {
if (!$this->running_javascript()) {
throw new coding_exception('You can only get an internal ID using the selenium driver.');
}
return $this->getSession()
->getDriver()
->getWebDriver()
->findElement(WebDriverBy::xpath($node->getXpath()))
->getID();
}
/**
* Checks if the provided text matches the field value.
*
* @param string $expectedvalue
* @param string|null $actualvalue The actual value. If not specified, this will be fetched from $this->get_value().
* @return bool
*/
protected function text_matches($expectedvalue, ?string $actualvalue = null): bool {
$actualvalue = $actualvalue ?? $this->get_value();
// Non strict string comparison.
if (trim($expectedvalue) == trim($actualvalue)) {
return true;
}
// Do one more matching attempt for floats that are valid with current decsep in use
// (let's continue non strict comparing them as strings, but once unformatted).
$expectedfloat = unformat_float(trim($expectedvalue), true);
$actualfloat = unformat_float(trim($actualvalue), true);
// If they aren't null or false, then we are good to be compared (basically is_numeric()).
$goodfloats = !is_null($expectedfloat) && ($expectedfloat !== false) &&
!is_null($actualfloat) && ($actualfloat !== false);
if ($goodfloats && ((string)$expectedfloat == (string)$actualfloat)) {
return true;
}
return false;
}
/**
* Gets the field locator.
*
* Defaults to the field label but you can
* specify other locators if you are interested.
*
* Public visibility as in most cases will be hard to
* use this method in a generic way, as fields can
* be selected using multiple ways (label, id, name...).
*
* @throws coding_exception
* @param string $locatortype
* @return string
*/
protected function get_field_locator($locatortype = false) {
if (!empty($this->fieldlocator)) {
return $this->fieldlocator;
}
$fieldid = $this->field->getAttribute('id');
// Defaults to label.
if ($locatortype == 'label' || $locatortype == false) {
$labelnode = $this->session->getPage()->find('xpath', "//label[@for='$fieldid']|//p[@id='{$fieldid}_label']");
// Exception only if $locatortype was specified.
if (!$labelnode && $locatortype == 'label') {
throw new coding_exception('Field with "' . $fieldid . '" id does not have a label.');
}
$this->fieldlocator = $labelnode->getText();
}
// Let's look for the name as a second option (more popular than
// id's when pointing to fields).
if (($locatortype == 'name' || $locatortype == false) &&
empty($this->fieldlocator)) {
$name = $this->field->getAttribute('name');
// Exception only if $locatortype was specified.
if (!$name && $locatortype == 'name') {
throw new coding_exception('Field with "' . $fieldid . '" id does not have a name attribute.');
}
$this->fieldlocator = $name;
}
// Otherwise returns the id if no specific locator type was provided.
if (empty($this->fieldlocator)) {
$this->fieldlocator = $fieldid;
}
return $this->fieldlocator;
}
}
@@ -0,0 +1,122 @@
<?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/>.
/**
* File manager form element.
*
* @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');
/**
* File manager form field.
*
* Simple filemanager field manager to allow
* forms to be filled using TableNodes. It only
* adds files and checks the field contents in the
* root directory. If you want to run complex actions
* that involves subdirectories or other repositories
* than 'Upload a file' you should use steps related with
* behat_filepicker::i_add_file_from_repository_to_filemanager
* this is intended to be used with multi-field
*
* This field manager allows you to:
* - Get: A comma-separated list of the root directory
* file names, including folders.
* - Set: Add a file, in case you want to add more than
* one file you can always set two table rows using
* the same locator.
* - Match: A comma-separated list of file names.
*
* @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_filemanager extends behat_form_field {
/**
* Gets the value.
*
* @return string A comma-separated list of the root directory file names.
*/
public function get_value() {
// Wait until DOM and JS is ready.
$this->session->wait(behat_base::get_timeout(), behat_base::PAGE_READY_JS);
// Get the label to restrict the files to this single form field.
$fieldlabel = $this->get_field_locator();
// Get the name of the current directory elements.
$xpath = "//p[normalize-space(.)='$fieldlabel']" .
"/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' fitem ')]" .
"/descendant::div[@data-fieldtype = 'filemanager']" .
"/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]";
// We don't need to wait here, also we don't have access to protected
// contexts find* methods.
$files = $this->session->getPage()->findAll('xpath', $xpath);
if (!$files) {
return '';
}
$filenames = array();
foreach ($files as $filenode) {
$filenames[] = $filenode->getText();
}
return implode(',', $filenames);
}
/**
* Sets the field value.
*
* @param string $value
* @return void
*/
public function set_value($value) {
// Getting the filemanager label from the DOM.
$fieldlabel = $this->get_field_locator();
// Getting the filepicker context and using the step definition
// to upload the requested file.
$uploadcontext = behat_context_helper::get('behat_repository_upload');
$uploadcontext->i_upload_file_to_filemanager($value, $fieldlabel);
}
/**
* Matches the provided filename/s against the current field value.
*
* If the filemanager contains more than one file the $expectedvalue
* value should include all the file names separating them by comma.
*
* @param string $expectedvalue
* @return bool The provided value matches the field value?
*/
public function matches($expectedvalue) {
return $this->text_matches($expectedvalue);
}
}
+43
View File
@@ -0,0 +1,43 @@
<?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 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
* @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 {
}
@@ -0,0 +1,74 @@
<?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/>.
/**
* Custom interaction with inplace editable elements.
*
* @package core_form
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @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');
/**
* Custom interaction with inplace editable elements.
*
* @package core_form
* @category test
* @copyright 2019 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_inplaceeditable extends behat_form_text {
/**
* Sets the value to a field.
*
* @param string $value
* @return void
*/
public function set_value($value) {
// Require JS to run this step.
self::require_javascript();
// Click to enable editing.
self::execute(
'behat_general::i_click_on_in_the',
[
'[data-inplaceeditablelink]',
'css_element',
$this->field,
'NodeElement',
]
);
// Note: It is not possible to use the NodeElement->keyDown() and related functions because
// this can trigger a focusOnElement call each time.
// Instead use the behat_base::type_keys() function.
// The inplace editable selects all existing content on focus.
// Clear the existing value.
self::type_keys($this->session, [behat_keys::BACKSPACE]);
// Type in the new value, followed by ENTER to save the value.
self::type_keys($this->session, array_merge(
str_split($value),
[behat_keys::ENTER]
));
}
}
@@ -0,0 +1,51 @@
<?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/>.
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/behat_form_select.php');
/**
* Custom interaction with inplace editable elements of type select
*
* @package core_form
* @copyright 2021 Paul Holden <paulh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_inplaceeditable_select extends behat_form_select {
/**
* Sets the value to a field
*
* @param string $value
*/
public function set_value($value): void {
// Require JS to run this step.
self::require_javascript();
// Enable editing.
self::execute('behat_general::i_click_on_in_the', [
'[data-inplaceeditablelink]',
'css_element',
$this->field,
'NodeElement',
]);
// After editing is enabled, set the select field value.
$select = $this->field->find('css', 'select');
$select->selectOption($value);
}
}
@@ -0,0 +1,42 @@
<?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/>.
/**
* Silly behat_form_select extension.
*
* @package core_form
* @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.
require_once(__DIR__ . '/behat_form_select.php');
/**
* Allows interaction with fmodvisible form fields.
*
* Plain behat_form_select extension as is the same
* kind of field.
*
* @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_modvisible extends behat_form_select {
}
@@ -0,0 +1,68 @@
<?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/>.
/**
* Silly behat_form_select extension.
*
* @package core_form
* @category test
* @copyright 2013 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
require_once(__DIR__ . '/behat_form_text.php');
/**
* Allows interaction with passwordunmask form fields.
*
* Plain behat_form_select extension as it is the same
* kind of field.
*
* @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_passwordunmask extends behat_form_text {
/**
* Sets the value to a field.
*
* @param string $value
* @return void
*/
public function set_value($value) {
if (!$this->running_javascript()) {
$this->field->setValue($value);
return;
}
$id = $this->field->getAttribute('id');
$wrapper = $this->field->getParent()->getParent()->getParent()->find('css', '[data-passwordunmask="wrapper"]');
$wrapper->click();
$this->wait_for_pending_js();
behat_base::type_keys($this->session, str_split($value));
$this->wait_for_pending_js();
// Press enter key after setting password to save.
behat_base::type_keys($this->session, [behat_keys::ENTER]);
}
}
+91
View File
@@ -0,0 +1,91 @@
<?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/>.
/**
* Radio input form element.
*
* @package core_form
* @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.
require_once(__DIR__ . '/behat_form_checkbox.php');
/**
* Radio input form field.
*
* Extends behat_form_checkbox as the set_value() behaviour
* 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
* a group of radio inputs, so is hard to find it alone and
* detect it by behat_field_manager::get_form_field(), where is useful
* is when the default behat_form_field class is being used, it
* finds a input[type=radio] and it delegates set_value() and
* get_value() to behat_form_radio.
*
* @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_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->isSelected();
}
/**
* 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()) {
// Check on radio button.
$this->field->click();
// Trigger the onchange event as triggered when 'selecting' the radio.
if (!empty($value) && !$this->field->isSelected()) {
$this->trigger_on_change();
}
} else {
// BrowserKit does not accept a check nor a click in an input[type=radio].
$this->field->setValue($this->field->getAttribute('value'));
}
}
}
+202
View File
@@ -0,0 +1,202 @@
<?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/>.
/**
* Single select form field class.
*
* @package core_form
* @category test
* @copyright 2012 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');
/**
* Single select form field.
*
* @package core_form
* @category test
* @copyright 2012 David Monllaó
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_select extends behat_form_field {
/**
* Sets the value(s) of a select element.
*
* Seems an easy select, but there are lots of combinations
* of browsers and operative systems and each one manages the
* autosubmits and the multiple option selects in a different way.
*
* @param string $value plain value or comma separated values if multiple. Commas in values escaped with backslash.
* @return void
*/
public function set_value($value) {
// Is the select multiple?
$multiple = $this->field->hasAttribute('multiple');
// Here we select the option(s).
if ($multiple) {
// Split and decode values. Comma separated list of values allowed. With valuable commas escaped with backslash.
$options = preg_replace('/\\\,/', ',', preg_split('/(?<!\\\),/', trim($value)));
// This is a multiple select, let's pass the multiple flag after first option.
$afterfirstoption = false;
foreach ($options as $option) {
$this->field->selectOption(trim($option), $afterfirstoption);
$afterfirstoption = true;
}
} else {
// By default, assume the passed value is a non-multiple option.
$this->field->selectOption(trim($value));
}
}
/**
* Returns the text of the currently selected options.
*
* @return string Comma separated if multiple options are selected. Commas in option texts escaped with backslash.
*/
public function get_value() {
return $this->get_selected_options();
}
/**
* Returns whether the provided argument matches the current value.
*
* @param mixed $expectedvalue
* @return bool
*/
public function matches($expectedvalue) {
$multiple = $this->field->hasAttribute('multiple');
// Same implementation as the parent if it is a single select.
if (!$multiple) {
$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;
}
// We are dealing with a multi-select.
// Unescape + trim all options and flip it to have the expected values as keys.
$expectedoptions = $this->get_unescaped_options($expectedvalue);
// Get currently selected option's texts.
$texts = $this->get_selected_options(true);
$selectedoptiontexts = $this->get_unescaped_options($texts);
// Get currently selected option's values.
$values = $this->get_selected_options(false);
$selectedoptionvalues = $this->get_unescaped_options($values);
// We check against string-ordered lists of options.
if ($expectedoptions !== $selectedoptiontexts &&
$expectedoptions !== $selectedoptionvalues) {
return false;
}
return true;
}
/**
* Cleans the list of options and returns it as a string separating options with |||.
*
* @param string $value The string containing the escaped options.
* @return string The options
*/
protected function get_unescaped_options($value) {
// Can be multiple comma separated, with valuable commas escaped with backslash.
$optionsarray = array_map(
'trim',
preg_replace('/\\\,/', ',',
preg_split('/(?<!\\\),/', $value)
)
);
// Sort by value (keeping the keys is irrelevant).
core_collator::asort($optionsarray, SORT_STRING);
// Returning it as a string which is easier to match against other values.
return implode('|||', $optionsarray);
}
/**
* Returns the field selected values.
*
* Externalized from the common behat_form_field API method get_value() as
* matches() needs to check against both values and texts.
*
* @param bool $returntexts Returns the options texts or the options values.
* @return string
*/
protected function get_selected_options($returntexts = true) {
$method = 'getHtml';
if ($returntexts === false) {
$method = 'getValue';
}
// Is the select multiple?
$multiple = $this->field->hasAttribute('multiple');
$selectedoptions = array(); // To accumulate found selected options.
// Driver returns the values as an array or as a string depending
// on whether multiple options are selected or not.
$values = $this->field->getValue();
if (!is_array($values)) {
$values = array($values);
}
// Get all the options in the select and extract their value/text pairs.
$alloptions = $this->field->findAll('xpath', '//option');
foreach ($alloptions as $option) {
// Is it selected?
if (in_array($option->getValue(), $values)) {
if ($multiple) {
// If the select is multiple, text commas must be encoded.
$selectedoptions[] = trim(str_replace(',', '\,', $option->{$method}()));
} else {
$selectedoptions[] = trim($option->{$method}());
}
}
}
return implode(', ', $selectedoptions);
}
/**
* Returns the opton XPath based on it's select xpath.
*
* @param string $option
* @param string $selectxpath
* @return string xpath
*/
protected function get_option_xpath($option, $selectxpath) {
$valueliteral = behat_context_helper::escape(trim($option));
return $selectxpath . "/descendant::option[(./@value=$valueliteral or normalize-space(.)=$valueliteral)]";
}
}
@@ -0,0 +1,101 @@
<?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/>.
declare(strict_types=1);
require_once(__DIR__ . '/behat_form_field.php');
use \Behat\Mink\Element\NodeElement;
/**
* Custom interaction with select_menu elements
*
* @package core_form
* @copyright 2022 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class behat_form_select_menu extends behat_form_field {
/**
* Sets the value of the select menu field.
*
* @param string $value The string that is used to identify an option within the select menu. If the string
* has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
* used to identify a particular group within the select menu, while the second ("Option")
* will be used to identify an option within that group. Otherwise, a string with a single
* item (ex. "Option") will be used to identify an option within the select menu regardless
* of any existing groups.
*/
public function set_value($value) {
self::require_javascript();
$this->field->click();
$option = $this->find_option($value);
$option->click();
}
public function get_value() {
$rootnode = $this->field->getParent();
$input = $rootnode->find('css', 'input');
return $input->getValue();
}
/**
* Checks whether a given option exists in the select menu field.
*
* @param string $option The string that is used to identify an option within the select menu. If the string
* has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
* used to identify a particular group within the select menu, while the second ("Option")
* will be used to identify an option within that group. Otherwise, a string with a single
* item (ex. "Option") will be used to identify an option within the select menu regardless
* of any existing groups.
* @return bool Whether the option exists in the select menu field or not.
*/
public function has_option(string $option): bool {
if ($this->find_option($option)) {
return true;
}
return false;
}
/**
* Finds and returns a given option from the select menu field.
*
* @param string $option The string that is used to identify an option within the select menu. If the string
* has two items separated by '>' (ex. "Group > Option"), the first item ("Group") will be
* used to identify a particular group within the select menu, while the second ("Option")
* will be used to identify an option within that group. Otherwise, a string with a single
* item (ex. "Option") will be used to identify an option within the select menu regardless
* of any existing groups.
* @return NodeElement|null The option element or null if it cannot be found.
*/
private function find_option(string $option): ?NodeElement {
// Split the value string by ">" to determine whether a group has been specified.
$path = preg_split('/\s*>\s*/', trim($option));
if (count($path) > 1) { // Group has been specified.
$optionxpath = '//li[contains(@role, "presentation") and normalize-space(text()) = "' .
$this->escape($path[0]) . '"]' .
'/following-sibling::li[contains(@role, "option") and normalize-space(text()) = "' .
$this->escape($path[1]) . '"]';
} else { // Group has not been specified.
$optionxpath = '//li[contains(@role, "option") and normalize-space(text()) = "' .
$this->escape($path[0]) . '"]';
}
return $this->field->getParent()->find('xpath', $optionxpath);
}
}
@@ -0,0 +1,42 @@
<?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/>.
/**
* Silly behat_form_select extension.
*
* @package core_form
* @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.
require_once(__DIR__ . '/behat_form_select.php');
/**
* Allows interaction with selectyesno form fields.
*
* Plain behat_form_select extension as it is the same
* kind of field.
*
* @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_selectyesno extends behat_form_select {
}
+69
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);
}
}
@@ -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 {
}