Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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   * Atto custom steps definitions.
  19   *
  20   * @package    editor_atto
  21   * @category   test
  22   * @copyright  2014 Damyon Wiese
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  use Behat\Behat\Hook\Scope\BeforeScenarioScope;
  27  
  28  // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  29  
  30  require_once (__DIR__ . '/../../../../behat/behat_base.php');
  31  require_once (__DIR__ . '/../../../../behat/classes/settable_editor.php');
  32  
  33  /**
  34   * Steps definitions to deal with the atto text editor
  35   *
  36   * @package    editor_atto
  37   * @category   test
  38   * @copyright  2014 Damyon Wiese
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class behat_editor_atto extends behat_base implements \core_behat\settable_editor {
  42  
  43      /**
  44       * Set the value for the editor.
  45       *
  46       * @param string $editorid
  47       * @param string $value
  48       */
  49      public function set_editor_value(string $editorid, string $value): void {
  50          $js = <<<EOF
  51              (function() {
  52                  const editableEditor = document.getElementById("$editorid}editable");
  53                  if (editableEditor && editableEditor.classList.contains('editor_atto_content')) {
  54                      editableEditor.innerHTML = "$value}";
  55                  }
  56                  const editor = document.getElementById("$editorid}");
  57                  if (editor) {
  58                      editor.value = "$value}";
  59                  }
  60              })();
  61          EOF;
  62  
  63          $this->execute_script($js);
  64      }
  65  
  66      /**
  67       * Select the text in an Atto field.
  68       *
  69       * @Given /^I select the text in the "([^"]*)" Atto editor$/
  70       * @throws ElementNotFoundException Thrown by behat_base::find
  71       * @param string $field
  72       * @return void
  73       */
  74      public function select_the_text_in_the_atto_editor($fieldlocator) {
  75          if (!$this->running_javascript()) {
  76              throw new coding_exception('Selecting text requires javascript.');
  77          }
  78          // We delegate to behat_form_field class, it will
  79          // guess the type properly.
  80          $field = behat_field_manager::get_form_field_from_label($fieldlocator, $this);
  81  
  82          if (!method_exists($field, 'select_text')) {
  83              throw new coding_exception('Field does not support the select_text function.');
  84          }
  85          $field->select_text();
  86      }
  87  
  88      /**
  89       * Ensure that the Atto editor is used for all tests using the editor_atto, or atto_* tags.
  90       *
  91       * This ensures, whatever the default editor, that the Atto editor is used for these tests.
  92       *
  93       * @BeforeScenario
  94       * @param BeforeScenarioScope $scope The Behat Scope
  95       */
  96      public function set_default_editor_flag(BeforeScenarioScope $scope): void {
  97          // This only applies to a scenario which matches the editor_atto, or an atto subplugin.
  98          $callback = function (string $tag): bool {
  99              return $tag === 'editor_atto' || substr($tag, 0, 5) === 'atto_';
 100          };
 101  
 102          if (!self::scope_tags_match($scope, $callback)) {
 103              return;
 104          }
 105  
 106          $this->execute('behat_general::the_default_editor_is_set_to', ['atto']);
 107      }
 108  }