/badges/tests/events_test.php

https://github.com/markn86/moodle · PHP · 375 lines · 198 code · 60 blank · 117 comment · 1 complexity · 4928df6d4fd3cdca08df90330a97c1d0 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. * Badge events tests.
  18. *
  19. * @package core_badges
  20. * @copyright 2015 onwards Simey Lameze <simey@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 . '/badges/tests/badgeslib_test.php');
  26. /**
  27. * Badge events tests class.
  28. *
  29. * @package core_badges
  30. * @copyright 2015 onwards Simey Lameze <simey@moodle.com>
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class events_test extends badgeslib_test {
  34. /**
  35. * Test badge awarded event.
  36. */
  37. public function test_badge_awarded() {
  38. $systemcontext = context_system::instance();
  39. $sink = $this->redirectEvents();
  40. $badge = new badge($this->badgeid);
  41. $badge->issue($this->user->id, true);
  42. $badge->is_issued($this->user->id);
  43. $events = $sink->get_events();
  44. $this->assertCount(1, $events);
  45. $event = reset($events);
  46. $this->assertInstanceOf('\core\event\badge_awarded', $event);
  47. $this->assertEquals($this->badgeid, $event->objectid);
  48. $this->assertEquals($this->user->id, $event->relateduserid);
  49. $this->assertEquals($systemcontext, $event->get_context());
  50. $sink->close();
  51. }
  52. /**
  53. * Test the badge created event.
  54. *
  55. * There is no external API for creating a badge, so the unit test will simply
  56. * create and trigger the event and ensure data is returned as expected.
  57. */
  58. public function test_badge_created() {
  59. $badge = new badge($this->badgeid);
  60. // Trigger an event: badge created.
  61. $eventparams = array(
  62. 'userid' => $badge->usercreated,
  63. 'objectid' => $badge->id,
  64. 'context' => $badge->get_context(),
  65. );
  66. $event = \core\event\badge_created::create($eventparams);
  67. // Trigger and capture the event.
  68. $sink = $this->redirectEvents();
  69. $event->trigger();
  70. $events = $sink->get_events();
  71. $event = reset($events);
  72. // Check that the event data is valid.
  73. $this->assertInstanceOf('\core\event\badge_created', $event);
  74. $this->assertEquals($badge->usercreated, $event->userid);
  75. $this->assertEquals($badge->id, $event->objectid);
  76. $this->assertDebuggingNotCalled();
  77. $sink->close();
  78. }
  79. /**
  80. * Test the badge archived event.
  81. *
  82. */
  83. public function test_badge_archived() {
  84. $badge = new badge($this->badgeid);
  85. $sink = $this->redirectEvents();
  86. // Trigger and capture the event.
  87. $badge->delete(true);
  88. $events = $sink->get_events();
  89. $this->assertCount(2, $events);
  90. $event = $events[1];
  91. // Check that the event data is valid.
  92. $this->assertInstanceOf('\core\event\badge_archived', $event);
  93. $this->assertEquals($badge->id, $event->objectid);
  94. $this->assertDebuggingNotCalled();
  95. $sink->close();
  96. }
  97. /**
  98. * Test the badge updated event.
  99. *
  100. */
  101. public function test_badge_updated() {
  102. $badge = new badge($this->badgeid);
  103. $sink = $this->redirectEvents();
  104. // Trigger and capture the event.
  105. $badge->save();
  106. $events = $sink->get_events();
  107. $event = reset($events);
  108. $this->assertCount(1, $events);
  109. // Check that the event data is valid.
  110. $this->assertInstanceOf('\core\event\badge_updated', $event);
  111. $this->assertEquals($badge->id, $event->objectid);
  112. $this->assertDebuggingNotCalled();
  113. $sink->close();
  114. }
  115. /**
  116. * Test the badge deleted event.
  117. */
  118. public function test_badge_deleted() {
  119. $badge = new badge($this->badgeid);
  120. $sink = $this->redirectEvents();
  121. // Trigger and capture the event.
  122. $badge->delete(false);
  123. $events = $sink->get_events();
  124. $event = reset($events);
  125. $this->assertCount(1, $events);
  126. // Check that the event data is valid.
  127. $this->assertInstanceOf('\core\event\badge_deleted', $event);
  128. $this->assertEquals($badge->id, $event->objectid);
  129. $this->assertDebuggingNotCalled();
  130. $sink->close();
  131. }
  132. /**
  133. * Test the badge duplicated event.
  134. *
  135. */
  136. public function test_badge_duplicated() {
  137. $badge = new badge($this->badgeid);
  138. $sink = $this->redirectEvents();
  139. // Trigger and capture the event.
  140. $newid = $badge->make_clone();
  141. $events = $sink->get_events();
  142. $event = reset($events);
  143. $this->assertCount(1, $events);
  144. // Check that the event data is valid.
  145. $this->assertInstanceOf('\core\event\badge_duplicated', $event);
  146. $this->assertEquals($newid, $event->objectid);
  147. $this->assertDebuggingNotCalled();
  148. $sink->close();
  149. }
  150. /**
  151. * Test the badge disabled event.
  152. *
  153. */
  154. public function test_badge_disabled() {
  155. $badge = new badge($this->badgeid);
  156. $sink = $this->redirectEvents();
  157. // Trigger and capture the event.
  158. $badge->set_status(BADGE_STATUS_INACTIVE);
  159. $events = $sink->get_events();
  160. $event = reset($events);
  161. $this->assertCount(2, $events);
  162. $event = $events[1];
  163. // Check that the event data is valid.
  164. $this->assertInstanceOf('\core\event\badge_disabled', $event);
  165. $this->assertEquals($badge->id, $event->objectid);
  166. $this->assertDebuggingNotCalled();
  167. $sink->close();
  168. }
  169. /**
  170. * Test the badge enabled event.
  171. *
  172. */
  173. public function test_badge_enabled() {
  174. $badge = new badge($this->badgeid);
  175. $sink = $this->redirectEvents();
  176. // Trigger and capture the event.
  177. $badge->set_status(BADGE_STATUS_ACTIVE);
  178. $events = $sink->get_events();
  179. $event = reset($events);
  180. $this->assertCount(2, $events);
  181. $event = $events[1];
  182. // Check that the event data is valid.
  183. $this->assertInstanceOf('\core\event\badge_enabled', $event);
  184. $this->assertEquals($badge->id, $event->objectid);
  185. $this->assertDebuggingNotCalled();
  186. $sink->close();
  187. }
  188. /**
  189. * Test the badge criteria created event.
  190. *
  191. * There is no external API for this, so the unit test will simply
  192. * create and trigger the event and ensure data is returned as expected.
  193. */
  194. public function test_badge_criteria_created() {
  195. $badge = new badge($this->badgeid);
  196. // Trigger and capture the event.
  197. $sink = $this->redirectEvents();
  198. $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
  199. $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
  200. $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
  201. $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
  202. $criteriaprofile->save($params);
  203. $events = $sink->get_events();
  204. $event = reset($events);
  205. // Check that the event data is valid.
  206. $this->assertCount(1, $events);
  207. $this->assertInstanceOf('\core\event\badge_criteria_created', $event);
  208. $this->assertEquals($criteriaprofile->id, $event->objectid);
  209. $this->assertEquals($criteriaprofile->badgeid, $event->other['badgeid']);
  210. $this->assertDebuggingNotCalled();
  211. $sink->close();
  212. }
  213. /**
  214. * Test the badge criteria updated event.
  215. *
  216. * There is no external API for this, so the unit test will simply
  217. * create and trigger the event and ensure data is returned as expected.
  218. */
  219. public function test_badge_criteria_updated() {
  220. $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
  221. $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
  222. $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
  223. $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address');
  224. $criteriaprofile->save($params);
  225. $badge = new badge($this->badgeid);
  226. // Trigger and capture the event.
  227. $sink = $this->redirectEvents();
  228. $criteria = $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE];
  229. $params2 = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'id' => $criteria->id);
  230. $criteria->save((array)$params2);
  231. $events = $sink->get_events();
  232. $event = reset($events);
  233. // Check that the event data is valid.
  234. $this->assertCount(1, $events);
  235. $this->assertInstanceOf('\core\event\badge_criteria_updated', $event);
  236. $this->assertEquals($criteria->id, $event->objectid);
  237. $this->assertEquals($this->badgeid, $event->other['badgeid']);
  238. $this->assertDebuggingNotCalled();
  239. $sink->close();
  240. }
  241. /**
  242. * Test the badge criteria deleted event.
  243. *
  244. * There is no external API for this, so the unit test will simply
  245. * create and trigger the event and ensure data is returned as expected.
  246. */
  247. public function test_badge_criteria_deleted() {
  248. $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
  249. $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
  250. $badge = new badge($this->badgeid);
  251. // Trigger and capture the event.
  252. $sink = $this->redirectEvents();
  253. $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->delete();
  254. $events = $sink->get_events();
  255. $event = reset($events);
  256. // Check that the event data is valid.
  257. $this->assertCount(1, $events);
  258. $this->assertInstanceOf('\core\event\badge_criteria_deleted', $event);
  259. $this->assertEquals($criteriaoverall->badgeid, $event->other['badgeid']);
  260. $this->assertDebuggingNotCalled();
  261. $sink->close();
  262. }
  263. /**
  264. * Test the badge viewed event.
  265. *
  266. * There is no external API for viewing a badge, so the unit test will simply
  267. * create and trigger the event and ensure data is returned as expected.
  268. */
  269. public function test_badge_viewed() {
  270. $badge = new badge($this->badgeid);
  271. // Trigger an event: badge viewed.
  272. $other = array('badgeid' => $badge->id, 'badgehash' => '12345678');
  273. $eventparams = array(
  274. 'context' => $badge->get_context(),
  275. 'other' => $other,
  276. );
  277. $event = \core\event\badge_viewed::create($eventparams);
  278. // Trigger and capture the event.
  279. $sink = $this->redirectEvents();
  280. $event->trigger();
  281. $events = $sink->get_events();
  282. $event = reset($events);
  283. // Check that the event data is valid.
  284. $this->assertInstanceOf('\core\event\badge_viewed', $event);
  285. $this->assertEquals('12345678', $event->other['badgehash']);
  286. $this->assertEquals($badge->id, $event->other['badgeid']);
  287. $this->assertDebuggingNotCalled();
  288. $sink->close();
  289. }
  290. /**
  291. * Test the badge listing viewed event.
  292. *
  293. * There is no external API for viewing a badge, so the unit test will simply
  294. * create and trigger the event and ensure data is returned as expected.
  295. */
  296. public function test_badge_listing_viewed() {
  297. // Trigger an event: badge listing viewed.
  298. $context = context_system::instance();
  299. $eventparams = array(
  300. 'context' => $context,
  301. 'other' => array('badgetype' => BADGE_TYPE_SITE)
  302. );
  303. $event = \core\event\badge_listing_viewed::create($eventparams);
  304. // Trigger and capture the event.
  305. $sink = $this->redirectEvents();
  306. $event->trigger();
  307. $events = $sink->get_events();
  308. $event = reset($events);
  309. // Check that the event data is valid.
  310. $this->assertInstanceOf('\core\event\badge_listing_viewed', $event);
  311. $this->assertEquals(BADGE_TYPE_SITE, $event->other['badgetype']);
  312. $this->assertDebuggingNotCalled();
  313. $sink->close();
  314. }
  315. }