Unit tests MDL-24975 Completion unit tests fix
[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;
39
40 public function setUp() {
41 parent::setUp();
42 $this->create_test_tables(array('course', 'groups', 'context'), 'lib');
43 $this->switch_to_test_db();
44
45 // Create default course
46 $course = new stdClass();
47 $course->category = 1;
48 $course->fullname = 'Anonymous test course';
49 $course->shortname = 'ANON';
50 $course->summary = '';
51 $course->id = $this->testdb->insert_record('course', $course);
52
53 // Create default group
54 $group = new stdClass();
55 $group->courseid = $course->id;
56 $group->name = 'ANON';
57 $group->id = $this->testdb->insert_record('groups', $group);
58
59 // Create required contexts
60 $contexts = array(CONTEXT_SYSTEM => 1, CONTEXT_COURSE => $course->id, CONTEXT_MODULE => 1);
61 foreach ($contexts as $level => $instance) {
62 $context = new stdClass;
63 $context->contextlevel = $level;
64 $context->instanceid = $instance;
65 $context->path = 'not initialised';
66 $context->depth = '13';
67 $this->testdb->insert_record('context', $context);
68 }
69
70 // Grab important ids
71 $this->courseid = $course->id;
72 $this->groupid = $group->id;
73 }
74
75 public function tearDown() {
76 parent::tearDown();
77 }
78
79
cae83708 80 public function test_overrides() {
1c7b8b93 81
cae83708 82 // Try all the filters at once: Only the entry filter is active
3f296828
EL
83 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1,
84 'group' => $this->groupid, 'user' => 1, 'tag' => 1, 'entry' => 1);
85 $blog_listing = new blog_listing($filters);
cae83708 86 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
87 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
88 $this->assertFalse(array_key_exists('module', $blog_listing->filters));
89 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
90 $this->assertFalse(array_key_exists('user', $blog_listing->filters));
91 $this->assertFalse(array_key_exists('tag', $blog_listing->filters));
92 $this->assertTrue(array_key_exists('entry', $blog_listing->filters));
93
94 // Again, but without the entry filter: This time, the tag, user and module filters are active
3f296828
EL
95 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1,
96 'group' => $this->groupid, 'user' => 1, 'tag' => 1);
97 $blog_listing = new blog_listing($filters);
cae83708 98 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
99 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
100 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
101 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
102 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
103 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
104
105 // We should get the same result by removing the 3 inactive filters: site, course and group:
3f296828
EL
106 $filters = array('module' => 1, 'user' => 1, 'tag' => 1);
107 $blog_listing = new blog_listing($filters);
cae83708 108 $this->assertFalse(array_key_exists('site', $blog_listing->filters));
109 $this->assertFalse(array_key_exists('course', $blog_listing->filters));
110 $this->assertFalse(array_key_exists('group', $blog_listing->filters));
111 $this->assertTrue(array_key_exists('module', $blog_listing->filters));
112 $this->assertTrue(array_key_exists('user', $blog_listing->filters));
113 $this->assertTrue(array_key_exists('tag', $blog_listing->filters));
114
cae83708 115 }
116
117 /**
118 * Some user, course, module, group and blog sample data needs to be setup for this test
119 */
120 public function test_blog_get_headers_case_1() {
121 global $CFG, $PAGE, $OUTPUT;
a6855934 122 $PAGE->url = new moodle_url('/blog/index.php', array('entryid' => 1));
cae83708 123 $blog_headers = blog_get_headers();
124
125 $this->assertEqual($blog_headers['title'], '');
126 }
127}