Skip to content

Commit

Permalink
Merge branch 'MDL-76853-master' of https://github.com/andrewnicols/mo…
Browse files Browse the repository at this point in the history
  • Loading branch information
HuongNV13 committed Jul 4, 2023
2 parents a5f2833 + fab0fa5 commit 49d6cad
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 188 deletions.
190 changes: 2 additions & 188 deletions lib/editor/tiny/tests/behat/behat_editor_tiny.php
Expand Up @@ -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.
Expand All @@ -39,81 +39,7 @@
* @copyright 2022 Andrew Lyons <andrew@nicols.co.uk>
*/
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 = <<<EOF
require(['editor_tiny/editor'], (editor) => {
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 = <<<EOF
return new Promise((resolve, reject) => {
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, <<<EOF
instance.setContent('{$value}');
instance.undoManager.add();
EOF);
}

/**
* Store the current value of the editor, if it is a Tiny editor, to the textarea.
*
* @param string $editorid The ID of the editor.
*/
public function store_current_value(string $editorid): void {
$this->execute_javascript_for_editor($editorid, "instance?.save();");
}
use editor_tiny_helpers;

/**
* Set Tiny as default editor before executing Tiny tests.
Expand Down Expand Up @@ -144,98 +70,6 @@ public function set_default_editor_flag(BeforeScenarioScope $scope): void {
$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 <textarea> for the specified locator.
*
* Moodle mostly referes to the textarea, rather than the editor itself and interactions are translated to the
* Editor using the TinyMCE API.
*
* @param string $locator A Moodle field locator
* @return NodeElement The element found by the find_field function
*/
protected function get_textarea_for_locator(string $locator): NodeElement {
return $this->find_field($locator);
}

/**
* Get the Mink NodeElement of the container for the specified locator.
*
* This is the top-most HTML element for the editor found by TinyMCE.getContainer().
*
* @param string $locator A Moodle field locator
* @return NodeElement The Mink NodeElement representing the container.
*/
protected function get_editor_container_for_locator(string $locator): NodeElement {
$textarea = $this->get_textarea_for_locator($locator);
$editorid = $textarea->getAttribute('id');

$targetid = uniqid();
$js = <<<EOF
const container = instance.getContainer();
if (!container.id) {
container.id = '{$targetid}';
}
resolve(container.id);
EOF;
$containerid = $this->evaluate_javascript_for_editor($editorid, $js);

return $this->find('css', "#{$containerid}");
}

/**
* Get the name of the iframe relating to the editor.
*
* If no name is found, then add one.
*
* If the editor it not found, then throw an exception.
*
* @param string $locator The name of the editor
* @return string The name of the iframe
*/
protected function get_editor_iframe_name(string $locator): string {
return $this->get_editor_iframe_name_for_element($this->get_textarea_for_locator($locator));
}

/**
* Get the name of the iframe relating to the editor.
*
* If no name is found, then add one.
*
* If the editor it not found, then throw an exception.
* @param NodeElement $editor The editor element
* @return string The name of the iframe
*/
protected function get_editor_iframe_name_for_element(NodeElement $editor): string {
$editorid = $editor->getAttribute('id');

// Ensure that a name is set on the iframe relating to the editorid.
$js = <<<EOF
if (!instance.iframeElement.name) {
instance.iframeElement.name = '{$editorid}';
}
resolve(instance.iframeElement.name);
EOF;

return $this->evaluate_javascript_for_editor($editorid, $js);
}

/**
* Click on a button for the specified TinyMCE editor.
*
Expand Down Expand Up @@ -375,26 +209,6 @@ public function i_upload_a_file_in_the_filepicker(string $filepath): void {
]);
}

/**
* Normalise the fixture file path relative to the dirroot.
*
* @param string $filepath
* @return string
*/
protected function normalise_fixture_filepath(string $filepath): string {
global $CFG;

$filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath);
if (!is_readable($filepath)) {
$filepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath;
if (!is_readable($filepath)) {
throw new ExpectationException('The file to be uploaded does not exist.', $this->getSession());
}
}

return $filepath;
}

/**
* Select in the editor.
*
Expand Down

0 comments on commit 49d6cad

Please sign in to comment.