Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]
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 /** 18 * External notes functions unit tests 19 * 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 24 */ 25 26 namespace core_notes; 27 28 use core_external\external_api; 29 use core_notes_external; 30 use externallib_advanced_testcase; 31 32 defined('MOODLE_INTERNAL') || die(); 33 34 global $CFG; 35 36 require_once($CFG->dirroot . '/webservice/tests/helpers.php'); 37 require_once($CFG->dirroot . '/notes/externallib.php'); 38 39 class externallib_test extends externallib_advanced_testcase { 40 41 /** 42 * Test create_notes 43 */ 44 public function test_create_notes() { 45 46 global $DB, $USER; 47 48 $this->resetAfterTest(true); 49 50 $course = self::getDataGenerator()->create_course(); 51 52 // Set the required capabilities by the external function. 53 $contextid = \context_course::instance($course->id)->id; 54 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid); 55 $this->assignUserCapability('moodle/course:view', $contextid, $roleid); 56 57 // Create test note data. 58 $note1 = array(); 59 $note1['userid'] = $USER->id; 60 $note1['publishstate'] = 'personal'; 61 $note1['courseid'] = $course->id; 62 $note1['text'] = 'the text'; 63 $note1['clientnoteid'] = 4; 64 $notes = array($note1); 65 66 $creatednotes = core_notes_external::create_notes($notes); 67 // We need to execute the return values cleaning process to simulate the web service server. 68 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); 69 70 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid'])); 71 72 // Confirm that base note data was inserted correctly. 73 $this->assertEquals($thenote->userid, $note1['userid']); 74 $this->assertEquals($thenote->courseid, $note1['courseid']); 75 $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT); 76 $this->assertEquals($thenote->content, $note1['text']); 77 $this->assertEquals($creatednotes[0]['clientnoteid'], $note1['clientnoteid']); 78 79 // Call without required capability. 80 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); 81 $this->expectException('\required_capability_exception'); 82 $creatednotes = core_notes_external::create_notes($notes); 83 } 84 85 public function test_delete_notes() { 86 87 global $DB, $USER; 88 89 $this->resetAfterTest(true); 90 91 $course = self::getDataGenerator()->create_course(); 92 93 // Set the required capabilities by the external function. 94 $contextid = \context_course::instance($course->id)->id; 95 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid); 96 $this->assignUserCapability('moodle/course:view', $contextid, $roleid); 97 98 // Create test note data. 99 $cnote = array(); 100 $cnote['userid'] = $USER->id; 101 $cnote['publishstate'] = 'personal'; 102 $cnote['courseid'] = $course->id; 103 $cnote['text'] = 'the text'; 104 $cnote['clientnoteid'] = 4; 105 $cnotes = array($cnote); 106 $creatednotes = core_notes_external::create_notes($cnotes); 107 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); 108 109 $dnotes1 = array($creatednotes[0]['noteid']); 110 $deletednotes1 = core_notes_external::delete_notes($dnotes1); 111 $deletednotes1 = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes1); 112 113 // Confirm that base note data was deleted correctly. 114 $notdeletedcount = $DB->count_records_select('post', 'id = ' . $creatednotes[0]['noteid']); 115 $this->assertEquals(0, $notdeletedcount); 116 117 $dnotes2 = array(33); // This note does not exist. 118 $deletednotes2 = core_notes_external::delete_notes($dnotes2); 119 $deletednotes2 = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes2); 120 121 $this->assertEquals("note", $deletednotes2[0]["item"]); 122 $this->assertEquals(33, $deletednotes2[0]["itemid"]); 123 $this->assertEquals("badid", $deletednotes2[0]["warningcode"]); 124 $this->assertEquals("Note does not exist", $deletednotes2[0]["message"]); 125 126 // Call without required capability. 127 $creatednotes = core_notes_external::create_notes($cnotes); 128 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); 129 $dnotes3 = array($creatednotes[0]['noteid']); 130 131 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); 132 $this->expectException('\required_capability_exception'); 133 $deletednotes = core_notes_external::delete_notes($dnotes3); 134 $deletednotes = external_api::clean_returnvalue(core_notes_external::delete_notes_returns(), $deletednotes); 135 } 136 137 public function test_get_notes() { 138 139 global $DB, $USER; 140 141 $this->resetAfterTest(true); 142 143 $course = self::getDataGenerator()->create_course(); 144 145 // Set the required capabilities by the external function. 146 $contextid = \context_course::instance($course->id)->id; 147 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid); 148 $this->assignUserCapability('moodle/notes:view', $contextid, $roleid); 149 $this->assignUserCapability('moodle/course:view', $contextid, $roleid); 150 151 // Create test note data. 152 $cnote = array(); 153 $cnote['userid'] = $USER->id; 154 $cnote['publishstate'] = 'personal'; 155 $cnote['courseid'] = $course->id; 156 $cnote['text'] = 'the text'; 157 $cnotes = array($cnote); 158 159 $creatednotes1 = core_notes_external::create_notes($cnotes); 160 $creatednotes2 = core_notes_external::create_notes($cnotes); 161 $creatednotes3 = core_notes_external::create_notes($cnotes); 162 163 $creatednotes1 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes1); 164 $creatednotes2 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes2); 165 $creatednotes3 = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes3); 166 167 // Note 33 does not exist. 168 $gnotes = array($creatednotes1[0]['noteid'], $creatednotes2[0]['noteid'], $creatednotes3[0]['noteid'], 33); 169 $getnotes = core_notes_external::get_notes($gnotes); 170 $getnotes = external_api::clean_returnvalue(core_notes_external::get_notes_returns(), $getnotes); 171 172 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); 173 // Confirm that base note data was retrieved correctly. 174 $this->assertEquals($cnote['userid'], $getnotes["notes"][0]["userid"]); 175 $this->assertEquals($cnote['text'], $getnotes["notes"][0]["text"]); 176 $this->assertEquals($cnote['userid'], $getnotes["notes"][1]["userid"]); 177 $this->assertEquals($cnote['text'], $getnotes["notes"][1]["text"]); 178 $this->assertEquals($cnote['userid'], $getnotes["notes"][2]["userid"]); 179 $this->assertEquals($cnote['text'], $getnotes["notes"][2]["text"]); 180 $this->assertEquals("note", $getnotes["warnings"][0]["item"]); 181 $this->assertEquals(33, $getnotes["warnings"][0]["itemid"]); 182 $this->assertEquals("badid", $getnotes["warnings"][0]["warningcode"]); 183 $this->assertEquals("Note does not exist", $getnotes["warnings"][0]["message"]); 184 185 // Call without required capability. 186 $this->unassignUserCapability('moodle/notes:view', $contextid, $roleid); 187 $this->expectException('\required_capability_exception'); 188 $creatednotes = core_notes_external::get_notes($gnotes); 189 } 190 191 public function test_update_notes() { 192 193 global $DB, $USER; 194 195 $this->resetAfterTest(true); 196 197 $course = self::getDataGenerator()->create_course(); 198 199 // Set the required capabilities by the external function. 200 $contextid = \context_course::instance($course->id)->id; 201 $roleid = $this->assignUserCapability('moodle/notes:manage', $contextid); 202 $this->assignUserCapability('moodle/course:view', $contextid, $roleid); 203 204 // Create test note data. 205 $note1 = array(); 206 $note1['userid'] = $USER->id; 207 $note1['publishstate'] = 'personal'; 208 $note1['courseid'] = $course->id; 209 $note1['text'] = 'the text'; 210 $note2['userid'] = $USER->id; 211 $note2['publishstate'] = 'course'; 212 $note2['courseid'] = $course->id; 213 $note2['text'] = 'the text'; 214 $note3['userid'] = $USER->id; 215 $note3['publishstate'] = 'site'; 216 $note3['courseid'] = $course->id; 217 $note3['text'] = 'the text'; 218 $notes1 = array($note1, $note2, $note3); 219 220 $creatednotes = core_notes_external::create_notes($notes1); 221 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); 222 223 $note2 = array(); 224 $note2["id"] = $creatednotes[0]['noteid']; 225 $note2['publishstate'] = 'personal'; 226 $note2['text'] = 'the new text'; 227 $note2['format'] = FORMAT_HTML; 228 $notes2 = array($note2); 229 230 $updatednotes = core_notes_external::update_notes($notes2); 231 232 $updatednotes = external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes); 233 $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid'])); 234 235 // Confirm that base note data was updated correctly. 236 $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT); 237 $this->assertEquals($note2['text'], $thenote->content); 238 239 // Call without required capability. 240 $creatednotes = core_notes_external::create_notes($notes1); 241 $creatednotes = external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes); 242 $this->unassignUserCapability('moodle/notes:manage', $contextid, $roleid); 243 $this->expectException('\required_capability_exception'); 244 $note2 = array(); 245 $note2["id"] = $creatednotes[0]['noteid']; 246 $note2['publishstate'] = 'personal'; 247 $note2['text'] = 'the new text'; 248 $note2['format'] = FORMAT_HTML; 249 $notes2 = array($note2); 250 $updatednotes = core_notes_external::update_notes($notes2); 251 $updatednotes = external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes); 252 } 253 254 /** 255 * Test get_course_notes 256 */ 257 public function test_get_course_notes() { 258 global $DB, $CFG; 259 260 $this->resetAfterTest(true); 261 $CFG->enablenotes = true; 262 263 // Take role definitions. 264 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 265 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); 266 267 // Create students and teachers. 268 $student1 = $this->getDataGenerator()->create_user(); 269 $student2 = $this->getDataGenerator()->create_user(); 270 $teacher1 = $this->getDataGenerator()->create_user(); 271 $teacher2 = $this->getDataGenerator()->create_user(); 272 $course1 = $this->getDataGenerator()->create_course(); 273 $course2 = $this->getDataGenerator()->create_course(); 274 275 // Enroll students and teachers to COURSE-1. 276 $this->getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id); 277 $this->getDataGenerator()->enrol_user($student2->id, $course1->id, $studentrole->id); 278 $this->getDataGenerator()->enrol_user($teacher1->id, $course1->id, $teacherrole->id); 279 $this->getDataGenerator()->enrol_user($teacher2->id, $course1->id, $teacherrole->id); 280 // Enroll students and teachers to COURSE-2 (teacher1 is not enrolled in Course 2). 281 $this->getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id); 282 $this->getDataGenerator()->enrol_user($student2->id, $course2->id, $studentrole->id); 283 284 $this->getDataGenerator()->enrol_user($teacher2->id, $course2->id, $teacherrole->id); 285 286 // Generate notes. 287 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes'); 288 289 $this->setUser($teacher1); 290 291 // NoteA1: on student1 (Course1) by Teacher1. 292 $params = array('courseid' => $course1->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_PUBLIC, 293 'usermodified' => $teacher1->id); 294 $notea1 = $gen->create_instance($params); 295 // NoteA2: on student1 (Course1) by Teacher1. 296 $params = array('courseid' => $course1->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_PUBLIC, 297 'usermodified' => $teacher1->id); 298 $notea2 = $gen->create_instance($params); 299 // NoteS1: on student1 SITE-LEVEL by teacher1. 300 $params = array('courseid' => $course1->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_SITE, 301 'usermodified' => $teacher1->id); 302 $notes1 = $gen->create_instance($params); 303 // NoteP1: on student1 PERSONAL by teacher1. 304 $params = array('courseid' => $course1->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_DRAFT, 305 'usermodified' => $teacher1->id); 306 $notep1 = $gen->create_instance($params); 307 // NoteB1: on student1 (Course2) by teacher1. 308 $params = array('courseid' => $course2->id, 'userid' => $student1->id, 'publishstate' => NOTES_STATE_PUBLIC, 309 'usermodified' => $teacher1->id); 310 $noteb1 = $gen->create_instance($params); 311 312 // Retrieve notes, normal case. 313 $result = core_notes_external::get_course_notes($course1->id, $student1->id); 314 $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); 315 $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']); 316 $this->assertCount(2, $result['coursenotes']); 317 // Teacher can manage only the course notes. 318 $this->assertFalse($result['canmanagesystemnotes']); 319 $this->assertTrue($result['canmanagecoursenotes']); 320 321 foreach ($result['coursenotes'] as $coursenote) { 322 if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) { 323 $this->fail('the returned notes ids does not match with the created ones'); 324 } 325 } 326 327 $this->assertEquals($notep1->id, $result['personalnotes'][0]['id']); 328 329 // Try to get notes from a course the user is not enrolled. 330 try { 331 $result = core_notes_external::get_course_notes($course2->id, $student1->id); 332 $this->fail('the user is not enrolled in the course'); 333 } catch (\require_login_exception $e) { 334 $this->assertEquals('requireloginerror', $e->errorcode); 335 } 336 337 $result = core_notes_external::get_course_notes(0, $student1->id); 338 $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); 339 $this->assertEmpty($result['sitenotes']); 340 // Teacher can't manage system notes. 341 $this->assertFalse($result['canmanagesystemnotes']); 342 $this->assertFalse($result['canmanagecoursenotes']); 343 344 foreach ($result['coursenotes'] as $coursenote) { 345 if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) { 346 $this->fail('the returned notes ids does not match with the created ones'); 347 } 348 } 349 350 $this->assertCount(2, $result['coursenotes']); 351 352 $this->setAdminUser(); 353 $result = core_notes_external::get_course_notes(0, $student1->id); 354 $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); 355 $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']); 356 $this->assertCount(1, $result['sitenotes']); 357 // Admin user can manage both system and course notes. 358 $this->assertTrue($result['canmanagesystemnotes']); 359 $this->assertTrue($result['canmanagecoursenotes']); 360 361 $this->setUser($teacher1); 362 $result = core_notes_external::get_course_notes(0, 0); 363 $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); 364 $this->assertEmpty($result['sitenotes']); 365 $this->assertEmpty($result['coursenotes']); 366 $this->assertEmpty($result['personalnotes']); 367 // Teacher can't manage system notes. 368 $this->assertFalse($result['canmanagesystemnotes']); 369 $this->assertFalse($result['canmanagecoursenotes']); 370 371 $this->setUser($teacher2); 372 $result = core_notes_external::get_course_notes($course1->id, $student1->id); 373 $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); 374 $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']); 375 376 foreach ($result['coursenotes'] as $coursenote) { 377 if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) { 378 $this->fail('the returned notes ids does not match with the created ones'); 379 } 380 } 381 382 $this->assertCount(1, $result['sitenotes']); 383 $this->assertCount(2, $result['coursenotes']); 384 // Teacher can manage only the course notes. 385 $this->assertFalse($result['canmanagesystemnotes']); 386 $this->assertTrue($result['canmanagecoursenotes']); 387 388 $result = core_notes_external::get_course_notes($course1->id, 0); 389 $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); 390 $this->assertEquals($notes1->id, $result['sitenotes'][0]['id']); 391 392 foreach ($result['coursenotes'] as $coursenote) { 393 if ($coursenote['id'] != $notea1->id and $coursenote['id'] != $notea2->id) { 394 $this->fail('the returned notes ids does not match with the created ones'); 395 } 396 } 397 398 $this->assertCount(1, $result['sitenotes']); 399 $this->assertCount(2, $result['coursenotes']); 400 401 $this->setUser($teacher1); 402 $result = core_notes_external::get_course_notes($course1->id, 0); 403 $result = external_api::clean_returnvalue(core_notes_external::get_course_notes_returns(), $result); 404 $this->assertEquals($notep1->id, $result['personalnotes'][0]['id']); 405 $this->assertCount(1, $result['personalnotes']); 406 // Teacher can manage only the course notes. 407 $this->assertFalse($result['canmanagesystemnotes']); 408 $this->assertTrue($result['canmanagecoursenotes']); 409 410 } 411 412 /** 413 * Test view_notes 414 */ 415 public function test_view_notes() { 416 global $DB, $CFG; 417 418 $this->resetAfterTest(true); 419 $CFG->enablenotes = true; 420 421 // Take role definitions. 422 $studentrole = $DB->get_record('role', array('shortname' => 'student')); 423 $teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); 424 425 // Create students and teachers. 426 $student = $this->getDataGenerator()->create_user(); 427 $teacher = $this->getDataGenerator()->create_user(); 428 $course = $this->getDataGenerator()->create_course(); 429 $coursecontext = \context_course::instance($course->id); 430 431 // Enroll students and teachers to course. 432 $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id); 433 $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id); 434 435 // Generate notes. 436 $gen = $this->getDataGenerator()->get_plugin_generator('core_notes'); 437 $this->setUser($teacher); 438 439 // NoteA1: on student (Course) by Teacher. 440 $params = array('courseid' => $course->id, 'userid' => $student->id, 'publishstate' => NOTES_STATE_PUBLIC, 441 'usermodified' => $teacher->id); 442 $notea1 = $gen->create_instance($params); 443 444 $sink = $this->redirectEvents(); 445 446 $result = core_notes_external::view_notes($course->id, $student->id); 447 $result = external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result); 448 449 $result = core_notes_external::view_notes($course->id); 450 $result = external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result); 451 452 $events = $sink->get_events(); 453 454 $this->assertCount(2, $events); 455 456 $this->assertInstanceOf('\core\event\notes_viewed', $events[0]); 457 $this->assertEquals($coursecontext, $events[0]->get_context()); 458 $this->assertEquals($student->id, $events[0]->relateduserid); 459 460 $this->assertInstanceOf('\core\event\notes_viewed', $events[1]); 461 $this->assertEquals($coursecontext, $events[1]->get_context()); 462 $this->assertEquals(0, $events[1]->relateduserid); 463 464 try { 465 core_notes_external::view_notes(0); 466 $this->fail('Exception expected due to invalid permissions at system level.'); 467 } catch (\moodle_exception $e) { 468 $this->assertEquals('nopermissions', $e->errorcode); 469 } 470 471 try { 472 core_notes_external::view_notes($course->id, $student->id + 100); 473 $this->fail('Exception expected due to invalid user id.'); 474 } catch (\moodle_exception $e) { 475 $this->assertEquals('invaliduser', $e->errorcode); 476 } 477 } 478 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body