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