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 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   * 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          $arr = array($this->eventnote->courseid, 'notes', 'delete', $logurl, 'delete note');
  85          $this->assertEventLegacyLogData($arr, $event);
  86          $this->assertEventContextNotUsed($event);
  87      }
  88  
  89      /**
  90       * Tests for event note_created.
  91       */
  92      public function test_note_created_event() {
  93  
  94          // Delete a note.
  95          $sink = $this->redirectEvents();
  96          $note = clone $this->eventnote;
  97          unset($note->id);
  98          note_save($note);
  99          $events = $sink->get_events();
 100          $event = array_pop($events); // Delete note event.
 101          $sink->close();
 102  
 103          // Validate event data.
 104          $this->assertInstanceOf('\core\event\note_created', $event);
 105          $this->assertEquals($note->id, $event->objectid);
 106          $this->assertEquals($note->usermodified, $event->userid);
 107          $this->assertEquals($note->userid, $event->relateduserid);
 108          $this->assertEquals('post', $event->objecttable);
 109          $this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']);
 110  
 111          // Test legacy data.
 112          $logurl = new \moodle_url('index.php',
 113              array('course' => $note->courseid, 'user' => $note->userid));
 114          $logurl->set_anchor('note-' . $note->id);
 115          $arr = array($note->courseid, 'notes', 'add', $logurl, 'add note');
 116          $this->assertEventLegacyLogData($arr, $event);
 117          $this->assertEventContextNotUsed($event);
 118      }
 119  
 120      /**
 121       * Tests for event note_updated.
 122       */
 123      public function test_note_updated_event() {
 124  
 125          // Delete a note.
 126          $sink = $this->redirectEvents();
 127          $note = clone $this->eventnote;
 128          $note->publishstate = NOTES_STATE_DRAFT;
 129          note_save($note);
 130          $events = $sink->get_events();
 131          $event = array_pop($events); // Delete note event.
 132          $sink->close();
 133  
 134          // Validate event data.
 135          $this->assertInstanceOf('\core\event\note_updated', $event);
 136          $this->assertEquals($note->id, $event->objectid);
 137          $this->assertEquals($note->usermodified, $event->userid);
 138          $this->assertEquals($note->userid, $event->relateduserid);
 139          $this->assertEquals('post', $event->objecttable);
 140          $this->assertEquals(NOTES_STATE_DRAFT, $event->other['publishstate']);
 141  
 142          // Test legacy data.
 143          $logurl = new \moodle_url('index.php',
 144              array('course' => $note->courseid, 'user' => $note->userid));
 145          $logurl->set_anchor('note-' . $note->id);
 146          $arr = array($note->courseid, 'notes', 'update', $logurl, 'update note');
 147          $this->assertEventLegacyLogData($arr, $event);
 148          $this->assertEventContextNotUsed($event);
 149      }
 150  
 151      /**
 152       * Test the notes viewed event.
 153       *
 154       * It's not possible to use the moodle API to simulate the viewing of notes, so here we
 155       * simply create the event and trigger it.
 156       */
 157      public function test_notes_viewed() {
 158          $coursecontext = \context_course::instance($this->eventnote->courseid);
 159          // Trigger event for notes viewed.
 160          $event = \core\event\notes_viewed::create(array(
 161              'context' => $coursecontext,
 162              'relateduserid' => $this->eventnote->userid
 163          ));
 164  
 165          // Trigger and capture the event.
 166          $sink = $this->redirectEvents();
 167          $event->trigger();
 168          $events = $sink->get_events();
 169          $event = reset($events);
 170  
 171          $this->assertInstanceOf('\core\event\notes_viewed', $event);
 172          $this->assertEquals($coursecontext, $event->get_context());
 173          $expected = array($this->eventnote->courseid, 'notes', 'view', 'index.php?course=' .
 174                  $this->eventnote->courseid.'&amp;user=' . $this->eventnote->userid, 'view notes');
 175          $this->assertEventLegacyLogData($expected, $event);
 176          $this->assertEventContextNotUsed($event);
 177      }
 178  }