MDL-29478 unittest - explicity include blog/lib.php. Was causing "random" errors
[moodle.git] / blog / simpletest / testbloglib.php
CommitLineData
cae83708 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18
19/**
20 * Unit tests for blog
21 *
22 * @package moodlecore
23 * @subpackage blog
24 * @copyright 2009 Nicolas Connault
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28require_once($CFG->dirroot . '/blog/locallib.php');
5169a039 29require_once($CFG->dirroot . '/blog/lib.php');
cae83708 30
3f296828
EL
31/**
32 * Test functions that rely on the DB tables
33 */
cae83708 34class bloglib_test extends UnitTestCaseUsingDatabase {
35
5169a039 36 public static $includecoverage = array('blog/locallib.php', 'blog/lib.php');
cae83708 37
3f296828
EL
38 private $courseid; // To store important ids to be used in tests
39 private $groupid;
e67c213c
SM
40 private $userid;
41 private $tagid;
3f296828
EL
42
43 public function setUp() {
44 parent::setUp();
e67c213c 45 $this->create_test_tables(array('course', 'groups', 'context', 'user', 'modules', 'course_modules', 'post', 'tag'), 'lib');
3f296828
EL
46 $this->switch_to_test_db();
47
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);
55
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);
61
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);
71 }
72
e67c213c
SM
73 // Create default user
74 $user = new stdClass();
75 $user->username = 'testuser';
76 $user->confirmed = 1;
77 $user->firstname = 'Jimmy';
78 $user->lastname = 'Kinnon';
79 $user->id = $this->testdb->insert_record('user', $user);
80
81 // Create default tag
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);
88
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);
95
3f296828
EL
96 // Grab important ids
97 $this->courseid = $course->id;
98 $this->groupid = $group->id;
e67c213c
SM
99 $this->userid = $user->id;
100 $this->tagid = $tag->id;
3f296828
EL
101 }
102
103 public function tearDown() {
104 parent::tearDown();
105 }
106
107
cae83708 108 public function test_overrides() {
1c7b8b93 109
cae83708 110 // Try all the filters at once: Only the entry filter is active
3f296828
EL
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);
cae83708 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));
121
122 // Again, but without the entry filter: This time, the tag, user and module filters are active
3f296828
EL
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);
cae83708 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));
132
133 // We should get the same result by removing the 3 inactive filters: site, course and group:
3f296828
EL
134 $filters = array('module' => 1, 'user' => 1, 'tag' => 1);
135 $blog_listing = new blog_listing($filters);
cae83708 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));
142
cae83708 143 }
144
e67c213c
SM
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.
147
cae83708 148 public function test_blog_get_headers_case_1() {
149 global $CFG, $PAGE, $OUTPUT;
cae83708 150 $blog_headers = blog_get_headers();
e67c213c
SM
151 $this->assertEqual($blog_headers['heading'], get_string('siteblog', 'blog', 'ANON'));
152 }
cae83708 153
e67c213c
SM
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'], '');
158 }
159
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'], '');
164 }
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'], '');
cae83708 169 }
170}