[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 grade/report/user/lib.php. 19 * 20 * @package core_grade 21 * @category phpunit 22 * @copyright 2012 Andrew Davis 23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot.'/grade/lib.php'); 30 require_once($CFG->dirroot.'/grade/report/grader/lib.php'); 31 32 /** 33 * Tests grade_report_grader (the grader report) 34 */ 35 class core_grade_report_graderlib_testcase extends advanced_testcase { 36 37 /** 38 * Tests grade_report_grader::process_data() 39 * 40 * process_data() processes submitted grade and feedback data 41 */ 42 public function test_process_data() { 43 global $DB, $CFG; 44 45 $this->resetAfterTest(true); 46 47 $course = $this->getDataGenerator()->create_course(); 48 49 // Create and enrol a student. 50 $student = $this->getDataGenerator()->create_user(array('username' => 'Student Sam')); 51 $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST); 52 $this->getDataGenerator()->enrol_user($student->id, $course->id, $role->id); 53 54 // Test with limited grades. 55 $CFG->unlimitedgrades = 0; 56 57 $forummax = 80; 58 $forum1 = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => $forummax, 'course' => $course->id)); 59 // Switch the stdClass instance for a grade item instance. 60 $forum1 = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum1->id, 'courseid' => $course->id)); 61 62 $report = $this->create_report($course); 63 $testgrade = 60.00; 64 65 $data = new stdClass(); 66 $data->id = $course->id; 67 $data->report = 'grader'; 68 $data->timepageload = time(); 69 70 $data->grade = array(); 71 $data->grade[$student->id] = array(); 72 $data->grade[$student->id][$forum1->id] = $testgrade; 73 74 $warnings = $report->process_data($data); 75 $this->assertEquals(count($warnings), 0); 76 77 $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id)); 78 $this->assertEquals($studentgrade->finalgrade, $testgrade); 79 80 // Grade above max. Should be pulled down to max. 81 $toobig = 200.00; 82 $data->grade[$student->id][$forum1->id] = $toobig; 83 $data->timepageload = time(); 84 $warnings = $report->process_data($data); 85 $this->assertEquals(count($warnings), 1); 86 87 $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id)); 88 $this->assertEquals($studentgrade->finalgrade, $forummax); 89 90 // Grade below min. Should be pulled up to min. 91 $toosmall = -10.00; 92 $data->grade[$student->id][$forum1->id] = $toosmall; 93 $data->timepageload = time(); 94 $warnings = $report->process_data($data); 95 $this->assertEquals(count($warnings), 1); 96 97 $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id)); 98 $this->assertEquals($studentgrade->finalgrade, 0); 99 100 // Test unlimited grades so we can give a student a grade about max. 101 $CFG->unlimitedgrades = 1; 102 103 $data->grade[$student->id][$forum1->id] = $toobig; 104 $data->timepageload = time(); 105 $warnings = $report->process_data($data); 106 $this->assertEquals(count($warnings), 0); 107 108 $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id)); 109 $this->assertEquals($studentgrade->finalgrade, $toobig); 110 } 111 112 public function test_collapsed_preferences() { 113 $this->resetAfterTest(true); 114 115 $emptypreferences = array('aggregatesonly' => array(), 'gradesonly' => array()); 116 117 $user1 = $this->getDataGenerator()->create_user(); 118 $course1 = $this->getDataGenerator()->create_course(); 119 $course2 = $this->getDataGenerator()->create_course(); 120 $course3 = $this->getDataGenerator()->create_course(); 121 122 $this->setUser($user1); 123 124 $report = $this->create_report($course1); 125 $this->assertEquals($emptypreferences, $report->collapsed); 126 127 // Validating preferences set/get for one course. 128 $report->process_action('cg13', 'switch_minus'); 129 $report = $this->create_report($course1); 130 $this->assertEquals(array(13), $report->collapsed['aggregatesonly']); 131 $this->assertEmpty($report->collapsed['gradesonly']); 132 133 $report->process_action('cg13', 'switch_plus'); 134 $report = $this->create_report($course1); 135 $this->assertEmpty($report->collapsed['aggregatesonly']); 136 $this->assertEquals(array(13), $report->collapsed['gradesonly']); 137 138 $report->process_action('cg13', 'switch_whole'); 139 $report = $this->create_report($course1); 140 $this->assertEquals($emptypreferences, $report->collapsed); 141 142 // Validating preferences set/get for several courses. 143 144 $course1cats = $course2cats = $course3cats = array(); 145 for ($i=0;$i<10;$i++) { 146 $course1cats[] = $this->create_grade_category($course1)->id; 147 $course2cats[] = $this->create_grade_category($course2)->id; 148 $course3cats[] = $this->create_grade_category($course3)->id; 149 } 150 151 $report1 = $this->create_report($course1); 152 foreach ($course1cats as $catid) { 153 $report1->process_action('cg'.$catid, 'switch_minus'); 154 } 155 $report2 = $this->create_report($course2); 156 foreach ($course2cats as $catid) { 157 $report2->process_action('cg'.$catid, 'switch_minus'); 158 $report2->process_action('cg'.$catid, 'switch_plus'); 159 } 160 $report3 = $this->create_report($course3); 161 foreach ($course3cats as $catid) { 162 $report3->process_action('cg'.$catid, 'switch_minus'); 163 if (($i++)%2) { 164 $report3->process_action('cg'.$catid, 'switch_plus'); 165 } 166 } 167 168 $report1 = $this->create_report($course1); 169 $this->assertEquals(10, count($report1->collapsed['aggregatesonly'])); 170 $this->assertEquals(0, count($report1->collapsed['gradesonly'])); 171 $report2 = $this->create_report($course2); 172 $this->assertEquals(0, count($report2->collapsed['aggregatesonly'])); 173 $this->assertEquals(10, count($report2->collapsed['gradesonly'])); 174 $report3 = $this->create_report($course3); 175 $this->assertEquals(5, count($report3->collapsed['aggregatesonly'])); 176 $this->assertEquals(5, count($report3->collapsed['gradesonly'])); 177 178 // Test upgrade script. 179 // Combine data generated for user1 and set it in the old format for user2, Try to retrieve it and make sure it is converted. 180 181 $user2 = $this->getDataGenerator()->create_user(); 182 $alldata = array( 183 'aggregatesonly' => array_merge($report1->collapsed['aggregatesonly'], $report2->collapsed['aggregatesonly'], $report3->collapsed['aggregatesonly']), 184 'gradesonly' => array_merge($report1->collapsed['gradesonly'], $report2->collapsed['gradesonly'], $report3->collapsed['gradesonly']), 185 ); 186 set_user_preference('grade_report_grader_collapsed_categories', serialize($alldata), $user2); 187 188 $this->setUser($user2); 189 $convertedreport1 = $this->create_report($course1); 190 $this->assertEquals($report1->collapsed, $convertedreport1->collapsed); 191 $convertedreport2 = $this->create_report($course2); 192 $this->assertEquals($report2->collapsed, $convertedreport2->collapsed); 193 $convertedreport3 = $this->create_report($course3); 194 $this->assertEquals($report3->collapsed, $convertedreport3->collapsed); 195 // Make sure the old style user preference is removed now. 196 $this->assertEmpty(get_user_preferences('grade_report_grader_collapsed_categories')); 197 198 // Test overflowing the setting with non-existing categories (only validated if new setting size exceeds 1333 chars). 199 200 $toobigvalue = $expectedvalue = $report1->collapsed; 201 for ($i = 0; strlen(json_encode($toobigvalue)) < 1333; $i++) { 202 $toobigvalue[($i < 7) ? 'gradesonly' : 'aggregatesonly'][] = $course1cats[9] + 1 + $i; 203 } 204 $lastvalue = array_pop($toobigvalue['gradesonly']); 205 set_user_preference('grade_report_grader_collapsed_categories'.$course1->id, json_encode($toobigvalue)); 206 207 $report1 = $this->create_report($course1); 208 $report1->process_action('cg'.$lastvalue, 'switch_minus'); 209 210 $report1 = $this->create_report($course1); 211 $this->assertEquals($expectedvalue, $report1->collapsed); 212 213 // Test overflowing the setting with existing categories. 214 215 $toobigvalue = $report1->collapsed; 216 for ($i = 0; strlen(json_encode($toobigvalue)) < 1333; $i++) { 217 $catid = $this->create_grade_category($course1)->id; 218 $toobigvalue[($i < 7) ? 'gradesonly' : 'aggregatesonly'][] = $catid; 219 } 220 $lastcatid = array_pop($toobigvalue['gradesonly']); 221 set_user_preference('grade_report_grader_collapsed_categories'.$course1->id, json_encode($toobigvalue)); 222 $toobigvalue['aggregatesonly'][] = $lastcatid; 223 224 $report1 = $this->create_report($course1); 225 $report1->process_action('cg'.$lastcatid, 'switch_minus'); 226 227 // One last value should be removed from both arrays. 228 $report1 = $this->create_report($course1); 229 $this->assertEquals(count($toobigvalue['aggregatesonly']) - 1, count($report1->collapsed['aggregatesonly'])); 230 $this->assertEquals(count($toobigvalue['gradesonly']) - 1, count($report1->collapsed['gradesonly'])); 231 } 232 233 private function create_grade_category($course) { 234 static $cnt = 0; 235 $cnt++; 236 $grade_category = new grade_category(array('courseid' => $course->id, 'fullname' => 'Cat '.$cnt), false); 237 $grade_category->apply_default_settings(); 238 $grade_category->apply_forced_settings(); 239 $grade_category->insert(); 240 return $grade_category; 241 } 242 243 private function create_report($course) { 244 245 $coursecontext = context_course::instance($course->id); 246 $gpr = new grade_plugin_return(array('type' => 'report', 'plugin'=>'grader', 'courseid' => $course->id)); 247 $report = new grade_report_grader($course->id, $gpr, $coursecontext); 248 249 return $report; 250 } 251 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |