Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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_admin\external;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
25
 
26
/**
27
 * Unit tests to configure the plugin order.
28
 *
29
 * Note: Not all plugins can be ordered, so this test is limited to those which support it.
30
 *
31
 * @package     core
32
 * @covers      \core_admin\external\set_plugin_state
33
 * @copyright   2023 Andrew Lyons <andrew@nicols.co.uk>
34
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class set_plugin_order_test extends \externallib_advanced_testcase {
1 efrain 37
    /**
38
     * Text execute method for editor plugins, which support ordering.
39
     *
40
     * @dataProvider execute_editor_provider
41
     * @param string $initialstate The initial state of the plugintype
42
     * @param string $plugin The name of the plugin
43
     * @param int $direction
44
     * @param array $neworder
45
     * @param string $newstate
46
     */
47
    public function test_execute_editors(
48
        string $initialstate,
49
        string $plugin,
50
        int $direction,
51
        array $neworder,
52
        string $newstate,
53
    ): void {
54
        global $CFG;
55
 
56
        $this->resetAfterTest();
57
        $this->setAdminUser();
58
 
59
        $CFG->texteditors = $initialstate;
60
 
61
        set_plugin_order::execute($plugin, $direction);
62
 
63
        $this->assertSame(
64
            $neworder,
65
            array_keys(\core\plugininfo\editor::get_sorted_plugins()),
66
        );
67
        $this->assertSame($newstate, $CFG->texteditors);
68
    }
69
 
70
    /**
71
     * Data provider for base tests of the execute method.
72
     *
73
     * @return array
74
     */
1441 ariadna 75
    public static function execute_editor_provider(): array {
1 efrain 76
        $pluginmanager = \core_plugin_manager::instance();
77
        $allplugins = array_keys($pluginmanager->get_plugins_of_type('editor'));
78
 
79
        // Disabled editors are listed alphabetically at the end.
80
        $getorder = function (array $plugins) use ($allplugins) {
81
            return array_merge(
82
                $plugins,
83
                array_diff($allplugins, array_values($plugins)),
84
            );
85
        };
86
        return [
87
            [
88
                'initialstate' => 'textarea,tiny',
1441 ariadna 89
                'plugin' => 'editor_textarea',
1 efrain 90
                'direction' => 1, // DOWN.
1441 ariadna 91
                'neworder' => $getorder([
1 efrain 92
                    'tiny',
93
                    'textarea',
94
                ]),
1441 ariadna 95
                'newstate' => 'tiny,textarea',
1 efrain 96
            ],
97
            [
98
                'initialstate' => 'textarea,tiny',
1441 ariadna 99
                'plugin' => 'editor_textarea',
1 efrain 100
                'direction' => -1, // UP.
1441 ariadna 101
                'neworder' => $getorder([
1 efrain 102
                    'textarea',
103
                    'tiny',
104
                ]),
1441 ariadna 105
                'newstate' => 'textarea,tiny',
1 efrain 106
            ],
107
            [
108
                'initialstate' => 'textarea,tiny',
1441 ariadna 109
                'plugin' => 'editor_tiny',
1 efrain 110
                'direction' => 1, // DOWN.
111
                // Tiny is already at the bottom of the list of enabled plugins.
1441 ariadna 112
                'neworder' => $getorder([
1 efrain 113
                    'textarea',
114
                    'tiny',
115
                ]),
1441 ariadna 116
                'newstate' => 'textarea,tiny',
1 efrain 117
            ],
118
            [
119
                'initialstate' => 'textarea,tiny',
1441 ariadna 120
                'plugin' => 'editor_atto',
1 efrain 121
                'direction' => 1, // DOWN.
122
                // Atto is not enabled. Disabled editors are listed lexically after enabled editors.
1441 ariadna 123
                'neworder' => $getorder([
1 efrain 124
                    'textarea',
125
                    'tiny',
126
                ]),
1441 ariadna 127
                'newstate' => 'textarea,tiny',
1 efrain 128
            ],
129
        ];
130
    }
131
 
132
    /**
1441 ariadna 133
     * Test re-ordering plugins where one plugin is not enabled.
134
     *
135
     *  Media plugins are ordered by rank, with enabled plugins first.
136
     * This is similar to the editors test but covers a scenario that cannot be covered by the editors test due to
137
     * not having enough plugins.
138
     */
139
    public function test_execute_media_including_disabled(): void {
140
        global $CFG;
141
 
142
        $this->resetAfterTest();
143
        $this->setAdminUser();
144
 
145
        $CFG->media_plugins_sortorder = 'videojs,vimeo,html5video';
146
 
147
        set_plugin_order::execute('youtube', -1);
148
 
149
        $this->assertSame('videojs,vimeo,html5video', $CFG->media_plugins_sortorder);
150
    }
151
 
152
    /**
1 efrain 153
     * Text execute method for plugins which do not support ordering.
154
     *
155
     * @dataProvider execute_non_orderable_provider
156
     * @param string $plugin
157
     */
158
    public function test_execute_editors_non_orderable(string $plugin): void {
159
        $this->resetAfterTest();
160
        $this->setAdminUser();
161
 
162
        $this->assertIsArray(set_plugin_order::execute($plugin, 1));
163
    }
164
 
1441 ariadna 165
    public static function execute_non_orderable_provider(): array {
1 efrain 166
        return [
167
            // Activities do not support ordering.
168
            ['mod_assign'],
169
            // Nor to blocks.
170
            ['block_login'],
171
        ];
172
    }
173
 
174
    /**
175
     * Test execute method with no login.
176
     */
177
    public function test_execute_no_login(): void {
178
        $this->expectException(\require_login_exception::class);
179
        set_plugin_order::execute('editor_tiny', 1);
180
    }
181
 
182
    /**
183
     * Test execute method with no login.
184
     */
185
    public function test_execute_no_capability(): void {
186
        $this->resetAfterTest();
187
        $user = $this->getDataGenerator()->create_user();
188
        $this->setUser($user);
189
        $this->expectException(\required_capability_exception::class);
190
        set_plugin_order::execute('editor_tiny', 1);
191
    }
192
}