2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * External notes functions unit tests
22 * @copyright 2012 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
31 require_once($CFG->dirroot
. '/notes/externallib.php');
33 class core_notes_externallib_testcase
extends externallib_advanced_testcase
{
38 public function test_create_notes() {
42 $this->resetAfterTest(true);
44 $course = self
::getDataGenerator()->create_course();
46 // Set the required capabilities by the external function.
47 $contextid = context_course
::instance($course->id
)->id
;
48 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
49 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
51 // Create test note data.
53 $note1['userid'] = $USER->id
;
54 $note1['publishstate'] = 'personal';
55 $note1['courseid'] = $course->id
;
56 $note1['text'] = 'the text';
57 $note1['clientnoteid'] = 4;
58 $notes = array($note1);
60 $creatednotes = core_notes_external
::create_notes($notes);
61 // We need to execute the return values cleaning process to simulate the web service server.
62 $creatednotes = external_api
::clean_returnvalue(core_notes_external
::create_notes_returns(), $creatednotes);
64 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
66 // Confirm that base note data was inserted correctly.
67 $this->assertEquals($thenote->userid
, $note1['userid']);
68 $this->assertEquals($thenote->courseid
, $note1['courseid']);
69 $this->assertEquals($thenote->publishstate
, NOTES_STATE_DRAFT
);
70 $this->assertEquals($thenote->content
, $note1['text']);
71 $this->assertEquals($creatednotes[0]['clientnoteid'], $note1['clientnoteid']);
73 // Call without required capability.
74 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
75 $this->expectException('required_capability_exception');
76 $creatednotes = core_notes_external
::create_notes($notes);
79 public function test_delete_notes() {
83 $this->resetAfterTest(true);
85 $course = self
::getDataGenerator()->create_course();
87 // Set the required capabilities by the external function.
88 $contextid = context_course
::instance($course->id
)->id
;
89 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
90 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
92 // Create test note data.
94 $cnote['userid'] = $USER->id
;
95 $cnote['publishstate'] = 'personal';
96 $cnote['courseid'] = $course->id
;
97 $cnote['text'] = 'the text';
98 $cnote['clientnoteid'] = 4;
99 $cnotes = array($cnote);
100 $creatednotes = core_notes_external
::create_notes($cnotes);
101 $creatednotes = external_api
::clean_returnvalue(core_notes_external
::create_notes_returns(), $creatednotes);
103 $dnotes1 = array($creatednotes[0]['noteid']);
104 $deletednotes1 = core_notes_external
::delete_notes($dnotes1);
105 $deletednotes1 = external_api
::clean_returnvalue(core_notes_external
::delete_notes_returns(), $deletednotes1);
107 // Confirm that base note data was deleted correctly.
108 $notdeletedcount = $DB->count_records_select('post', 'id = ' . $creatednotes[0]['noteid']);
109 $this->assertEquals(0, $notdeletedcount);
111 $dnotes2 = array(33); // This note does not exist.
112 $deletednotes2 = core_notes_external
::delete_notes($dnotes2);
113 $deletednotes2 = external_api
::clean_returnvalue(core_notes_external
::delete_notes_returns(), $deletednotes2);
115 $this->assertEquals("note", $deletednotes2[0]["item"]);
116 $this->assertEquals(33, $deletednotes2[0]["itemid"]);
117 $this->assertEquals("badid", $deletednotes2[0]["warningcode"]);
118 $this->assertEquals("Note does not exist", $deletednotes2[0]["message"]);
120 // Call without required capability.
121 $creatednotes = core_notes_external
::create_notes($cnotes);
122 $creatednotes = external_api
::clean_returnvalue(core_notes_external
::create_notes_returns(), $creatednotes);
123 $dnotes3 = array($creatednotes[0]['noteid']);
125 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
126 $this->expectException('required_capability_exception');
127 $deletednotes = core_notes_external
::delete_notes($dnotes3);
128 $deletednotes = external_api
::clean_returnvalue(core_notes_external
::delete_notes_returns(), $deletednotes);
131 public function test_get_notes() {
135 $this->resetAfterTest(true);
137 $course = self
::getDataGenerator()->create_course();
139 // Set the required capabilities by the external function.
140 $contextid = context_course
::instance($course->id
)->id
;
141 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
142 $this->assignUserCapability('moodle/notes:view', $contextid, $roleid);
143 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
145 // Create test note data.
147 $cnote['userid'] = $USER->id
;
148 $cnote['publishstate'] = 'personal';
149 $cnote['courseid'] = $course->id
;
150 $cnote['text'] = 'the text';
151 $cnotes = array($cnote);
153 $creatednotes1 = core_notes_external
::create_notes($cnotes);
154 $creatednotes2 = core_notes_external
::create_notes($cnotes);
155 $creatednotes3 = core_notes_external
::create_notes($cnotes);
157 $creatednotes1 = external_api
::clean_returnvalue(core_notes_external
::create_notes_returns(), $creatednotes1);
158 $creatednotes2 = external_api
::clean_returnvalue(core_notes_external
::create_notes_returns(), $creatednotes2);
159 $creatednotes3 = external_api
::clean_returnvalue(core_notes_external
::create_notes_returns(), $creatednotes3);
161 // Note 33 does not exist.
162 $gnotes = array($creatednotes1[0]['noteid'], $creatednotes2[0]['noteid'], $creatednotes3[0]['noteid'], 33);
163 $getnotes = core_notes_external
::get_notes($gnotes);
164 $getnotes = external_api
::clean_returnvalue(core_notes_external
::get_notes_returns(), $getnotes);
166 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
167 // Confirm that base note data was retrieved correctly.
168 $this->assertEquals($cnote['userid'], $getnotes["notes"][0]["userid"]);
169 $this->assertEquals($cnote['text'], $getnotes["notes"][0]["text"]);
170 $this->assertEquals($cnote['userid'], $getnotes["notes"][1]["userid"]);
171 $this->assertEquals($cnote['text'], $getnotes["notes"][1]["text"]);
172 $this->assertEquals($cnote['userid'], $getnotes["notes"][2]["userid"]);
173 $this->assertEquals($cnote['text'], $getnotes["notes"][2]["text"]);
174 $this->assertEquals("note", $getnotes["warnings"][0]["item"]);
175 $this->assertEquals(33, $getnotes["warnings"][0]["itemid"]);
176 $this->assertEquals("badid", $getnotes["warnings"][0]["warningcode"]);
177 $this->assertEquals("Note does not exist", $getnotes["warnings"][0]["message"]);
179 // Call without required capability.
180 $this->unassignUserCapability('moodle/notes:view', $contextid, $roleid);
181 $this->expectException('required_capability_exception');
182 $creatednotes = core_notes_external
::get_notes($gnotes);
185 public function test_update_notes() {
189 $this->resetAfterTest(true);
191 $course = self
::getDataGenerator()->create_course();
193 // Set the required capabilities by the external function.
194 $contextid = context_course
::instance($course->id
)->id
;
195 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
196 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
198 // Create test note data.
200 $note1['userid'] = $USER->id
;
201 $note1['publishstate'] = 'personal';
202 $note1['courseid'] = $course->id
;
203 $note1['text'] = 'the text';
204 $note2['userid'] = $USER->id
;
205 $note2['publishstate'] = 'course';
206 $note2['courseid'] = $course->id
;
207 $note2['text'] = 'the text';
208 $note3['userid'] = $USER->id
;
209 $note3['publishstate'] = 'site';
210 $note3['courseid'] = $course->id
;
211 $note3['text'] = 'the text';
212 $notes1 = array($note1, $note2, $note3);
214 $creatednotes = core_notes_external
::create_notes($notes1);
215 $creatednotes = external_api
::clean_returnvalue(core_notes_external
::create_notes_returns(), $creatednotes);
218 $note2["id"] = $creatednotes[0]['noteid'];
219 $note2['publishstate'] = 'personal';
220 $note2['text'] = 'the new text';
221 $note2['format'] = FORMAT_HTML
;
222 $notes2 = array($note2);
224 $updatednotes = core_notes_external
::update_notes($notes2);
226 $updatednotes = external_api
::clean_returnvalue(core_notes_external
::update_notes_returns(), $updatednotes);
227 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
229 // Confirm that base note data was updated correctly.
230 $this->assertEquals($thenote->publishstate
, NOTES_STATE_DRAFT
);
231 $this->assertEquals($note2['text'], $thenote->content
);
233 // Call without required capability.
234 $creatednotes = core_notes_external
::create_notes($notes1);
235 $creatednotes = external_api
::clean_returnvalue(core_notes_external
::create_notes_returns(), $creatednotes);
236 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
237 $this->expectException('required_capability_exception');
239 $note2["id"] = $creatednotes[0]['noteid'];
240 $note2['publishstate'] = 'personal';
241 $note2['text'] = 'the new text';
242 $note2['format'] = FORMAT_HTML
;
243 $notes2 = array($note2);
244 $updatednotes = core_notes_external
::update_notes($notes2);
245 $updatednotes = external_api
::clean_returnvalue(core_notes_external
::update_notes_returns(), $updatednotes);
249 * Test get_course_notes
251 public function test_get_course_notes() {
254 $this->resetAfterTest(true);
255 $CFG->enablenotes
= true;
257 // Take role definitions.
258 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
259 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
261 // Create students and teachers.
262 $student1 = $this->getDataGenerator()->create_user();
263 $student2 = $this->getDataGenerator()->create_user();
264 $teacher1 = $this->getDataGenerator()->create_user();
265 $teacher2 = $this->getDataGenerator()->create_user();
266 $course1 = $this->getDataGenerator()->create_course();
267 $course2 = $this->getDataGenerator()->create_course();
269 // Enroll students and teachers to COURSE-1.
270 $this->getDataGenerator()->enrol_user($student1->id
, $course1->id
, $studentrole->id
);
271 $this->getDataGenerator()->enrol_user($student2->id
, $course1->id
, $studentrole->id
);
272 $this->getDataGenerator()->enrol_user($teacher1->id
, $course1->id
, $teacherrole->id
);
273 $this->getDataGenerator()->enrol_user($teacher2->id
, $course1->id
, $teacherrole->id
);
274 // Enroll students and teachers to COURSE-2 (teacher1 is not enrolled in Course 2).
275 $this->getDataGenerator()->enrol_user($student1->id
, $course2->id
, $studentrole->id
);
276 $this->getDataGenerator()->enrol_user($student2->id
, $course2->id
, $studentrole->id
);
278 $this->getDataGenerator()->enrol_user($teacher2->id
, $course2->id
, $teacherrole->id
);
281 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
283 $this->setUser($teacher1);
285 // NoteA1: on student1 (Course1) by Teacher1.
286 $params = array('courseid' => $course1->id
, 'userid' => $student1->id
, 'publishstate' => NOTES_STATE_PUBLIC
,
287 'usermodified' => $teacher1->id
);
288 $notea1 = $gen->create_instance($params);
289 // NoteA2: on student1 (Course1) by Teacher1.
290 $params = array('courseid' => $course1->id
, 'userid' => $student1->id
, 'publishstate' => NOTES_STATE_PUBLIC
,
291 'usermodified' => $teacher1->id
);
292 $notea2 = $gen->create_instance($params);
293 // NoteS1: on student1 SITE-LEVEL by teacher1.
294 $params = array('courseid' => $course1->id
, 'userid' => $student1->id
, 'publishstate' => NOTES_STATE_SITE
,
295 'usermodified' => $teacher1->id
);
296 $notes1 = $gen->create_instance($params);
297 // NoteP1: on student1 PERSONAL by teacher1.
298 $params = array('courseid' => $course1->id
, 'userid' => $student1->id
, 'publishstate' => NOTES_STATE_DRAFT
,
299 'usermodified' => $teacher1->id
);
300 $notep1 = $gen->create_instance($params);
301 // NoteB1: on student1 (Course2) by teacher1.
302 $params = array('courseid' => $course2->id
, 'userid' => $student1->id
, 'publishstate' => NOTES_STATE_PUBLIC
,
303 'usermodified' => $teacher1->id
);
304 $noteb1 = $gen->create_instance($params);
306 // Retrieve notes, normal case.
307 $result = core_notes_external
::get_course_notes($course1->id
, $student1->id
);
308 $result = external_api
::clean_returnvalue(core_notes_external
::get_course_notes_returns(), $result);
309 $this->assertEquals($notes1->id
, $result['sitenotes'][0]['id']);
310 $this->assertCount(2, $result['coursenotes']);
311 // Teacher can manage only the course notes.
312 $this->assertFalse($result['canmanagesystemnotes']);
313 $this->assertTrue($result['canmanagecoursenotes']);
315 foreach ($result['coursenotes'] as $coursenote) {
316 if ($coursenote['id'] != $notea1->id
and $coursenote['id'] != $notea2->id
) {
317 $this->fail('the returned notes ids does not match with the created ones');
321 $this->assertEquals($notep1->id
, $result['personalnotes'][0]['id']);
323 // Try to get notes from a course the user is not enrolled.
325 $result = core_notes_external
::get_course_notes($course2->id
, $student1->id
);
326 $this->fail('the user is not enrolled in the course');
327 } catch (require_login_exception
$e) {
328 $this->assertEquals('requireloginerror', $e->errorcode
);
331 $result = core_notes_external
::get_course_notes(0, $student1->id
);
332 $result = external_api
::clean_returnvalue(core_notes_external
::get_course_notes_returns(), $result);
333 $this->assertEmpty($result['sitenotes']);
334 // Teacher can't manage system notes.
335 $this->assertFalse($result['canmanagesystemnotes']);
336 $this->assertFalse($result['canmanagecoursenotes']);
338 foreach ($result['coursenotes'] as $coursenote) {
339 if ($coursenote['id'] != $notea1->id
and $coursenote['id'] != $notea2->id
) {
340 $this->fail('the returned notes ids does not match with the created ones');
344 $this->assertCount(2, $result['coursenotes']);
346 $this->setAdminUser();
347 $result = core_notes_external
::get_course_notes(0, $student1->id
);
348 $result = external_api
::clean_returnvalue(core_notes_external
::get_course_notes_returns(), $result);
349 $this->assertEquals($notes1->id
, $result['sitenotes'][0]['id']);
350 $this->assertCount(1, $result['sitenotes']);
351 // Admin user can manage both system and course notes.
352 $this->assertTrue($result['canmanagesystemnotes']);
353 $this->assertTrue($result['canmanagecoursenotes']);
355 $this->setUser($teacher1);
356 $result = core_notes_external
::get_course_notes(0, 0);
357 $result = external_api
::clean_returnvalue(core_notes_external
::get_course_notes_returns(), $result);
358 $this->assertEmpty($result['sitenotes']);
359 $this->assertEmpty($result['coursenotes']);
360 $this->assertEmpty($result['personalnotes']);
361 // Teacher can't manage system notes.
362 $this->assertFalse($result['canmanagesystemnotes']);
363 $this->assertFalse($result['canmanagecoursenotes']);
365 $this->setUser($teacher2);
366 $result = core_notes_external
::get_course_notes($course1->id
, $student1->id
);
367 $result = external_api
::clean_returnvalue(core_notes_external
::get_course_notes_returns(), $result);
368 $this->assertEquals($notes1->id
, $result['sitenotes'][0]['id']);
370 foreach ($result['coursenotes'] as $coursenote) {
371 if ($coursenote['id'] != $notea1->id
and $coursenote['id'] != $notea2->id
) {
372 $this->fail('the returned notes ids does not match with the created ones');
376 $this->assertCount(1, $result['sitenotes']);
377 $this->assertCount(2, $result['coursenotes']);
378 // Teacher can manage only the course notes.
379 $this->assertFalse($result['canmanagesystemnotes']);
380 $this->assertTrue($result['canmanagecoursenotes']);
382 $result = core_notes_external
::get_course_notes($course1->id
, 0);
383 $result = external_api
::clean_returnvalue(core_notes_external
::get_course_notes_returns(), $result);
384 $this->assertEquals($notes1->id
, $result['sitenotes'][0]['id']);
386 foreach ($result['coursenotes'] as $coursenote) {
387 if ($coursenote['id'] != $notea1->id
and $coursenote['id'] != $notea2->id
) {
388 $this->fail('the returned notes ids does not match with the created ones');
392 $this->assertCount(1, $result['sitenotes']);
393 $this->assertCount(2, $result['coursenotes']);
395 $this->setUser($teacher1);
396 $result = core_notes_external
::get_course_notes($course1->id
, 0);
397 $result = external_api
::clean_returnvalue(core_notes_external
::get_course_notes_returns(), $result);
398 $this->assertEquals($notep1->id
, $result['personalnotes'][0]['id']);
399 $this->assertCount(1, $result['personalnotes']);
400 // Teacher can manage only the course notes.
401 $this->assertFalse($result['canmanagesystemnotes']);
402 $this->assertTrue($result['canmanagecoursenotes']);
409 public function test_view_notes() {
412 $this->resetAfterTest(true);
413 $CFG->enablenotes
= true;
415 // Take role definitions.
416 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
417 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
419 // Create students and teachers.
420 $student = $this->getDataGenerator()->create_user();
421 $teacher = $this->getDataGenerator()->create_user();
422 $course = $this->getDataGenerator()->create_course();
423 $coursecontext = context_course
::instance($course->id
);
425 // Enroll students and teachers to course.
426 $this->getDataGenerator()->enrol_user($student->id
, $course->id
, $studentrole->id
);
427 $this->getDataGenerator()->enrol_user($teacher->id
, $course->id
, $teacherrole->id
);
430 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
431 $this->setUser($teacher);
433 // NoteA1: on student (Course) by Teacher.
434 $params = array('courseid' => $course->id
, 'userid' => $student->id
, 'publishstate' => NOTES_STATE_PUBLIC
,
435 'usermodified' => $teacher->id
);
436 $notea1 = $gen->create_instance($params);
438 $sink = $this->redirectEvents();
440 $result = core_notes_external
::view_notes($course->id
, $student->id
);
441 $result = external_api
::clean_returnvalue(core_notes_external
::view_notes_returns(), $result);
443 $result = core_notes_external
::view_notes($course->id
);
444 $result = external_api
::clean_returnvalue(core_notes_external
::view_notes_returns(), $result);
446 $events = $sink->get_events();
448 $this->assertCount(2, $events);
450 $this->assertInstanceOf('\core\event\notes_viewed', $events[0]);
451 $this->assertEquals($coursecontext, $events[0]->get_context());
452 $this->assertEquals($student->id
, $events[0]->relateduserid
);
454 $this->assertInstanceOf('\core\event\notes_viewed', $events[1]);
455 $this->assertEquals($coursecontext, $events[1]->get_context());
456 $this->assertEquals(0, $events[1]->relateduserid
);
459 core_notes_external
::view_notes(0);
460 $this->fail('Exception expected due to invalid permissions at system level.');
461 } catch (moodle_exception
$e) {
462 $this->assertEquals('nopermissions', $e->errorcode
);
466 core_notes_external
::view_notes($course->id
, $student->id +
100);
467 $this->fail('Exception expected due to invalid user id.');
468 } catch (moodle_exception
$e) {
469 $this->assertEquals('invaliduser', $e->errorcode
);