1441 |
ariadna |
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 editor_tiny\external;
|
|
|
20 |
|
|
|
21 |
use advanced_testcase;
|
|
|
22 |
use core_external\external_api;
|
|
|
23 |
use editor_tiny\plugininfo\tiny;
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Unit tests for the editor_tiny\external get_configuration class.
|
|
|
27 |
*
|
|
|
28 |
* @package editor_tiny
|
|
|
29 |
* @covers \editor_tiny\external\get_configuration
|
|
|
30 |
* @covers \editor_tiny\manager::get_plugin_configuration_for_external
|
|
|
31 |
* @covers \editor_tiny\plugin::is_enabled_for_external
|
|
|
32 |
* @copyright 2025 Moodle Pty Ltd
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
*/
|
|
|
35 |
final class get_configuration_test extends advanced_testcase {
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Basic setup for tests.
|
|
|
39 |
*/
|
|
|
40 |
public function setUp(): void {
|
|
|
41 |
parent::setUp();
|
|
|
42 |
$this->resetAfterTest(true);
|
|
|
43 |
|
|
|
44 |
// Global editor settings.
|
|
|
45 |
set_config('branding', false, 'editor_tiny');
|
|
|
46 |
set_config('extended_valid_elements', 'script[*]', 'editor_tiny');
|
|
|
47 |
|
|
|
48 |
// Editor plugins.
|
|
|
49 |
foreach (\editor_tiny\plugininfo\tiny::get_enabled_plugins() as $plugin) {
|
|
|
50 |
tiny::enable_plugin($plugin, 0);
|
|
|
51 |
}
|
|
|
52 |
tiny::enable_plugin('h5p', 1); // Plugin with get_plugin_configuration_for_external.
|
|
|
53 |
tiny::enable_plugin('media', 1); // Plugin with is_enabled_for_external.
|
|
|
54 |
tiny::enable_plugin('html', 1); // Plugin with no overriden methods.
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Test the external function.
|
|
|
59 |
*
|
|
|
60 |
* @dataProvider execute_provider
|
|
|
61 |
* @param string $contextlevel Context level: system, course or module.
|
|
|
62 |
* @param ?string $role Role name to assign to use. If null, no role is assigned.
|
|
|
63 |
* @return void
|
|
|
64 |
*/
|
|
|
65 |
public function test_execute(string $contextlevel, ?string $role): void {
|
|
|
66 |
global $CFG;
|
|
|
67 |
|
|
|
68 |
// Setup user and context.
|
|
|
69 |
$generator = $this->getDataGenerator();
|
|
|
70 |
$user = $generator->create_user();
|
|
|
71 |
|
|
|
72 |
if ($contextlevel == 'system') {
|
|
|
73 |
$context = \core\context\system::instance();
|
|
|
74 |
if ($role) {
|
|
|
75 |
$generator->role_assign($role, $user->id, $context);
|
|
|
76 |
}
|
|
|
77 |
} else if ($contextlevel == 'course' || $contextlevel == 'module') {
|
|
|
78 |
$course = $generator->create_course();
|
|
|
79 |
if ($contextlevel == 'module') {
|
|
|
80 |
$module = $generator->create_module('forum', ['course' => $course->id]);
|
|
|
81 |
$context = \core\context\module::instance($module->cmid);
|
|
|
82 |
} else {
|
|
|
83 |
$context = \core\context\course::instance($course->id);
|
|
|
84 |
}
|
|
|
85 |
if ($role) {
|
|
|
86 |
$generator->enrol_user($user->id, $course->id, $role);
|
|
|
87 |
} else {
|
|
|
88 |
$this->expectException(\core\exception\require_login_exception::class);
|
|
|
89 |
}
|
|
|
90 |
} else {
|
|
|
91 |
throw new \coding_exception("Invalid context level: {$contextlevel}");
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$this->setUser($user);
|
|
|
95 |
|
|
|
96 |
// Execute function.
|
|
|
97 |
$result = get_configuration::execute($contextlevel, (int) $context->instanceid);
|
|
|
98 |
$result = external_api::clean_returnvalue(get_configuration::execute_returns(), $result);
|
|
|
99 |
|
|
|
100 |
// Check context id.
|
|
|
101 |
self::assertEquals($context->id, $result['contextid']);
|
|
|
102 |
|
|
|
103 |
// Check global settings.
|
|
|
104 |
self::assertEquals(get_config('editor_tiny', 'branding'), $result['branding']);
|
|
|
105 |
self::assertEquals(get_config('editor_tiny', 'extended_valid_elements'), $result['extendedvalidelements']);
|
|
|
106 |
self::assertEquals(self::get_installed_languages(), $result['installedlanguages']);
|
|
|
107 |
|
|
|
108 |
// Check plugin settings.
|
|
|
109 |
$plugins = [];
|
|
|
110 |
if (\tiny_h5p\plugininfo::is_enabled($context, ['pluginname' => 'h5p'], [])) {
|
|
|
111 |
$settings = [];
|
|
|
112 |
foreach (\tiny_h5p\plugininfo::get_plugin_configuration_for_external($context) as $name => $value) {
|
|
|
113 |
$settings[] = ['name' => $name, 'value' => $value];
|
|
|
114 |
}
|
|
|
115 |
$plugins[] = ['name' => 'h5p', 'settings' => $settings];
|
|
|
116 |
}
|
|
|
117 |
$plugins[] = ['name' => 'html', 'settings' => []];
|
|
|
118 |
if (\tiny_media\plugininfo::is_enabled_for_external($context, ['plugginname' => 'media'])) {
|
|
|
119 |
$plugins[] = ['name' => 'media', 'settings' => []];
|
|
|
120 |
}
|
|
|
121 |
self::assertEquals($plugins, $result['plugins']);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Data provider for test_execute.
|
|
|
126 |
*
|
|
|
127 |
* @return array
|
|
|
128 |
*/
|
|
|
129 |
public static function execute_provider(): array {
|
|
|
130 |
return [
|
|
|
131 |
['contextlevel' => 'system', 'role' => null],
|
|
|
132 |
['contextlevel' => 'system', 'role' => 'guest'],
|
|
|
133 |
['contextlevel' => 'system', 'role' => 'manager'],
|
|
|
134 |
['contextlevel' => 'course', 'role' => null],
|
|
|
135 |
['contextlevel' => 'course', 'role' => 'guest'],
|
|
|
136 |
['contextlevel' => 'course', 'role' => 'student'],
|
|
|
137 |
['contextlevel' => 'course', 'role' => 'teacher'],
|
|
|
138 |
['contextlevel' => 'course', 'role' => 'editingteacher'],
|
|
|
139 |
['contextlevel' => 'module', 'role' => null],
|
|
|
140 |
['contextlevel' => 'module', 'role' => 'guest'],
|
|
|
141 |
['contextlevel' => 'module', 'role' => 'student'],
|
|
|
142 |
['contextlevel' => 'module', 'role' => 'teacher'],
|
|
|
143 |
['contextlevel' => 'module', 'role' => 'editingteacher'],
|
|
|
144 |
];
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Test the external function with an invalid context level.
|
|
|
149 |
*/
|
|
|
150 |
public function test_execute_invalid_context_level(): void {
|
|
|
151 |
$this->expectException(\invalid_parameter_exception::class);
|
|
|
152 |
|
|
|
153 |
get_configuration::execute('invalid', (int) SITEID);
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Test the external function with an invalid instance ID.
|
|
|
158 |
*/
|
|
|
159 |
public function test_execute_invalid_instance_id(): void {
|
|
|
160 |
$this->expectException(\invalid_parameter_exception::class);
|
|
|
161 |
|
|
|
162 |
get_configuration::execute('course', -1);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Returns the expected list of installed languages returned by the external function.
|
|
|
167 |
*
|
|
|
168 |
* @return array
|
|
|
169 |
*/
|
|
|
170 |
private static function get_installed_languages(): array {
|
|
|
171 |
$installedlanguages = [];
|
|
|
172 |
foreach (get_string_manager()->get_list_of_translations(true) as $lang => $name) {
|
|
|
173 |
$installedlanguages[] = ['lang' => $lang, 'name' => $name];
|
|
|
174 |
}
|
|
|
175 |
return $installedlanguages;
|
|
|
176 |
}
|
|
|
177 |
}
|