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
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
. '/blog/locallib.php');
30 require_once($CFG->dirroot
. '/blog/lib.php');
33 * Test functions that rely on the DB tables
35 class core_blog_lib_testcase
extends advanced_testcase
{
44 protected function setUp() {
48 $this->resetAfterTest();
50 // Create default course.
51 $course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
52 $this->assertNotEmpty($course);
53 $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id
));
54 $this->assertNotEmpty($page);
56 // Create default group.
57 $group = new stdClass();
58 $group->courseid
= $course->id
;
59 $group->name
= 'ANON';
60 $group->id
= $DB->insert_record('groups', $group);
62 // Create default user.
63 $user = $this->getDataGenerator()->create_user(array(
64 'username' => 'testuser',
65 'firstname' => 'Jimmy',
66 'lastname' => 'Kinnon'
69 // Create default tag.
70 $tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id
,
71 'rawname' => 'Testtagname', 'isstandard' => 1));
73 // Create default post.
74 $post = new stdClass();
75 $post->userid
= $user->id
;
76 $post->groupid
= $group->id
;
77 $post->content
= 'test post content text';
78 $post->module
= 'blog';
79 $post->id
= $DB->insert_record('post', $post);
81 // Grab important ids.
82 $this->courseid
= $course->id
;
83 $this->cmid
= $page->cmid
;
84 $this->groupid
= $group->id
;
85 $this->userid
= $user->id
;
86 $this->tagid
= $tag->id
;
87 $this->postid
= $post->id
;
91 public function test_overrides() {
94 // Try all the filters at once: Only the entry filter is active.
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
);
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
));
106 // Again, but without the entry filter: This time, the tag, user and module filters are active.
107 $filters = array('site' => $SITE->id
, 'course' => $this->courseid
, 'module' => $this->cmid
,
108 'group' => $this->groupid
, 'user' => $this->userid
, 'tag' => $this->postid
);
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
));
117 // We should get the same result by removing the 3 inactive filters: site, course and group.
118 $filters = array('module' => $this->cmid
, 'user' => $this->userid
, 'tag' => $this->tagid
);
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
));
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.
132 public function test_blog_get_headers_case_1() {
133 global $CFG, $PAGE, $OUTPUT;
134 $blogheaders = blog_get_headers();
135 $this->assertEquals($blogheaders['heading'], get_string('siteblogheading', 'blog'));
138 public function test_blog_get_headers_case_6() {
139 global $CFG, $PAGE, $OUTPUT;
140 $blogheaders = blog_get_headers($this->courseid
, null, $this->userid
);
141 $this->assertNotEquals($blogheaders['heading'], '');
144 public function test_blog_get_headers_case_7() {
145 global $CFG, $PAGE, $OUTPUT;
146 $blogheaders = blog_get_headers(null, $this->groupid
);
147 $this->assertNotEquals($blogheaders['heading'], '');
150 public function test_blog_get_headers_case_10() {
151 global $CFG, $PAGE, $OUTPUT;
152 $blogheaders = blog_get_headers($this->courseid
);
153 $this->assertNotEquals($blogheaders['heading'], '');
157 * Tests the core_blog_myprofile_navigation() function.
159 public function test_core_blog_myprofile_navigation() {
163 $tree = new \core_user\output\myprofile\tree
();
164 $this->setAdminUser();
165 $iscurrentuser = true;
169 set_config('enableblogs', true);
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));
180 * Tests the core_blog_myprofile_navigation() function as a guest.
182 public function test_core_blog_myprofile_navigation_as_guest() {
186 $tree = new \core_user\output\myprofile\tree
();
187 $iscurrentuser = false;
190 // Set user as guest.
191 $this->setGuestUser();
193 // Check the node tree is correct.
194 core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
195 $reflector = new ReflectionObject($tree);
196 $nodes = $reflector->getProperty('nodes');
197 $nodes->setAccessible(true);
198 $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
202 * Tests the core_blog_myprofile_navigation() function when blogs are disabled.
204 public function test_core_blog_myprofile_navigation_blogs_disabled() {
208 $tree = new \core_user\output\myprofile\tree
();
209 $this->setAdminUser();
210 $iscurrentuser = false;
214 set_config('enableblogs', false);
216 // Check the node tree is correct.
217 core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
218 $reflector = new ReflectionObject($tree);
219 $nodes = $reflector->getProperty('nodes');
220 $nodes->setAccessible(true);
221 $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
224 public function test_blog_get_listing_course() {
225 $this->setAdminUser();
226 $coursecontext = context_course
::instance($this->courseid
);
227 $anothercourse = $this->getDataGenerator()->create_course();
229 // Add blog associations with a course.
230 $blog = new blog_entry($this->postid
);
231 $blog->add_association($coursecontext->id
);
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());
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());
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());
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());
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());
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
));
260 // Add blog associations with a course.
261 $blog = new blog_entry($this->postid
);
262 $blog->add_association($contextmodule->id
);
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());
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());
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());
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());
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());