Merge branch 'MDL-61566-33' of git://github.com/andrewnicols/moodle into MOODLE_33_STABLE
[moodle.git] / notes / tests / events_test.php
blob02d5dbe703473f6375ed0e13993b328b52c1ea60
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.
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/>.
17 /**
18 * Tests for notes events.
20 * @package core_notes
21 * @copyright 2013 Ankit Agarwal
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Class core_notes_events_testcase
30 * Class for tests related to notes events.
32 * @package core_notes
33 * @copyright 2013 Ankit Agarwal
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
36 class core_notes_events_testcase extends advanced_testcase {
38 /** @var stdClass A note object. */
39 private $eventnote;
41 /** @var stdClass A complete record from post table */
42 private $noterecord;
44 public function setUp() {
45 global $DB;
47 $this->resetAfterTest();
48 $this->setAdminUser();
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);
59 /**
60 * Tests for event note_deleted.
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();
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']);
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);
89 /**
90 * Tests for event note_created.
92 public function test_note_created_event() {
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();
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']);
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);
121 * Tests for event note_updated.
123 public function test_note_updated_event() {
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();
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']);
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);
152 * Test the notes viewed event.
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.
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
165 // Trigger and capture the event.
166 $sink = $this->redirectEvents();
167 $event->trigger();
168 $events = $sink->get_events();
169 $event = reset($events);
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);