2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
22 * @copyright 2009 Nicolas Connault
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once($CFG->dirroot . '/blog/locallib.php');
28 require_once($CFG->dirroot . '/blog/lib.php');
31 * Test functions that rely on the DB tables
33 class core_bloglib_testcase extends advanced_testcase {
35 private $courseid; // To store important ids to be used in tests
42 protected function 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'));
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);
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;
88 public function test_overrides() {
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));
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 $blog_headers = blog_get_headers();
132 $this->assertEquals($blog_headers['heading'], get_string('siteblog', 'blog', 'phpunit'));
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'], '');
141 public function test_blog_get_headers_case_7() {
142 global $CFG, $PAGE, $OUTPUT;
143 $blog_headers = blog_get_headers(NULL, $this->groupid);
144 $this->assertNotEquals($blog_headers['heading'], '');
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'], '');
154 * Test various blog related events.
156 public function test_blog_entry_created_event() {
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);
170 $events = $sink->get_events();
172 $event = reset($events);
173 $sitecontext = context_system::instance();
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);
180 $this->assertEquals($this->userid, $event->relateduserid);
181 $this->assertEquals("post", $event->objecttable);
182 $arr = array(SITEID, 'blog', 'add', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
183 $this->assertEventLegacyLogData($arr, $event);
184 $this->assertEquals("blog_entry_added", $event->get_legacy_eventname());
185 $this->assertEventLegacyData($blog, $event);
189 * Tests for event blog_entry_updated.
191 public function test_blog_entry_updated_event() {
194 $this->setAdminUser();
195 $this->resetAfterTest();
196 $sitecontext = context_system::instance();
198 // Edit a blog entry as Admin.
199 $blog = new blog_entry($this->postid);
200 $sink = $this->redirectEvents();
201 $blog->summary_editor = array('text' => 'Something', 'format' => FORMAT_MOODLE);
202 $blog->edit(array(), null, array(), array());
203 $events = $sink->get_events();
204 $event = array_pop($events);
207 // Validate event data.
208 $this->assertInstanceOf('\core\event\blog_entry_updated', $event);
209 $this->assertEquals($sitecontext->id, $event->contextid);
210 $this->assertEquals($blog->id, $event->objectid);
211 $this->assertEquals($USER->id, $event->userid);
212 $this->assertEquals($this->userid, $event->relateduserid);
213 $this->assertEquals("post", $event->objecttable);
214 $this->assertEquals("blog_entry_edited", $event->get_legacy_eventname());
215 $this->assertEventLegacyData($blog, $event);
216 $arr = array (SITEID, 'blog', 'update', 'index.php?userid=' . $this->userid . '&entryid=' . $blog->id, $blog->subject);
217 $this->assertEventLegacyLogData($arr, $event);
221 * Tests for event blog_entry_deleted.
223 public function test_blog_entry_deleted_event() {
226 $this->setAdminUser();
227 $this->resetAfterTest();
228 $sitecontext = context_system::instance();
230 // Delete a user blog entry as Admin.
231 $blog = new blog_entry($this->postid);
232 $sink = $this->redirectEvents();
233 $record = $DB->get_record('post', array('id' => $blog->id));
235 $events = $sink->get_events();
236 $event = array_pop($events);
239 // Validate event data.
240 $this->assertInstanceOf('\core\event\blog_entry_deleted', $event);
241 $this->assertEquals($sitecontext->id, $event->contextid);
242 $this->assertEquals($blog->id, $event->objectid);
243 $this->assertEquals($USER->id, $event->userid);
244 $this->assertEquals($this->userid, $event->relateduserid);
245 $this->assertEquals("post", $event->objecttable);
246 $this->assertEquals($record, $event->get_record_snapshot("post", $blog->id));
247 $this->assertSame('blog_entry_deleted', $event->get_legacy_eventname());
248 $arr = array(SITEID, 'blog', 'delete', 'index.php?userid=' . $blog->userid, 'deleted blog entry with entry id# ' .
250 $this->assertEventLegacyLogData($arr, $event);
251 $this->assertEventLegacyData($blog, $event);