From c7b1cf265181afaa09625d20a2ee018c8d587d94 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 9 Aug 2023 13:46:35 +0800 Subject: [PATCH] MDL-78251 tiny: Reduce use of UI for setting test expectations --- .../tests/behat/codehighlighter_filter.feature | 18 ++++------ lib/editor/tiny/tests/behat/scripts.feature | 12 +++++++ lib/editor/tiny/tests/behat/source_code.feature | 14 -------- lib/tests/behat/behat_forms.php | 38 ++++++++++++++++++++++ 4 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 lib/editor/tiny/tests/behat/scripts.feature delete mode 100644 lib/editor/tiny/tests/behat/source_code.feature diff --git a/filter/codehighlighter/tests/behat/codehighlighter_filter.feature b/filter/codehighlighter/tests/behat/codehighlighter_filter.feature index 3ece6c303a5..f0e0adb9af3 100644 --- a/filter/codehighlighter/tests/behat/codehighlighter_filter.feature +++ b/filter/codehighlighter/tests/behat/codehighlighter_filter.feature @@ -1,21 +1,15 @@ -@editor @editor_tiny @filter @filter_codehighlighter +@filter @filter_codehighlighter Feature: Render text content using a codehighliter filter To display code to be well-styled - I need to render text content. @javascript Scenario: Update admin profile description with a code content Given the "codehighlighter" filter is "on" - And I log in as "admin" - And I follow "Profile" in the user menu - When I click on "Edit profile" "link" in the "region-main" "region" - And I click on "//span[@class='tox-mbtn__select-label'][contains(text(), 'Insert')]" "xpath_element" - And I click on "//div[@class='tox-collection__item-label'][contains(text(), 'Code sample...')]" "xpath_element" - And I set the field with xpath "//div[@class='tox-selectfield']/select" to "PHP" - And I set the field with xpath "//textarea" to "
$t = date();
" - And I click on "//button[@class='tox-button'][contains(text(), 'Save')]" "xpath_element" - And I click on "Update profile" "button" - Then I should see "Changes saved" - And "//span[@class='token variable'][contains(text(),'$t')]" "xpath_element" should exist + And the following "user" exists: + | username | example | + | description |
$t = date(); |
+    And I am on the Profile page logged in as "example"
+    Then "//span[@class='token variable'][contains(text(),'$t')]" "xpath_element" should exist
     And "//span[@class='token operator'][contains(text(),'=')]" "xpath_element" should exist
     And "//span[@class='token punctuation'][contains(text(),'(')]" "xpath_element" should exist
     And "//span[@class='token punctuation'][contains(text(),')')]" "xpath_element" should exist
diff --git a/lib/editor/tiny/tests/behat/scripts.feature b/lib/editor/tiny/tests/behat/scripts.feature
new file mode 100644
index 00000000000..6254c824f98
--- /dev/null
+++ b/lib/editor/tiny/tests/behat/scripts.feature
@@ -0,0 +1,12 @@
+@core @editor_tiny @javascript
+Feature: A user can insert script tag in TinyMCE using the default TinyMCE functionalities.
+
+  Scenario: Allow script elements in the editor with the additional HTML plugin disabled.
+    Given the following config values are set as admin:
+      | config   | value | plugin |
+      | disabled | 1     | tiny_html |
+    And I am on the "Profile advanced editing" page logged in as "admin"
+    And I set the field "Description" to "

" + When I click on the "Tools > Source code" menu item for the "Description" TinyMCE editor + And I click on "Save" "button" + Then the field "Description" matches expression "@@s" diff --git a/lib/editor/tiny/tests/behat/source_code.feature b/lib/editor/tiny/tests/behat/source_code.feature deleted file mode 100644 index 1ec8f550fbb..00000000000 --- a/lib/editor/tiny/tests/behat/source_code.feature +++ /dev/null @@ -1,14 +0,0 @@ -@core @editor_tiny @source_code @javascript -Feature: A user can insert script tag in TinyMCE using the default TinyMCE functionalities. - - Scenario: Allow script elements in the editor with the additional HTML plugin disabled. - Given the following config values are set as admin: - | config | value | plugin | - | disabled | 1 | tiny_html | - And I log in as "admin" - And I open my profile in edit mode - When I click on the "Tools > Source code" menu item for the "Description" TinyMCE editor - Then I set the field with xpath "//textarea[@class='tox-textarea']" to "

" - And I click on "Save" "button" - When I click on the "Tools > Source code" menu item for the "Description" TinyMCE editor - Then the field with xpath "//textarea[@class='tox-textarea']" matches value "

" diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index 7acbe001d40..c854f1c1ad5 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -319,6 +319,44 @@ class behat_forms extends behat_base { } /** + * Checks, the field contains the value. + * + * @Then /^the field "(?P(?:[^"]|\\")*)" (?Pdoes not )?match(?:es)* expression "(?P(?:[^"]|\\")*)"$/ + * @throws ElementNotFoundException Thrown by behat_base::find + * @param string $field The naem or reference to the field + * @param bool $doesnot + * @param string $expression The Perl-like regular expression, including any delimeters and flag + * @return void + */ + public function the_field_matches_expression( + string $field, + bool $doesnot, + string $expression, + ): void { + // Get the field. + $formfield = behat_field_manager::get_form_field_from_label($field, $this); + + // Checks if the provided value matches the current field value. + $fieldvalue = $formfield->get_value(); + $matches = preg_match($expression, $fieldvalue); + if ($matches === 1 && $doesnot) { + throw new ExpectationException( + "The '{$field}' field matches the expression '{$expression}' and it should not", + $this->getSession() + ); + } else if ($matches === 0 && !$doesnot) { + throw new ExpectationException( + "The '{$field}' field does not match the expression '{$expression}'", + $this->getSession() + ); + } else if ($matches === false) { + throw new coding_exception( + "The expression '{$expression}' was not valid", + ); + } + } + + /** * Checks, the field matches the value. * * @Then /^the field "(?P(?:[^"]|\\")*)" matches multiline:$/ -- 2.11.4.GIT