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.
This commit is contained in:
Simey Lameze 2022-08-01 10:35:59 +08:00 committed by Andrew Nicols
parent ebc7d31232
commit 1bd0ad6fa3

View File

@ -35,7 +35,35 @@ require_once(__DIR__ . '/../../../../behat/behat_base.php');
* @category test
* @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>
*/
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 = <<<EOF
require(['editor_tiny/editor'], (editor) => {
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.
*