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 declare(strict_types=1); 18 19 namespace core\plugininfo; 20 21 use advanced_testcase; 22 23 /** 24 * Unit tests for the editor plugininfo class 25 * 26 * @package core 27 * @covers \core\plugininfo\editor 28 * @copyright 2023 Andrew Lyons <andrew@nicols.co.uk> 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class editor_test extends advanced_testcase { 32 33 /** 34 * Test that editor::get_enabled_plugins() returns the correct list of enabled plugins. 35 */ 36 public function test_get_enabled_plugins(): void { 37 $this->resetAfterTest(); 38 39 // All plugins are enabled by default. 40 $plugins = editor::get_enabled_plugins(); 41 $this->assertArrayHasKey('tiny', $plugins); 42 $this->assertArrayHasKey('textarea', $plugins); 43 44 // Disable tiny. 45 editor::enable_plugin('textarea', 0); 46 47 $plugins = editor::get_enabled_plugins(); 48 $this->assertArrayHasKey('tiny', $plugins); 49 $this->assertArrayNotHasKey('textarea', $plugins); 50 } 51 52 /** 53 * Test that editor::enable_plugin set to disable all plugins will leave the textarea enabled. 54 */ 55 public function test_enable_plugin_all(): void { 56 $this->resetAfterTest(); 57 58 // All plugins are enabled by default. 59 $plugins = editor::get_enabled_plugins(); 60 foreach ($plugins as $plugin) { 61 editor::enable_plugin($plugin, 0); 62 } 63 64 $plugins = editor::get_enabled_plugins(); 65 $this->assertCount(1, $plugins); 66 $this->assertArrayHasKey('textarea', $plugins); 67 } 68 69 /** 70 * Ensure that plugintype_supports_ordering() returns true. 71 */ 72 public function test_plugintype_supports_ordering(): void { 73 $this->assertTrue(editor::plugintype_supports_ordering()); 74 } 75 76 /** 77 * Ensure that get_sorted_plugins() returns the correct list of plugins. 78 * 79 * @dataProvider get_sorted_plugins_provider 80 * @param string $texteditors The $CFG->texteditors value to use as a base 81 * @param bool $enabledonly 82 * @param array $expected The expected order 83 */ 84 public function test_get_sorted_plugins( 85 string $texteditors, 86 bool $enabledonly, 87 array $expected, 88 ): void { 89 global $CFG; 90 $this->resetAfterTest(true); 91 92 $CFG->texteditors = $texteditors; 93 $this->assertSame( 94 $expected, 95 array_keys(editor::get_sorted_plugins($enabledonly)), 96 ); 97 } 98 99 /** 100 * Data provider for the get_sorted_plugins tests. 101 * 102 * @return array 103 */ 104 public function get_sorted_plugins_provider(): array { 105 $pluginmanager = \core_plugin_manager::instance(); 106 $allplugins = array_keys($pluginmanager->get_plugins_of_type('editor')); 107 108 // Disabled editors are listed alphabetically at the end. 109 $getorder = function (array $plugins) use ($allplugins) { 110 return array_merge( 111 $plugins, 112 array_diff($allplugins, array_values($plugins)), 113 ); 114 }; 115 return [ 116 [ 117 'texteditors' => 'textarea,tiny', 118 'enabledonly' => true, 119 'expected' => [ 120 'textarea', 121 'tiny', 122 ], 123 ], 124 [ 125 'texteditors' => 'tiny,textarea', 126 'enabledonly' => true, 127 'expected' => [ 128 'tiny', 129 'textarea', 130 ], 131 ], 132 [ 133 'texteditors' => 'tiny', 134 'enabledonly' => true, 135 'expected' => [ 136 'tiny', 137 ], 138 ], 139 'Phantom values are removed from the list' => [ 140 'texteditors' => 'fakeeditor', 141 'enabledonly' => true, 142 'expected' => [ 143 ], 144 ], 145 [ 146 'texteditors' => 'textarea,tiny', 147 'enabledonly' => false, 148 'expected' => $getorder([ 149 'textarea', 150 'tiny', 151 ]), 152 ], 153 [ 154 'texteditors' => 'tiny', 155 'enabledonly' => false, 156 'expected' => $getorder([ 157 'tiny', 158 ]), 159 ], 160 ]; 161 } 162 163 /** 164 * Ensure that change_plugin_order() changes the order of the plugins. 165 * 166 * @dataProvider change_plugin_order_provider 167 * @param string $texteditors 168 * @param string $pluginname 169 * @param int $direction 170 * @param array $neworder 171 * @param string $newtexteditors 172 */ 173 public function test_change_plugin_order( 174 string $texteditors, 175 string $pluginname, 176 int $direction, 177 array $neworder, 178 string $newtexteditors, 179 ): void { 180 global $CFG; 181 $this->resetAfterTest(true); 182 183 $CFG->texteditors = $texteditors; 184 editor::change_plugin_order($pluginname, $direction); 185 186 $this->assertSame( 187 $neworder, 188 array_keys(editor::get_sorted_plugins()), 189 ); 190 $this->assertSame($newtexteditors, $CFG->texteditors); 191 } 192 193 /** 194 * Data provider fro the change_plugin_order() tests. 195 * 196 * @return array 197 */ 198 public function change_plugin_order_provider(): array { 199 $pluginmanager = \core_plugin_manager::instance(); 200 $allplugins = array_keys($pluginmanager->get_plugins_of_type('editor')); 201 202 // Disabled editors are listed alphabetically at the end. 203 $getorder = function (array $plugins) use ($allplugins) { 204 return array_merge( 205 $plugins, 206 array_diff($allplugins, array_values($plugins)), 207 ); 208 }; 209 return [ 210 [ 211 'texteditors' => 'textarea,tiny', 212 'pluginname' => 'textarea', 213 'direction' => base::MOVE_DOWN, 214 'expected' => $getorder([ 215 'tiny', 216 'textarea', 217 ]), 218 'newtexteditors' => 'tiny,textarea', 219 ], 220 [ 221 'texteditors' => 'textarea,tiny', 222 'pluginname' => 'tiny', 223 'direction' => base::MOVE_DOWN, 224 // Tiny is already at the bottom of the enabled plugins. 225 'expected' => $getorder([ 226 'textarea', 227 'tiny', 228 ]), 229 'newtexteditors' => 'textarea,tiny', 230 ], 231 [ 232 'texteditors' => 'textarea,tiny', 233 'pluginname' => 'atto', 234 'direction' => base::MOVE_DOWN, 235 // Atto is not enabled. No change expected. 236 'expected' => $getorder([ 237 'textarea', 238 'tiny', 239 ]), 240 'newtexteditors' => 'textarea,tiny', 241 ], 242 [ 243 'texteditors' => 'textarea,tiny', 244 'pluginname' => 'tiny', 245 'direction' => base::MOVE_UP, 246 'expected' => $getorder([ 247 'tiny', 248 'textarea', 249 ]), 250 'newtexteditors' => 'tiny,textarea', 251 ], 252 [ 253 'texteditors' => 'textarea,tiny', 254 'pluginname' => 'tiny', 255 'direction' => base::MOVE_UP, 256 // Tiny is already at the top of the enabled plugins. 257 'expected' => $getorder([ 258 'tiny', 259 'textarea', 260 ]), 261 'newtexteditors' => 'tiny,textarea', 262 ], 263 [ 264 'texteditors' => 'textarea,tiny', 265 'pluginname' => 'atto', 266 'direction' => base::MOVE_UP, 267 // Atto is not enabled. No change expected. 268 'expected' => $getorder([ 269 'textarea', 270 'tiny', 271 ]), 272 'newtexteditors' => 'textarea,tiny', 273 ], 274 [ 275 'texteditors' => 'textarea,tiny', 276 'pluginname' => 'atto', 277 'direction' => base::MOVE_UP, 278 // Atto is not enabled. No change expected. 279 'expected' => $getorder([ 280 'textarea', 281 'tiny', 282 ]), 283 'newtexteditors' => 'textarea,tiny', 284 ], 285 [ 286 'texteditors' => 'textarea,tiny', 287 'pluginname' => 'fakeeditor', 288 'direction' => base::MOVE_UP, 289 // The fakeeditor plugin does not exist. No change expected. 290 'expected' => $getorder([ 291 'textarea', 292 'tiny', 293 ]), 294 'newtexteditors' => 'textarea,tiny', 295 ], 296 ]; 297 } 298 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body