Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [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   * Tests for notes events.
  19   *
  20   * @package    core_notes
  21   * @copyright  2013 Ankit Agarwal
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  23   */
  24  
  25  namespace core_notes\event;
  26  
  27  /**
  28   * Class core_notes_events_testcase
  29   *
  30   * Class for tests related to notes events.
  31   *
  32   * @package    core_notes
  33   * @copyright  2013 Ankit Agarwal
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
  35   */
  36  class events_test extends \advanced_testcase {
  37  
  38      /** @var  stdClass A note object. */
  39      private $eventnote;
  40  
  41      /** @var stdClass A complete record from post table */
  42      private $noterecord;
  43  
  44      public function setUp(): void {
  45          global $DB;
  46  
  47          $this->resetAfterTest();
  48          $this->setAdminUser();
  49  
  50          $course = $this->getDataGenerator()->create_course();
  51          $user = $this->getDataGenerator()->create_user();
  52          $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
  53          $this->eventnote = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id));
  54          // Get the full record, note_load doesn't return everything.
  55          $this->noterecord = $DB->get_record('post', array('id' => $this->eventnote->id), '*', MUST_EXIST);
  56  
  57      }
  58  
  59      /**
  60       * Tests for event note_deleted.
  61       */
  62      public function test_note_deleted_event() {
  63          // Delete a note.
  64          $sink = $this->redirectEvents();
  65          note_delete($this->eventnote);
  66          $events = $sink->get_events();
  67          $event = array_pop($events); // Delete note event.
  68          $sink->close();
  69  
  70          // Validate event data.
  71          $this->assertInstanceOf('\core\event\note_deleted', $event);
  72          $this->assertEquals($this->eventnote->id, $event->objectid);
  73          $this->assertEquals($this->eventnote->usermodified, $event->userid);
  74          $this->assertEquals($this->eventnote->userid, $event->relateduserid);
  75          $this->assertEquals('post', $event->objecttable);
  76          $this->assertEquals(null, $event->get_url());
  77          $this->assertEquals($this->noterecord, $event->get_record_snapshot('post', $event->objectid));
  78          $this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']);
  79  
  80          // Test legacy data.
  81          $logurl = new \moodle_url('index.php',
  82                  array('course' => $this->eventnote->courseid, 'user' => $this->eventnote->userid));
  83          $logurl->set_anchor('note-' . $this->eventnote->id);
  84          $this->assertEventContextNotUsed($event);
  85      }
  86  
  87      /**
  88       * Tests for event note_created.
  89       */
  90      public function test_note_created_event() {
  91  
  92          // Delete a note.
  93          $sink = $this->redirectEvents();
  94          $note = clone $this->eventnote;
  95          unset($note->id);
  96          note_save($note);
  97          $events = $sink->get_events();
  98          $event = array_pop($events); // Delete note event.
  99          $sink->close();
 100  
 101          // Validate event data.
 102          $this->assertInstanceOf('\core\event\note_created', $event);
 103          $this->assertEquals($note->id, $event->objectid);
 104          $this->assertEquals($note->usermodified, $event->userid);
 105          $this->assertEquals($note->userid, $event->relateduserid);
 106          $this->assertEquals('post', $event->objecttable);
 107          $this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']);
 108          $this->assertEventContextNotUsed($event);
 109      }
 110  
 111      /**
 112       * Tests for event note_updated.
 113       */
 114      public function test_note_updated_event() {
 115  
 116          // Delete a note.
 117          $sink = $this->redirectEvents();
 118          $note = clone $this->eventnote;
 119          $note->publishstate = NOTES_STATE_DRAFT;
 120          note_save($note);
 121          $events = $sink->get_events();
 122          $event = array_pop($events); // Delete note event.
 123          $sink->close();
 124  
 125          // Validate event data.
 126          $this->assertInstanceOf('\core\event\note_updated', $event);
 127          $this->assertEquals($note->id, $event->objectid);
 128          $this->assertEquals($note->usermodified, $event->userid);
 129          $this->assertEquals($note->userid, $event->relateduserid);
 130          $this->assertEquals('post', $event->objecttable);
 131          $this->assertEquals(NOTES_STATE_DRAFT, $event->other['publishstate']);
 132          $this->assertEventContextNotUsed($event);
 133      }
 134  
 135      /**
 136       * Test the notes viewed event.
 137       *
 138       * It's not possible to use the moodle API to simulate the viewing of notes, so here we
 139       * simply create the event and trigger it.
 140       */
 141      public function test_notes_viewed() {
 142          $coursecontext = \context_course::instance($this->eventnote->courseid);
 143          // Trigger event for notes viewed.
 144          $event = \core\event\notes_viewed::create(array(
 145              'context' => $coursecontext,
 146              'relateduserid' => $this->eventnote->userid
 147          ));
 148  
 149          // Trigger and capture the event.
 150          $sink = $this->redirectEvents();
 151          $event->trigger();
 152          $events = $sink->get_events();
 153          $event = reset($events);
 154  
 155          $this->assertInstanceOf('\core\event\notes_viewed', $event);
 156          $this->assertEquals($coursecontext, $event->get_context());
 157          $this->assertEventContextNotUsed($event);
 158      }
 159  }