3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 require_once($CFG->dirroot . '/blog/locallib.php');
29 require_once($CFG->dirroot . '/blog/lib.php');
32 * Test functions that rely on the DB tables
34 class bloglib_test extends UnitTestCaseUsingDatabase {
36 public static $includecoverage = array('blog/locallib.php', 'blog/lib.php');
38 private $courseid; // To store important ids to be used in tests
43 public function setUp() {
45 $this->create_test_tables(array('course', 'groups', 'context', 'user', 'modules', 'course_modules', 'post', 'tag'), 'lib');
46 $this->switch_to_test_db();
48 // Create default course
49 $course = new stdClass();
50 $course->category = 1;
51 $course->fullname = 'Anonymous test course';
52 $course->shortname = 'ANON';
53 $course->summary = '';
54 $course->id = $this->testdb->insert_record('course', $course);
56 // Create default group
57 $group = new stdClass();
58 $group->courseid = $course->id;
59 $group->name = 'ANON';
60 $group->id = $this->testdb->insert_record('groups', $group);
62 // Create required contexts
63 $contexts = array(CONTEXT_SYSTEM => 1, CONTEXT_COURSE => $course->id, CONTEXT_MODULE => 1);
64 foreach ($contexts as $level => $instance) {
65 $context = new stdClass;
66 $context->contextlevel = $level;
67 $context->instanceid = $instance;
68 $context->path = 'not initialised';
69 $context->depth = '13';
70 $this->testdb->insert_record('context', $context);
73 // Create default user
74 $user = new stdClass();
75 $user->username = 'testuser';
77 $user->firstname = 'Jimmy';
78 $user->lastname = 'Kinnon';
79 $user->id = $this->testdb->insert_record('user', $user);
82 $tag = new stdClass();
83 $tag->userid = $user->id;
84 $tag->name = 'testtagname';
85 $tag->rawname = 'Testtagname';
86 $tag->tagtype = 'official';
87 $tag->id = $this->testdb->insert_record('tag', $tag);
89 // Create default post
90 $post = new stdClass();
91 $post->userid = $user->id;
92 $post->groupid = $group->id;
93 $post->content = 'test post content text';
94 $post->id = $this->testdb->insert_record('post', $post);
97 $this->courseid = $course->id;
98 $this->groupid = $group->id;
99 $this->userid = $user->id;
100 $this->tagid = $tag->id;
103 public function tearDown() {
108 public function test_overrides() {
110 // Try all the filters at once: Only the entry filter is active
111 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1,
112 'group' => $this->groupid, 'user' => 1, 'tag' => 1, 'entry' => 1);
113 $blog_listing = new blog_listing($filters);
114 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
115 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
116 $this->assertFalse(array_key_exists('module', $blog_listing->filters));
117 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
118 $this->assertFalse(array_key_exists('user', $blog_listing->filters));
119 $this->assertFalse(array_key_exists('tag', $blog_listing->filters));
120 $this->assertTrue(array_key_exists('entry', $blog_listing->filters));
122 // Again, but without the entry filter: This time, the tag, user and module filters are active
123 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1,
124 'group' => $this->groupid, 'user' => 1, 'tag' => 1);
125 $blog_listing = new blog_listing($filters);
126 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
127 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
128 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
129 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
130 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
131 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
133 // We should get the same result by removing the 3 inactive filters: site, course and group:
134 $filters = array('module' => 1, 'user' => 1, 'tag' => 1);
135 $blog_listing = new blog_listing($filters);
136 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
137 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
138 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
139 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
140 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
141 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
145 // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php.
146 // Some cases are omitted due to the optional_param variables used.
148 public function test_blog_get_headers_case_1() {
149 global $CFG, $PAGE, $OUTPUT;
150 $blog_headers = blog_get_headers();
151 $this->assertEqual($blog_headers['heading'], get_string('siteblog', 'blog', 'ANON'));
154 public function test_blog_get_headers_case_6() {
155 global $CFG, $PAGE, $OUTPUT;
156 $blog_headers = blog_get_headers($this->courseid, NULL, $this->userid);
157 $this->assertNotEqual($blog_headers['heading'], '');
160 public function test_blog_get_headers_case_7() {
161 global $CFG, $PAGE, $OUTPUT;
162 $blog_headers = blog_get_headers(NULL, 1);
163 $this->assertNotEqual($blog_headers['heading'], '');
165 public function test_blog_get_headers_case_10() {
166 global $CFG, $PAGE, $OUTPUT;
167 $blog_headers = blog_get_headers($this->courseid);
168 $this->assertNotEqual($blog_headers['heading'], '');