Merge branch 'MDL-76835-401' of https://github.com/aya-saad1/moodle into MOODLE_401_S...
[moodle.git] / notes / tests / externallib_test.php
blob61ca6245a73f298e34cc7f2380b43d7dc9621299
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.
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/>.
17 /**
18 * External notes functions unit tests
20 * @package core_notes
21 * @category external
22 * @copyright 2012 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 namespace core_notes;
28 use core_notes_external;
29 use externallib_advanced_testcase;
31 defined('MOODLE_INTERNAL') || die();
33 global $CFG;
35 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
36 require_once($CFG->dirroot . '/notes/externallib.php');
38 class externallib_test extends externallib_advanced_testcase {
40 /**
41 * Test create_notes
43 public function test_create_notes() {
45 global $DB, $USER;
47 $this->resetAfterTest(true);
49 $course = self::getDataGenerator()->create_course();
51 // Set the required capabilities by the external function.
52 $contextid = \context_course::instance($course->id)->id;
53 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
54 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
56 // Create test note data.
57 $note1 = array();
58 $note1['userid'] = $USER->id;
59 $note1['publishstate'] = 'personal';
60 $note1['courseid'] = $course->id;
61 $note1['text'] = 'the text';
62 $note1['clientnoteid'] = 4;
63 $notes = array($note1);
65 $creatednotes = core_notes_external::create_notes($notes);
66 // We need to execute the return values cleaning process to simulate the web service server.
67 $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
69 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
71 // Confirm that base note data was inserted correctly.
72 $this->assertEquals($thenote->userid, $note1['userid']);
73 $this->assertEquals($thenote->courseid, $note1['courseid']);
74 $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT);
75 $this->assertEquals($thenote->content, $note1['text']);
76 $this->assertEquals($creatednotes[0]['clientnoteid'], $note1['clientnoteid']);
78 // Call without required capability.
79 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
80 $this->expectException('\required_capability_exception');
81 $creatednotes = core_notes_external::create_notes($notes);
84 public function test_delete_notes() {
86 global $DB, $USER;
88 $this->resetAfterTest(true);
90 $course = self::getDataGenerator()->create_course();
92 // Set the required capabilities by the external function.
93 $contextid = \context_course::instance($course->id)->id;
94 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
95 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
97 // Create test note data.
98 $cnote = array();
99 $cnote['userid'] = $USER->id;
100 $cnote['publishstate'] = 'personal';
101 $cnote['courseid'] = $course->id;
102 $cnote['text'] = 'the text';
103 $cnote['clientnoteid'] = 4;
104 $cnotes = array($cnote);
105 $creatednotes = core_notes_external::create_notes($cnotes);
106 $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
108 $dnotes1 = array($creatednotes[0]['noteid']);
109 $deletednotes1 = core_notes_external::delete_notes($dnotes1);
110 $deletednotes1 = \external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes1);
112 // Confirm that base note data was deleted correctly.
113 $notdeletedcount = $DB->count_records_select('post', 'id = ' . $creatednotes[0]['noteid']);
114 $this->assertEquals(0, $notdeletedcount);
116 $dnotes2 = array(33); // This note does not exist.
117 $deletednotes2 = core_notes_external::delete_notes($dnotes2);
118 $deletednotes2 = \external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes2);
120 $this->assertEquals("note", $deletednotes2[0]["item"]);
121 $this->assertEquals(33, $deletednotes2[0]["itemid"]);
122 $this->assertEquals("badid", $deletednotes2[0]["warningcode"]);
123 $this->assertEquals("Note does not exist", $deletednotes2[0]["message"]);
125 // Call without required capability.
126 $creatednotes = core_notes_external::create_notes($cnotes);
127 $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
128 $dnotes3 = array($creatednotes[0]['noteid']);
130 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
131 $this->expectException('\required_capability_exception');
132 $deletednotes = core_notes_external::delete_notes($dnotes3);
133 $deletednotes = \external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes);
136 public function test_get_notes() {
138 global $DB, $USER;
140 $this->resetAfterTest(true);
142 $course = self::getDataGenerator()->create_course();
144 // Set the required capabilities by the external function.
145 $contextid = \context_course::instance($course->id)->id;
146 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
147 $this->assignUserCapability('moodle/notes:view', $contextid, $roleid);
148 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
150 // Create test note data.
151 $cnote = array();
152 $cnote['userid'] = $USER->id;
153 $cnote['publishstate'] = 'personal';
154 $cnote['courseid'] = $course->id;
155 $cnote['text'] = 'the text';
156 $cnotes = array($cnote);
158 $creatednotes1 = core_notes_external::create_notes($cnotes);
159 $creatednotes2 = core_notes_external::create_notes($cnotes);
160 $creatednotes3 = core_notes_external::create_notes($cnotes);
162 $creatednotes1 = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes1);
163 $creatednotes2 = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes2);
164 $creatednotes3 = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes3);
166 // Note 33 does not exist.
167 $gnotes = array($creatednotes1[0]['noteid'], $creatednotes2[0]['noteid'], $creatednotes3[0]['noteid'], 33);
168 $getnotes = core_notes_external::get_notes($gnotes);
169 $getnotes = \external_api::clean_returnvalue(core_notes_external::get_notes_returns(), $getnotes);
171 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
172 // Confirm that base note data was retrieved correctly.
173 $this->assertEquals($cnote['userid'], $getnotes["notes"][0]["userid"]);
174 $this->assertEquals($cnote['text'], $getnotes["notes"][0]["text"]);
175 $this->assertEquals($cnote['userid'], $getnotes["notes"][1]["userid"]);
176 $this->assertEquals($cnote['text'], $getnotes["notes"][1]["text"]);
177 $this->assertEquals($cnote['userid'], $getnotes["notes"][2]["userid"]);
178 $this->assertEquals($cnote['text'], $getnotes["notes"][2]["text"]);
179 $this->assertEquals("note", $getnotes["warnings"][0]["item"]);
180 $this->assertEquals(33, $getnotes["warnings"][0]["itemid"]);
181 $this->assertEquals("badid", $getnotes["warnings"][0]["warningcode"]);
182 $this->assertEquals("Note does not exist", $getnotes["warnings"][0]["message"]);
184 // Call without required capability.
185 $this->unassignUserCapability('moodle/notes:view', $contextid, $roleid);
186 $this->expectException('\required_capability_exception');
187 $creatednotes = core_notes_external::get_notes($gnotes);
190 public function test_update_notes() {
192 global $DB, $USER;
194 $this->resetAfterTest(true);
196 $course = self::getDataGenerator()->create_course();
198 // Set the required capabilities by the external function.
199 $contextid = \context_course::instance($course->id)->id;
200 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid);
201 $this->assignUserCapability('moodle/course:view', $contextid, $roleid);
203 // Create test note data.
204 $note1 = array();
205 $note1['userid'] = $USER->id;
206 $note1['publishstate'] = 'personal';
207 $note1['courseid'] = $course->id;
208 $note1['text'] = 'the text';
209 $note2['userid'] = $USER->id;
210 $note2['publishstate'] = 'course';
211 $note2['courseid'] = $course->id;
212 $note2['text'] = 'the text';
213 $note3['userid'] = $USER->id;
214 $note3['publishstate'] = 'site';
215 $note3['courseid'] = $course->id;
216 $note3['text'] = 'the text';
217 $notes1 = array($note1, $note2, $note3);
219 $creatednotes = core_notes_external::create_notes($notes1);
220 $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
222 $note2 = array();
223 $note2["id"] = $creatednotes[0]['noteid'];
224 $note2['publishstate'] = 'personal';
225 $note2['text'] = 'the new text';
226 $note2['format'] = FORMAT_HTML;
227 $notes2 = array($note2);
229 $updatednotes = core_notes_external::update_notes($notes2);
231 $updatednotes = \external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes);
232 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
234 // Confirm that base note data was updated correctly.
235 $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT);
236 $this->assertEquals($note2['text'], $thenote->content);
238 // Call without required capability.
239 $creatednotes = core_notes_external::create_notes($notes1);
240 $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
241 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid);
242 $this->expectException('\required_capability_exception');
243 $note2 = array();
244 $note2["id"] = $creatednotes[0]['noteid'];
245 $note2['publishstate'] = 'personal';
246 $note2['text'] = 'the new text';
247 $note2['format'] = FORMAT_HTML;
248 $notes2 = array($note2);
249 $updatednotes = core_notes_external::update_notes($notes2);
250 $updatednotes = \external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes);
254 * Test get_course_notes
256 public function test_get_course_notes() {
257 global $DB, $CFG;
259 $this->resetAfterTest(true);
260 $CFG->enablenotes = true;
262 // Take role definitions.
263 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
264 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
266 // Create students and teachers.
267 $student1 = $this->getDataGenerator()->create_user();
268 $student2 = $this->getDataGenerator()->create_user();
269 $teacher1 = $this->getDataGenerator()->create_user();
270 $teacher2 = $this->getDataGenerator()->create_user();
271 $course1 = $this->getDataGenerator()->create_course();
272 $course2 = $this->getDataGenerator()->create_course();
274 // Enroll students and teachers to COURSE-1.
275 $this->getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
276 $this->getDataGenerator()->enrol_user($student2->id, $course1->id, $studentrole->id);
277 $this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, $teacherrole->id);
278 $this->getDataGenerator()->enrol_user($teacher2->id, $course1->id, $teacherrole->id);
279 // Enroll students and teachers to COURSE-2 (teacher1 is not enrolled in Course 2).
280 $this->getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id);
281 $this->getDataGenerator()->enrol_user($student2->id, $course2->id, $studentrole->id);
283 $this->getDataGenerator()->enrol_user($teacher2->id, $course2->id, $teacherrole->id);
285 // Generate notes.
286 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
288 $this->setUser($teacher1);
290 // NoteA1: on student1 (Course1) by Teacher1.
291 $params = array('courseid' => $course1->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_PUBLIC,
292 'usermodified' => $teacher1->id);
293 $notea1 = $gen->create_instance($params);
294 // NoteA2: on student1 (Course1) by Teacher1.
295 $params = array('courseid' => $course1->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_PUBLIC,
296 'usermodified' => $teacher1->id);
297 $notea2 = $gen->create_instance($params);
298 // NoteS1: on student1 SITE-LEVEL by teacher1.
299 $params = array('courseid' => $course1->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_SITE,
300 'usermodified' => $teacher1->id);
301 $notes1 = $gen->create_instance($params);
302 // NoteP1: on student1 PERSONAL by teacher1.
303 $params = array('courseid' => $course1->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_DRAFT,
304 'usermodified' => $teacher1->id);
305 $notep1 = $gen->create_instance($params);
306 // NoteB1: on student1 (Course2) by teacher1.
307 $params = array('courseid' => $course2->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_PUBLIC,
308 'usermodified' => $teacher1->id);
309 $noteb1 = $gen->create_instance($params);
311 // Retrieve notes, normal case.
312 $result = core_notes_external::get_course_notes($course1->id, $student1->id);
313 $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
314 $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']);
315 $this->assertCount(2, $result['coursenotes']);
316 // Teacher can manage only the course notes.
317 $this->assertFalse($result['canmanagesystemnotes']);
318 $this->assertTrue($result['canmanagecoursenotes']);
320 foreach ($result['coursenotes'] as $coursenote) {
321 if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) {
322 $this->fail('the returned notes ids does not match with the created ones');
326 $this->assertEquals($notep1->id, $result['personalnotes'][0]['id']);
328 // Try to get notes from a course the user is not enrolled.
329 try {
330 $result = core_notes_external::get_course_notes($course2->id, $student1->id);
331 $this->fail('the user is not enrolled in the course');
332 } catch (\require_login_exception $e) {
333 $this->assertEquals('requireloginerror', $e->errorcode);
336 $result = core_notes_external::get_course_notes(0, $student1->id);
337 $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
338 $this->assertEmpty($result['sitenotes']);
339 // Teacher can't manage system notes.
340 $this->assertFalse($result['canmanagesystemnotes']);
341 $this->assertFalse($result['canmanagecoursenotes']);
343 foreach ($result['coursenotes'] as $coursenote) {
344 if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) {
345 $this->fail('the returned notes ids does not match with the created ones');
349 $this->assertCount(2, $result['coursenotes']);
351 $this->setAdminUser();
352 $result = core_notes_external::get_course_notes(0, $student1->id);
353 $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
354 $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']);
355 $this->assertCount(1, $result['sitenotes']);
356 // Admin user can manage both system and course notes.
357 $this->assertTrue($result['canmanagesystemnotes']);
358 $this->assertTrue($result['canmanagecoursenotes']);
360 $this->setUser($teacher1);
361 $result = core_notes_external::get_course_notes(0, 0);
362 $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
363 $this->assertEmpty($result['sitenotes']);
364 $this->assertEmpty($result['coursenotes']);
365 $this->assertEmpty($result['personalnotes']);
366 // Teacher can't manage system notes.
367 $this->assertFalse($result['canmanagesystemnotes']);
368 $this->assertFalse($result['canmanagecoursenotes']);
370 $this->setUser($teacher2);
371 $result = core_notes_external::get_course_notes($course1->id, $student1->id);
372 $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
373 $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']);
375 foreach ($result['coursenotes'] as $coursenote) {
376 if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) {
377 $this->fail('the returned notes ids does not match with the created ones');
381 $this->assertCount(1, $result['sitenotes']);
382 $this->assertCount(2, $result['coursenotes']);
383 // Teacher can manage only the course notes.
384 $this->assertFalse($result['canmanagesystemnotes']);
385 $this->assertTrue($result['canmanagecoursenotes']);
387 $result = core_notes_external::get_course_notes($course1->id, 0);
388 $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
389 $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']);
391 foreach ($result['coursenotes'] as $coursenote) {
392 if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) {
393 $this->fail('the returned notes ids does not match with the created ones');
397 $this->assertCount(1, $result['sitenotes']);
398 $this->assertCount(2, $result['coursenotes']);
400 $this->setUser($teacher1);
401 $result = core_notes_external::get_course_notes($course1->id, 0);
402 $result = \external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result);
403 $this->assertEquals($notep1->id, $result['personalnotes'][0]['id']);
404 $this->assertCount(1, $result['personalnotes']);
405 // Teacher can manage only the course notes.
406 $this->assertFalse($result['canmanagesystemnotes']);
407 $this->assertTrue($result['canmanagecoursenotes']);
412 * Test view_notes
414 public function test_view_notes() {
415 global $DB, $CFG;
417 $this->resetAfterTest(true);
418 $CFG->enablenotes = true;
420 // Take role definitions.
421 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
422 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
424 // Create students and teachers.
425 $student = $this->getDataGenerator()->create_user();
426 $teacher = $this->getDataGenerator()->create_user();
427 $course = $this->getDataGenerator()->create_course();
428 $coursecontext = \context_course::instance($course->id);
430 // Enroll students and teachers to course.
431 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
432 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
434 // Generate notes.
435 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
436 $this->setUser($teacher);
438 // NoteA1: on student (Course) by Teacher.
439 $params = array('courseid' => $course->id, 'userid' => $student->id, 'publishstate' => NOTES_STATE_PUBLIC,
440 'usermodified' => $teacher->id);
441 $notea1 = $gen->create_instance($params);
443 $sink = $this->redirectEvents();
445 $result = core_notes_external::view_notes($course->id, $student->id);
446 $result = \external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result);
448 $result = core_notes_external::view_notes($course->id);
449 $result = \external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result);
451 $events = $sink->get_events();
453 $this->assertCount(2, $events);
455 $this->assertInstanceOf('\core\event\notes_viewed', $events[0]);
456 $this->assertEquals($coursecontext, $events[0]->get_context());
457 $this->assertEquals($student->id, $events[0]->relateduserid);
459 $this->assertInstanceOf('\core\event\notes_viewed', $events[1]);
460 $this->assertEquals($coursecontext, $events[1]->get_context());
461 $this->assertEquals(0, $events[1]->relateduserid);
463 try {
464 core_notes_external::view_notes(0);
465 $this->fail('Exception expected due to invalid permissions at system level.');
466 } catch (\moodle_exception $e) {
467 $this->assertEquals('nopermissions', $e->errorcode);
470 try {
471 core_notes_external::view_notes($course->id, $student->id + 100);
472 $this->fail('Exception expected due to invalid user id.');
473 } catch (\moodle_exception $e) {
474 $this->assertEquals('invaliduser', $e->errorcode);