From fab0fa5dfc204a8b29a785faedce8f118a195974 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 29 Jun 2023 10:28:13 +0800 Subject: [PATCH] MDL-76853 editor_tiny: Refactor behat helpers into trait --- .../tiny/tests/behat/behat_editor_tiny.php | 190 +-------------- .../tiny/tests/behat/editor_tiny_helpers.php | 216 ++++++++++++++++++ 2 files changed, 218 insertions(+), 188 deletions(-) create mode 100644 lib/editor/tiny/tests/behat/editor_tiny_helpers.php diff --git a/lib/editor/tiny/tests/behat/behat_editor_tiny.php b/lib/editor/tiny/tests/behat/behat_editor_tiny.php index 8918b19e340..d810cc6dee3 100644 --- a/lib/editor/tiny/tests/behat/behat_editor_tiny.php +++ b/lib/editor/tiny/tests/behat/behat_editor_tiny.php @@ -24,12 +24,12 @@ */ use Behat\Behat\Hook\Scope\BeforeScenarioScope; -use Behat\Mink\Element\NodeElement; use Behat\Mink\Exception\DriverException; use Behat\Mink\Exception\ExpectationException; // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. require_once(__DIR__ . '/../../../../behat/behat_base.php'); +require_once(__DIR__ . '/editor_tiny_helpers.php'); /** * TinyMCE custom behat step definitions. @@ -39,81 +39,7 @@ require_once(__DIR__ . '/../../../../behat/behat_base.php'); * @copyright 2022 Andrew Lyons */ class behat_editor_tiny extends behat_base implements \core_behat\settable_editor { - /** - * Execute some JavaScript for a particular Editor instance. - * - * The editor instance is available on the 'instnace' variable. - * - * @param string $editorid The ID of the editor - * @param string $code The code to execute - */ - protected function execute_javascript_for_editor(string $editorid, string $code): void { - $js = << { - const instance = editor.getInstanceForElementId('{$editorid}'); - {$code} - }); - EOF; - - $this->execute_script($js); - } - - /** - * Resolve some JavaScript for a particular Editor instance. - * - * The editor instance is available on the 'instnace' variable. - * The code should return a value by passing it to the `resolve` function. - * - * @param string $editorid The ID of the editor - * @param string $code The code to evaluate - * @return string|null|array - */ - protected function evaluate_javascript_for_editor(string $editorid, string $code) { - $js = << { - require(['editor_tiny/editor'], (editor) => { - const instance = editor.getInstanceForElementId('{$editorid}'); - if (!instance) { - reject("Instance '{$editorid}' not found"); - } - - {$code} - }); - }); - EOF; - - return $this->evaluate_script($js); - } - - /** - * Set the value for the editor. - * - * Note: This function is called by the behat_form_editor class. - * It is called regardless of the current default editor as editor selection is a user preference. - * Therefore it must fail gracefully and only set a value if the editor instance was found on the page. - * - * @param string $editorid - * @param string $value - */ - public function set_editor_value(string $editorid, string $value): void { - if (!$this->running_javascript()) { - return; - } - - $this->execute_javascript_for_editor($editorid, <<execute_javascript_for_editor($editorid, "instance?.save();"); - } + use editor_tiny_helpers; /** * Set Tiny as default editor before executing Tiny tests. @@ -144,98 +70,6 @@ class behat_editor_tiny extends behat_base implements \core_behat\settable_edito $this->execute('behat_general::the_default_editor_is_set_to', ['tiny']); } - /** - * Ensure that the editor_tiny tag is in use. - * - * This function should be used for any step defined in this file. - * - * @throws DriverException Thrown if the editor_tiny tag is not specified for this file - */ - protected function require_tiny_tags(): void { - // Ensure that this step only runs in TinyMCE tags. - if (!$this->has_tag('editor_tiny')) { - throw new DriverException( - 'TinyMCE tests using this step must have the @editor_tiny tag on either the scenario or feature.' - ); - } - } - - /** - * Get the Mink NodeElement of the