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