|
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 // Include the code to test 00030 require_once($CFG->dirroot . '/mod/workshop/locallib.php'); 00031 require_once($CFG->dirroot . '/mod/workshop/form/accumulative/lib.php'); 00032 00033 global $DB; 00034 Mock::generate(get_class($DB), 'mockDB'); 00035 00039 class testable_workshop_accumulative_strategy extends workshop_accumulative_strategy { 00040 00042 public $dimensions = array(); 00043 00047 public function calculate_peer_grade(array $grades) { 00048 return parent::calculate_peer_grade($grades); 00049 } 00050 } 00051 00052 class workshop_accumulative_strategy_test extends UnitTestCase { 00053 00055 protected $realDB; 00056 00058 protected $workshop; 00059 00061 protected $strategy; 00062 00066 public function setUp() { 00067 global $DB; 00068 $this->realDB = $DB; 00069 $DB = new mockDB(); 00070 00071 $cm = new stdclass(); 00072 $course = new stdclass(); 00073 $context = new stdclass(); 00074 $workshop = (object)array('id' => 42, 'strategy' => 'accumulative'); 00075 $this->workshop = new workshop($workshop, $cm, $course, $context); 00076 $this->strategy = new testable_workshop_accumulative_strategy($this->workshop); 00077 } 00078 00079 public function tearDown() { 00080 global $DB; 00081 $DB = $this->realDB; 00082 00083 $this->workshop = null; 00084 $this->strategy = null; 00085 } 00086 00087 public function test_calculate_peer_grade_null_grade() { 00088 // fixture set-up 00089 $this->strategy->dimensions = array(); 00090 $grades = array(); 00091 // exercise SUT 00092 $suggested = $this->strategy->calculate_peer_grade($grades); 00093 // validate 00094 $this->assertNull($suggested); 00095 } 00096 00097 public function test_calculate_peer_grade_one_numerical() { 00098 // fixture set-up 00099 $this->strategy->dimensions[1003] = (object)array('grade' => '20', 'weight' => '1'); 00100 $grades[] = (object)array('dimensionid' => 1003, 'grade' => '5.00000'); 00101 // exercise SUT 00102 $suggested = $this->strategy->calculate_peer_grade($grades); 00103 // validate 00104 $this->assertEqual(grade_floatval(5/20 * 100), $suggested); 00105 } 00106 00107 public function test_calculate_peer_grade_negative_weight() { 00108 // fixture set-up 00109 $this->strategy->dimensions[1003] = (object)array('grade' => '20', 'weight' => '-1'); 00110 $grades[] = (object)array('dimensionid' => 1003, 'grade' => '20'); 00111 $this->expectException('coding_exception'); 00112 // exercise SUT 00113 $suggested = $this->strategy->calculate_peer_grade($grades); 00114 } 00115 00116 public function test_calculate_peer_grade_one_numerical_weighted() { 00117 // fixture set-up 00118 $this->strategy->dimensions[1003] = (object)array('grade' => '20', 'weight' => '3'); 00119 $grades[] = (object)array('dimensionid' => '1003', 'grade' => '5'); 00120 // exercise SUT 00121 $suggested = $this->strategy->calculate_peer_grade($grades); 00122 // validate 00123 $this->assertEqual(grade_floatval(5/20 * 100), $suggested); 00124 } 00125 00126 public function test_calculate_peer_grade_three_numericals_same_weight() { 00127 // fixture set-up 00128 $this->strategy->dimensions[1003] = (object)array('grade' => '20', 'weight' => '2'); 00129 $this->strategy->dimensions[1004] = (object)array('grade' => '100', 'weight' => '2'); 00130 $this->strategy->dimensions[1005] = (object)array('grade' => '10', 'weight' => '2'); 00131 00132 $grades[] = (object)array('dimensionid' => 1003, 'grade' => '11.00000'); 00133 $grades[] = (object)array('dimensionid' => 1004, 'grade' => '87.00000'); 00134 $grades[] = (object)array('dimensionid' => 1005, 'grade' => '10.00000'); 00135 00136 // exercise SUT 00137 $suggested = $this->strategy->calculate_peer_grade($grades); 00138 00139 // validate 00140 $this->assertEqual(grade_floatval((11/20 + 87/100 + 10/10)/3 * 100), $suggested); 00141 } 00142 00143 public function test_calculate_peer_grade_three_numericals_different_weights() { 00144 // fixture set-up 00145 $this->strategy->dimensions[1003] = (object)array('grade' => '15', 'weight' => 3); 00146 $this->strategy->dimensions[1004] = (object)array('grade' => '80', 'weight' => 1); 00147 $this->strategy->dimensions[1005] = (object)array('grade' => '5', 'weight' => 2); 00148 00149 $grades[] = (object)array('dimensionid' => 1003, 'grade' => '7.00000'); 00150 $grades[] = (object)array('dimensionid' => 1004, 'grade' => '66.00000'); 00151 $grades[] = (object)array('dimensionid' => 1005, 'grade' => '4.00000'); 00152 00153 // exercise SUT 00154 $suggested = $this->strategy->calculate_peer_grade($grades); 00155 00156 // validate 00157 $this->assertEqual(grade_floatval((7/15*3 + 66/80*1 + 4/5*2)/6 * 100), $suggested); 00158 } 00159 00160 public function test_calculate_peer_grade_one_scale_max() { 00161 global $DB; 00162 00163 // fixture set-up 00164 $mockscale = 'E,D,C,B,A'; 00165 $this->strategy->dimensions[1008] = (object)array('grade' => '-10', 'weight' => 1); 00166 $grades[] = (object)array('dimensionid' => 1008, 'grade' => '5.00000'); 00167 $DB->expectOnce('get_field', array('scale', 'scale', array('id' => 10), MUST_EXIST)); 00168 $DB->setReturnValue('get_field', $mockscale); 00169 00170 // exercise SUT 00171 $suggested = $this->strategy->calculate_peer_grade($grades); 00172 00173 // validate 00174 $this->assertEqual(100.00000, $suggested); 00175 } 00176 00177 public function test_calculate_peer_grade_one_scale_min_with_scale_caching() { 00178 global $DB; 00179 00180 // fixture set-up 00181 $this->strategy->dimensions[1008] = (object)array('grade' => '-10', 'weight' => 1); 00182 $grades[] = (object)array('dimensionid' => 1008, 'grade' => '1.00000'); 00183 $DB->expectNever('get_field', array('scale', 'scale', array('id' => 10), MUST_EXIST)); // cached 00184 00185 // exercise SUT 00186 $suggested = $this->strategy->calculate_peer_grade($grades); 00187 00188 // validate 00189 $this->assertEqual(0.00000, $suggested); 00190 } 00191 00192 public function test_calculate_peer_grade_two_scales_weighted() { 00193 global $DB; 00194 00195 // fixture set-up 00196 $mockscale13 = 'Poor,Good,Excellent'; 00197 $mockscale17 = '-,*,**,***,****,*****,******'; 00198 00199 $this->strategy->dimensions[1012] = (object)array('grade' => '-13', 'weight' => 2); 00200 $this->strategy->dimensions[1019] = (object)array('grade' => '-17', 'weight' => 3); 00201 00202 $grades[] = (object)array('dimensionid' => 1012, 'grade' => '2.00000'); // "Good" 00203 $grades[] = (object)array('dimensionid' => 1019, 'grade' => '5.00000'); // "****" 00204 00205 $DB->expectAt(0, 'get_field', array('scale', 'scale', array('id' => 13), MUST_EXIST)); 00206 $DB->setReturnValueAt(0, 'get_field', $mockscale13); 00207 00208 $DB->expectAt(1, 'get_field', array('scale', 'scale', array('id' => 17), MUST_EXIST)); 00209 $DB->setReturnValueAt(1, 'get_field', $mockscale17); 00210 00211 // exercise SUT 00212 $suggested = $this->strategy->calculate_peer_grade($grades); 00213 00214 // validate 00215 $this->assertEqual(grade_floatval((1/2*2 + 4/6*3)/5 * 100), $suggested); 00216 } 00217 00218 public function test_calculate_peer_grade_scale_exception() { 00219 global $DB; 00220 00221 // fixture set-up 00222 $mockscale13 = 'Poor,Good,Excellent'; 00223 $this->strategy->dimensions[1012] = (object)array('grade' => -13, 'weight' => 1); 00224 $DB->expectNever('get_field', array('scale', 'scale', array('id' => 13), MUST_EXIST)); // cached 00225 $grades[] = (object)array('dimensionid' => 1012, 'grade' => '4.00000'); // exceeds the number of scale items 00226 $this->expectException('coding_exception'); 00227 00228 // exercise SUT 00229 $suggested = $this->strategy->calculate_peer_grade($grades); 00230 } 00231 }