From 9f07f05a4897f2901aea5f681d746a03cb8d647f Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Thu, 13 Feb 2014 15:57:44 +0800 Subject: [PATCH] MDL-43883 Behat: Make Atto the default text editor and adjust behat to use it. --- lib/behat/behat_base.php | 62 +--------- lib/behat/form_field/behat_form_editor.php | 116 +----------------- lib/db/install.php | 2 +- lib/editor/atto/db/install.php | 49 -------- .../tests/behat/edit_available_icons.feature | 5 + lib/tests/behat/behat_forms.php | 3 - lib/tests/behat/behat_permissions.php | 6 - 7 files changed, 15 insertions(+), 228 deletions(-) delete mode 100644 lib/editor/atto/db/install.php diff --git a/lib/behat/behat_base.php b/lib/behat/behat_base.php index 12b7e4975d0..061b896c3be 100644 --- a/lib/behat/behat_base.php +++ b/lib/behat/behat_base.php @@ -577,70 +577,14 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext { /** * Ensures that all the page's editors are loaded. * - * This method is expensive as it waits for .mceEditor CSS - * so use with caution and only where there will be editors. - * + * @deprecated since Moodle 2.7 MDL-44084 - please do not use this function any more. * @throws ElementNotFoundException * @throws ExpectationException * @return void */ protected function ensure_editors_are_loaded() { - - if (!$this->running_javascript()) { - return; - } - - // If there are no editors we don't need to wait. - try { - $this->find('css', '.mceEditor', false, false, self::REDUCED_TIMEOUT); - } catch (ElementNotFoundException $e) { - return; - } - - // Exception if it timesout and the element is not appearing. - $msg = 'The editors are not completely loaded'; - $exception = new ExpectationException($msg, $this->getSession()); - - // Here we know that there are .mceEditor editors in the page and we will - // probably need to interact with them, if we use tinyMCE JS var before - // it exists it will throw an exception and we want to catch it until all - // the page's editors are ready to interact with them. - $this->spin( - function($context) { - - // It may return 0 if tinyMCE is loaded but not the instances, so we just loop again. - $neditors = $context->getSession()->evaluateScript('return tinyMCE.editors.length;'); - if ($neditors == 0) { - return false; - } - - // It may be there but not ready. - $iframeready = $context->getSession()->evaluateScript(' - var readyeditors = new Array; - for (editorid in tinyMCE.editors) { - if (tinyMCE.editors[editorid].getDoc().readyState === "complete") { - readyeditors[editorid] = editorid; - } - } - if (tinyMCE.editors.length === readyeditors.length) { - return "complete"; - } - return ""; - '); - - // Now we know that the editors are there. - if ($iframeready) { - return true; - } - - // Loop again if it is not ready. - return false; - }, - false, - self::EXTENDED_TIMEOUT, - $exception, - true - ); + debugging('Function behat_base::ensure_editors_are_loaded() is deprecated. It is no longer required.'); + return; } } diff --git a/lib/behat/form_field/behat_form_editor.php b/lib/behat/form_field/behat_form_editor.php index b97976abf2e..0ac5dd90b58 100644 --- a/lib/behat/form_field/behat_form_editor.php +++ b/lib/behat/form_field/behat_form_editor.php @@ -48,118 +48,14 @@ class behat_form_editor extends behat_form_textarea { */ public function set_value($value) { - $lastexception = null; - - // We want the editor to be ready, otherwise the value can not - // be set and an exception is thrown. - for ($i = 0; $i < behat_base::EXTENDED_TIMEOUT; $i++) { - try { - // Get tinyMCE editor id if it exists. - if ($editorid = $this->get_editor_id()) { - - // Set the value to the iframe and save it to the textarea. - $value = str_replace('"', '\"', $value); - $this->session->executeScript(' - tinyMCE.get("'.$editorid.'").setContent("' . $value . '"); - tinyMCE.get("'.$editorid.'").save(); - '); - - } else { - // Set the value to a textarea otherwise. - parent::set_value($value); - } - return; - - } catch (Exception $e) { - // Catching any kind of exception and ignoring it until times out. - $lastexception = $e; - - // Waiting 0.1 seconds. - usleep(100000); - } + $editorid = $this->field->getAttribute('id'); + if ($this->running_javascript()) { + $js = 'M.editor_atto.get_editable_node("'.$editorid.'").setHTML("' . $value . '").focus();'; + $result = $this->session->executeScript($js); + } else { + parent::set_value($value); } - - // If it is not available we throw the last exception. - throw $lastexception; } - /** - * Returns the field value. - * - * @return string - */ - public function get_value() { - - // Can be be a string value or an exception depending whether the editor loads or not. - $lastoutcome = ''; - - // We want the editor to be ready to return the correct value, sometimes the - // page loads too fast and the returned value may be '' if the editor didn't - // have enough time to load completely despite having a different value. - for ($i = 0; $i < behat_base::EXTENDED_TIMEOUT; $i++) { - try { - - // Get tinyMCE editor id if it exists. - if ($editorid = $this->get_editor_id()) { - - // Save the current iframe value in case default value has been edited. - $this->session->executeScript('tinyMCE.get("'.$editorid.'").save();'); - } - - $lastoutcome = $this->field->getValue(); - - // We only want to wait until it times out if the value is empty. - if ($lastoutcome != '') { - return $lastoutcome; - } - - } catch (Exception $e) { - // Catching any kind of exception and ignoring it until times out. - $lastoutcome = $e; - - // Waiting 0.1 seconds. - usleep(100000); - } - } - - // If it is not available we throw the last exception. - if (is_a($lastoutcome, 'Exception')) { - throw $lastoutcome; - } - - // Return the value if there are no exceptions it will be '' at this point - return $lastoutcome; - } - - /** - * Returns the tinyMCE editor id or false if it is not available. - * - * The editor availability depends on the driver running the tests; Goutte - * can not execute Javascript, also some Moodle settings disables the HTML - * editor. - * - * @return mixed The id of the editor of false if it is not available - */ - protected function get_editor_id() { - - // Non-JS drivers throws exceptions when running JS. - try { - $available = $this->session->evaluateScript('return (typeof tinyMCE != "undefined")'); - - // Also checking that it exists a tinyMCE editor for the requested field. - $editorid = $this->field->getAttribute('id'); - $available = $this->session->evaluateScript('return (typeof tinyMCE.get("'.$editorid.'") != "undefined")'); - - } catch (Exception $e) { - return false; - } - - // No available if JS drivers returned false. - if ($available == false) { - return false; - } - - return $editorid; - } } diff --git a/lib/db/install.php b/lib/db/install.php index c5e41506285..bfc55d2bfa9 100644 --- a/lib/db/install.php +++ b/lib/db/install.php @@ -129,7 +129,7 @@ function xmldb_main_install() { 'sessiontimeout' => 7200, // must be present during roles installation 'stringfilters' => '', // These two are managed in a strange way by the filters 'filterall' => 0, // setting page, so have to be initialised here. - 'texteditors' => 'tinymce,textarea', + 'texteditors' => 'atto,tinymce,textarea', ); foreach($defaults as $key => $value) { set_config($key, $value); diff --git a/lib/editor/atto/db/install.php b/lib/editor/atto/db/install.php deleted file mode 100644 index 6fd16b58178..00000000000 --- a/lib/editor/atto/db/install.php +++ /dev/null @@ -1,49 +0,0 @@ -. - -/** - * Atto text editor installation steps. - * - * @package editor_atto - * @copyright 2013 Damyon Wiese - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); - -/** - * Enable this text editor by default. - * - * @return bool - */ -function xmldb_editor_atto_install() { - global $CFG; - // Get the current list of editors. - $currentconfig = $CFG->texteditors; - if (is_null($currentconfig)) { - $currentconfig = ''; - } - $editors = explode(',', $currentconfig); - // Insert atto in the second position. - array_splice($editors, 1, 0, array('atto')); - // Remove duplicates. - $editors = array_unique($editors); - // Set the new config. - unset_config('texteditors'); - set_config('texteditors', implode(',', $editors)); - - return true; -} diff --git a/lib/editor/tinymce/tests/behat/edit_available_icons.feature b/lib/editor/tinymce/tests/behat/edit_available_icons.feature index fc8c7d64a81..e953607a6ea 100644 --- a/lib/editor/tinymce/tests/behat/edit_available_icons.feature +++ b/lib/editor/tinymce/tests/behat/edit_available_icons.feature @@ -9,6 +9,11 @@ Feature: Add or remove items from the TinyMCE editor toolbar | fullname | shortname | category | | Course 1 | C1 | 0 | And I log in as "admin" + And I follow "Admin User" + And I follow "Edit profile" + And I select "TinyMCE HTML editor" from "Text editor" + And I press "Update profile" + And I follow "Home" @javascript Scenario: Remove icons diff --git a/lib/tests/behat/behat_forms.php b/lib/tests/behat/behat_forms.php index ab39bc553f7..127b74f7588 100644 --- a/lib/tests/behat/behat_forms.php +++ b/lib/tests/behat/behat_forms.php @@ -99,9 +99,6 @@ class behat_forms extends behat_base { */ protected function expand_all_fields() { - // We ensure that all the editors are loaded and we can interact with them. - $this->ensure_editors_are_loaded(); - // We already know that we waited for the DOM and the JS to be loaded, even the editor // so, we will use the reduced timeout as it is a common task and we should save time. try { diff --git a/lib/tests/behat/behat_permissions.php b/lib/tests/behat/behat_permissions.php index 94dcd33612c..fcb1646bf82 100644 --- a/lib/tests/behat/behat_permissions.php +++ b/lib/tests/behat/behat_permissions.php @@ -95,12 +95,6 @@ class behat_permissions extends behat_base { try { $advancedtoggle = $this->find_button(get_string('showadvanced', 'form')); if ($advancedtoggle) { - - // As we are interacting with a moodle form we wait for the editor to be ready - // otherwise we may have problems when setting values on it or clicking on elements - // as the position of the elements will change once the editor is loaded. - $this->ensure_editors_are_loaded(); - $advancedtoggle->click(); // Wait for the page to load.