Commit | Line | Data |
---|---|---|
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 | ||
26 | global $CFG; | |
27 | require_once($CFG->dirroot . '/blog/locallib.php'); | |
28 | require_once($CFG->dirroot . '/blog/lib.php'); | |
29 | ||
30 | ||
31 | /** | |
32 | * Test functions that rely on the DB tables | |
33 | */ | |
8252b7c2 | 34 | class core_bloglib_testcase extends advanced_testcase { |
814a96ed PS |
35 | |
36 | private $courseid; // To store important ids to be used in tests | |
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 PS |
48 | |
49 | // Create default course | |
714f3998 PS |
50 | $course = $this->getDataGenerator()->create_course(array('category'=>1, 'shortname'=>'ANON')); |
51 | $this->assertNotEmpty($course); | |
814a96ed | 52 | $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id)); |
714f3998 | 53 | $this->assertNotEmpty($page); |
814a96ed PS |
54 | |
55 | // Create default group | |
56 | $group = new stdClass(); | |
57 | $group->courseid = $course->id; | |
58 | $group->name = 'ANON'; | |
59 | $group->id = $DB->insert_record('groups', $group); | |
60 | ||
61 | // Create default user | |
62 | $user = $this->getDataGenerator()->create_user(array('username'=>'testuser', 'firstname'=>'Jimmy', 'lastname'=>'Kinnon')); | |
63 | ||
64 | // Create default tag | |
65 | $tag = new stdClass(); | |
66 | $tag->userid = $user->id; | |
67 | $tag->name = 'testtagname'; | |
68 | $tag->rawname = 'Testtagname'; | |
69 | $tag->tagtype = 'official'; | |
70 | $tag->id = $DB->insert_record('tag', $tag); | |
71 | ||
72 | // Create default post | |
73 | $post = new stdClass(); | |
74 | $post->userid = $user->id; | |
75 | $post->groupid = $group->id; | |
76 | $post->content = 'test post content text'; | |
77 | $post->id = $DB->insert_record('post', $post); | |
78 | ||
79 | // Grab important ids | |
80 | $this->courseid = $course->id; | |
714f3998 | 81 | $this->cmid = $page->cmid; |
814a96ed PS |
82 | $this->groupid = $group->id; |
83 | $this->userid = $user->id; | |
84 | $this->tagid = $tag->id; | |
714f3998 | 85 | $this->postid = $post->id; |
814a96ed PS |
86 | } |
87 | ||
88 | ||
89 | public function test_overrides() { | |
714f3998 | 90 | global $SITE; |
814a96ed PS |
91 | |
92 | // Try all the filters at once: Only the entry filter is active | |
714f3998 PS |
93 | $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid, |
94 | 'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->tagid, 'entry' => $this->postid); | |
814a96ed PS |
95 | $blog_listing = new blog_listing($filters); |
96 | $this->assertFalse(array_key_exists('site', $blog_listing->filters)); | |
97 | $this->assertFalse(array_key_exists('course', $blog_listing->filters)); | |
98 | $this->assertFalse(array_key_exists('module', $blog_listing->filters)); | |
99 | $this->assertFalse(array_key_exists('group', $blog_listing->filters)); | |
100 | $this->assertFalse(array_key_exists('user', $blog_listing->filters)); | |
101 | $this->assertFalse(array_key_exists('tag', $blog_listing->filters)); | |
102 | $this->assertTrue(array_key_exists('entry', $blog_listing->filters)); | |
103 | ||
104 | // Again, but without the entry filter: This time, the tag, user and module filters are active | |
714f3998 PS |
105 | $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid, |
106 | 'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->postid); | |
814a96ed PS |
107 | $blog_listing = new blog_listing($filters); |
108 | $this->assertFalse(array_key_exists('site', $blog_listing->filters)); | |
109 | $this->assertFalse(array_key_exists('course', $blog_listing->filters)); | |
110 | $this->assertFalse(array_key_exists('group', $blog_listing->filters)); | |
111 | $this->assertTrue(array_key_exists('module', $blog_listing->filters)); | |
112 | $this->assertTrue(array_key_exists('user', $blog_listing->filters)); | |
113 | $this->assertTrue(array_key_exists('tag', $blog_listing->filters)); | |
114 | ||
115 | // We should get the same result by removing the 3 inactive filters: site, course and group: | |
714f3998 | 116 | $filters = array('module' => $this->cmid, 'user' => $this->userid, 'tag' => $this->tagid); |
814a96ed PS |
117 | $blog_listing = new blog_listing($filters); |
118 | $this->assertFalse(array_key_exists('site', $blog_listing->filters)); | |
119 | $this->assertFalse(array_key_exists('course', $blog_listing->filters)); | |
120 | $this->assertFalse(array_key_exists('group', $blog_listing->filters)); | |
121 | $this->assertTrue(array_key_exists('module', $blog_listing->filters)); | |
122 | $this->assertTrue(array_key_exists('user', $blog_listing->filters)); | |
123 | $this->assertTrue(array_key_exists('tag', $blog_listing->filters)); | |
124 | ||
125 | } | |
126 | ||
127 | // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php. | |
128 | // Some cases are omitted due to the optional_param variables used. | |
129 | ||
130 | public function test_blog_get_headers_case_1() { | |
131 | global $CFG, $PAGE, $OUTPUT; | |
132 | $blog_headers = blog_get_headers(); | |
133 | $this->assertEquals($blog_headers['heading'], get_string('siteblog', 'blog', 'phpunit')); | |
134 | } | |
135 | ||
136 | public function test_blog_get_headers_case_6() { | |
137 | global $CFG, $PAGE, $OUTPUT; | |
138 | $blog_headers = blog_get_headers($this->courseid, NULL, $this->userid); | |
139 | $this->assertNotEquals($blog_headers['heading'], ''); | |
140 | } | |
141 | ||
142 | public function test_blog_get_headers_case_7() { | |
143 | global $CFG, $PAGE, $OUTPUT; | |
714f3998 | 144 | $blog_headers = blog_get_headers(NULL, $this->groupid); |
814a96ed PS |
145 | $this->assertNotEquals($blog_headers['heading'], ''); |
146 | } | |
714f3998 | 147 | |
814a96ed PS |
148 | public function test_blog_get_headers_case_10() { |
149 | global $CFG, $PAGE, $OUTPUT; | |
150 | $blog_headers = blog_get_headers($this->courseid); | |
151 | $this->assertNotEquals($blog_headers['heading'], ''); | |
152 | } | |
3049780a AA |
153 | |
154 | /** | |
155 | * Test various blog related events. | |
156 | */ | |
157 | public function test_blog_entry_events() { | |
6c66b7f3 | 158 | global $USER, $DB; |
3049780a AA |
159 | |
160 | $this->setAdminUser(); | |
161 | $this->resetAfterTest(); | |
77037e27 | 162 | $user = $this->getDataGenerator()->create_user(); |
3049780a | 163 | |
77037e27 | 164 | // Create a blog entry for another user as Admin. |
3049780a | 165 | $blog = new blog_entry(); |
77037e27 | 166 | $blog->userid = $user->id; |
3049780a AA |
167 | $blog->summary = "This is summary of blog"; |
168 | $blog->subject = "Subject of blog"; | |
169 | $states = blog_entry::get_applicable_publish_states(); | |
170 | $blog->publishstate = reset($states); | |
171 | $sink = $this->redirectEvents(); | |
172 | $blog->add(); | |
173 | $events = $sink->get_events(); | |
174 | $event = reset($events); | |
175 | $sitecontext = context_system::instance(); | |
176 | ||
177 | // Validate event data. | |
178 | $this->assertInstanceOf('\core\event\blog_entry_created', $event); | |
179 | $this->assertEquals($sitecontext->id, $event->contextid); | |
180 | $this->assertEquals($blog->id, $event->objectid); | |
181 | $this->assertEquals($USER->id, $event->userid); | |
77037e27 | 182 | $this->assertEquals($user->id, $event->relateduserid); |
3049780a | 183 | $this->assertEquals("post", $event->objecttable); |
77037e27 AA |
184 | $arr = array(SITEID, 'blog', 'add', 'index.php?userid=' . $user->id . '&entryid=' . $blog->id, $blog->subject); |
185 | $this->assertEventLegacyLogData($arr, $event); | |
6c66b7f3 | 186 | |
77037e27 | 187 | // Delete a user blog entry as Admin. |
6c66b7f3 AA |
188 | $record = $DB->get_record('post', array('id' => $blog->id)); |
189 | $blog->delete(); | |
190 | $events = $sink->get_events(); | |
191 | $event = array_pop($events); | |
192 | ||
193 | // Validate event data. | |
194 | $this->assertInstanceOf('\core\event\blog_entry_deleted', $event); | |
195 | $this->assertEquals(context_system::instance()->id, $event->contextid); | |
196 | $this->assertEquals($blog->id, $event->objectid); | |
197 | $this->assertEquals($USER->id, $event->userid); | |
77037e27 | 198 | $this->assertEquals($user->id, $event->relateduserid); |
6c66b7f3 AA |
199 | $this->assertEquals("post", $event->objecttable); |
200 | $this->assertEquals($record, $event->get_record_snapshot("post", $blog->id)); | |
201 | $this->assertSame('blog_entry_deleted', $event->get_legacy_eventname()); | |
77037e27 AA |
202 | $arr = array(SITEID, 'blog', 'delete', 'index.php?userid=' . $user->id, 'deleted blog entry with entry id# '. $blog->id); |
203 | $this->assertEventLegacyLogData($arr, $event); | |
3049780a | 204 | } |
814a96ed | 205 | } |