MDL-45890 Blog: add additional events
[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
02ce2e41
SB
26defined('MOODLE_INTERNAL') || die();
27
814a96ed
PS
28global $CFG;
29require_once($CFG->dirroot . '/blog/locallib.php');
30require_once($CFG->dirroot . '/blog/lib.php');
31
814a96ed
PS
32/**
33 * Test functions that rely on the DB tables
34 */
bef0d6b0 35class core_blog_lib_testcase extends advanced_testcase {
814a96ed 36
2b6e53e8 37 private $courseid;
714f3998 38 private $cmid;
814a96ed
PS
39 private $groupid;
40 private $userid;
41 private $tagid;
714f3998 42 private $postid;
814a96ed
PS
43
44 protected function setUp() {
45 global $DB;
46 parent::setUp();
47
714f3998 48 $this->resetAfterTest();
814a96ed 49
2b6e53e8 50 // Create default course.
0e32a565 51 $course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
714f3998 52 $this->assertNotEmpty($course);
0e32a565 53 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
714f3998 54 $this->assertNotEmpty($page);
814a96ed 55
2b6e53e8 56 // Create default group.
814a96ed
PS
57 $group = new stdClass();
58 $group->courseid = $course->id;
59 $group->name = 'ANON';
60 $group->id = $DB->insert_record('groups', $group);
61
2b6e53e8 62 // Create default user.
f0899a25
AG
63 $user = $this->getDataGenerator()->create_user(array(
64 'username' => 'testuser',
65 'firstname' => 'Jimmy',
66 'lastname' => 'Kinnon'
67 ));
814a96ed 68
2b6e53e8 69 // Create default tag.
abea2c5d 70 $tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id,
e11d7380 71 'rawname' => 'Testtagname', 'isstandard' => 1));
814a96ed 72
2b6e53e8 73 // Create default post.
814a96ed
PS
74 $post = new stdClass();
75 $post->userid = $user->id;
76 $post->groupid = $group->id;
77 $post->content = 'test post content text';
abea2c5d 78 $post->module = 'blog';
814a96ed
PS
79 $post->id = $DB->insert_record('post', $post);
80
2b6e53e8 81 // Grab important ids.
814a96ed 82 $this->courseid = $course->id;
714f3998 83 $this->cmid = $page->cmid;
814a96ed
PS
84 $this->groupid = $group->id;
85 $this->userid = $user->id;
86 $this->tagid = $tag->id;
714f3998 87 $this->postid = $post->id;
814a96ed
PS
88 }
89
90
91 public function test_overrides() {
714f3998 92 global $SITE;
814a96ed 93
2b6e53e8 94 // Try all the filters at once: Only the entry filter is active.
714f3998
PS
95 $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
96 'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->tagid, 'entry' => $this->postid);
0e32a565
AD
97 $bloglisting = new blog_listing($filters);
98 $this->assertFalse(array_key_exists('site', $bloglisting->filters));
99 $this->assertFalse(array_key_exists('course', $bloglisting->filters));
100 $this->assertFalse(array_key_exists('module', $bloglisting->filters));
101 $this->assertFalse(array_key_exists('group', $bloglisting->filters));
102 $this->assertFalse(array_key_exists('user', $bloglisting->filters));
103 $this->assertFalse(array_key_exists('tag', $bloglisting->filters));
104 $this->assertTrue(array_key_exists('entry', $bloglisting->filters));
814a96ed 105
2b6e53e8 106 // Again, but without the entry filter: This time, the tag, user and module filters are active.
714f3998
PS
107 $filters = array('site' => $SITE->id, 'course' => $this->courseid, 'module' => $this->cmid,
108 'group' => $this->groupid, 'user' => $this->userid, 'tag' => $this->postid);
0e32a565
AD
109 $bloglisting = new blog_listing($filters);
110 $this->assertFalse(array_key_exists('site', $bloglisting->filters));
111 $this->assertFalse(array_key_exists('course', $bloglisting->filters));
112 $this->assertFalse(array_key_exists('group', $bloglisting->filters));
113 $this->assertTrue(array_key_exists('module', $bloglisting->filters));
114 $this->assertTrue(array_key_exists('user', $bloglisting->filters));
115 $this->assertTrue(array_key_exists('tag', $bloglisting->filters));
814a96ed 116
2b6e53e8 117 // We should get the same result by removing the 3 inactive filters: site, course and group.
714f3998 118 $filters = array('module' => $this->cmid, 'user' => $this->userid, 'tag' => $this->tagid);
0e32a565
AD
119 $bloglisting = new blog_listing($filters);
120 $this->assertFalse(array_key_exists('site', $bloglisting->filters));
121 $this->assertFalse(array_key_exists('course', $bloglisting->filters));
122 $this->assertFalse(array_key_exists('group', $bloglisting->filters));
123 $this->assertTrue(array_key_exists('module', $bloglisting->filters));
124 $this->assertTrue(array_key_exists('user', $bloglisting->filters));
125 $this->assertTrue(array_key_exists('tag', $bloglisting->filters));
814a96ed
PS
126
127 }
128
129 // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php.
130 // Some cases are omitted due to the optional_param variables used.
131
132 public function test_blog_get_headers_case_1() {
133 global $CFG, $PAGE, $OUTPUT;
2b6e53e8 134 $blogheaders = blog_get_headers();
4e1f6047 135 $this->assertEquals($blogheaders['heading'], get_string('siteblogheading', 'blog'));
814a96ed
PS
136 }
137
138 public function test_blog_get_headers_case_6() {
139 global $CFG, $PAGE, $OUTPUT;
2b6e53e8
AD
140 $blogheaders = blog_get_headers($this->courseid, null, $this->userid);
141 $this->assertNotEquals($blogheaders['heading'], '');
814a96ed
PS
142 }
143
144 public function test_blog_get_headers_case_7() {
145 global $CFG, $PAGE, $OUTPUT;
2b6e53e8
AD
146 $blogheaders = blog_get_headers(null, $this->groupid);
147 $this->assertNotEquals($blogheaders['heading'], '');
814a96ed 148 }
714f3998 149
814a96ed
PS
150 public function test_blog_get_headers_case_10() {
151 global $CFG, $PAGE, $OUTPUT;
2b6e53e8
AD
152 $blogheaders = blog_get_headers($this->courseid);
153 $this->assertNotEquals($blogheaders['heading'], '');
814a96ed 154 }
3049780a 155
bef0d6b0 156 /**
be5e0110 157 * Tests the core_blog_myprofile_navigation() function.
bef0d6b0
AA
158 */
159 public function test_core_blog_myprofile_navigation() {
160 global $USER;
161
be5e0110
MN
162 // Set up the test.
163 $tree = new \core_user\output\myprofile\tree();
164 $this->setAdminUser();
165 $iscurrentuser = true;
bef0d6b0 166 $course = null;
be5e0110
MN
167
168 // Enable blogs.
169 set_config('enableblogs', true);
170
171 // Check the node tree is correct.
172 core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
173 $reflector = new ReflectionObject($tree);
174 $nodes = $reflector->getProperty('nodes');
175 $nodes->setAccessible(true);
176 $this->assertArrayHasKey('blogs', $nodes->getValue($tree));
177 }
178
179 /**
180 * Tests the core_blog_myprofile_navigation() function as a guest.
181 */
182 public function test_core_blog_myprofile_navigation_as_guest() {
183 global $USER;
184
185 // Set up the test.
186 $tree = new \core_user\output\myprofile\tree();
bef0d6b0 187 $iscurrentuser = false;
be5e0110
MN
188 $course = null;
189
190 // Set user as guest.
191 $this->setGuestUser();
192
193 // Check the node tree is correct.
bef0d6b0 194 core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
be5e0110
MN
195 $reflector = new ReflectionObject($tree);
196 $nodes = $reflector->getProperty('nodes');
197 $nodes->setAccessible(true);
198 $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
199 }
bef0d6b0 200
be5e0110
MN
201 /**
202 * Tests the core_blog_myprofile_navigation() function when blogs are disabled.
203 */
204 public function test_core_blog_myprofile_navigation_blogs_disabled() {
205 global $USER;
206
207 // Set up the test.
208 $tree = new \core_user\output\myprofile\tree();
bef0d6b0 209 $this->setAdminUser();
be5e0110
MN
210 $iscurrentuser = false;
211 $course = null;
212
213 // Disable blogs.
bef0d6b0 214 set_config('enableblogs', false);
bef0d6b0 215
be5e0110 216 // Check the node tree is correct.
bef0d6b0 217 core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
be5e0110
MN
218 $reflector = new ReflectionObject($tree);
219 $nodes = $reflector->getProperty('nodes');
220 $nodes->setAccessible(true);
221 $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
bef0d6b0 222 }
abea2c5d
MG
223
224 public function test_blog_get_listing_course() {
225 $this->setAdminUser();
226 $coursecontext = context_course::instance($this->courseid);
227 $anothercourse = $this->getDataGenerator()->create_course();
228
229 // Add blog associations with a course.
230 $blog = new blog_entry($this->postid);
231 $blog->add_association($coursecontext->id);
232
233 // There is one entry associated with a course.
234 $bloglisting = new blog_listing(array('course' => $this->courseid));
235 $this->assertCount(1, $bloglisting->get_entries());
236
237 // There is no entry associated with a wrong course.
238 $bloglisting = new blog_listing(array('course' => $anothercourse->id));
239 $this->assertCount(0, $bloglisting->get_entries());
240
241 // There is no entry associated with a module.
242 $bloglisting = new blog_listing(array('module' => $this->cmid));
243 $this->assertCount(0, $bloglisting->get_entries());
244
245 // There is one entry associated with a site (id is ignored).
246 $bloglisting = new blog_listing(array('site' => 12345));
247 $this->assertCount(1, $bloglisting->get_entries());
248
249 // There is one entry associated with course context.
250 $bloglisting = new blog_listing(array('context' => $coursecontext->id));
251 $this->assertCount(1, $bloglisting->get_entries());
252 }
253
254 public function test_blog_get_listing_module() {
255 $this->setAdminUser();
256 $coursecontext = context_course::instance($this->courseid);
257 $contextmodule = context_module::instance($this->cmid);
258 $anothermodule = $this->getDataGenerator()->create_module('page', array('course' => $this->courseid));
259
260 // Add blog associations with a course.
261 $blog = new blog_entry($this->postid);
262 $blog->add_association($contextmodule->id);
263
264 // There is no entry associated with a course.
265 $bloglisting = new blog_listing(array('course' => $this->courseid));
266 $this->assertCount(0, $bloglisting->get_entries());
267
268 // There is one entry associated with a module.
269 $bloglisting = new blog_listing(array('module' => $this->cmid));
270 $this->assertCount(1, $bloglisting->get_entries());
271
272 // There is no entry associated with a wrong module.
273 $bloglisting = new blog_listing(array('module' => $anothermodule->cmid));
274 $this->assertCount(0, $bloglisting->get_entries());
275
276 // There is one entry associated with a site (id is ignored).
277 $bloglisting = new blog_listing(array('site' => 12345));
278 $this->assertCount(1, $bloglisting->get_entries());
279
280 // There is one entry associated with course context (module is a subcontext of a course).
281 $bloglisting = new blog_listing(array('context' => $coursecontext->id));
282 $this->assertCount(1, $bloglisting->get_entries());
283 }
814a96ed 284}
32dea439 285