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 namespace tool_brickfield; 18 19 use tool_brickfield\local\tool\filter; 20 21 /** 22 * Unit tests for {@filter tool_brickfield\local\tool\filter}. 23 * 24 * @package tool_brickfield 25 * @copyright 2020 onward: Brickfield Education Labs, www.brickfield.ie 26 * @author Jay Churchward (jay.churchward@poetopensource.org) 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 28 */ 29 class filters_test extends \advanced_testcase { 30 public function test_constructor() { 31 $this->resetAfterTest(); 32 33 // Variables. 34 $courseid = 1; 35 $categoryid = 2; 36 $tab = 'tab'; 37 $page = 3; 38 $perpage = 4; 39 $url = 'url'; 40 41 // Test responses. 42 $object = new filter(); 43 $this->assertEquals($object->courseid, 0); 44 $this->assertEquals($object->categoryid, 0); 45 $this->assertEquals($object->tab, ''); 46 $this->assertEquals($object->page, 0); 47 $this->assertEquals($object->perpage, 0); 48 $this->assertEquals($object->url, ''); 49 50 $object = new filter($courseid, $categoryid, $tab, $page, $perpage, $url); 51 $this->assertEquals($object->courseid, $courseid); 52 $this->assertEquals($object->categoryid, $categoryid); 53 $this->assertEquals($object->tab, $tab); 54 $this->assertEquals($object->page, $page); 55 $this->assertEquals($object->perpage, $perpage); 56 $this->assertEquals($object->url, $url); 57 } 58 59 public function test_get_course_sql() { 60 $this->resetAfterTest(); 61 $object = new filter(); 62 63 $output = $object->get_course_sql(); 64 65 $this->assertIsArray($output); 66 $this->assertEquals($output[0], ''); 67 68 $object = $this->create_object_with_params(); 69 $output = $object->get_course_sql(); 70 71 $this->assertEquals($output[0], ' AND (courseid = ?)'); 72 $this->assertEquals($output[1][0], $object->courseid); 73 74 } 75 76 public function test_validate_filters() { 77 $this->resetAfterTest(); 78 // Variables. 79 $courseid = 0; 80 $categoryid = 2; 81 $tab = 'tab'; 82 $page = 3; 83 $perpage = 4; 84 $url = 'url'; 85 $object = new filter(); 86 87 $output = $object->validate_filters(); 88 $this->assertTrue($output); 89 90 $object = $this->create_object(); 91 $output = $object->validate_filters(); 92 $this->assertTrue($output); 93 94 $object = new filter($courseid, $categoryid, $tab, $page, $perpage); 95 $output = $object->validate_filters(); 96 $this->assertFalse($output); 97 98 $category = $this->getDataGenerator()->create_category(); 99 100 $object = new filter($courseid, $category->id, $tab, $page, $perpage); 101 $output = $object->validate_filters(); 102 $this->assertFalse($output); 103 } 104 105 public function test_has_course_filters() { 106 $this->resetAfterTest(); 107 108 $object = new filter(); 109 $output = $object->has_course_filters(); 110 $this->assertFalse($output); 111 112 $object = $this->create_object(); 113 $output = $object->has_course_filters(); 114 $this->assertTrue($output); 115 } 116 117 public function test_has_capability_in_context() { 118 global $DB; 119 120 $this->resetAfterTest(); 121 122 $object = $this->create_object_with_params(); 123 $capability = accessibility::get_capability_name('viewcoursetools'); 124 $output = $object->has_capability_in_context($capability, \context_system::instance()); 125 $this->assertFalse($output); 126 127 $output = $object->has_capability_in_context($capability, \context_coursecat::instance($object->categoryid)); 128 $this->assertFalse($output); 129 130 $output = $object->has_capability_in_context($capability, \context_course::instance($object->courseid)); 131 $this->assertFalse($output); 132 133 $course = $this->getDataGenerator()->create_course(); 134 $user = $this->getDataGenerator()->create_and_enrol($course, 'teacher'); 135 $this->setUser($user); 136 137 $output = $object->has_capability_in_context($capability, \context_system::instance()); 138 $this->assertFalse($output); 139 140 $output = $object->has_capability_in_context($capability, \context_coursecat::instance($object->categoryid)); 141 $this->assertFalse($output); 142 143 $output = $object->has_capability_in_context($capability, \context_course::instance($course->id)); 144 $this->assertTrue($output); 145 146 $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']); 147 $categorycontext = \context_coursecat::instance($object->categoryid); 148 $this->getDataGenerator()->role_assign($teacherrole->id, $user->id, $categorycontext->id); 149 150 $output = $object->has_capability_in_context($capability, $categorycontext); 151 $this->assertTrue($output); 152 } 153 154 public function test_get_errormessage() { 155 $this->resetAfterTest(); 156 // Variables. 157 $courseid = 0; 158 $categoryid = 2; 159 $tab = 'tab'; 160 $page = 3; 161 $perpage = 4; 162 $url = 'url'; 163 164 $object = new filter(); 165 $output = $object->get_errormessage(); 166 $this->assertNull($output); 167 168 $object = new filter($courseid, $categoryid, $tab, $page, $perpage); 169 $object->validate_filters(); 170 $output = $object->get_errormessage(); 171 $this->assertEquals($output, 'Invalid category, please check your input'); 172 173 $category = $this->getDataGenerator()->create_category(); 174 $object = new filter($courseid, $category->id, $tab, $page, $perpage); 175 $object->validate_filters(); 176 $output = $object->get_errormessage(); 177 $this->assertEquals($output, 'No courses found for category ' . $category->id); 178 } 179 180 /** 181 * Create a filter object and return it. 182 * @return filter 183 */ 184 private function create_object() { 185 // Variables. 186 $courseid = 1; 187 $categoryid = 2; 188 $tab = 'tab'; 189 $page = 3; 190 $perpage = 4; 191 $url = 'url'; 192 193 $object = new filter($courseid, $categoryid, $tab, $page, $perpage); 194 195 return $object; 196 } 197 198 /** 199 * Create a filter object with some parameters and return it. 200 * @return filter 201 */ 202 private function create_object_with_params() { 203 // Variables. 204 $tab = 'tab'; 205 $page = 3; 206 $perpage = 4; 207 $url = 'url'; 208 209 $category = $this->getDataGenerator()->create_category(); 210 $course = $this->getDataGenerator()->create_course((object)['category' => $category->id]); 211 212 $object = new filter($course->id, $category->id, $tab, $page, $perpage); 213 214 return $object; 215 } 216 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body