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