| 1 | 
           efrain | 
           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 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           36 | 
           final class events_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           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;
  | 
        
        
           | 1441 | 
           ariadna | 
           46 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           49 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           52 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           53 | 
                   $gen = $this->getDataGenerator()->get_plugin_generator('core_notes');
  | 
        
        
            | 
            | 
           54 | 
                   $this->eventnote = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id));
  | 
        
        
            | 
            | 
           55 | 
                   // Get the full record, note_load doesn't return everything.
  | 
        
        
            | 
            | 
           56 | 
                   $this->noterecord = $DB->get_record('post', array('id' => $this->eventnote->id), '*', MUST_EXIST);
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
               }
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
            | 
            | 
           60 | 
               /**
  | 
        
        
            | 
            | 
           61 | 
                * Tests for event note_deleted.
  | 
        
        
            | 
            | 
           62 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           63 | 
               public function test_note_deleted_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           64 | 
                   // Delete a note.
  | 
        
        
            | 
            | 
           65 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           66 | 
                   note_delete($this->eventnote);
  | 
        
        
            | 
            | 
           67 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           68 | 
                   $event = array_pop($events); // Delete note event.
  | 
        
        
            | 
            | 
           69 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                   // Validate event data.
  | 
        
        
            | 
            | 
           72 | 
                   $this->assertInstanceOf('\core\event\note_deleted', $event);
  | 
        
        
            | 
            | 
           73 | 
                   $this->assertEquals($this->eventnote->id, $event->objectid);
  | 
        
        
            | 
            | 
           74 | 
                   $this->assertEquals($this->eventnote->usermodified, $event->userid);
  | 
        
        
            | 
            | 
           75 | 
                   $this->assertEquals($this->eventnote->userid, $event->relateduserid);
  | 
        
        
            | 
            | 
           76 | 
                   $this->assertEquals('post', $event->objecttable);
  | 
        
        
            | 
            | 
           77 | 
                   $this->assertEquals(null, $event->get_url());
  | 
        
        
            | 
            | 
           78 | 
                   $this->assertEquals($this->noterecord, $event->get_record_snapshot('post', $event->objectid));
  | 
        
        
            | 
            | 
           79 | 
                   $this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']);
  | 
        
        
            | 
            | 
           80 | 
              | 
        
        
            | 
            | 
           81 | 
                   // Test legacy data.
  | 
        
        
            | 
            | 
           82 | 
                   $logurl = new \moodle_url('index.php',
  | 
        
        
            | 
            | 
           83 | 
                           array('course' => $this->eventnote->courseid, 'user' => $this->eventnote->userid));
  | 
        
        
            | 
            | 
           84 | 
                   $logurl->set_anchor('note-' . $this->eventnote->id);
  | 
        
        
            | 
            | 
           85 | 
                   $this->assertEventContextNotUsed($event);
  | 
        
        
            | 
            | 
           86 | 
               }
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
               /**
  | 
        
        
            | 
            | 
           89 | 
                * Tests for event note_created.
  | 
        
        
            | 
            | 
           90 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           91 | 
               public function test_note_created_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                   // Delete a note.
  | 
        
        
            | 
            | 
           94 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           95 | 
                   $note = clone $this->eventnote;
  | 
        
        
            | 
            | 
           96 | 
                   unset($note->id);
  | 
        
        
            | 
            | 
           97 | 
                   note_save($note);
  | 
        
        
            | 
            | 
           98 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           99 | 
                   $event = array_pop($events); // Delete note event.
  | 
        
        
            | 
            | 
           100 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                   // Validate event data.
  | 
        
        
            | 
            | 
           103 | 
                   $this->assertInstanceOf('\core\event\note_created', $event);
  | 
        
        
            | 
            | 
           104 | 
                   $this->assertEquals($note->id, $event->objectid);
  | 
        
        
            | 
            | 
           105 | 
                   $this->assertEquals($note->usermodified, $event->userid);
  | 
        
        
            | 
            | 
           106 | 
                   $this->assertEquals($note->userid, $event->relateduserid);
  | 
        
        
            | 
            | 
           107 | 
                   $this->assertEquals('post', $event->objecttable);
  | 
        
        
            | 
            | 
           108 | 
                   $this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']);
  | 
        
        
            | 
            | 
           109 | 
                   $this->assertEventContextNotUsed($event);
  | 
        
        
            | 
            | 
           110 | 
               }
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
               /**
  | 
        
        
            | 
            | 
           113 | 
                * Tests for event note_updated.
  | 
        
        
            | 
            | 
           114 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           115 | 
               public function test_note_updated_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
                   // Delete a note.
  | 
        
        
            | 
            | 
           118 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           119 | 
                   $note = clone $this->eventnote;
  | 
        
        
            | 
            | 
           120 | 
                   $note->publishstate = NOTES_STATE_DRAFT;
  | 
        
        
            | 
            | 
           121 | 
                   note_save($note);
  | 
        
        
            | 
            | 
           122 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           123 | 
                   $event = array_pop($events); // Delete note event.
  | 
        
        
            | 
            | 
           124 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
                   // Validate event data.
  | 
        
        
            | 
            | 
           127 | 
                   $this->assertInstanceOf('\core\event\note_updated', $event);
  | 
        
        
            | 
            | 
           128 | 
                   $this->assertEquals($note->id, $event->objectid);
  | 
        
        
            | 
            | 
           129 | 
                   $this->assertEquals($note->usermodified, $event->userid);
  | 
        
        
            | 
            | 
           130 | 
                   $this->assertEquals($note->userid, $event->relateduserid);
  | 
        
        
            | 
            | 
           131 | 
                   $this->assertEquals('post', $event->objecttable);
  | 
        
        
            | 
            | 
           132 | 
                   $this->assertEquals(NOTES_STATE_DRAFT, $event->other['publishstate']);
  | 
        
        
            | 
            | 
           133 | 
                   $this->assertEventContextNotUsed($event);
  | 
        
        
            | 
            | 
           134 | 
               }
  | 
        
        
            | 
            | 
           135 | 
              | 
        
        
            | 
            | 
           136 | 
               /**
  | 
        
        
            | 
            | 
           137 | 
                * Test the notes viewed event.
  | 
        
        
            | 
            | 
           138 | 
                *
  | 
        
        
            | 
            | 
           139 | 
                * It's not possible to use the moodle API to simulate the viewing of notes, so here we
  | 
        
        
            | 
            | 
           140 | 
                * simply create the event and trigger it.
  | 
        
        
            | 
            | 
           141 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           142 | 
               public function test_notes_viewed(): void {
  | 
        
        
           | 1 | 
           efrain | 
           143 | 
                   $coursecontext = \context_course::instance($this->eventnote->courseid);
  | 
        
        
            | 
            | 
           144 | 
                   // Trigger event for notes viewed.
  | 
        
        
            | 
            | 
           145 | 
                   $event = \core\event\notes_viewed::create(array(
  | 
        
        
            | 
            | 
           146 | 
                       'context' => $coursecontext,
  | 
        
        
            | 
            | 
           147 | 
                       'relateduserid' => $this->eventnote->userid
  | 
        
        
            | 
            | 
           148 | 
                   ));
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
                   // Trigger and capture the event.
  | 
        
        
            | 
            | 
           151 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           152 | 
                   $event->trigger();
  | 
        
        
            | 
            | 
           153 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           154 | 
                   $event = reset($events);
  | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
                   $this->assertInstanceOf('\core\event\notes_viewed', $event);
  | 
        
        
            | 
            | 
           157 | 
                   $this->assertEquals($coursecontext, $event->get_context());
  | 
        
        
            | 
            | 
           158 | 
                   $this->assertEventContextNotUsed($event);
  | 
        
        
            | 
            | 
           159 | 
               }
  | 
        
        
            | 
            | 
           160 | 
           }
  |