|
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 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->dirroot . '/mod/workshop/locallib.php'); // Include the code to test 00030 00031 global $DB; 00032 Mock::generate(get_class($DB), 'mockDB'); 00033 00037 class testable_workshop extends workshop { 00038 00039 public function __construct() { 00040 $this->id = 16; 00041 $this->cm = new stdclass(); 00042 $this->course = new stdclass(); 00043 $this->context = new stdclass(); 00044 } 00045 00046 public function aggregate_submission_grades_process(array $assessments) { 00047 parent::aggregate_submission_grades_process($assessments); 00048 } 00049 00050 public function aggregate_grading_grades_process(array $assessments, $timegraded = null) { 00051 parent::aggregate_grading_grades_process($assessments, $timegraded); 00052 } 00053 00054 } 00055 00059 class workshop_internal_api_test extends UnitTestCase { 00060 00062 protected $workshop; 00063 00065 public function setUp() { 00066 global $DB; 00067 $this->realDB = $DB; 00068 $DB = new mockDB(); 00069 00070 $this->workshop = new testable_workshop(); 00071 } 00072 00073 public function tearDown() { 00074 global $DB; 00075 $DB = $this->realDB; 00076 $this->workshop = null; 00077 } 00078 00079 public function test_aggregate_submission_grades_process_notgraded() { 00080 global $DB; 00081 00082 // fixture set-up 00083 $batch = array(); // batch of a submission's assessments 00084 $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null); 00085 $DB->expectNever('update_record'); 00086 // exercise SUT 00087 $this->workshop->aggregate_submission_grades_process($batch); 00088 } 00089 00090 public function test_aggregate_submission_grades_process_single() { 00091 global $DB; 00092 00093 // fixture set-up 00094 $batch = array(); // batch of a submission's assessments 00095 $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.12345); 00096 $expected = 10.12345; 00097 $DB->expectOnce('update_record'); 00098 // exercise SUT 00099 $this->workshop->aggregate_submission_grades_process($batch); 00100 } 00101 00102 public function test_aggregate_submission_grades_process_null_doesnt_influence() { 00103 global $DB; 00104 00105 // fixture set-up 00106 $batch = array(); // batch of a submission's assessments 00107 $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => 45.54321); 00108 $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null); 00109 $expected = 45.54321; 00110 $DB->expectOnce('update_record'); 00111 // exercise SUT 00112 $this->workshop->aggregate_submission_grades_process($batch); 00113 } 00114 00115 public function test_aggregate_submission_grades_process_weighted_single() { 00116 global $DB; 00117 00118 // fixture set-up 00119 $batch = array(); // batch of a submission's assessments 00120 $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 4, 'grade' => 14.00012); 00121 $expected = 14.00012; 00122 $DB->expectOnce('update_record'); 00123 // exercise SUT 00124 $this->workshop->aggregate_submission_grades_process($batch); 00125 } 00126 00127 public function test_aggregate_submission_grades_process_mean() { 00128 global $DB; 00129 00130 // fixture set-up 00131 $batch = array(); // batch of a submission's assessments 00132 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 56.12000); 00133 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 12.59000); 00134 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.00000); 00135 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 0.00000); 00136 $expected = 19.67750; 00137 $DB->expectOnce('update_record'); 00138 // exercise SUT 00139 $this->workshop->aggregate_submission_grades_process($batch); 00140 } 00141 00142 public function test_aggregate_submission_grades_process_mean_changed() { 00143 global $DB; 00144 00145 // fixture set-up 00146 $batch = array(); // batch of a submission's assessments 00147 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 56.12000); 00148 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 12.59000); 00149 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 10.00000); 00150 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 12.57750, 'weight' => 1, 'grade' => 0.00000); 00151 $expected = 19.67750; 00152 $DB->expectOnce('update_record'); 00153 // exercise SUT 00154 $this->workshop->aggregate_submission_grades_process($batch); 00155 } 00156 00157 public function test_aggregate_submission_grades_process_mean_nochange() { 00158 global $DB; 00159 00160 // fixture set-up 00161 $batch = array(); // batch of a submission's assessments 00162 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 56.12000); 00163 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 12.59000); 00164 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 10.00000); 00165 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 0.00000); 00166 $DB->expectNever('update_record'); 00167 // exercise SUT 00168 $this->workshop->aggregate_submission_grades_process($batch); 00169 } 00170 00171 public function test_aggregate_submission_grades_process_rounding() { 00172 global $DB; 00173 00174 // fixture set-up 00175 $batch = array(); // batch of a submission's assessments 00176 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 4.00000); 00177 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 2.00000); 00178 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 1.00000); 00179 $expected = 2.33333; 00180 $DB->expectOnce('update_record'); 00181 // exercise SUT 00182 $this->workshop->aggregate_submission_grades_process($batch); 00183 } 00184 00185 public function test_aggregate_submission_grades_process_weighted_mean() { 00186 global $DB; 00187 00188 // fixture set-up 00189 $batch = array(); // batch of a submission's assessments 00190 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 3, 'grade' => 12.00000); 00191 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 2, 'grade' => 30.00000); 00192 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 1, 'grade' => 10.00000); 00193 $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => null, 'weight' => 0, 'grade' => 1000.00000); 00194 $expected = 17.66667; 00195 $DB->expectOnce('update_record'); 00196 // exercise SUT 00197 $this->workshop->aggregate_submission_grades_process($batch); 00198 } 00199 00200 public function test_aggregate_grading_grades_process_nograding() { 00201 global $DB; 00202 // fixture set-up 00203 $batch = array(); 00204 $batch[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null); 00205 // expectation 00206 $DB->expectNever('update_record'); 00207 // excersise SUT 00208 $this->workshop->aggregate_grading_grades_process($batch); 00209 } 00210 00211 public function test_aggregate_grading_grades_process_single_grade_new() { 00212 global $DB; 00213 // fixture set-up 00214 $batch = array(); 00215 $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>82.87670, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null); 00216 // expectation 00217 $now = time(); 00218 $expected = new stdclass(); 00219 $expected->workshopid = $this->workshop->id; 00220 $expected->userid = 3; 00221 $expected->gradinggrade = 82.87670; 00222 $expected->timegraded = $now; 00223 $DB->expectOnce('insert_record', array('workshop_aggregations', $expected)); 00224 // excersise SUT 00225 $this->workshop->aggregate_grading_grades_process($batch, $now); 00226 } 00227 00228 public function test_aggregate_grading_grades_process_single_grade_update() { 00229 global $DB; 00230 // fixture set-up 00231 $batch = array(); 00232 $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>82.87670); 00233 // expectation 00234 $DB->expectOnce('update_record'); 00235 // excersise SUT 00236 $this->workshop->aggregate_grading_grades_process($batch); 00237 } 00238 00239 public function test_aggregate_grading_grades_process_single_grade_uptodate() { 00240 global $DB; 00241 // fixture set-up 00242 $batch = array(); 00243 $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>90.00000); 00244 // expectation 00245 $DB->expectNever('update_record'); 00246 // excersise SUT 00247 $this->workshop->aggregate_grading_grades_process($batch); 00248 } 00249 00250 public function test_aggregate_grading_grades_process_single_grade_overridden() { 00251 global $DB; 00252 // fixture set-up 00253 $batch = array(); 00254 $batch[] = (object)array('reviewerid'=>4, 'gradinggrade'=>91.56700, 'gradinggradeover'=>82.32105, 'aggregationid'=>2, 'aggregatedgrade'=>91.56700); 00255 // expectation 00256 $DB->expectOnce('update_record'); 00257 // excersise SUT 00258 $this->workshop->aggregate_grading_grades_process($batch); 00259 } 00260 00261 public function test_aggregate_grading_grades_process_multiple_grades_new() { 00262 global $DB; 00263 // fixture set-up 00264 $batch = array(); 00265 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>99.45670, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null); 00266 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null); 00267 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null); 00268 // expectation 00269 $now = time(); 00270 $expected = new stdclass(); 00271 $expected->workshopid = $this->workshop->id; 00272 $expected->userid = 5; 00273 $expected->gradinggrade = 79.3066; 00274 $expected->timegraded = $now; 00275 $DB->expectOnce('insert_record', array('workshop_aggregations', $expected)); 00276 // excersise SUT 00277 $this->workshop->aggregate_grading_grades_process($batch, $now); 00278 } 00279 00280 public function test_aggregate_grading_grades_process_multiple_grades_update() { 00281 global $DB; 00282 // fixture set-up 00283 $batch = array(); 00284 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>56.23400, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660); 00285 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660); 00286 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>79.30660); 00287 // expectation 00288 $DB->expectOnce('update_record'); 00289 // excersise SUT 00290 $this->workshop->aggregate_grading_grades_process($batch); 00291 } 00292 00293 public function test_aggregate_grading_grades_process_multiple_grades_overriden() { 00294 global $DB; 00295 // fixture set-up 00296 $batch = array(); 00297 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>56.23400, 'gradinggradeover'=>99.45670, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904); 00298 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>87.34311, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904); 00299 $batch[] = (object)array('reviewerid'=>5, 'gradinggrade'=>51.12000, 'gradinggradeover'=>null, 'aggregationid'=>2, 'aggregatedgrade'=>64.89904); 00300 // expectation 00301 $DB->expectOnce('update_record'); 00302 // excersise SUT 00303 $this->workshop->aggregate_grading_grades_process($batch); 00304 } 00305 00306 public function test_aggregate_grading_grades_process_multiple_grades_one_missing() { 00307 global $DB; 00308 // fixture set-up 00309 $batch = array(); 00310 $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>50.00000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000); 00311 $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000); 00312 $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>52.20000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000); 00313 // expectation 00314 $DB->expectOnce('update_record'); 00315 // excersise SUT 00316 $this->workshop->aggregate_grading_grades_process($batch); 00317 } 00318 00319 public function test_aggregate_grading_grades_process_multiple_grades_missing_overridden() { 00320 global $DB; 00321 // fixture set-up 00322 $batch = array(); 00323 $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>50.00000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000); 00324 $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>null, 'gradinggradeover'=>69.00000, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000); 00325 $batch[] = (object)array('reviewerid'=>6, 'gradinggrade'=>52.20000, 'gradinggradeover'=>null, 'aggregationid'=>3, 'aggregatedgrade'=>100.00000); 00326 // expectation 00327 $DB->expectOnce('update_record'); 00328 // excersise SUT 00329 $this->workshop->aggregate_grading_grades_process($batch); 00330 } 00331 00332 public function test_percent_to_value() { 00333 // fixture setup 00334 $total = 185; 00335 $percent = 56.6543; 00336 // exercise SUT 00337 $part = workshop::percent_to_value($percent, $total); 00338 // verify 00339 $this->assertEqual($part, $total * $percent / 100); 00340 } 00341 00342 public function test_percent_to_value_negative() { 00343 // fixture setup 00344 $total = 185; 00345 $percent = -7.098; 00346 // set expectation 00347 $this->expectException('coding_exception'); 00348 // exercise SUT 00349 $part = workshop::percent_to_value($percent, $total); 00350 } 00351 00352 public function test_percent_to_value_over_hundred() { 00353 // fixture setup 00354 $total = 185; 00355 $percent = 121.08; 00356 // set expectation 00357 $this->expectException('coding_exception'); 00358 // exercise SUT 00359 $part = workshop::percent_to_value($percent, $total); 00360 } 00361 00362 public function test_lcm() { 00363 // fixture setup + exercise SUT + verify in one step 00364 $this->assertEqual(workshop::lcm(1,4), 4); 00365 $this->assertEqual(workshop::lcm(2,4), 4); 00366 $this->assertEqual(workshop::lcm(4,2), 4); 00367 $this->assertEqual(workshop::lcm(2,3), 6); 00368 $this->assertEqual(workshop::lcm(6,4), 12); 00369 } 00370 00371 public function test_lcm_array() { 00372 // fixture setup 00373 $numbers = array(5,3,15); 00374 // excersise SUT 00375 $lcm = array_reduce($numbers, 'workshop::lcm', 1); 00376 // verify 00377 $this->assertEqual($lcm, 15); 00378 } 00379 00380 public function test_prepare_example_assessment() { 00381 // fixture setup 00382 $fakerawrecord = (object)array( 00383 'id' => 42, 00384 'submissionid' => 56, 00385 'weight' => 0, 00386 'timecreated' => time() - 10, 00387 'timemodified' => time() - 5, 00388 'grade' => null, 00389 'gradinggrade' => null, 00390 'gradinggradeover' => null, 00391 ); 00392 // excersise SUT 00393 $a = $this->workshop->prepare_example_assessment($fakerawrecord); 00394 // verify 00395 $this->assertTrue($a instanceof workshop_example_assessment); 00396 $this->assertTrue($a->url instanceof moodle_url); 00397 00398 // modify setup 00399 $fakerawrecord->weight = 1; 00400 $this->expectException('coding_exception'); 00401 // excersise SUT 00402 $a = $this->workshop->prepare_example_assessment($fakerawrecord); 00403 } 00404 00405 public function test_prepare_example_reference_assessment() { 00406 global $USER; 00407 // fixture setup 00408 $fakerawrecord = (object)array( 00409 'id' => 38, 00410 'submissionid' => 56, 00411 'weight' => 1, 00412 'timecreated' => time() - 100, 00413 'timemodified' => time() - 50, 00414 'grade' => 0.75000, 00415 'gradinggrade' => 1.00000, 00416 'gradinggradeover' => null, 00417 ); 00418 // excersise SUT 00419 $a = $this->workshop->prepare_example_reference_assessment($fakerawrecord); 00420 // verify 00421 $this->assertTrue($a instanceof workshop_example_reference_assessment); 00422 00423 // modify setup 00424 $fakerawrecord->weight = 0; 00425 $this->expectException('coding_exception'); 00426 // excersise SUT 00427 $a = $this->workshop->prepare_example_reference_assessment($fakerawrecord); 00428 } 00429 }