From 1bd0ad6fa383e7732e10de243c3fa2023ae01812 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 1 Aug 2022 10:35:59 +0800 Subject: [PATCH] MDL-75282 editor_tiny: Add ability to set value regardless of the editor This is a follow-on part to MDL-75887. Part of MDL-75966. --- .../tiny/tests/behat/behat_editor_tiny.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/editor/tiny/tests/behat/behat_editor_tiny.php b/lib/editor/tiny/tests/behat/behat_editor_tiny.php index 4a8eb7dd73f..b6741a0cca3 100644 --- a/lib/editor/tiny/tests/behat/behat_editor_tiny.php +++ b/lib/editor/tiny/tests/behat/behat_editor_tiny.php @@ -35,7 +35,35 @@ require_once(__DIR__ . '/../../../../behat/behat_base.php'); * @category test * @copyright 2022 Andrew Lyons */ -class behat_editor_tiny extends behat_base { +class behat_editor_tiny extends behat_base implements \core_behat\settable_editor { + /** + * 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; + } + + $js = << { + const instance = editor.getInstanceForElementId('${editorid}'); + if (instance) { + instance.setContent('${value}'); + instance.undoManager.add(); + } + }); + EOF; + + $this->execute_script($js); + } + /** * Set Tiny as default editor before executing Tiny tests. *