PageRenderTime 21ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/rating/tests/rating_test.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 241 lines | 140 code | 32 blank | 69 comment | 1 complexity | f235f40d737b9b32e2eb7f34fea8b1ea MD5 | raw file
  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. * Unit tests for rating/lib.php
  18. *
  19. * @package core_ratings
  20. * @category phpunit
  21. * @copyright 2011 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. // Include all the needed stuff
  26. global $CFG;
  27. require_once($CFG->dirroot . '/rating/lib.php');
  28. /**
  29. * Unit test case for all the rating/lib.php requiring DB mockup & manipulation
  30. */
  31. class core_rating_testcase extends advanced_testcase {
  32. protected $syscontext;
  33. protected $neededcaps = array('view', 'viewall', 'viewany', 'rate');
  34. protected $originaldefaultfrontpageroleid;
  35. public function setUp() {
  36. global $CFG;
  37. parent::setUp();
  38. $this->resetAfterTest(true);
  39. $CFG->defaultfrontpageroleid = null;
  40. }
  41. /**
  42. * Test the current get_ratings method main sql
  43. */
  44. function test_get_ratings_sql() {
  45. global $DB;
  46. // We load 3 items. Each is rated twice. For simplicity itemid == user id of the item owner
  47. $ctxid = context_system::instance()->id;
  48. $ratings = array(
  49. //user 1's items. Average == 2
  50. array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>1 ,'scaleid'=>10 ,'rating'=>1 ,'userid'=>2 ,'timecreated'=>1 ,'timemodified'=>1),
  51. array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>1 ,'scaleid'=>10 ,'rating'=>3 ,'userid'=>3 ,'timecreated'=>1 ,'timemodified'=>1),
  52. //user 2's items. Average == 3
  53. array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>2 ,'scaleid'=>10 ,'rating'=>1 ,'userid'=>1 ,'timecreated'=>1 ,'timemodified'=>1),
  54. array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>2 ,'scaleid'=>10 ,'rating'=>5 ,'userid'=>3 ,'timecreated'=>1 ,'timemodified'=>1),
  55. //user 3's items. Average == 4
  56. array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>3 ,'scaleid'=>10 ,'rating'=>3 ,'userid'=>1 ,'timecreated'=>1 ,'timemodified'=>1),
  57. array('contextid'=>$ctxid ,'component'=>'mod_forum','ratingarea'=>'post','itemid'=>3 ,'scaleid'=>10 ,'rating'=>5 ,'userid'=>2 ,'timecreated'=>1 ,'timemodified'=>1)
  58. );
  59. foreach ($ratings as $rating) {
  60. $DB->insert_record('rating', $rating);
  61. }
  62. // a post (item) by user 1 (rated above by user 2 and 3 with average = 2)
  63. $user1posts = array(
  64. (object)array('id' => 1, 'userid' => 1, 'message' => 'hello'));
  65. // a post (item) by user 2 (rated above by user 1 and 3 with average = 3)
  66. $user2posts = array(
  67. (object)array('id' => 2, 'userid' => 2, 'message' => 'world'));
  68. // a post (item) by user 3 (rated above by user 1 and 2 with average = 4)
  69. $user3posts = array(
  70. (object)array('id' => 3, 'userid' => 3, 'message' => 'moodle'));
  71. // Prepare the default options
  72. $defaultoptions = array (
  73. 'context' => context_system::instance(),
  74. 'component' => 'mod_forum',
  75. 'ratingarea' => 'post',
  76. 'scaleid' => 10,
  77. 'aggregate' => RATING_AGGREGATE_AVERAGE);
  78. $rm = new mockup_rating_manager();
  79. // STEP 1: Retreive ratings using the current user
  80. // Get results for user 1's item (expected average 1 + 3 / 2 = 2)
  81. $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts));
  82. $result = $rm->get_ratings($toptions);
  83. $this->assertEquals(count($result), count($user1posts));
  84. $this->assertEquals($result[0]->id, $user1posts[0]->id);
  85. $this->assertEquals($result[0]->userid, $user1posts[0]->userid);
  86. $this->assertEquals($result[0]->message, $user1posts[0]->message);
  87. $this->assertEquals($result[0]->rating->count, 2);
  88. $this->assertEquals($result[0]->rating->aggregate, 2);
  89. // Note that $result[0]->rating->rating is somewhat random
  90. // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
  91. // Get results for items of user 2 (expected average 1 + 5 / 2 = 3)
  92. $toptions = (object)array_merge($defaultoptions, array('items' => $user2posts));
  93. $result = $rm->get_ratings($toptions);
  94. $this->assertEquals(count($result), count($user2posts));
  95. $this->assertEquals($result[0]->id, $user2posts[0]->id);
  96. $this->assertEquals($result[0]->userid, $user2posts[0]->userid);
  97. $this->assertEquals($result[0]->message, $user2posts[0]->message);
  98. $this->assertEquals($result[0]->rating->count, 2);
  99. $this->assertEquals($result[0]->rating->aggregate, 3);
  100. // Note that $result[0]->rating->rating is somewhat random
  101. // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
  102. // Get results for items of user 3 (expected average 3 + 5 / 2 = 4)
  103. $toptions = (object)array_merge($defaultoptions, array('items' => $user3posts));
  104. $result = $rm->get_ratings($toptions);
  105. $this->assertEquals(count($result), count($user3posts));
  106. $this->assertEquals($result[0]->id, $user3posts[0]->id);
  107. $this->assertEquals($result[0]->userid, $user3posts[0]->userid);
  108. $this->assertEquals($result[0]->message, $user3posts[0]->message);
  109. $this->assertEquals($result[0]->rating->count, 2);
  110. $this->assertEquals($result[0]->rating->aggregate, 4);
  111. // Note that $result[0]->rating->rating is somewhat random
  112. // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
  113. // Get results for items of user 1 & 2 together (expected averages are 2 and 3, as tested above)
  114. $posts = array_merge($user1posts, $user2posts);
  115. $toptions = (object)array_merge($defaultoptions, array('items' => $posts));
  116. $result = $rm->get_ratings($toptions);
  117. $this->assertEquals(count($result), count($posts));
  118. $this->assertEquals($result[0]->id, $posts[0]->id);
  119. $this->assertEquals($result[0]->userid, $posts[0]->userid);
  120. $this->assertEquals($result[0]->message, $posts[0]->message);
  121. $this->assertEquals($result[0]->rating->count, 2);
  122. $this->assertEquals($result[0]->rating->aggregate, 2);
  123. // Note that $result[0]->rating->rating is somewhat random
  124. // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
  125. $this->assertEquals($result[1]->id, $posts[1]->id);
  126. $this->assertEquals($result[1]->userid, $posts[1]->userid);
  127. $this->assertEquals($result[1]->message, $posts[1]->message);
  128. $this->assertEquals($result[1]->rating->count, 2);
  129. $this->assertEquals($result[1]->rating->aggregate, 3);
  130. // Note that $result[0]->rating->rating is somewhat random
  131. // We didn't supply a user ID so $USER was used which will vary depending on who runs the tests
  132. // STEP 2: Retrieve ratings by a specified user
  133. // We still expect complete aggregations and counts
  134. // Get results for items of user 1 rated by user 2 (avg 2, rating 1)
  135. $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 2));
  136. $result = $rm->get_ratings($toptions);
  137. $this->assertEquals(count($result), count($user1posts));
  138. $this->assertEquals($result[0]->id, $user1posts[0]->id);
  139. $this->assertEquals($result[0]->userid, $user1posts[0]->userid);
  140. $this->assertEquals($result[0]->message, $user1posts[0]->message);
  141. $this->assertEquals($result[0]->rating->count, 2);
  142. $this->assertEquals($result[0]->rating->aggregate, 2);
  143. $this->assertEquals($result[0]->rating->rating, 1); //user 2 rated user 1 "1"
  144. $this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid
  145. // Get results for items of user 1 rated by user 3
  146. $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts, 'userid' => 3));
  147. $result = $rm->get_ratings($toptions);
  148. $this->assertEquals(count($result), count($user1posts));
  149. $this->assertEquals($result[0]->id, $user1posts[0]->id);
  150. $this->assertEquals($result[0]->userid, $user1posts[0]->userid);
  151. $this->assertEquals($result[0]->message, $user1posts[0]->message);
  152. $this->assertEquals($result[0]->rating->count, 2);
  153. $this->assertEquals($result[0]->rating->aggregate, 2);
  154. $this->assertEquals($result[0]->rating->rating, 3); //user 3 rated user 1 "3"
  155. $this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid
  156. // Get results for items of user 1 & 2 together rated by user 3
  157. $posts = array_merge($user1posts, $user2posts);
  158. $toptions = (object)array_merge($defaultoptions, array('items' => $posts, 'userid' => 3));
  159. $result = $rm->get_ratings($toptions);
  160. $this->assertEquals(count($result), count($posts));
  161. $this->assertEquals($result[0]->id, $posts[0]->id);
  162. $this->assertEquals($result[0]->userid, $posts[0]->userid);
  163. $this->assertEquals($result[0]->message, $posts[0]->message);
  164. $this->assertEquals($result[0]->rating->count, 2);
  165. $this->assertEquals($result[0]->rating->aggregate, 2);
  166. $this->assertEquals($result[0]->rating->rating, 3); //user 3 rated user 1 "3"
  167. $this->assertEquals($result[0]->rating->userid, $toptions->userid); // Must be the passed userid
  168. $this->assertEquals($result[1]->id, $posts[1]->id);
  169. $this->assertEquals($result[1]->userid, $posts[1]->userid);
  170. $this->assertEquals($result[1]->message, $posts[1]->message);
  171. $this->assertEquals($result[1]->rating->count, 2);
  172. $this->assertEquals($result[1]->rating->aggregate, 3);
  173. $this->assertEquals($result[0]->rating->rating, 3); //user 3 rated user 2 "5"
  174. $this->assertEquals($result[1]->rating->userid, $toptions->userid); // Must be the passed userid
  175. // STEP 3: Some special cases
  176. // Get results for user 1's items (expected average 1 + 3 / 2 = 2)
  177. // supplying a non-existent user id so no rating from that user should be found
  178. $toptions = (object)array_merge($defaultoptions, array('items' => $user1posts));
  179. $toptions->userid = 123456; //non-existent user
  180. $result = $rm->get_ratings($toptions);
  181. $this->assertNull($result[0]->rating->userid);
  182. $this->assertNull($result[0]->rating->rating);
  183. $this->assertEquals($result[0]->rating->aggregate, 2);//should still get the aggregate
  184. // Get results for items of user 2 (expected average 1 + 5 / 2 = 3)
  185. // Supplying the user id of the user who owns the items so no rating should be found
  186. $toptions = (object)array_merge($defaultoptions, array('items' => $user2posts));
  187. $toptions->userid = 2; //user 2 viewing the ratings of their own item
  188. $result = $rm->get_ratings($toptions);
  189. //these should be null as the user is viewing their own item and thus cannot rate
  190. $this->assertNull($result[0]->rating->userid);
  191. $this->assertNull($result[0]->rating->rating);
  192. $this->assertEquals($result[0]->rating->aggregate, 3);//should still get the aggregate
  193. }
  194. }
  195. /**
  196. * rating_manager subclass for unit testing without requiring capabilities to be loaded
  197. */
  198. class mockup_rating_manager extends rating_manager {
  199. /**
  200. * Overwrite get_plugin_permissions_array() so it always return granted perms for unit testing
  201. */
  202. public function get_plugin_permissions_array($contextid, $component, $ratingarea) {
  203. return array(
  204. 'rate' => true,
  205. 'view' => true,
  206. 'viewany' => true,
  207. 'viewall' => true);
  208. }
  209. }