MDL-44717 events: Event restore process must call the init() method
[moodle.git] / blog / tests / bloglib_test.php
CommitLineData
814a96ed
PS
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
814a96ed
PS
17/**
18 * Unit tests for blog
19 *
20 * @package core_blog
21 * @category phpunit
22 * @copyright 2009 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26global $CFG;
27require_once($CFG->dirroot . '/blog/locallib.php');
28require_once($CFG->dirroot . '/blog/lib.php');
29
814a96ed
PS
30/**
31 * Test functions that rely on the DB tables
32 */
8252b7c2 33class core_bloglib_testcase extends advanced_testcase {
814a96ed
PS
34
35 private $courseid; // To store important ids to be used in tests
714f3998 36 private $cmid;
814a96ed
PS
37 private $groupid;
38 private $userid;
39 private $tagid;
714f3998 40 private $postid;
814a96ed
PS
41
42 protected function setUp() {
43 global $DB;
44 parent::setUp();
45
714f3998 46 $this->resetAfterTest();
814a96ed
PS
47
48 // Create default course
714f3998
PS
49 $course = $this->getDataGenerator()->create_course(array('category'=>1, 'shortname'=>'ANON'));
50 $this->assertNotEmpty($course);
814a96ed 51 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
714f3998 52 $this->assertNotEmpty($page);
814a96ed
PS
53
54 // Create default group
55 $group = new stdClass();
56 $group->courseid = $course->id;
57 $group->name = 'ANON';
58 $group->id = $DB->insert_record('groups', $group);
59
60 // Create default user
61 $user = $this->getDataGenerator()->create_user(array('username'=>'testuser', 'firstname'=>'Jimmy', 'lastname'=>'Kinnon'));
62
63 // Create default tag
64 $tag = new stdClass();
65 $tag->userid = $user->id;
66 $tag->name = 'testtagname';
67 $tag->rawname = 'Testtagname';
68 $tag->tagtype = 'official';
69 $tag->id = $DB->insert_record('tag', $tag);
70
71 // Create default post
72 $post = new stdClass();
73 $post->userid = $user->id;
74 $post->groupid = $group->id;
75 $post->content = 'test post content text';
76 $post->id = $DB->insert_record('post', $post);
77
78 // Grab important ids
79 $this->courseid = $course->id;
714f3998 80 $this->cmid = $page->cmid;
814a96ed
PS
81 $this->groupid = $group->id;
82 $this->userid = $user->id;
83 $this->tagid = $tag->id;
714f3998 84 $this->postid = $post->id;
814a96ed
PS
85 }
86
87
88 public function test_overrides() {
714f3998 89 global $SITE;
814a96ed
PS
90
91 // Try all the filters at once: Only the entry filter is active
714f3998
PS
92 $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
93 'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->tagid, 'entry' => $this->postid);
814a96ed
PS
94 $blog_listing = new blog_listing($filters);
95 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
96 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
97 $this->assertFalse(array_key_exists('module', $blog_listing->filters));
98 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
99 $this->assertFalse(array_key_exists('user', $blog_listing->filters));
100 $this->assertFalse(array_key_exists('tag', $blog_listing->filters));
101 $this->assertTrue(array_key_exists('entry', $blog_listing->filters));
102
103 // Again, but without the entry filter: This time, the tag, user and module filters are active
714f3998
PS
104 $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
105 'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->postid);
814a96ed
PS
106 $blog_listing = new blog_listing($filters);
107 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
108 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
109 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
110 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
111 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
112 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
113
114 // We should get the same result by removing the 3 inactive filters: site, course and group:
714f3998 115 $filters = array('module' => $this->cmid, 'user' => $this->userid, 'tag' => $this->tagid);
814a96ed
PS
116 $blog_listing = new blog_listing($filters);
117 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
118 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
119 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
120 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
121 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
122 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
123
124 }
125
126 // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php.
127 // Some cases are omitted due to the optional_param variables used.
128
129 public function test_blog_get_headers_case_1() {
130 global $CFG, $PAGE, $OUTPUT;
131 $blog_headers = blog_get_headers();
132 $this->assertEquals($blog_headers['heading'], get_string('siteblog', 'blog', 'phpunit'));
133 }
134
135 public function test_blog_get_headers_case_6() {
136 global $CFG, $PAGE, $OUTPUT;
137 $blog_headers = blog_get_headers($this->courseid, NULL, $this->userid);
138 $this->assertNotEquals($blog_headers['heading'], '');
139 }
140
141 public function test_blog_get_headers_case_7() {
142 global $CFG, $PAGE, $OUTPUT;
714f3998 143 $blog_headers = blog_get_headers(NULL, $this->groupid);
814a96ed
PS
144 $this->assertNotEquals($blog_headers['heading'], '');
145 }
714f3998 146
814a96ed
PS
147 public function test_blog_get_headers_case_10() {
148 global $CFG, $PAGE, $OUTPUT;
149 $blog_headers = blog_get_headers($this->courseid);
150 $this->assertNotEquals($blog_headers['heading'], '');
151 }
3049780a
AA
152
153 /**
154 * Test various blog related events.
155 */
32dea439
AA
156 public function test_blog_entry_created_event() {
157 global $USER;
3049780a
AA
158
159 $this->setAdminUser();
160 $this->resetAfterTest();
161
77037e27 162 // Create a blog entry for another user as Admin.
32dea439 163 $sink = $this->redirectEvents();
3049780a 164 $blog = new blog_entry();
3049780a 165 $blog->subject = "Subject of blog";
32dea439 166 $blog->userid = $this->userid;
3049780a
AA
167 $states = blog_entry::get_applicable_publish_states();
168 $blog->publishstate = reset($states);
3049780a
AA
169 $blog->add();
170 $events = $sink->get_events();
32dea439 171 $sink->close();
3049780a
AA
172 $event = reset($events);
173 $sitecontext = context_system::instance();
174
175 // Validate event data.
176 $this->assertInstanceOf('\core\event\blog_entry_created', $event);
177 $this->assertEquals($sitecontext->id, $event->contextid);
178 $this->assertEquals($blog->id, $event->objectid);
179 $this->assertEquals($USER->id, $event->userid);
32dea439 180 $this->assertEquals($this->userid, $event->relateduserid);
3049780a 181 $this->assertEquals("post", $event->objecttable);
32dea439 182 $arr = array(SITEID, 'blog', 'add', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
77037e27 183 $this->assertEventLegacyLogData($arr, $event);
32dea439
AA
184 $this->assertEquals("blog_entry_added", $event->get_legacy_eventname());
185 $this->assertEventLegacyData($blog, $event);
623a32e5 186 $this->assertEventContextNotUsed($event);
32dea439
AA
187 }
188
189 /**
190 * Tests for event blog_entry_updated.
191 */
192 public function test_blog_entry_updated_event() {
193 global $USER;
194
195 $this->setAdminUser();
196 $this->resetAfterTest();
197 $sitecontext = context_system::instance();
198
199 // Edit a blog entry as Admin.
200 $blog = new blog_entry($this->postid);
201 $sink = $this->redirectEvents();
202 $blog->summary_editor = array('text' => 'Something', 'format' => FORMAT_MOODLE);
203 $blog->edit(array(), null, array(), array());
204 $events = $sink->get_events();
205 $event = array_pop($events);
206 $sink->close();
207
208 // Validate event data.
209 $this->assertInstanceOf('\core\event\blog_entry_updated', $event);
210 $this->assertEquals($sitecontext->id, $event->contextid);
211 $this->assertEquals($blog->id, $event->objectid);
212 $this->assertEquals($USER->id, $event->userid);
213 $this->assertEquals($this->userid, $event->relateduserid);
214 $this->assertEquals("post", $event->objecttable);
215 $this->assertEquals("blog_entry_edited", $event->get_legacy_eventname());
216 $this->assertEventLegacyData($blog, $event);
217 $arr = array (SITEID, 'blog', 'update', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
218 $this->assertEventLegacyLogData($arr, $event);
623a32e5 219 $this->assertEventContextNotUsed($event);
32dea439
AA
220 }
221
222 /**
223 * Tests for event blog_entry_deleted.
224 */
225 public function test_blog_entry_deleted_event() {
226 global $USER, $DB;
227
228 $this->setAdminUser();
229 $this->resetAfterTest();
230 $sitecontext = context_system::instance();
6c66b7f3 231
77037e27 232 // Delete a user blog entry as Admin.
32dea439
AA
233 $blog = new blog_entry($this->postid);
234 $sink = $this->redirectEvents();
6c66b7f3
AA
235 $record = $DB->get_record('post', array('id' => $blog->id));
236 $blog->delete();
237 $events = $sink->get_events();
238 $event = array_pop($events);
32dea439 239 $sink->close();
6c66b7f3
AA
240
241 // Validate event data.
242 $this->assertInstanceOf('\core\event\blog_entry_deleted', $event);
32dea439 243 $this->assertEquals($sitecontext->id, $event->contextid);
6c66b7f3
AA
244 $this->assertEquals($blog->id, $event->objectid);
245 $this->assertEquals($USER->id, $event->userid);
32dea439 246 $this->assertEquals($this->userid, $event->relateduserid);
6c66b7f3
AA
247 $this->assertEquals("post", $event->objecttable);
248 $this->assertEquals($record, $event->get_record_snapshot("post", $blog->id));
249 $this->assertSame('blog_entry_deleted', $event->get_legacy_eventname());
32dea439
AA
250 $arr = array(SITEID, 'blog', 'delete', 'index.php?userid=' . $blog->userid, 'deleted blog entry with entry id# ' .
251 $blog->id);
77037e27 252 $this->assertEventLegacyLogData($arr, $event);
32dea439 253 $this->assertEventLegacyData($blog, $event);
623a32e5 254 $this->assertEventContextNotUsed($event);
3049780a 255 }
cf0116db
AA
256
257
258 /**
259 * Tests for event blog_association_created.
260 */
261 public function test_blog_association_created_event() {
262 global $USER;
263
264 $this->setAdminUser();
265 $this->resetAfterTest();
266 $sitecontext = context_system::instance();
267 $coursecontext = context_course::instance($this->courseid);
268 $contextmodule = context_module::instance($this->cmid);
269
270 // Add blog associations with a course.
271 $blog = new blog_entry($this->postid);
272 $sink = $this->redirectEvents();
273 $blog->add_association($coursecontext->id);
274 $events = $sink->get_events();
275 $event = reset($events);
276 $sink->close();
277
278 // Validate event data.
279 $this->assertInstanceOf('\core\event\blog_association_created', $event);
280 $this->assertEquals($sitecontext->id, $event->contextid);
281 $this->assertEquals($blog->id, $event->other['blogid']);
282 $this->assertEquals($this->courseid, $event->other['associateid']);
283 $this->assertEquals('course', $event->other['associatetype']);
284 $this->assertEquals($blog->subject, $event->other['subject']);
285 $this->assertEquals($USER->id, $event->userid);
286 $this->assertEquals($this->userid, $event->relateduserid);
287 $this->assertEquals('blog_association', $event->objecttable);
288 $arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
289 $blog->subject, 0, $this->userid);
290 $this->assertEventLegacyLogData($arr, $event);
291
292 // Add blog associations with a module.
293 $blog = new blog_entry($this->postid);
294 $sink = $this->redirectEvents();
295 $blog->add_association($contextmodule->id);
296 $events = $sink->get_events();
297 $event = reset($events);
298 $sink->close();
299
300 // Validate event data.
301 $this->assertEquals($blog->id, $event->other['blogid']);
302 $this->assertEquals($this->cmid, $event->other['associateid']);
303 $this->assertEquals('coursemodule', $event->other['associatetype']);
304 $arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
305 $blog->subject, $this->cmid, $this->userid);
306 $this->assertEventLegacyLogData($arr, $event);
623a32e5 307 $this->assertEventContextNotUsed($event);
cf0116db
AA
308 }
309
310 /**
311 * Tests for event blog_association_created validations.
312 */
313 public function test_blog_association_created_event_validations() {
314
315 $this->resetAfterTest();
316
317 // Make sure associatetype validations work.
318 try {
319 \core\event\blog_association_created::create(array(
320 'contextid' => 1,
321 'objectid' => 3,
322 'other' => array('associateid' => 2 , 'blogid' => 3, 'subject' => 'blog subject')));
323 } catch (coding_exception $e) {
324 $this->assertContains('Invalid associatetype', $e->getMessage());
325 }
326 try {
327 \core\event\blog_association_created::create(array(
328 'contextid' => 1,
329 'objectid' => 3,
330 'other' => array('associateid' => 2 , 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject')));
331 } catch (coding_exception $e) {
332 $this->assertContains('Invalid associatetype', $e->getMessage());
333 }
334 // Make sure associateid validations work.
335 try {
336 \core\event\blog_association_created::create(array(
337 'contextid' => 1,
338 'objectid' => 3,
339 'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
340 } catch (coding_exception $e) {
341 $this->assertContains('Associate id must be set', $e->getMessage());
342 }
343 // Make sure blogid validations work.
344 try {
345 \core\event\blog_association_created::create(array(
346 'contextid' => 1,
347 'objectid' => 3,
348 'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
349 } catch (coding_exception $e) {
350 $this->assertContains('Blog id must be set', $e->getMessage());
351 }
352 // Make sure blogid validations work.
353 try {
354 \core\event\blog_association_created::create(array(
355 'contextid' => 1,
356 'objectid' => 3,
357 'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course')));
358 } catch (coding_exception $e) {
359 $this->assertContains('Subject must be set', $e->getMessage());
360 }
361 }
362
363 /**
364 * Tests for event blog_entries_viewed.
365 */
366 public function test_blog_entries_viewed_event() {
367
368 $this->setAdminUser();
cdfd6074 369
cf0116db
AA
370 $other = array('entryid' => $this->postid, 'tagid' => $this->tagid, 'userid' => $this->userid, 'modid' => $this->cmid,
371 'groupid' => $this->groupid, 'courseid' => $this->courseid, 'search' => 'search', 'fromstart' => 2);
372
373 // Trigger event.
374 $sink = $this->redirectEvents();
375 $eventparams = array('other' => $other);
376 $eventinst = \core\event\blog_entries_viewed::create($eventparams);
377 $eventinst->trigger();
378 $events = $sink->get_events();
379 $event = reset($events);
380 $sink->close();
381
382 // Validate event data.
383 $url = new moodle_url('/blog/index.php', $other);
384 $url2 = new moodle_url('index.php', $other);
385 $this->assertEquals($url, $event->get_url());
386 $arr = array(SITEID, 'blog', 'view', $url2->out(), 'view blog entry');
387 $this->assertEventLegacyLogData($arr, $event);
623a32e5 388 $this->assertEventContextNotUsed($event);
cf0116db 389 }
cdfd6074
RT
390
391 /**
392 * Test comment_created event.
393 */
394 public function test_blog_comment_created_event() {
395 global $USER, $CFG;
396
397 $this->setAdminUser();
398
399 require_once($CFG->dirroot . '/comment/lib.php');
400 $context = context_user::instance($USER->id);
401
402 $cmt = new stdClass();
403 $cmt->context = $context;
404 $cmt->courseid = $this->courseid;
405 $cmt->area = 'format_blog';
406 $cmt->itemid = $this->postid;
407 $cmt->showcount = 1;
408 $cmt->component = 'blog';
409 $manager = new comment($cmt);
410
411 // Triggering and capturing the event.
412 $sink = $this->redirectEvents();
413 $manager->add("New comment");
414 $events = $sink->get_events();
415 $this->assertCount(1, $events);
416 $event = reset($events);
417
418 // Checking that the event contains the expected values.
419 $this->assertInstanceOf('\core\event\blog_comment_created', $event);
420 $this->assertEquals($context, $event->get_context());
421 $this->assertEquals($this->postid, $event->other['itemid']);
422 $url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
423 $this->assertEquals($url, $event->get_url());
623a32e5 424 $this->assertEventContextNotUsed($event);
cdfd6074
RT
425 }
426
427 /**
428 * Test comment_deleted event.
429 */
430 public function test_blog_comment_deleted_event() {
431 global $USER, $CFG;
432
433 $this->setAdminUser();
434
435 require_once($CFG->dirroot . '/comment/lib.php');
436 $context = context_user::instance($USER->id);
437
438 $cmt = new stdClass();
439 $cmt->context = $context;
440 $cmt->courseid = $this->courseid;
441 $cmt->area = 'format_blog';
442 $cmt->itemid = $this->postid;
443 $cmt->showcount = 1;
444 $cmt->component = 'blog';
445 $manager = new comment($cmt);
446 $newcomment = $manager->add("New comment");
447
448 // Triggering and capturing the event.
449 $sink = $this->redirectEvents();
450 $manager->delete($newcomment->id);
451 $events = $sink->get_events();
452 $this->assertCount(1, $events);
453 $event = reset($events);
454
455 // Checking that the event contains the expected values.
456 $this->assertInstanceOf('\core\event\blog_comment_deleted', $event);
457 $this->assertEquals($context, $event->get_context());
458 $this->assertEquals($this->postid, $event->other['itemid']);
459 $url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
460 $this->assertEquals($url, $event->get_url());
623a32e5 461 $this->assertEventContextNotUsed($event);
cdfd6074 462 }
814a96ed 463}
32dea439 464