MDL-32323 cache structure of all tables and reset sequences only in table where id...
[moodle.git] / blog / tests / bloglib_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
17
18/**
19 * Unit tests for blog
20 *
21 * @package core_blog
22 * @category phpunit
23 * @copyright 2009 Nicolas Connault
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27global $CFG;
28require_once($CFG->dirroot . '/blog/locallib.php');
29require_once($CFG->dirroot . '/blog/lib.php');
30
31
32/**
33 * Test functions that rely on the DB tables
34 */
4b027431 35class bloglib_testcase extends advanced_testcase {
814a96ed
PS
36
37 private $courseid; // To store important ids to be used in tests
38 private $groupid;
39 private $userid;
40 private $tagid;
41
42 protected function setUp() {
43 global $DB;
44 parent::setUp();
45
46 $this->resetAfterTest(true);
47
48 // Create default course
49 $course = $this->getDataGenerator()->create_course(array('category'=>1, 'fullname'=>'Anonymous test course', 'shortname'=>'ANON'));
50 $page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id));
51
52 // Create default group
53 $group = new stdClass();
54 $group->courseid = $course->id;
55 $group->name = 'ANON';
56 $group->id = $DB->insert_record('groups', $group);
57
58 // Create default user
59 $user = $this->getDataGenerator()->create_user(array('username'=>'testuser', 'firstname'=>'Jimmy', 'lastname'=>'Kinnon'));
60
61 // Create default tag
62 $tag = new stdClass();
63 $tag->userid = $user->id;
64 $tag->name = 'testtagname';
65 $tag->rawname = 'Testtagname';
66 $tag->tagtype = 'official';
67 $tag->id = $DB->insert_record('tag', $tag);
68
69 // Create default post
70 $post = new stdClass();
71 $post->userid = $user->id;
72 $post->groupid = $group->id;
73 $post->content = 'test post content text';
74 $post->id = $DB->insert_record('post', $post);
75
76 // Grab important ids
77 $this->courseid = $course->id;
78 $this->groupid = $group->id;
79 $this->userid = $user->id;
80 $this->tagid = $tag->id;
81 }
82
83
84 public function test_overrides() {
85
86 // Try all the filters at once: Only the entry filter is active
87 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1,
88 'group' => $this->groupid, 'user' => 1, 'tag' => 1, 'entry' => 1);
89 $blog_listing = new blog_listing($filters);
90 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
91 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
92 $this->assertFalse(array_key_exists('module', $blog_listing->filters));
93 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
94 $this->assertFalse(array_key_exists('user', $blog_listing->filters));
95 $this->assertFalse(array_key_exists('tag', $blog_listing->filters));
96 $this->assertTrue(array_key_exists('entry', $blog_listing->filters));
97
98 // Again, but without the entry filter: This time, the tag, user and module filters are active
99 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1,
100 'group' => $this->groupid, 'user' => 1, 'tag' => 1);
101 $blog_listing = new blog_listing($filters);
102 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
103 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
104 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
105 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
106 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
107 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
108
109 // We should get the same result by removing the 3 inactive filters: site, course and group:
110 $filters = array('module' => 1, 'user' => 1, 'tag' => 1);
111 $blog_listing = new blog_listing($filters);
112 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
113 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
114 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
115 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
116 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
117 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
118
119 }
120
121 // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php.
122 // Some cases are omitted due to the optional_param variables used.
123
124 public function test_blog_get_headers_case_1() {
125 global $CFG, $PAGE, $OUTPUT;
126 $blog_headers = blog_get_headers();
127 $this->assertEquals($blog_headers['heading'], get_string('siteblog', 'blog', 'phpunit'));
128 }
129
130 public function test_blog_get_headers_case_6() {
131 global $CFG, $PAGE, $OUTPUT;
132 $blog_headers = blog_get_headers($this->courseid, NULL, $this->userid);
133 $this->assertNotEquals($blog_headers['heading'], '');
134 }
135
136 public function test_blog_get_headers_case_7() {
137 global $CFG, $PAGE, $OUTPUT;
138 $blog_headers = blog_get_headers(NULL, 1);
139 $this->assertNotEquals($blog_headers['heading'], '');
140 }
141 public function test_blog_get_headers_case_10() {
142 global $CFG, $PAGE, $OUTPUT;
143 $blog_headers = blog_get_headers($this->courseid);
144 $this->assertNotEquals($blog_headers['heading'], '');
145 }
146}