|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 defined('MOODLE_INTERNAL') || die(); 00027 00028 // Include all the needed stuff 00029 require_once($CFG->dirroot . '/rating/lib.php'); 00030 00034 class rating_db_test extends UnitTestCaseUsingDatabase { 00035 00036 public static $includecoverage = array( 00037 'rating/lib.php' 00038 ); 00039 00040 protected $testtables = array( 00041 'lib' => array( 00042 'rating', 'scale', 'context', 'capabilities', 'role_assignments', 'role_capabilities', 'course')); 00043 00044 protected $syscontext; 00045 protected $neededcaps = array('view', 'viewall', 'viewany', 'rate'); 00046 protected $originaldefaultfrontpageroleid; 00047 00048 public function setUp() { 00049 global $CFG; 00050 parent::setUp(); 00051 00052 foreach ($this->testtables as $dir => $tables) { 00053 $this->create_test_tables($tables, $dir); // Create tables 00054 } 00055 00056 $this->switch_to_test_db(); // Switch to test DB for all the execution 00057 00058 // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily) 00059 $this->create_system_context_record(); 00060 // Reset all caches 00061 accesslib_clear_all_caches_for_unit_testing(); 00062 00063 $this->syscontext = get_context_instance(CONTEXT_SYSTEM); 00064 00065 $this->fill_records(); 00066 00067 // Ignore any frontpageroleid, that would require to crete more contexts 00068 $this->originaldefaultfrontpageroleid = $CFG->defaultfrontpageroleid; 00069 $CFG->defaultfrontpageroleid = null; 00070 } 00071 00072 public function tearDown() { 00073 global $CFG; 00074 // Recover original frontpageroleid 00075 $CFG->defaultfrontpageroleid = $this->originaldefaultfrontpageroleid; 00076 parent::tearDown(); 00077 } 00078 00079 private function fill_records() { 00080 global $DB; 00081 00082 // Add the capabilities used by ratings 00083 foreach ($this->neededcaps as $neededcap) { 00084 $this->testdb->insert_record('capabilities', (object)array('name' => 'moodle/rating:' . $neededcap, 00085 'contextlevel' => CONTEXT_COURSE)); 00086 } 00087 } 00088 00092 function test_get_ratings_sql() { 00093 00094 // We load 3 items. Each is rated twice. For simplicity itemid == user id of the item owner 00095 $ctxid = $this->syscontext->id; 00096 $this->load_test_data('rating', 00097 array('contextid', 'component', 'ratingarea', 'itemid', 'scaleid', 'rating', 'userid', 'timecreated', 'timemodified'), array( 00098 00099 //user 1's items. Average == 2 00100 array( $ctxid , 'mod_forum', 'post', 1 , 10 , 1 , 2 , 1 , 1), 00101 array( $ctxid , 'mod_forum', 'post', 1 , 10 , 3 , 3 , 1 , 1), 00102 00103 //user 2's items. Average == 3 00104 array( $ctxid , 'mod_forum', 'post', 2 , 10 , 1 , 1 , 1 , 1), 00105 array( $ctxid , 'mod_forum', 'post', 2 , 10 , 5 , 3 , 1 , 1), 00106 00107 //user 3's items. Average == 4 00108 array( $ctxid , 'mod_forum', 'post', 3 , 10 , 3 , 1 , 1 , 1), 00109 array( $ctxid , 'mod_forum', 'post', 3 , 10 , 5 , 2 , 1 , 1) 00110 )); 00111 00112 // a post (item) by user 1 (rated above by user 2 and 3 with average = 2) 00113 $user1posts = array( 00114 (object)array('id' => 1, 'userid' => 1, 'message' => 'hello')); 00115 // a post (item) by user 2 (rated above by user 1 and 3 with average = 3) 00116 $user2posts = array( 00117 (object)array('id' => 2, 'userid' => 2, 'message' => 'world')); 00118 // a post (item) by user 3 (rated above by user 1 and 2 with average = 4) 00119 $user3posts = array( 00120 (object)array('id' => 3, 'userid' => 3, 'message' => 'moodle')); 00121 00122 // Prepare the default options 00123 $defaultoptions = array ( 00124 'context' => $this->syscontext, 00125 'component' => 'mod_forum', 00126 'ratingarea' => 'post', 00127 'scaleid' => 10, 00128 'aggregate' => RATING_AGGREGATE_AVERAGE); 00129 00130 $rm = new mockup_rating_manager(); 00131 00132 // STEP 1: Retreive ratings using the current user 00133 00134 // Get results for user 1's item (expected average 1 + 3 / 2 = 2) 00135 $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts)); 00136 $result = $rm->get_ratings($toptions); 00137 $this->assertEqual(count($result), count($user1posts)); 00138 $this->assertEqual($result[0]->id, $user1posts[0]->id); 00139 $this->assertEqual($result[0]->userid, $user1posts[0]->userid); 00140 $this->assertEqual($result[0]->message, $user1posts[0]->message); 00141 $this->assertEqual($result[0]->rating->count, 2); 00142 $this->assertEqual($result[0]->rating->aggregate, 2); 00143 // Note that $result[0]->rating->rating is somewhat random 00144 // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests 00145 00146 // Get results for items of user 2 (expected average 1 + 5 / 2 = 3) 00147 $toptions = (object)array_merge($defaultoptions, array('items' => $user2posts)); 00148 $result = $rm->get_ratings($toptions); 00149 $this->assertEqual(count($result), count($user2posts)); 00150 $this->assertEqual($result[0]->id, $user2posts[0]->id); 00151 $this->assertEqual($result[0]->userid, $user2posts[0]->userid); 00152 $this->assertEqual($result[0]->message, $user2posts[0]->message); 00153 $this->assertEqual($result[0]->rating->count, 2); 00154 $this->assertEqual($result[0]->rating->aggregate, 3); 00155 // Note that $result[0]->rating->rating is somewhat random 00156 // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests 00157 00158 // Get results for items of user 3 (expected average 3 + 5 / 2 = 4) 00159 $toptions = (object)array_merge($defaultoptions, array('items' => $user3posts)); 00160 $result = $rm->get_ratings($toptions); 00161 $this->assertEqual(count($result), count($user3posts)); 00162 $this->assertEqual($result[0]->id, $user3posts[0]->id); 00163 $this->assertEqual($result[0]->userid, $user3posts[0]->userid); 00164 $this->assertEqual($result[0]->message, $user3posts[0]->message); 00165 $this->assertEqual($result[0]->rating->count, 2); 00166 $this->assertEqual($result[0]->rating->aggregate, 4); 00167 // Note that $result[0]->rating->rating is somewhat random 00168 // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests 00169 00170 // Get results for items of user 1 & 2 together (expected averages are 2 and 3, as tested above) 00171 $posts = array_merge($user1posts, $user2posts); 00172 $toptions = (object)array_merge($defaultoptions, array('items' => $posts)); 00173 $result = $rm->get_ratings($toptions); 00174 $this->assertEqual(count($result), count($posts)); 00175 $this->assertEqual($result[0]->id, $posts[0]->id); 00176 $this->assertEqual($result[0]->userid, $posts[0]->userid); 00177 $this->assertEqual($result[0]->message, $posts[0]->message); 00178 $this->assertEqual($result[0]->rating->count, 2); 00179 $this->assertEqual($result[0]->rating->aggregate, 2); 00180 // Note that $result[0]->rating->rating is somewhat random 00181 // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests 00182 00183 $this->assertEqual($result[1]->id, $posts[1]->id); 00184 $this->assertEqual($result[1]->userid, $posts[1]->userid); 00185 $this->assertEqual($result[1]->message, $posts[1]->message); 00186 $this->assertEqual($result[1]->rating->count, 2); 00187 $this->assertEqual($result[1]->rating->aggregate, 3); 00188 // Note that $result[0]->rating->rating is somewhat random 00189 // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests 00190 00191 // STEP 2: Retrieve ratings by a specified user 00192 // We still expect complete aggregations and counts 00193 00194 // Get results for items of user 1 rated by user 2 (avg 2, rating 1) 00195 $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 2)); 00196 $result = $rm->get_ratings($toptions); 00197 $this->assertEqual(count($result), count($user1posts)); 00198 $this->assertEqual($result[0]->id, $user1posts[0]->id); 00199 $this->assertEqual($result[0]->userid, $user1posts[0]->userid); 00200 $this->assertEqual($result[0]->message, $user1posts[0]->message); 00201 $this->assertEqual($result[0]->rating->count, 2); 00202 $this->assertEqual($result[0]->rating->aggregate, 2); 00203 $this->assertEqual($result[0]->rating->rating, 1); //user 2 rated user 1 "1" 00204 $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid 00205 00206 // Get results for items of user 1 rated by user 3 00207 $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 3)); 00208 $result = $rm->get_ratings($toptions); 00209 $this->assertEqual(count($result), count($user1posts)); 00210 $this->assertEqual($result[0]->id, $user1posts[0]->id); 00211 $this->assertEqual($result[0]->userid, $user1posts[0]->userid); 00212 $this->assertEqual($result[0]->message, $user1posts[0]->message); 00213 $this->assertEqual($result[0]->rating->count, 2); 00214 $this->assertEqual($result[0]->rating->aggregate, 2); 00215 $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 1 "3" 00216 $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid 00217 00218 // Get results for items of user 1 & 2 together rated by user 3 00219 $posts = array_merge($user1posts, $user2posts); 00220 $toptions = (object)array_merge($defaultoptions, array('items' => $posts, 'userid' => 3)); 00221 $result = $rm->get_ratings($toptions); 00222 $this->assertEqual(count($result), count($posts)); 00223 $this->assertEqual($result[0]->id, $posts[0]->id); 00224 $this->assertEqual($result[0]->userid, $posts[0]->userid); 00225 $this->assertEqual($result[0]->message, $posts[0]->message); 00226 $this->assertEqual($result[0]->rating->count, 2); 00227 $this->assertEqual($result[0]->rating->aggregate, 2); 00228 $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 1 "3" 00229 $this->assertEqual($result[0]->rating->userid, $toptions->userid); // Must be the passed userid 00230 00231 $this->assertEqual($result[1]->id, $posts[1]->id); 00232 $this->assertEqual($result[1]->userid, $posts[1]->userid); 00233 $this->assertEqual($result[1]->message, $posts[1]->message); 00234 $this->assertEqual($result[1]->rating->count, 2); 00235 $this->assertEqual($result[1]->rating->aggregate, 3); 00236 $this->assertEqual($result[0]->rating->rating, 3); //user 3 rated user 2 "5" 00237 $this->assertEqual($result[1]->rating->userid, $toptions->userid); // Must be the passed userid 00238 00239 // STEP 3: Some special cases 00240 00241 // Get results for user 1's items (expected average 1 + 3 / 2 = 2) 00242 // supplying a non-existent user id so no rating from that user should be found 00243 $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts)); 00244 $toptions->userid = 123456; //non-existent user 00245 $result = $rm->get_ratings($toptions); 00246 $this->assertNull($result[0]->rating->userid); 00247 $this->assertNull($result[0]->rating->rating); 00248 $this->assertEqual($result[0]->rating->aggregate, 2);//should still get the aggregate 00249 00250 // Get results for items of user 2 (expected average 1 + 5 / 2 = 3) 00251 // Supplying the user id of the user who owns the items so no rating should be found 00252 $toptions = (object)array_merge($defaultoptions, array('items' => $user2posts)); 00253 $toptions->userid = 2; //user 2 viewing the ratings of their own item 00254 $result = $rm->get_ratings($toptions); 00255 //these should be null as the user is viewing their own item and thus cannot rate 00256 $this->assertNull($result[0]->rating->userid); 00257 $this->assertNull($result[0]->rating->rating); 00258 $this->assertEqual($result[0]->rating->aggregate, 3);//should still get the aggregate 00259 } 00260 } 00261 00265 class mockup_rating_manager extends rating_manager { 00266 00270 public function get_plugin_permissions_array($contextid, $component, $ratingarea) { 00271 return array( 00272 'rate' => true, 00273 'view' => true, 00274 'viewany' => true, 00275 'viewall' => true); 00276 } 00277 00278 }