See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]
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 * Unit tests for the grade condition. 19 * 20 * @package availability_grade 21 * @copyright 2014 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use availability_grade\condition; 28 29 /** 30 * Unit tests for the grade condition. 31 * 32 * @package availability_grade 33 * @copyright 2014 The Open University 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class availability_grade_condition_testcase extends advanced_testcase { 37 /** 38 * Tests constructing and using grade condition. 39 */ 40 public function test_usage() { 41 global $USER, $CFG; 42 require_once($CFG->dirroot . '/mod/assign/locallib.php'); 43 44 $this->resetAfterTest(); 45 $CFG->enableavailability = true; 46 47 // Make a test course and user. 48 $course = $this->getDataGenerator()->create_course(); 49 $user = $this->getDataGenerator()->create_user(); 50 $this->getDataGenerator()->enrol_user($user->id, $course->id); 51 52 // Make assign module. 53 $assignrow = $this->getDataGenerator()->create_module('assign', array( 54 'course' => $course->id, 'name' => 'Test!')); 55 $assign = new assign(context_module::instance($assignrow->cmid), false, false); 56 $modinfo = get_fast_modinfo($course); 57 $cm = $modinfo->get_cm($assignrow->cmid); 58 59 $info = new \core_availability\info_module($cm); 60 61 // Get the actual grade item. 62 $item = $assign->get_grade_item(); 63 64 // Construct tree with grade condition (any grade, specified item). 65 $structure = (object)array('type' => 'grade', 'id' => (int)$item->id); 66 $cond = new condition($structure); 67 68 // Check if available (not available). 69 $this->assertFalse($cond->is_available(false, $info, true, $user->id)); 70 $information = $cond->get_description(false, false, $info); 71 $this->assertRegExp('~have a grade.*Test!~', $information); 72 $this->assertTrue($cond->is_available(true, $info, true, $user->id)); 73 74 // Add grade and check available. 75 self::set_grade($assignrow, $user->id, 37.2); 76 $this->assertTrue($cond->is_available(false, $info, true, $user->id)); 77 $this->assertFalse($cond->is_available(true, $info, true, $user->id)); 78 $information = $cond->get_description(false, true, $info); 79 $this->assertRegExp('~do not have a grade.*Test!~', $information); 80 81 // Construct directly and test remaining conditions; first, min grade (fail). 82 self::set_grade($assignrow, $user->id, 29.99999); 83 $structure->min = 30.0; 84 $cond = new condition($structure); 85 $this->assertFalse($cond->is_available(false, $info, true, $user->id)); 86 $information = $cond->get_description(false, false, $info); 87 $this->assertRegExp('~achieve a required score.*Test!~', $information); 88 $this->assertTrue($cond->is_available(true, $info, true, $user->id)); 89 90 // Min grade (success). 91 self::set_grade($assignrow, $user->id, 30); 92 $this->assertTrue($cond->is_available(false, $info, true, $user->id)); 93 $this->assertFalse($cond->is_available(true, $info, true, $user->id)); 94 $information = $cond->get_description(false, true, $info); 95 $this->assertRegExp('~do not get certain scores.*Test!~', $information); 96 97 // Max grade (fail). 98 unset($structure->min); 99 $structure->max = 30.0; 100 $cond = new condition($structure); 101 $this->assertFalse($cond->is_available(false, $info, true, $user->id)); 102 $information = $cond->get_description(false, false, $info); 103 $this->assertRegExp('~get an appropriate score.*Test!~', $information); 104 $this->assertTrue($cond->is_available(true, $info, true, $user->id)); 105 106 // Max grade (success). 107 self::set_grade($assignrow, $user->id, 29.99999); 108 $this->assertTrue($cond->is_available(false, $info, true, $user->id)); 109 $this->assertFalse($cond->is_available(true, $info, true, $user->id)); 110 $information = $cond->get_description(false, true, $info); 111 $this->assertRegExp('~do not get certain scores.*Test!~', $information); 112 113 // Max and min (fail). 114 $structure->min = 30.0; 115 $structure->max = 34.12345; 116 $cond = new condition($structure); 117 $this->assertFalse($cond->is_available(false, $info, true, $user->id)); 118 $information = $cond->get_description(false, false, $info); 119 $this->assertRegExp('~get a particular score.*Test!~', $information); 120 $this->assertTrue($cond->is_available(true, $info, true, $user->id)); 121 122 // Still fail (other end). 123 self::set_grade($assignrow, $user->id, 34.12345); 124 $this->assertFalse($cond->is_available(false, $info, true, $user->id)); 125 126 // Success (top end). 127 self::set_grade($assignrow, $user->id, 34.12344); 128 $this->assertTrue($cond->is_available(false, $info, true, $user->id)); 129 $this->assertFalse($cond->is_available(true, $info, true, $user->id)); 130 $information = $cond->get_description(false, true, $info); 131 $this->assertRegExp('~do not get certain scores.*Test!~', $information); 132 133 // Success (bottom end). 134 self::set_grade($assignrow, $user->id, 30.0); 135 $this->assertTrue($cond->is_available(false, $info, true, $user->id)); 136 $this->assertFalse($cond->is_available(true, $info, true, $user->id)); 137 $information = $cond->get_description(false, true, $info); 138 $this->assertRegExp('~do not get certain scores.*Test!~', $information); 139 } 140 141 /** 142 * Tests the constructor including error conditions. Also tests the 143 * string conversion feature (intended for debugging only). 144 */ 145 public function test_constructor() { 146 // No parameters. 147 $structure = new stdClass(); 148 try { 149 $cond = new condition($structure); 150 $this->fail(); 151 } catch (coding_exception $e) { 152 $this->assertContains('Missing or invalid ->id', $e->getMessage()); 153 } 154 155 // Invalid id (not int). 156 $structure->id = 'bourne'; 157 try { 158 $cond = new condition($structure); 159 $this->fail(); 160 } catch (coding_exception $e) { 161 $this->assertContains('Missing or invalid ->id', $e->getMessage()); 162 } 163 164 // Invalid min (not number). 165 $structure->id = 42; 166 $structure->min = 'ute'; 167 try { 168 $cond = new condition($structure); 169 $this->fail(); 170 } catch (coding_exception $e) { 171 $this->assertContains('Missing or invalid ->min', $e->getMessage()); 172 } 173 174 // Invalid max (not number). 175 $structure->min = 3.89; 176 $structure->max = '9000'; 177 try { 178 $cond = new condition($structure); 179 $this->fail(); 180 } catch (coding_exception $e) { 181 $this->assertContains('Missing or invalid ->max', $e->getMessage()); 182 } 183 184 // All valid. 185 $structure->max = 4.0; 186 $cond = new condition($structure); 187 $this->assertEquals('{grade:#42 >= 3.89000, < 4.00000}', (string)$cond); 188 189 // No max. 190 unset($structure->max); 191 $cond = new condition($structure); 192 $this->assertEquals('{grade:#42 >= 3.89000}', (string)$cond); 193 194 // No min. 195 unset($structure->min); 196 $structure->max = 32.768; 197 $cond = new condition($structure); 198 $this->assertEquals('{grade:#42 < 32.76800}', (string)$cond); 199 200 // No nothing (only requires that grade exists). 201 unset($structure->max); 202 $cond = new condition($structure); 203 $this->assertEquals('{grade:#42}', (string)$cond); 204 } 205 206 /** 207 * Tests the save() function. 208 */ 209 public function test_save() { 210 $structure = (object)array('id' => 19); 211 $cond = new condition($structure); 212 $structure->type = 'grade'; 213 $this->assertEquals($structure, $cond->save()); 214 215 $structure = (object)array('id' => 19, 'min' => 4.12345, 'max' => 90); 216 $cond = new condition($structure); 217 $structure->type = 'grade'; 218 $this->assertEquals($structure, $cond->save()); 219 } 220 221 /** 222 * Updates the grade of a user in the given assign module instance. 223 * 224 * @param stdClass $assignrow Assignment row from database 225 * @param int $userid User id 226 * @param float $grade Grade 227 */ 228 protected static function set_grade($assignrow, $userid, $grade) { 229 $grades = array(); 230 $grades[$userid] = (object)array( 231 'rawgrade' => $grade, 'userid' => $userid); 232 $assignrow->cmidnumber = null; 233 assign_grade_item_update($assignrow, $grades); 234 } 235 236 /** 237 * Tests the update_dependency_id() function. 238 */ 239 public function test_update_dependency_id() { 240 $cond = new condition((object)array('id' => 123)); 241 $this->assertFalse($cond->update_dependency_id('frogs', 123, 456)); 242 $this->assertFalse($cond->update_dependency_id('grade_items', 12, 34)); 243 $this->assertTrue($cond->update_dependency_id('grade_items', 123, 456)); 244 $after = $cond->save(); 245 $this->assertEquals(456, $after->id); 246 } 247 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body