MDL-46346 blog: coding style fixes
[moodle.git] / blog / tests / bloglib_test.php
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/>.
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  */
26 global $CFG;
27 require_once($CFG->dirroot . '/blog/locallib.php');
28 require_once($CFG->dirroot . '/blog/lib.php');
30 /**
31  * Test functions that rely on the DB tables
32  */
33 class core_bloglib_testcase extends advanced_testcase {
35     private $courseid;
36     private $cmid;
37     private $groupid;
38     private $userid;
39     private $tagid;
40     private $postid;
42     protected function setUp() {
43         global $DB;
44         parent::setUp();
46         $this->resetAfterTest();
48         // Create default course.
49         $course = $this->getDataGenerator()->create_course(array('category'=>1, 'shortname'=>'ANON'));
50         $this->assertNotEmpty($course);
51         $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
52         $this->assertNotEmpty($page);
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);
60         // Create default user.
61         $user = $this->getDataGenerator()->create_user(array('username'=>'testuser', 'firstname'=>'Jimmy', 'lastname'=>'Kinnon'));
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);
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);
78         // Grab important ids.
79         $this->courseid = $course->id;
80         $this->cmid = $page->cmid;
81         $this->groupid  = $group->id;
82         $this->userid  = $user->id;
83         $this->tagid  = $tag->id;
84         $this->postid = $post->id;
85     }
88     public function test_overrides() {
89         global $SITE;
91         // Try all the filters at once: Only the entry filter is active.
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);
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));
103         // Again, but without the entry filter: This time, the tag, user and module filters are active.
104         $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
105             'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->postid);
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));
114         // We should get the same result by removing the 3 inactive filters: site, course and group.
115         $filters = array('module' => $this->cmid, 'user' => $this->userid, 'tag' => $this->tagid);
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));
124     }
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.
129     public function test_blog_get_headers_case_1() {
130         global $CFG, $PAGE, $OUTPUT;
131         $blogheaders = blog_get_headers();
132         $this->assertEquals($blogheaders['heading'], get_string('siteblog', 'blog', 'phpunit'));
133     }
135     public function test_blog_get_headers_case_6() {
136         global $CFG, $PAGE, $OUTPUT;
137         $blogheaders = blog_get_headers($this->courseid, null, $this->userid);
138         $this->assertNotEquals($blogheaders['heading'], '');
139     }
141     public function test_blog_get_headers_case_7() {
142         global $CFG, $PAGE, $OUTPUT;
143         $blogheaders = blog_get_headers(null, $this->groupid);
144         $this->assertNotEquals($blogheaders['heading'], '');
145     }
147     public function test_blog_get_headers_case_10() {
148         global $CFG, $PAGE, $OUTPUT;
149         $blogheaders = blog_get_headers($this->courseid);
150         $this->assertNotEquals($blogheaders['heading'], '');
151     }
153     /**
154      * Test various blog related events.
155      */
156     public function test_blog_entry_created_event() {
157         global $USER;
159         $this->setAdminUser();
160         $this->resetAfterTest();
162         // Create a blog entry for another user as Admin.
163         $sink = $this->redirectEvents();
164         $blog = new blog_entry();
165         $blog->subject = "Subject of blog";
166         $blog->userid = $this->userid;
167         $states = blog_entry::get_applicable_publish_states();
168         $blog->publishstate = reset($states);
169         $blog->add();
170         $events = $sink->get_events();
171         $sink->close();
172         $event = reset($events);
173         $sitecontext = context_system::instance();
175         // Validate event data.
176         $this->assertInstanceOf('\core\event\blog_entry_created', $event);
177         $url = new moodle_url('/blog/index.php', array('entryid' => $event->objectid));
178         $this->assertEquals($url, $event->get_url());
179         $this->assertEquals($sitecontext->id, $event->contextid);
180         $this->assertEquals($blog->id, $event->objectid);
181         $this->assertEquals($USER->id, $event->userid);
182         $this->assertEquals($this->userid, $event->relateduserid);
183         $this->assertEquals("post", $event->objecttable);
184         $arr = array(SITEID, 'blog', 'add', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
185         $this->assertEventLegacyLogData($arr, $event);
186         $this->assertEquals("blog_entry_added", $event->get_legacy_eventname());
187         $this->assertEventLegacyData($blog, $event);
188         $this->assertEventContextNotUsed($event);
189     }
191     /**
192      * Tests for event blog_entry_updated.
193      */
194     public function test_blog_entry_updated_event() {
195         global $USER;
197         $this->setAdminUser();
198         $this->resetAfterTest();
199         $sitecontext = context_system::instance();
201         // Edit a blog entry as Admin.
202         $blog = new blog_entry($this->postid);
203         $sink = $this->redirectEvents();
204         $blog->summary_editor = array('text' => 'Something', 'format' => FORMAT_MOODLE);
205         $blog->edit(array(), null, array(), array());
206         $events = $sink->get_events();
207         $event = array_pop($events);
208         $sink->close();
210         // Validate event data.
211         $this->assertInstanceOf('\core\event\blog_entry_updated', $event);
212         $url = new moodle_url('/blog/index.php', array('entryid' => $event->objectid));
213         $this->assertEquals($url, $event->get_url());
214         $this->assertEquals($sitecontext->id, $event->contextid);
215         $this->assertEquals($blog->id, $event->objectid);
216         $this->assertEquals($USER->id, $event->userid);
217         $this->assertEquals($this->userid, $event->relateduserid);
218         $this->assertEquals("post", $event->objecttable);
219         $this->assertEquals("blog_entry_edited", $event->get_legacy_eventname());
220         $this->assertEventLegacyData($blog, $event);
221         $arr = array (SITEID, 'blog', 'update', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
222         $this->assertEventLegacyLogData($arr, $event);
223         $this->assertEventContextNotUsed($event);
224     }
226     /**
227      * Tests for event blog_entry_deleted.
228      */
229     public function test_blog_entry_deleted_event() {
230         global $USER, $DB;
232         $this->setAdminUser();
233         $this->resetAfterTest();
234         $sitecontext = context_system::instance();
236         // Delete a user blog entry as Admin.
237         $blog = new blog_entry($this->postid);
238         $sink = $this->redirectEvents();
239         $record = $DB->get_record('post', array('id' => $blog->id));
240         $blog->delete();
241         $events = $sink->get_events();
242         $event = array_pop($events);
243         $sink->close();
245         // Validate event data.
246         $this->assertInstanceOf('\core\event\blog_entry_deleted', $event);
247         $this->assertEquals(null, $event->get_url());
248         $this->assertEquals($sitecontext->id, $event->contextid);
249         $this->assertEquals($blog->id, $event->objectid);
250         $this->assertEquals($USER->id, $event->userid);
251         $this->assertEquals($this->userid, $event->relateduserid);
252         $this->assertEquals("post", $event->objecttable);
253         $this->assertEquals($record, $event->get_record_snapshot("post", $blog->id));
254         $this->assertSame('blog_entry_deleted', $event->get_legacy_eventname());
255         $arr = array(SITEID, 'blog', 'delete', 'index.php?userid=' . $blog->userid, 'deleted blog entry with entry id# ' .
256                 $blog->id);
257         $this->assertEventLegacyLogData($arr, $event);
258         $this->assertEventLegacyData($blog, $event);
259         $this->assertEventContextNotUsed($event);
260     }
263     /**
264      * Tests for event blog_association_created.
265      */
266     public function test_blog_association_created_event() {
267         global $USER;
269         $this->setAdminUser();
270         $this->resetAfterTest();
271         $sitecontext = context_system::instance();
272         $coursecontext = context_course::instance($this->courseid);
273         $contextmodule = context_module::instance($this->cmid);
275         // Add blog associations with a course.
276         $blog = new blog_entry($this->postid);
277         $sink = $this->redirectEvents();
278         $blog->add_association($coursecontext->id);
279         $events = $sink->get_events();
280         $event = reset($events);
281         $sink->close();
283         // Validate event data.
284         $this->assertInstanceOf('\core\event\blog_association_created', $event);
285         $this->assertEquals($sitecontext->id, $event->contextid);
286         $url = new moodle_url('/blog/index.php', array('entryid' => $event->other['blogid']));
287         $this->assertEquals($url, $event->get_url());
288         $this->assertEquals($blog->id, $event->other['blogid']);
289         $this->assertEquals($this->courseid, $event->other['associateid']);
290         $this->assertEquals('course', $event->other['associatetype']);
291         $this->assertEquals($blog->subject, $event->other['subject']);
292         $this->assertEquals($USER->id, $event->userid);
293         $this->assertEquals($this->userid, $event->relateduserid);
294         $this->assertEquals('blog_association', $event->objecttable);
295         $arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
296                      $blog->subject, 0, $this->userid);
297         $this->assertEventLegacyLogData($arr, $event);
299         // Add blog associations with a module.
300         $blog = new blog_entry($this->postid);
301         $sink = $this->redirectEvents();
302         $blog->add_association($contextmodule->id);
303         $events = $sink->get_events();
304         $event = reset($events);
305         $sink->close();
307         // Validate event data.
308         $this->assertEquals($blog->id, $event->other['blogid']);
309         $this->assertEquals($this->cmid, $event->other['associateid']);
310         $this->assertEquals('coursemodule', $event->other['associatetype']);
311         $arr = array(SITEID, 'blog', 'add association', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id,
312                      $blog->subject, $this->cmid, $this->userid);
313         $this->assertEventLegacyLogData($arr, $event);
314         $this->assertEventContextNotUsed($event);
315     }
317     /**
318      * Tests for event blog_association_created validations.
319      */
320     public function test_blog_association_created_event_validations() {
322         $this->resetAfterTest();
324          // Make sure associatetype validations work.
325         try {
326             \core\event\blog_association_created::create(array(
327                 'contextid' => 1,
328                 'objectid' => 3,
329                 'relateduserid' => 2,
330                 'other' => array('associateid' => 2 , 'blogid' => 3, 'subject' => 'blog subject')));
331         } catch (coding_exception $e) {
332             $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
333         }
334         try {
335             \core\event\blog_association_created::create(array(
336                 'contextid' => 1,
337                 'objectid' => 3,
338                 'relateduserid' => 2,
339                 'other' => array('associateid' => 2 , 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject')));
340         } catch (coding_exception $e) {
341             $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
342         }
343         // Make sure associateid validations work.
344         try {
345             \core\event\blog_association_created::create(array(
346                 'contextid' => 1,
347                 'objectid' => 3,
348                 'relateduserid' => 2,
349                 'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
350         } catch (coding_exception $e) {
351             $this->assertContains('The \'associateid\' value must be set in other.', $e->getMessage());
352         }
353         // Make sure blogid validations work.
354         try {
355             \core\event\blog_association_created::create(array(
356                 'contextid' => 1,
357                 'objectid' => 3,
358                 'relateduserid' => 2,
359                 'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
360         } catch (coding_exception $e) {
361             $this->assertContains('The \'blogid\' value must be set in other.', $e->getMessage());
362         }
363         // Make sure blogid validations work.
364         try {
365             \core\event\blog_association_created::create(array(
366                 'contextid' => 1,
367                 'objectid' => 3,
368                 'relateduserid' => 2,
369                 'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course')));
370         } catch (coding_exception $e) {
371             $this->assertContains('The \'subject\' value must be set in other.', $e->getMessage());
372         }
373     }
375     /**
376      * Tests for event blog_entries_viewed.
377      */
378     public function test_blog_entries_viewed_event() {
380         $this->setAdminUser();
382         $other = array('entryid' => $this->postid, 'tagid' => $this->tagid, 'userid' => $this->userid, 'modid' => $this->cmid,
383                        'groupid' => $this->groupid, 'courseid' => $this->courseid, 'search' => 'search', 'fromstart' => 2);
385         // Trigger event.
386         $sink = $this->redirectEvents();
387         $eventparams = array('other' => $other);
388         $eventinst = \core\event\blog_entries_viewed::create($eventparams);
389         $eventinst->trigger();
390         $events = $sink->get_events();
391         $event = reset($events);
392         $sink->close();
394         // Validate event data.
395         $url = new moodle_url('/blog/index.php', $other);
396         $url2 = new moodle_url('index.php', $other);
397         $this->assertEquals($url, $event->get_url());
398         $arr = array(SITEID, 'blog', 'view', $url2->out(), 'view blog entry');
399         $this->assertEventLegacyLogData($arr, $event);
400         $this->assertEventContextNotUsed($event);
401     }
403     /**
404      * Test comment_created event.
405      */
406     public function test_blog_comment_created_event() {
407         global $USER, $CFG;
409         $this->setAdminUser();
411         require_once($CFG->dirroot . '/comment/lib.php');
412         $context = context_user::instance($USER->id);
414         $cmt = new stdClass();
415         $cmt->context = $context;
416         $cmt->courseid = $this->courseid;
417         $cmt->area = 'format_blog';
418         $cmt->itemid = $this->postid;
419         $cmt->showcount = 1;
420         $cmt->component = 'blog';
421         $manager = new comment($cmt);
423         // Triggering and capturing the event.
424         $sink = $this->redirectEvents();
425         $manager->add("New comment");
426         $events = $sink->get_events();
427         $this->assertCount(1, $events);
428         $event = reset($events);
430         // Checking that the event contains the expected values.
431         $this->assertInstanceOf('\core\event\blog_comment_created', $event);
432         $this->assertEquals($context, $event->get_context());
433         $this->assertEquals($this->postid, $event->other['itemid']);
434         $url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
435         $this->assertEquals($url, $event->get_url());
436         $this->assertEventContextNotUsed($event);
437     }
439     /**
440      * Test comment_deleted event.
441      */
442     public function test_blog_comment_deleted_event() {
443         global $USER, $CFG;
445         $this->setAdminUser();
447         require_once($CFG->dirroot . '/comment/lib.php');
448         $context = context_user::instance($USER->id);
450         $cmt = new stdClass();
451         $cmt->context = $context;
452         $cmt->courseid = $this->courseid;
453         $cmt->area = 'format_blog';
454         $cmt->itemid = $this->postid;
455         $cmt->showcount = 1;
456         $cmt->component = 'blog';
457         $manager = new comment($cmt);
458         $newcomment = $manager->add("New comment");
460         // Triggering and capturing the event.
461         $sink = $this->redirectEvents();
462         $manager->delete($newcomment->id);
463         $events = $sink->get_events();
464         $this->assertCount(1, $events);
465         $event = reset($events);
467         // Checking that the event contains the expected values.
468         $this->assertInstanceOf('\core\event\blog_comment_deleted', $event);
469         $this->assertEquals($context, $event->get_context());
470         $this->assertEquals($this->postid, $event->other['itemid']);
471         $url = new moodle_url('/blog/index.php', array('entryid' => $this->postid));
472         $this->assertEquals($url, $event->get_url());
473         $this->assertEventContextNotUsed($event);
474     }