Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 and 403]

   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_notes_external;
  29  use externallib_advanced_testcase;
  30  
  31  defined('MOODLE_INTERNAL') || die();
  32  
  33  global $CFG;
  34  
  35  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  36  require_once($CFG->dirroot . '/notes/externallib.php');
  37  
  38  class externallib_test extends externallib_advanced_testcase {
  39  
  40      /**
  41       * Test create_notes
  42       */
  43      public function test_create_notes() {
  44  
  45          global $DB, $USER;
  46  
  47          $this->resetAfterTest(true);
  48  
  49          $course = self::getDataGenerator()->create_course();
  50  
  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);
  55  
  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);
  64  
  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);
  68  
  69          $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
  70  
  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']);
  77  
  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);
  82      }
  83  
  84      public function test_delete_notes() {
  85  
  86          global $DB, $USER;
  87  
  88          $this->resetAfterTest(true);
  89  
  90          $course = self::getDataGenerator()->create_course();
  91  
  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);
  96  
  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);
 107  
 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);
 111  
 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);
 115  
 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);
 119  
 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"]);
 124  
 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']);
 129  
 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);
 134      }
 135  
 136      public function test_get_notes() {
 137  
 138          global $DB, $USER;
 139  
 140          $this->resetAfterTest(true);
 141  
 142          $course = self::getDataGenerator()->create_course();
 143  
 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);
 149  
 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);
 157  
 158          $creatednotes1 = core_notes_external::create_notes($cnotes);
 159          $creatednotes2 = core_notes_external::create_notes($cnotes);
 160          $creatednotes3 = core_notes_external::create_notes($cnotes);
 161  
 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);
 165  
 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);
 170  
 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"]);
 183  
 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);
 188      }
 189  
 190      public function test_update_notes() {
 191  
 192          global $DB, $USER;
 193  
 194          $this->resetAfterTest(true);
 195  
 196          $course = self::getDataGenerator()->create_course();
 197  
 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);
 202  
 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);
 218  
 219          $creatednotes = core_notes_external::create_notes($notes1);
 220          $creatednotes = \external_api::clean_returnvalue(core_notes_external::create_notes_returns(), $creatednotes);
 221  
 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);
 228  
 229          $updatednotes = core_notes_external::update_notes($notes2);
 230  
 231          $updatednotes = \external_api::clean_returnvalue(core_notes_external::update_notes_returns(), $updatednotes);
 232          $thenote = $DB->get_record('post', array('id' => $creatednotes[0]['noteid']));
 233  
 234          // Confirm that base note data was updated correctly.
 235          $this->assertEquals($thenote->publishstate, NOTES_STATE_DRAFT);
 236          $this->assertEquals($note2['text'], $thenote->content);
 237  
 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);
 251      }
 252  
 253      /**
 254       * Test get_course_notes
 255       */
 256      public function test_get_course_notes() {
 257          global $DB, $CFG;
 258  
 259          $this->resetAfterTest(true);
 260          $CFG->enablenotes = true;
 261  
 262          // Take role definitions.
 263          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 264          $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
 265  
 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();
 273  
 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);
 282  
 283          $this->getDataGenerator()->enrol_user($teacher2->id, $course2->id, $teacherrole->id);
 284  
 285          // Generate notes.
 286          $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
 287  
 288          $this->setUser($teacher1);
 289  
 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);
 310  
 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']);
 319  
 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');
 323              }
 324          }
 325  
 326          $this->assertEquals($notep1->id, $result['personalnotes'][0]['id']);
 327  
 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);
 334          }
 335  
 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']);
 342  
 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');
 346              }
 347          }
 348  
 349          $this->assertCount(2, $result['coursenotes']);
 350  
 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']);
 359  
 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']);
 369  
 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']);
 374  
 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');
 378              }
 379          }
 380  
 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']);
 386  
 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']);
 390  
 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');
 394              }
 395          }
 396  
 397          $this->assertCount(1, $result['sitenotes']);
 398          $this->assertCount(2, $result['coursenotes']);
 399  
 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']);
 408  
 409      }
 410  
 411      /**
 412       * Test view_notes
 413       */
 414      public function test_view_notes() {
 415          global $DB, $CFG;
 416  
 417          $this->resetAfterTest(true);
 418          $CFG->enablenotes = true;
 419  
 420          // Take role definitions.
 421          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 422          $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
 423  
 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);
 429  
 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);
 433  
 434          // Generate notes.
 435          $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
 436          $this->setUser($teacher);
 437  
 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);
 442  
 443          $sink = $this->redirectEvents();
 444  
 445          $result = core_notes_external::view_notes($course->id, $student->id);
 446          $result = \external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result);
 447  
 448          $result = core_notes_external::view_notes($course->id);
 449          $result = \external_api::clean_returnvalue(core_notes_external::view_notes_returns(), $result);
 450  
 451          $events = $sink->get_events();
 452  
 453          $this->assertCount(2, $events);
 454  
 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);
 458  
 459          $this->assertInstanceOf('\core\event\notes_viewed', $events[1]);
 460          $this->assertEquals($coursecontext, $events[1]->get_context());
 461          $this->assertEquals(0, $events[1]->relateduserid);
 462  
 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);
 468          }
 469  
 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);
 475          }
 476      }
 477  }