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 |
namespace mod_quiz\external;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
require_once(__DIR__ . '/../../../../webservice/tests/helpers.php');
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* Tests for override webservices
|
|
|
25 |
*
|
|
|
26 |
* @package mod_quiz
|
|
|
27 |
* @copyright 2024 Matthew Hilton <matthewhilton@catalyst-au.net>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
* @covers \mod_quiz\external\get_overrides
|
|
|
30 |
* @covers \mod_quiz\external\save_overrides
|
|
|
31 |
* @covers \mod_quiz\external\delete_overrides
|
|
|
32 |
*/
|
|
|
33 |
final class override_test extends \externallib_advanced_testcase {
|
|
|
34 |
/**
|
|
|
35 |
* Creates a quiz for testing.
|
|
|
36 |
*
|
|
|
37 |
* @return object $quiz
|
|
|
38 |
*/
|
|
|
39 |
private function create_quiz(): object {
|
|
|
40 |
$course = $this->getDataGenerator()->create_course();
|
|
|
41 |
return $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Provides values to test_get_overrides
|
|
|
46 |
*
|
|
|
47 |
* @return array
|
|
|
48 |
*/
|
|
|
49 |
public static function get_override_provider(): array {
|
|
|
50 |
return [
|
|
|
51 |
'quiz that exists' => [
|
|
|
52 |
'quizid' => ':quizid',
|
|
|
53 |
],
|
|
|
54 |
'quiz that does not exist' => [
|
|
|
55 |
'quizid' => -1,
|
|
|
56 |
'expectedexception' => \dml_missing_record_exception::class,
|
|
|
57 |
],
|
|
|
58 |
];
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Tests get_overrides
|
|
|
63 |
*
|
|
|
64 |
* @param int|string $quizid
|
|
|
65 |
* @param string $expectedexception
|
|
|
66 |
* @dataProvider get_override_provider
|
|
|
67 |
*/
|
|
|
68 |
public function test_get_overrides(int|string $quizid, string $expectedexception = ''): void {
|
|
|
69 |
global $DB;
|
|
|
70 |
|
|
|
71 |
$this->resetAfterTest();
|
|
|
72 |
$this->setAdminUser();
|
|
|
73 |
|
|
|
74 |
$quiz = $this->create_quiz();
|
|
|
75 |
|
|
|
76 |
// Create an override.
|
|
|
77 |
$DB->insert_record('quiz_overrides', ['quiz' => $quiz->id]);
|
|
|
78 |
|
|
|
79 |
// Replace placeholders.
|
|
|
80 |
if ($quizid == ":quizid") {
|
|
|
81 |
$quizid = $quiz->id;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if (!empty($expectedexception)) {
|
|
|
85 |
$this->expectException($expectedexception);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$result = get_overrides::execute($quizid);
|
|
|
89 |
$this->assertNotEmpty($result);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Provides values to test_save_overrides
|
|
|
94 |
*
|
|
|
95 |
* @return array
|
|
|
96 |
*/
|
|
|
97 |
public static function save_overrides_provider(): array {
|
|
|
98 |
return [
|
|
|
99 |
'good insert' => [
|
|
|
100 |
'data' => [
|
|
|
101 |
'timeopen' => 999,
|
|
|
102 |
],
|
|
|
103 |
],
|
|
|
104 |
'bad insert' => [
|
|
|
105 |
'data' => [
|
|
|
106 |
'id' => ':existingid',
|
|
|
107 |
'timeopen' => -1,
|
|
|
108 |
],
|
|
|
109 |
'expectedexception' => \invalid_parameter_exception::class,
|
|
|
110 |
],
|
|
|
111 |
'good update' => [
|
|
|
112 |
'data' => [
|
|
|
113 |
'timeopen' => 999,
|
|
|
114 |
],
|
|
|
115 |
],
|
|
|
116 |
'bad update' => [
|
|
|
117 |
'data' => [
|
|
|
118 |
'id' => ':existingid',
|
|
|
119 |
'timeopen' => -1,
|
|
|
120 |
],
|
|
|
121 |
'expectedexception' => \invalid_parameter_exception::class,
|
|
|
122 |
],
|
|
|
123 |
];
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Tests save_overrides
|
|
|
128 |
*
|
|
|
129 |
* @dataProvider save_overrides_provider
|
|
|
130 |
* @param array $data
|
|
|
131 |
* @param string $expectedexception
|
|
|
132 |
*/
|
|
|
133 |
public function test_save_overrides(array $data, string $expectedexception = ''): void {
|
|
|
134 |
global $DB;
|
|
|
135 |
|
|
|
136 |
$this->resetAfterTest();
|
|
|
137 |
$this->setAdminUser();
|
|
|
138 |
|
|
|
139 |
$quiz = $this->create_quiz();
|
|
|
140 |
$user = $this->getDataGenerator()->create_user();
|
|
|
141 |
|
|
|
142 |
if (!empty($data['id'])) {
|
|
|
143 |
$data['id'] = $DB->insert_record('quiz_overrides', ['quiz' => $quiz->id, 'userid' => $user->id]);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
// Make a new user to insert a new override for.
|
|
|
147 |
$user = $this->getDataGenerator()->create_user();
|
|
|
148 |
$data = array_merge($data, ['userid' => $user->id]);
|
|
|
149 |
|
|
|
150 |
$payload = [
|
|
|
151 |
'quizid' => $quiz->id,
|
|
|
152 |
'overrides' => [
|
|
|
153 |
$data,
|
|
|
154 |
],
|
|
|
155 |
];
|
|
|
156 |
|
|
|
157 |
if (!empty($expectedexception)) {
|
|
|
158 |
$this->expectException($expectedexception);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
$result = save_overrides::execute($payload);
|
|
|
162 |
|
|
|
163 |
// If has reached here, but not thrown exception and was expected to, fail the test.
|
|
|
164 |
if ($expectedexception) {
|
|
|
165 |
$this->fail("Expected exception " . $expectedexception . " was not thrown");
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
$this->assertNotEmpty($result['ids']);
|
|
|
169 |
$this->assertCount(1, $result['ids']);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Provides values to test_delete_overrides
|
|
|
174 |
*
|
|
|
175 |
* @return array
|
|
|
176 |
*/
|
|
|
177 |
public static function delete_overrides_provider(): array {
|
|
|
178 |
return [
|
|
|
179 |
'delete existing override' => [
|
|
|
180 |
'id' => ':existingid',
|
|
|
181 |
],
|
|
|
182 |
'delete override that does not exist' => [
|
|
|
183 |
'id' => -1,
|
|
|
184 |
'expectedexception' => \invalid_parameter_exception::class,
|
|
|
185 |
],
|
|
|
186 |
];
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Tests delete_overrides
|
|
|
191 |
*
|
|
|
192 |
* @dataProvider delete_overrides_provider
|
|
|
193 |
* @param int|string $id
|
|
|
194 |
* @param string $expectedexception
|
|
|
195 |
*/
|
|
|
196 |
public function test_delete_overrides(int|string $id, string $expectedexception = ''): void {
|
|
|
197 |
global $DB;
|
|
|
198 |
|
|
|
199 |
$this->resetAfterTest();
|
|
|
200 |
$this->setAdminUser();
|
|
|
201 |
|
|
|
202 |
$quiz = $this->create_quiz();
|
|
|
203 |
$user = $this->getDataGenerator()->create_user();
|
|
|
204 |
|
|
|
205 |
if ($id == ':existingid') {
|
|
|
206 |
$id = $DB->insert_record('quiz_overrides', ['quiz' => $quiz->id, 'userid' => $user->id]);
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
if (!empty($expectedexception)) {
|
|
|
210 |
$this->expectException($expectedexception);
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
$result = delete_overrides::execute(['quizid' => $quiz->id, 'ids' => [$id]]);
|
|
|
214 |
|
|
|
215 |
// If has reached here, but not thrown exception and was expected to, fail the test.
|
|
|
216 |
if ($expectedexception) {
|
|
|
217 |
$this->fail("Expected exception " . $expectedexception . " was not thrown");
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
$this->assertNotEmpty($result['ids']);
|
|
|
221 |
$this->assertContains($id, $result['ids']);
|
|
|
222 |
}
|
|
|
223 |
}
|