PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/choice/tests/events_test.php

https://gitlab.com/JrLucena/moodle
PHP | 372 lines | 209 code | 60 blank | 103 comment | 1 complexity | b337cee65d00bf774def4d2127b90b30 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. * Events tests.
  18. *
  19. * @package mod_choice
  20. * @copyright 2013 Adrian Greeve <adrian@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die();
  24. global $CFG;
  25. require_once($CFG->dirroot . '/mod/choice/lib.php');
  26. /**
  27. * Events tests class.
  28. *
  29. * @package mod_choice
  30. * @copyright 2013 Adrian Greeve <adrian@moodle.com>
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class mod_choice_events_testcase extends advanced_testcase {
  34. /** @var choice_object */
  35. protected $choice;
  36. /** @var course_object */
  37. protected $course;
  38. /** @var cm_object Course module object. */
  39. protected $cm;
  40. /** @var context_object */
  41. protected $context;
  42. /**
  43. * Setup often used objects for the following tests.
  44. */
  45. protected function setup() {
  46. global $DB;
  47. $this->resetAfterTest();
  48. $this->course = $this->getDataGenerator()->create_course();
  49. $this->choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course->id));
  50. $this->cm = $DB->get_record('course_modules', array('id' => $this->choice->cmid));
  51. $this->context = context_module::instance($this->choice->cmid);
  52. }
  53. /**
  54. * Test to ensure that event data is being stored correctly.
  55. */
  56. public function test_answer_submitted() {
  57. global $DB;
  58. // Generate user data.
  59. $user = $this->getDataGenerator()->create_user();
  60. $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
  61. // Redirect event.
  62. $sink = $this->redirectEvents();
  63. choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm);
  64. $events = $sink->get_events();
  65. // Data checking.
  66. $this->assertCount(1, $events);
  67. $this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]);
  68. $this->assertEquals($user->id, $events[0]->userid);
  69. $this->assertEquals(context_module::instance($this->choice->cmid), $events[0]->get_context());
  70. $this->assertEquals($this->choice->id, $events[0]->other['choiceid']);
  71. $this->assertEquals(array($optionids[3]), $events[0]->other['optionid']);
  72. $expected = array($this->course->id, "choice", "choose", 'view.php?id=' . $this->cm->id, $this->choice->id, $this->cm->id);
  73. $this->assertEventLegacyLogData($expected, $events[0]);
  74. $this->assertEventContextNotUsed($events[0]);
  75. $sink->close();
  76. }
  77. /**
  78. * Test to ensure that multiple choice data is being stored correctly.
  79. */
  80. public function test_answer_submitted_multiple() {
  81. global $DB;
  82. // Generate user data.
  83. $user = $this->getDataGenerator()->create_user();
  84. // Create multiple choice.
  85. $choice = $this->getDataGenerator()->create_module('choice', array('course' => $this->course->id,
  86. 'allowmultiple' => 1));
  87. $cm = $DB->get_record('course_modules', array('id' => $choice->cmid));
  88. $context = context_module::instance($choice->cmid);
  89. $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $choice->id)));
  90. $submittedoptionids = array($optionids[1], $optionids[3]);
  91. // Redirect event.
  92. $sink = $this->redirectEvents();
  93. choice_user_submit_response($submittedoptionids, $choice, $user->id, $this->course, $cm);
  94. $events = $sink->get_events();
  95. // Data checking.
  96. $this->assertCount(1, $events);
  97. $this->assertInstanceOf('\mod_choice\event\answer_submitted', $events[0]);
  98. $this->assertEquals($user->id, $events[0]->userid);
  99. $this->assertEquals(context_module::instance($choice->cmid), $events[0]->get_context());
  100. $this->assertEquals($choice->id, $events[0]->other['choiceid']);
  101. $this->assertEquals($submittedoptionids, $events[0]->other['optionid']);
  102. $expected = array($this->course->id, "choice", "choose", 'view.php?id=' . $cm->id, $choice->id, $cm->id);
  103. $this->assertEventLegacyLogData($expected, $events[0]);
  104. $this->assertEventContextNotUsed($events[0]);
  105. $sink->close();
  106. }
  107. /**
  108. * Test custom validations.
  109. */
  110. public function test_answer_submitted_other_exception() {
  111. // Generate user data.
  112. $user = $this->getDataGenerator()->create_user();
  113. $eventdata = array();
  114. $eventdata['context'] = $this->context;
  115. $eventdata['objectid'] = 2;
  116. $eventdata['userid'] = $user->id;
  117. $eventdata['courseid'] = $this->course->id;
  118. $eventdata['other'] = array();
  119. // Make sure content identifier is always set.
  120. $this->setExpectedException('coding_exception');
  121. $event = \mod_choice\event\answer_submitted::create($eventdata);
  122. $event->trigger();
  123. $this->assertEventContextNotUsed($event);
  124. }
  125. /**
  126. * Test to ensure that event data is being stored correctly.
  127. */
  128. public function test_answer_updated() {
  129. global $DB;
  130. // Generate user data.
  131. $user = $this->getDataGenerator()->create_user();
  132. $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
  133. // Create the first answer.
  134. choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm);
  135. // Redirect event.
  136. $sink = $this->redirectEvents();
  137. // Now choose a different answer.
  138. choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm);
  139. $events = $sink->get_events();
  140. // Data checking.
  141. $this->assertCount(1, $events);
  142. $this->assertInstanceOf('\mod_choice\event\answer_updated', $events[0]);
  143. $this->assertEquals($user->id, $events[0]->userid);
  144. $this->assertEquals(context_module::instance($this->choice->cmid), $events[0]->get_context());
  145. $this->assertEquals($this->choice->id, $events[0]->other['choiceid']);
  146. $this->assertEquals($optionids[3], $events[0]->other['optionid']);
  147. $expected = array($this->course->id, "choice", "choose again", 'view.php?id=' . $this->cm->id,
  148. $this->choice->id, $this->cm->id);
  149. $this->assertEventLegacyLogData($expected, $events[0]);
  150. $this->assertEventContextNotUsed($events[0]);
  151. $sink->close();
  152. }
  153. /**
  154. * Test custom validations
  155. * for answer_updated event.
  156. */
  157. public function test_answer_updated_other_exception() {
  158. // Generate user data.
  159. $user = $this->getDataGenerator()->create_user();
  160. $eventdata = array();
  161. $eventdata['context'] = $this->context;
  162. $eventdata['objectid'] = 2;
  163. $eventdata['userid'] = $user->id;
  164. $eventdata['courseid'] = $this->course->id;
  165. $eventdata['other'] = array();
  166. // Make sure content identifier is always set.
  167. $this->setExpectedException('coding_exception');
  168. $event = \mod_choice\event\answer_updated::create($eventdata);
  169. $event->trigger();
  170. $this->assertEventContextNotUsed($event);
  171. }
  172. /**
  173. * Test to ensure that event data is being stored correctly.
  174. */
  175. public function test_answer_deleted() {
  176. global $DB, $USER;
  177. // Generate user data.
  178. $user = $this->getDataGenerator()->create_user();
  179. $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
  180. // Create the first answer.
  181. choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm);
  182. // Get the users response.
  183. $answer = $DB->get_record('choice_answers', array('userid' => $user->id, 'choiceid' => $this->choice->id),
  184. '*', $strictness = IGNORE_MULTIPLE);
  185. // Redirect event.
  186. $sink = $this->redirectEvents();
  187. // Now delete the answer.
  188. choice_delete_responses(array($answer->id), $this->choice, $this->cm, $this->course);
  189. // Get our event event.
  190. $events = $sink->get_events();
  191. $event = reset($events);
  192. // Data checking.
  193. $this->assertInstanceOf('\mod_choice\event\answer_deleted', $event);
  194. $this->assertEquals($USER->id, $event->userid);
  195. $this->assertEquals($user->id, $event->relateduserid);
  196. $this->assertEquals(context_module::instance($this->choice->cmid), $event->get_context());
  197. $this->assertEquals($this->choice->id, $event->other['choiceid']);
  198. $this->assertEquals($answer->optionid, $event->other['optionid']);
  199. $this->assertEventContextNotUsed($event);
  200. $sink->close();
  201. }
  202. /**
  203. * Test to ensure that event data is being stored correctly.
  204. */
  205. public function test_report_viewed() {
  206. global $USER;
  207. $this->resetAfterTest();
  208. // Generate user data.
  209. $this->setAdminUser();
  210. $eventdata = array();
  211. $eventdata['objectid'] = $this->choice->id;
  212. $eventdata['context'] = $this->context;
  213. $eventdata['courseid'] = $this->course->id;
  214. $eventdata['other']['content'] = 'choicereportcontentviewed';
  215. // This is fired in a page view so we can't run this through a function.
  216. $event = \mod_choice\event\report_viewed::create($eventdata);
  217. // Redirect event.
  218. $sink = $this->redirectEvents();
  219. $event->trigger();
  220. $event = $sink->get_events();
  221. // Data checking.
  222. $this->assertCount(1, $event);
  223. $this->assertInstanceOf('\mod_choice\event\report_viewed', $event[0]);
  224. $this->assertEquals($USER->id, $event[0]->userid);
  225. $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
  226. $expected = array($this->course->id, "choice", "report", 'report.php?id=' . $this->context->instanceid,
  227. $this->choice->id, $this->context->instanceid);
  228. $this->assertEventLegacyLogData($expected, $event[0]);
  229. $this->assertEventContextNotUsed($event[0]);
  230. $sink->close();
  231. }
  232. /**
  233. * Test to ensure that event data is being stored correctly.
  234. */
  235. public function test_report_downloaded() {
  236. global $USER;
  237. $this->resetAfterTest();
  238. // Generate user data.
  239. $this->setAdminUser();
  240. $eventdata = array();
  241. $eventdata['context'] = $this->context;
  242. $eventdata['courseid'] = $this->course->id;
  243. $eventdata['other']['content'] = 'choicereportcontentviewed';
  244. $eventdata['other']['format'] = 'csv';
  245. $eventdata['other']['choiceid'] = $this->choice->id;
  246. // This is fired in a page view so we can't run this through a function.
  247. $event = \mod_choice\event\report_downloaded::create($eventdata);
  248. // Redirect event.
  249. $sink = $this->redirectEvents();
  250. $event->trigger();
  251. $event = $sink->get_events();
  252. // Data checking.
  253. $this->assertCount(1, $event);
  254. $this->assertInstanceOf('\mod_choice\event\report_downloaded', $event[0]);
  255. $this->assertEquals($USER->id, $event[0]->userid);
  256. $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
  257. $this->assertEquals('csv', $event[0]->other['format']);
  258. $this->assertEquals($this->choice->id, $event[0]->other['choiceid']);
  259. $this->assertEventContextNotUsed($event[0]);
  260. $sink->close();
  261. }
  262. /**
  263. * Test to ensure that event data is being stored correctly.
  264. */
  265. public function test_course_module_viewed() {
  266. global $USER;
  267. // Generate user data.
  268. $this->setAdminUser();
  269. $eventdata = array();
  270. $eventdata['objectid'] = $this->choice->id;
  271. $eventdata['context'] = $this->context;
  272. $eventdata['courseid'] = $this->course->id;
  273. $eventdata['other']['content'] = 'pageresourceview';
  274. // This is fired in a page view so we can't run this through a function.
  275. $event = \mod_choice\event\course_module_viewed::create($eventdata);
  276. // Redirect event.
  277. $sink = $this->redirectEvents();
  278. $event->trigger();
  279. $event = $sink->get_events();
  280. // Data checking.
  281. $this->assertCount(1, $event);
  282. $this->assertInstanceOf('\mod_choice\event\course_module_viewed', $event[0]);
  283. $this->assertEquals($USER->id, $event[0]->userid);
  284. $this->assertEquals(context_module::instance($this->choice->cmid), $event[0]->get_context());
  285. $expected = array($this->course->id, "choice", "view", 'view.php?id=' . $this->context->instanceid,
  286. $this->choice->id, $this->context->instanceid);
  287. $this->assertEventLegacyLogData($expected, $event[0]);
  288. $this->assertEventContextNotUsed($event[0]);
  289. $sink->close();
  290. }
  291. /**
  292. * Test to ensure that event data is being stored correctly.
  293. */
  294. public function test_course_module_instance_list_viewed_viewed() {
  295. global $USER;
  296. // Not much can be tested here as the event is only triggered on a page load,
  297. // let's just check that the event contains the expected basic information.
  298. $this->setAdminUser();
  299. $params = array('context' => context_course::instance($this->course->id));
  300. $event = \mod_choice\event\course_module_instance_list_viewed::create($params);
  301. $sink = $this->redirectEvents();
  302. $event->trigger();
  303. $events = $sink->get_events();
  304. $event = reset($events);
  305. $this->assertInstanceOf('\mod_choice\event\course_module_instance_list_viewed', $event);
  306. $this->assertEquals($USER->id, $event->userid);
  307. $this->assertEquals(context_course::instance($this->course->id), $event->get_context());
  308. $expected = array($this->course->id, 'choice', 'view all', 'index.php?id=' . $this->course->id, '');
  309. $this->assertEventLegacyLogData($expected, $event);
  310. $this->assertEventContextNotUsed($event);
  311. }
  312. }