Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400]

   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   * TinyMCE tests.
  19   *
  20   * @package   editor_tinymce
  21   * @category  test
  22   * @copyright 2012 Petr Skoda {@link http://skodak.org}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace editor_tinymce;
  27  
  28  use editor_tinymce_plugin;
  29  use tinymce_texteditor;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  /**
  34   * TinyMCE tests.
  35   *
  36   * @package   editor_tinymce
  37   * @category  test
  38   * @copyright 2012 Petr Skoda {@link http://skodak.org}
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class editor_test extends \advanced_testcase {
  42  
  43      public function test_autoloading() {
  44          // Note: This test core_frankestyle calssloader.
  45          $this->assertTrue(class_exists('editor_tinymce_plugin'));
  46          $this->assertFalse(class_exists('editor_tinymce_plugin_xx_yy'));
  47          $this->assertFalse(class_exists('\editor_tinymce\plugin'));
  48      }
  49  
  50      public function test_toolbar_parsing() {
  51          global $CFG;
  52          require_once("$CFG->dirroot/lib/editorlib.php");
  53          require_once("$CFG->dirroot/lib/editor/tinymce/lib.php");
  54  
  55          $result = tinymce_texteditor::parse_toolbar_setting("bold,italic\npreview");
  56          $this->assertSame(array('bold,italic', 'preview'), $result);
  57  
  58          $result = tinymce_texteditor::parse_toolbar_setting("| bold,|italic*blink\rpreview\n\n| \n paste STYLE | ");
  59          $this->assertSame(array('bold,|,italic,blink', 'preview', 'paste,style'), $result);
  60  
  61          $result = tinymce_texteditor::parse_toolbar_setting("| \n\n| \n \r");
  62          $this->assertSame(array(), $result);
  63  
  64          $result = tinymce_texteditor::parse_toolbar_setting("one\ntwo\n\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten");
  65          $this->assertSame(array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'), $result);
  66      }
  67  
  68      public function test_add_button() {
  69          global $CFG;
  70          $plugin = new tinymce_testplugin(__DIR__);
  71          $config = get_config('editor_tinymce');
  72          $params = array(
  73              'moodle_config' => $config,
  74              'entity_encoding' => "raw",
  75              'plugins' => 'lists,table,style,layer,advhr,advlink,emotions,inlinepopups,' .
  76                  'searchreplace,paste,directionality,fullscreen,nonbreaking,contextmenu,' .
  77                  'insertdatetime,save,iespell,preview,print,noneditable,visualchars,' .
  78                  'xhtmlxtras,template,pagebreak',
  79              'gecko_spellcheck' => true,
  80              'theme_advanced_font_sizes' => "1,2,3,4,5,6,7",
  81              'moodle_plugin_base' => "$CFG->wwwroot/lib/editor/tinymce/plugins/",
  82              'theme_advanced_font_sizes' => "1,2,3,4,5,6,7",
  83              'theme_advanced_layout_manager' => "SimpleLayout",
  84              'theme_advanced_buttons1' => 'one,two,|,three,four',
  85              'theme_advanced_buttons2' => 'five,six',
  86              'theme_advanced_buttons3' => 'seven,eight,|',
  87              'theme_advanced_buttons4' => '|,nine',
  88              'theme_advanced_buttons5' => 'ten,eleven,twelve',
  89              'theme_advanced_buttons6' => 'thirteen,fourteen',
  90              'theme_advanced_buttons7' => 'fiveteen',
  91              'theme_advanced_buttons' => 'zero', // this is a fake entry, it is not a button row.
  92              'theme_something' => 123,
  93          );
  94  
  95          // Count number of rows.
  96          $this->assertSame(7, $plugin->test_count_button_rows($params));
  97  
  98          // Find button - first button in a row.
  99          $this->assertSame(1, $plugin->test_find_button($params, 'one'));
 100          // Find button - last button in a row.
 101          $this->assertSame(4, $plugin->test_find_button($params, 'nine'));
 102          // Find button - middle button in a row.
 103          $this->assertSame(5, $plugin->test_find_button($params, 'eleven'));
 104          // Find button - the only button in a row.
 105          $this->assertSame(7, $plugin->test_find_button($params, 'fiveteen'));
 106          // Find button - button not present.
 107          $this->assertSame(false, $plugin->test_find_button($params, 'sixteen'));
 108          // Find button - button not present.
 109          $this->assertSame(false, $plugin->test_find_button($params, 'zero'));
 110  
 111          // Adding button in the beginning of the row.
 112          $this->assertTrue($plugin->test_add_button_before($params, 1, 'new1', '', true));
 113          $this->assertSame('new1,one,two,|,three,four', $params['theme_advanced_buttons1']);
 114          // Adding button that already exists (nothing changes).
 115          $this->assertTrue($plugin->test_add_button_before($params, 1, 'new1', '', true));
 116          $this->assertSame('new1,one,two,|,three,four', $params['theme_advanced_buttons1']);
 117          // Adding button before existing button.
 118          $this->assertTrue($plugin->test_add_button_before($params, 1, 'new2', 'two', true));
 119          $this->assertSame('new1,one,new2,two,|,three,four', $params['theme_advanced_buttons1']);
 120          // Adding button before another button that does not exist ($alwaysadd = false).
 121          $this->assertTrue($plugin->test_add_button_before($params, 4, 'new3', 'fiveteen', true));
 122          $this->assertSame('new3,|,nine', $params['theme_advanced_buttons4']);
 123          // Adding button before another button that does not exist ($alwaysadd = false).
 124          $this->assertFalse($plugin->test_add_button_before($params, 4, 'new4', 'fiveteen', false));
 125          $this->assertSame('new3,|,nine', $params['theme_advanced_buttons4']);
 126          // Adding button into non-existing 0 row.
 127          $this->assertTrue($plugin->test_add_button_before($params, 0, 'new9'));
 128          $this->assertSame('new9,new1,one,new2,two,|,three,four', $params['theme_advanced_buttons1']);
 129          $this->assertFalse(isset($params['theme_advanced_buttons0']));
 130          // Adding button into non-existing 9 row.
 131          $this->assertTrue($plugin->test_add_button_before($params, 9, 'new10'));
 132          $this->assertSame('new10,fiveteen', $params['theme_advanced_buttons7']);
 133          $this->assertFalse(isset($params['theme_advanced_buttons9']));
 134  
 135          // Adding button in the end of the row.
 136          $this->assertTrue($plugin->test_add_button_after($params, 5, 'new5', '', true));
 137          $this->assertSame('ten,eleven,twelve,new5', $params['theme_advanced_buttons5']);
 138          // Adding button that already exists.
 139          $this->assertTrue($plugin->test_add_button_after($params, 5, 'new5', '', true));
 140          $this->assertSame('ten,eleven,twelve,new5', $params['theme_advanced_buttons5']);
 141          // Adding button after the existing button.
 142          $this->assertTrue($plugin->test_add_button_after($params, 6, 'new6', 'thirteen', true));
 143          $this->assertSame('thirteen,new6,fourteen', $params['theme_advanced_buttons6']);
 144          // Adding button after another button that does not exist ($alwaysadd = true).
 145          $this->assertTrue($plugin->test_add_button_after($params, 6, 'new7', 'fiveteen', true));
 146          $this->assertSame('thirteen,new6,fourteen,new7', $params['theme_advanced_buttons6']);
 147          // Adding button after another button that does not exist ($alwaysadd = false).
 148          $this->assertFalse($plugin->test_add_button_after($params, 6, 'new8', 'fiveteen', false));
 149          $this->assertSame('thirteen,new6,fourteen,new7', $params['theme_advanced_buttons6']);
 150          // Adding button into non-existing 0 row.
 151          $this->assertTrue($plugin->test_add_button_after($params, 0, 'new11'));
 152          $this->assertSame('new9,new1,one,new2,two,|,three,four,new11', $params['theme_advanced_buttons1']);
 153          $this->assertFalse(isset($params['theme_advanced_buttons0']));
 154          // Adding button into non-existing 9 row.
 155          $this->assertTrue($plugin->test_add_button_after($params, 9, 'new12'));
 156          $this->assertSame('new10,fiveteen,new12', $params['theme_advanced_buttons7']);
 157          $this->assertFalse(isset($params['theme_advanced_buttons9']));
 158      }
 159  }
 160  
 161  /**
 162   * Pseudo plugin class for testing editor_tinymce_plugin protected methods
 163   *
 164   * @package   editor_tinymce
 165   * @category  phpunit
 166   * @copyright 2013 Marina Glancy
 167   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 168   */
 169  class tinymce_testplugin extends editor_tinymce_plugin {
 170      protected function update_init_params(array &$params, \context $context, array $options = null) {
 171          // Empty function just to make a class not abstract.
 172      }
 173  
 174      public function test_count_button_rows(array &$params) {
 175          return parent::count_button_rows($params);
 176      }
 177  
 178      public function test_find_button(array &$params, $button) {
 179          return parent::find_button($params, $button);
 180      }
 181  
 182      public function test_add_button_after(array &$params, $row, $button, $after = '', $alwaysadd = true) {
 183          return parent::add_button_after($params, $row, $button, $after, $alwaysadd);
 184      }
 185  
 186      public function test_add_button_before(array &$params, $row, $button, $before = '', $alwaysadd = true) {
 187          return parent::add_button_before($params, $row, $button, $before, $alwaysadd);
 188      }
 189  }