diff --git a/admin/tool/behat/tests/behat/inplaceeditable.feature b/admin/tool/behat/tests/behat/inplaceeditable.feature new file mode 100644 index 00000000000..014c5f5972b --- /dev/null +++ b/admin/tool/behat/tests/behat/inplaceeditable.feature @@ -0,0 +1,30 @@ +@tool_behat +Feature: Verify that the inplace editable field works as expected + In order to use behat step definitions + As a test write + I need to ensure that the inplace editable works in forms + + Background: + Given the following "course" exists: + | fullname | Course 1 | + | shortname | C1 | + And the following "activities" exist: + | activity | course | name | idnumber | + | forum | C1 | My first forum | forum1 | + | assign | C1 | My first assignment | assign1 | + | quiz | C1 | My first quiz | quiz1 | + And I log in as "admin" + And I am on "Course 1" course homepage with editing mode on + + @javascript + Scenario: Using an inplace editable updates the name of an activity + When I set the field "Edit title" in the "My first assignment" "activity" to "Coursework submission" + Then I should see "Coursework submission" + And I should not see "My first assignment" + But I should see "My first forum" + And I should see "My first quiz" + And I set the field "Edit title" in the "Coursework submission" "activity" to "My first assignment" + And I should not see "Coursework submission" + But I should see "My first assignment" + And I should see "My first forum" + And I should see "My first quiz" diff --git a/lib/behat/behat_field_manager.php b/lib/behat/behat_field_manager.php index b98f843799b..dcef9e27b5e 100644 --- a/lib/behat/behat_field_manager.php +++ b/lib/behat/behat_field_manager.php @@ -48,7 +48,6 @@ class behat_field_manager { * @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. // The DOM node. @@ -172,6 +171,10 @@ class behat_field_manager { } else if ($tagname == 'select') { // Select tag. return 'select'; + } else if ($tagname == 'span') { + if ($fieldnode->hasAttribute('data-inplaceeditable') && $fieldnode->getAttribute('data-inplaceeditable')) { + return 'inplaceeditable'; + } } // We can not provide a closer field type. diff --git a/lib/behat/classes/partial_named_selector.php b/lib/behat/classes/partial_named_selector.php index 1b2d43e822f..60ae36aa3eb 100644 --- a/lib/behat/classes/partial_named_selector.php +++ b/lib/behat/classes/partial_named_selector.php @@ -135,7 +135,7 @@ class behat_partial_named_selector extends \Behat\Mink\Selector\PartialNamedSele */ protected static $moodleselectors = array( 'activity' => << << <<session; + } + /** * General constructor with the node and the session to interact with. diff --git a/lib/behat/form_field/behat_form_inplaceeditable.php b/lib/behat/form_field/behat_form_inplaceeditable.php new file mode 100644 index 00000000000..57cc8b77c94 --- /dev/null +++ b/lib/behat/form_field/behat_form_inplaceeditable.php @@ -0,0 +1,74 @@ +. + +/** + * Custom interaction with inplace editable elements. + * + * @package core_form + * @category test + * @copyright 2019 Andrew Nicols + * @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 + * @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] + )); + } +}