Unit tests MDL-24907 Fix blog unit tests
[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');
29
3f296828
EL
30/**
31 * Test functions that rely on the DB tables
32 */
cae83708 33class bloglib_test extends UnitTestCaseUsingDatabase {
34
35 public static $includecoverage = array('blog/locallib.php');
36
3f296828
EL
37 private $courseid; // To store important ids to be used in tests
38 private $groupid;
e67c213c
SM
39 private $userid;
40 private $tagid;
3f296828
EL
41
42 public function setUp() {
43 parent::setUp();
e67c213c 44 $this->create_test_tables(array('course', 'groups', 'context', 'user', 'modules', 'course_modules', 'post', 'tag'), 'lib');
3f296828
EL
45 $this->switch_to_test_db();
46
47 // Create default course
48 $course = new stdClass();
49 $course->category = 1;
50 $course->fullname = 'Anonymous test course';
51 $course->shortname = 'ANON';
52 $course->summary = '';
53 $course->id = $this->testdb->insert_record('course', $course);
54
55 // Create default group
56 $group = new stdClass();
57 $group->courseid = $course->id;
58 $group->name = 'ANON';
59 $group->id = $this->testdb->insert_record('groups', $group);
60
61 // Create required contexts
62 $contexts = array(CONTEXT_SYSTEM => 1, CONTEXT_COURSE => $course->id, CONTEXT_MODULE => 1);
63 foreach ($contexts as $level => $instance) {
64 $context = new stdClass;
65 $context->contextlevel = $level;
66 $context->instanceid = $instance;
67 $context->path = 'not initialised';
68 $context->depth = '13';
69 $this->testdb->insert_record('context', $context);
70 }
71
e67c213c
SM
72 // Create default user
73 $user = new stdClass();
74 $user->username = 'testuser';
75 $user->confirmed = 1;
76 $user->firstname = 'Jimmy';
77 $user->lastname = 'Kinnon';
78 $user->id = $this->testdb->insert_record('user', $user);
79
80 // Create default tag
81 $tag = new stdClass();
82 $tag->userid = $user->id;
83 $tag->name = 'testtagname';
84 $tag->rawname = 'Testtagname';
85 $tag->tagtype = 'official';
86 $tag->id = $this->testdb->insert_record('tag', $tag);
87
88 // Create default post
89 $post = new stdClass();
90 $post->userid = $user->id;
91 $post->groupid = $group->id;
92 $post->content = 'test post content text';
93 $post->id = $this->testdb->insert_record('post', $post);
94
3f296828
EL
95 // Grab important ids
96 $this->courseid = $course->id;
97 $this->groupid = $group->id;
e67c213c
SM
98 $this->userid = $user->id;
99 $this->tagid = $tag->id;
3f296828
EL
100 }
101
102 public function tearDown() {
103 parent::tearDown();
104 }
105
106
cae83708 107 public function test_overrides() {
1c7b8b93 108
cae83708 109 // Try all the filters at once: Only the entry filter is active
3f296828
EL
110 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1,
111 'group' => $this->groupid, 'user' => 1, 'tag' => 1, 'entry' => 1);
112 $blog_listing = new blog_listing($filters);
cae83708 113 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
114 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
115 $this->assertFalse(array_key_exists('module', $blog_listing->filters));
116 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
117 $this->assertFalse(array_key_exists('user', $blog_listing->filters));
118 $this->assertFalse(array_key_exists('tag', $blog_listing->filters));
119 $this->assertTrue(array_key_exists('entry', $blog_listing->filters));
120
121 // Again, but without the entry filter: This time, the tag, user and module filters are active
3f296828
EL
122 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1,
123 'group' => $this->groupid, 'user' => 1, 'tag' => 1);
124 $blog_listing = new blog_listing($filters);
cae83708 125 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
126 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
127 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
128 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
129 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
130 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
131
132 // We should get the same result by removing the 3 inactive filters: site, course and group:
3f296828
EL
133 $filters = array('module' => 1, 'user' => 1, 'tag' => 1);
134 $blog_listing = new blog_listing($filters);
cae83708 135 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
136 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
137 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
138 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
139 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
140 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
141
cae83708 142 }
143
e67c213c
SM
144 // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php.
145 // Some cases are omitted due to the optional_param variables used.
146
cae83708 147 public function test_blog_get_headers_case_1() {
148 global $CFG, $PAGE, $OUTPUT;
cae83708 149 $blog_headers = blog_get_headers();
e67c213c
SM
150 $this->assertEqual($blog_headers['heading'], get_string('siteblog', 'blog', 'ANON'));
151 }
cae83708 152
e67c213c
SM
153 public function test_blog_get_headers_case_6() {
154 global $CFG, $PAGE, $OUTPUT;
155 $blog_headers = blog_get_headers($this->courseid, NULL, $this->userid);
156 $this->assertNotEqual($blog_headers['heading'], '');
157 }
158
159 public function test_blog_get_headers_case_7() {
160 global $CFG, $PAGE, $OUTPUT;
161 $blog_headers = blog_get_headers(NULL, 1);
162 $this->assertNotEqual($blog_headers['heading'], '');
163 }
164 public function test_blog_get_headers_case_10() {
165 global $CFG, $PAGE, $OUTPUT;
166 $blog_headers = blog_get_headers($this->courseid);
167 $this->assertNotEqual($blog_headers['heading'], '');
cae83708 168 }
169}