Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Moodle editor field.
  19   *
  20   * @package    core_form
  21   * @category   test
  22   * @copyright  2012 David MonllaĆ³
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  27  
  28  use Behat\Mink\Element\NodeElement as NodeElement;
  29  
  30  require_once (__DIR__ . '/behat_form_textarea.php');
  31  
  32  /**
  33   * Moodle editor field.
  34   *
  35   * @todo Support for multiple editors
  36   * @package   core_form
  37   * @category  test
  38   * @copyright 2012 David MonllaĆ³
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class behat_form_editor extends behat_form_textarea {
  42  
  43      /**
  44       * Sets the value to a field.
  45       *
  46       * @param string $value
  47       * @return void
  48       */
  49      public function set_value($value) {
  50  
  51          $editorid = $this->field->getAttribute('id');
  52          if ($this->running_javascript()) {
  53              $value = addslashes($value);
  54              // This will be transported in JSON, which doesn't allow newlines in strings, so we must escape them.
  55              $value = str_replace("\n", "\\n", $value);
  56              $js = '
  57  (function() {
  58      var editor = Y.one(document.getElementById("'.$editorid.'editable"));
  59      if (editor) {
  60          editor.setHTML("' . $value . '");
  61      }
  62      editor = Y.one(document.getElementById("'.$editorid.'"));
  63      editor.set("value", "' . $value . '");
  64  })();
  65  ';
  66              behat_base::execute_script_in_session($this->session, $js);
  67          } else {
  68              parent::set_value($value);
  69          }
  70      }
  71  
  72      /**
  73       * Select all the text in the form field.
  74       *
  75       */
  76      public function select_text() {
  77          // NodeElement.keyPress simply doesn't work.
  78          if (!$this->running_javascript()) {
  79              throw new coding_exception('Selecting text requires javascript.');
  80          }
  81  
  82          $editorid = $this->field->getAttribute('id');
  83          $js = ' (function() {
  84      var e = document.getElementById("'.$editorid.'editable"),
  85          r = rangy.createRange(),
  86          s = rangy.getSelection();
  87  
  88      while ((e.firstChild !== null) && (e.firstChild.nodeType != document.TEXT_NODE)) {
  89          e = e.firstChild;
  90      }
  91      e.focus();
  92      r.selectNodeContents(e);
  93      s.setSingleRange(r);
  94  }()); ';
  95          behat_base::execute_script_in_session($this->session, $js);
  96      }
  97  
  98      /**
  99       * Matches the provided value against the current field value.
 100       *
 101       * @param string $expectedvalue
 102       * @return bool The provided value matches the field value?
 103       */
 104      public function matches($expectedvalue) {
 105          // A text editor may silently wrap the content in p tags (or not). Neither is an error.
 106          return $this->text_matches($expectedvalue) || $this->text_matches('<p>' . $expectedvalue . '</p>');
 107      }
 108  }
 109