|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 00028 require_once($CFG->dirroot . '/blog/locallib.php'); 00029 require_once($CFG->dirroot . '/blog/lib.php'); 00030 00034 class bloglib_test extends UnitTestCaseUsingDatabase { 00035 00036 public static $includecoverage = array('blog/locallib.php', 'blog/lib.php'); 00037 00038 private $courseid; // To store important ids to be used in tests 00039 private $groupid; 00040 private $userid; 00041 private $tagid; 00042 00043 public function setUp() { 00044 parent::setUp(); 00045 $this->create_test_tables(array('course', 'groups', 'context', 'user', 'modules', 'course_modules', 'post', 'tag'), 'lib'); 00046 $this->switch_to_test_db(); 00047 00048 // Create default course 00049 $course = new stdClass(); 00050 $course->category = 1; 00051 $course->fullname = 'Anonymous test course'; 00052 $course->shortname = 'ANON'; 00053 $course->summary = ''; 00054 $course->id = $this->testdb->insert_record('course', $course); 00055 00056 // Create default group 00057 $group = new stdClass(); 00058 $group->courseid = $course->id; 00059 $group->name = 'ANON'; 00060 $group->id = $this->testdb->insert_record('groups', $group); 00061 00062 // Create required contexts 00063 $contexts = array(CONTEXT_SYSTEM => 1, CONTEXT_COURSE => $course->id, CONTEXT_MODULE => 1); 00064 foreach ($contexts as $level => $instance) { 00065 $context = new stdClass; 00066 $context->contextlevel = $level; 00067 $context->instanceid = $instance; 00068 $context->path = 'not initialised'; 00069 $context->depth = '13'; 00070 $this->testdb->insert_record('context', $context); 00071 } 00072 00073 // Create default user 00074 $user = new stdClass(); 00075 $user->username = 'testuser'; 00076 $user->confirmed = 1; 00077 $user->firstname = 'Jimmy'; 00078 $user->lastname = 'Kinnon'; 00079 $user->id = $this->testdb->insert_record('user', $user); 00080 00081 // Create default tag 00082 $tag = new stdClass(); 00083 $tag->userid = $user->id; 00084 $tag->name = 'testtagname'; 00085 $tag->rawname = 'Testtagname'; 00086 $tag->tagtype = 'official'; 00087 $tag->id = $this->testdb->insert_record('tag', $tag); 00088 00089 // Create default post 00090 $post = new stdClass(); 00091 $post->userid = $user->id; 00092 $post->groupid = $group->id; 00093 $post->content = 'test post content text'; 00094 $post->id = $this->testdb->insert_record('post', $post); 00095 00096 // Grab important ids 00097 $this->courseid = $course->id; 00098 $this->groupid = $group->id; 00099 $this->userid = $user->id; 00100 $this->tagid = $tag->id; 00101 } 00102 00103 public function tearDown() { 00104 parent::tearDown(); 00105 } 00106 00107 00108 public function test_overrides() { 00109 00110 // Try all the filters at once: Only the entry filter is active 00111 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1, 00112 'group' => $this->groupid, 'user' => 1, 'tag' => 1, 'entry' => 1); 00113 $blog_listing = new blog_listing($filters); 00114 $this->assertFalse(array_key_exists('site', $blog_listing->filters)); 00115 $this->assertFalse(array_key_exists('course', $blog_listing->filters)); 00116 $this->assertFalse(array_key_exists('module', $blog_listing->filters)); 00117 $this->assertFalse(array_key_exists('group', $blog_listing->filters)); 00118 $this->assertFalse(array_key_exists('user', $blog_listing->filters)); 00119 $this->assertFalse(array_key_exists('tag', $blog_listing->filters)); 00120 $this->assertTrue(array_key_exists('entry', $blog_listing->filters)); 00121 00122 // Again, but without the entry filter: This time, the tag, user and module filters are active 00123 $filters = array('site' => 1, 'course' => $this->courseid, 'module' => 1, 00124 'group' => $this->groupid, 'user' => 1, 'tag' => 1); 00125 $blog_listing = new blog_listing($filters); 00126 $this->assertFalse(array_key_exists('site', $blog_listing->filters)); 00127 $this->assertFalse(array_key_exists('course', $blog_listing->filters)); 00128 $this->assertFalse(array_key_exists('group', $blog_listing->filters)); 00129 $this->assertTrue(array_key_exists('module', $blog_listing->filters)); 00130 $this->assertTrue(array_key_exists('user', $blog_listing->filters)); 00131 $this->assertTrue(array_key_exists('tag', $blog_listing->filters)); 00132 00133 // We should get the same result by removing the 3 inactive filters: site, course and group: 00134 $filters = array('module' => 1, 'user' => 1, 'tag' => 1); 00135 $blog_listing = new blog_listing($filters); 00136 $this->assertFalse(array_key_exists('site', $blog_listing->filters)); 00137 $this->assertFalse(array_key_exists('course', $blog_listing->filters)); 00138 $this->assertFalse(array_key_exists('group', $blog_listing->filters)); 00139 $this->assertTrue(array_key_exists('module', $blog_listing->filters)); 00140 $this->assertTrue(array_key_exists('user', $blog_listing->filters)); 00141 $this->assertTrue(array_key_exists('tag', $blog_listing->filters)); 00142 00143 } 00144 00145 // The following series of 'test_blog..' functions correspond to the blog_get_headers() function within blog/lib.php. 00146 // Some cases are omitted due to the optional_param variables used. 00147 00148 public function test_blog_get_headers_case_1() { 00149 global $CFG, $PAGE, $OUTPUT; 00150 $blog_headers = blog_get_headers(); 00151 $this->assertEqual($blog_headers['heading'], get_string('siteblog', 'blog', 'ANON')); 00152 } 00153 00154 public function test_blog_get_headers_case_6() { 00155 global $CFG, $PAGE, $OUTPUT; 00156 $blog_headers = blog_get_headers($this->courseid, NULL, $this->userid); 00157 $this->assertNotEqual($blog_headers['heading'], ''); 00158 } 00159 00160 public function test_blog_get_headers_case_7() { 00161 global $CFG, $PAGE, $OUTPUT; 00162 $blog_headers = blog_get_headers(NULL, 1); 00163 $this->assertNotEqual($blog_headers['heading'], ''); 00164 } 00165 public function test_blog_get_headers_case_10() { 00166 global $CFG, $PAGE, $OUTPUT; 00167 $blog_headers = blog_get_headers($this->courseid); 00168 $this->assertNotEqual($blog_headers['heading'], ''); 00169 } 00170 }