PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/scorm/tests/events_test.php

http://github.com/moodle/moodle
PHP | 637 lines | 421 code | 51 blank | 165 comment | 8 complexity | f3ebd50d4dd6399b80f55ad19166570b MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * This file contains tests for scorm events.
  18. *
  19. * @package mod_scorm
  20. * @copyright 2013 onwards Ankit Agarwal
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. global $CFG;
  24. require_once($CFG->dirroot . '/mod/scorm/locallib.php');
  25. require_once($CFG->dirroot . '/mod/scorm/lib.php');
  26. /**
  27. * Test class for various events related to Scorm.
  28. *
  29. * @package mod_scorm
  30. * @copyright 2013 onwards Ankit Agarwal
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class mod_scorm_event_testcase extends advanced_testcase {
  34. /** @var stdClass store course object */
  35. protected $eventcourse;
  36. /** @var stdClass store user object */
  37. protected $eventuser;
  38. /** @var stdClass store scorm object */
  39. protected $eventscorm;
  40. /** @var stdClass store course module object */
  41. protected $eventcm;
  42. protected function setUp() {
  43. $this->setAdminUser();
  44. $this->eventcourse = $this->getDataGenerator()->create_course();
  45. $this->eventuser = $this->getDataGenerator()->create_user();
  46. $record = new stdClass();
  47. $record->course = $this->eventcourse->id;
  48. $this->eventscorm = $this->getDataGenerator()->create_module('scorm', $record);
  49. $this->eventcm = get_coursemodule_from_instance('scorm', $this->eventscorm->id);
  50. }
  51. /**
  52. * Tests for attempt deleted event
  53. *
  54. * @expectedException coding_exception
  55. */
  56. public function test_attempt_deleted_event() {
  57. global $USER;
  58. $this->resetAfterTest();
  59. scorm_insert_track(2, $this->eventscorm->id, 1, 4, 'cmi.core.score.raw', 10);
  60. $sink = $this->redirectEvents();
  61. scorm_delete_attempt(2, $this->eventscorm, 4);
  62. $events = $sink->get_events();
  63. $sink->close();
  64. $event = reset($events);
  65. // Verify data.
  66. $this->assertCount(3, $events);
  67. $this->assertInstanceOf('\mod_scorm\event\attempt_deleted', $event);
  68. $this->assertEquals($USER->id, $event->userid);
  69. $this->assertEquals(context_module::instance($this->eventcm->id), $event->get_context());
  70. $this->assertEquals(4, $event->other['attemptid']);
  71. $this->assertEquals(2, $event->relateduserid);
  72. $expected = array($this->eventcourse->id, 'scorm', 'delete attempts', 'report.php?id=' . $this->eventcm->id,
  73. 4, $this->eventcm->id);
  74. $this->assertEventLegacyLogData($expected, $events[0]);
  75. $this->assertEventContextNotUsed($event);
  76. // Test event validations.
  77. \mod_scorm\event\attempt_deleted::create(array(
  78. 'contextid' => 5,
  79. 'relateduserid' => 2
  80. ));
  81. $this->fail('event \\mod_scorm\\event\\attempt_deleted is not validating events properly');
  82. }
  83. /**
  84. * Tests for course module viewed event.
  85. *
  86. * There is no api involved so the best we can do is test legacy data by triggering event manually.
  87. */
  88. public function test_course_module_viewed_event() {
  89. $this->resetAfterTest();
  90. $event = \mod_scorm\event\course_module_viewed::create(array(
  91. 'objectid' => $this->eventscorm->id,
  92. 'context' => context_module::instance($this->eventcm->id),
  93. 'courseid' => $this->eventcourse->id
  94. ));
  95. // Trigger and capture the event.
  96. $sink = $this->redirectEvents();
  97. $event->trigger();
  98. $events = $sink->get_events();
  99. $event = reset($events);
  100. // Check that the legacy log data is valid.
  101. $expected = array($this->eventcourse->id, 'scorm', 'pre-view', 'view.php?id=' . $this->eventcm->id,
  102. $this->eventscorm->id, $this->eventcm->id);
  103. $this->assertEventLegacyLogData($expected, $event);
  104. $this->assertEventContextNotUsed($event);
  105. }
  106. /**
  107. * Tests for instance list viewed event.
  108. *
  109. * There is no api involved so the best we can do is test legacy data by triggering event manually.
  110. */
  111. public function test_course_module_instance_list_viewed_event() {
  112. $this->resetAfterTest();
  113. $event = \mod_scorm\event\course_module_instance_list_viewed::create(array(
  114. 'context' => context_course::instance($this->eventcourse->id),
  115. 'courseid' => $this->eventcourse->id
  116. ));
  117. // Trigger and capture the event.
  118. $sink = $this->redirectEvents();
  119. $event->trigger();
  120. $events = $sink->get_events();
  121. $event = reset($events);
  122. // Check that the legacy log data is valid.
  123. $expected = array($this->eventcourse->id, 'scorm', 'view all', 'index.php?id=' . $this->eventcourse->id, '');
  124. $this->assertEventLegacyLogData($expected, $event);
  125. $this->assertEventContextNotUsed($event);
  126. }
  127. /**
  128. * Tests for interactions viewed.
  129. *
  130. * There is no api involved so the best we can do is test legacy data by triggering event manually and test validations.
  131. */
  132. public function test_interactions_viewed_event() {
  133. $this->resetAfterTest();
  134. $event = \mod_scorm\event\interactions_viewed::create(array(
  135. 'relateduserid' => 5,
  136. 'context' => context_module::instance($this->eventcm->id),
  137. 'courseid' => $this->eventcourse->id,
  138. 'other' => array('attemptid' => 2, 'instanceid' => $this->eventscorm->id)
  139. ));
  140. // Trigger and capture the event.
  141. $sink = $this->redirectEvents();
  142. $event->trigger();
  143. $events = $sink->get_events();
  144. $event = reset($events);
  145. // Check that the legacy log data is valid.
  146. $expected = array($this->eventcourse->id, 'scorm', 'userreportinteractions', 'report/userreportinteractions.php?id=' .
  147. $this->eventcm->id . '&user=5&attempt=' . 2, $this->eventscorm->id, $this->eventcm->id);
  148. $this->assertEventLegacyLogData($expected, $event);
  149. $this->assertEventContextNotUsed($event);
  150. }
  151. /**
  152. * Tests for interactions viewed validations.
  153. */
  154. public function test_interactions_viewed_event_validations() {
  155. $this->resetAfterTest();
  156. try {
  157. \mod_scorm\event\interactions_viewed::create(array(
  158. 'context' => context_module::instance($this->eventcm->id),
  159. 'courseid' => $this->eventcourse->id,
  160. 'other' => array('attemptid' => 2)
  161. ));
  162. $this->fail("Event validation should not allow \\mod_scorm\\event\\interactions_viewed to be triggered without
  163. other['instanceid']");
  164. } catch (Exception $e) {
  165. $this->assertInstanceOf('coding_exception', $e);
  166. }
  167. try {
  168. \mod_scorm\event\interactions_viewed::create(array(
  169. 'context' => context_module::instance($this->eventcm->id),
  170. 'courseid' => $this->eventcourse->id,
  171. 'other' => array('instanceid' => 2)
  172. ));
  173. $this->fail("Event validation should not allow \\mod_scorm\\event\\interactions_viewed to be triggered without
  174. other['attemptid']");
  175. } catch (Exception $e) {
  176. $this->assertInstanceOf('coding_exception', $e);
  177. }
  178. }
  179. /** Tests for report viewed.
  180. *
  181. * There is no api involved so the best we can do is test legacy data and validations by triggering event manually.
  182. */
  183. public function test_report_viewed_event() {
  184. $this->resetAfterTest();
  185. $event = \mod_scorm\event\report_viewed::create(array(
  186. 'context' => context_module::instance($this->eventcm->id),
  187. 'courseid' => $this->eventcourse->id,
  188. 'other' => array(
  189. 'scormid' => $this->eventscorm->id,
  190. 'mode' => 'basic'
  191. )
  192. ));
  193. // Trigger and capture the event.
  194. $sink = $this->redirectEvents();
  195. $event->trigger();
  196. $events = $sink->get_events();
  197. $event = reset($events);
  198. // Check that the legacy log data is valid.
  199. $expected = array($this->eventcourse->id, 'scorm', 'report', 'report.php?id=' . $this->eventcm->id . '&mode=basic',
  200. $this->eventscorm->id, $this->eventcm->id);
  201. $this->assertEventLegacyLogData($expected, $event);
  202. $this->assertEventContextNotUsed($event);
  203. }
  204. /** Tests for sco launched event.
  205. *
  206. * There is no api involved so the best we can do is test legacy data and validations by triggering event manually.
  207. *
  208. * @expectedException coding_exception
  209. */
  210. public function test_sco_launched_event() {
  211. $this->resetAfterTest();
  212. $event = \mod_scorm\event\sco_launched::create(array(
  213. 'objectid' => 2,
  214. 'context' => context_module::instance($this->eventcm->id),
  215. 'courseid' => $this->eventcourse->id,
  216. 'other' => array('loadedcontent' => 'url_to_content_that_was_laoded.php')
  217. ));
  218. // Trigger and capture the event.
  219. $sink = $this->redirectEvents();
  220. $event->trigger();
  221. $events = $sink->get_events();
  222. $event = reset($events);
  223. // Check that the legacy log data is valid.
  224. $expected = array($this->eventcourse->id, 'scorm', 'launch', 'view.php?id=' . $this->eventcm->id,
  225. 'url_to_content_that_was_laoded.php', $this->eventcm->id);
  226. $this->assertEventLegacyLogData($expected, $event);
  227. $this->assertEventContextNotUsed($event);
  228. // Test validations.
  229. \mod_scorm\event\sco_launched::create(array(
  230. 'objectid' => $this->eventscorm->id,
  231. 'context' => context_module::instance($this->eventcm->id),
  232. 'courseid' => $this->eventcourse->id,
  233. ));
  234. $this->fail('Event \\mod_scorm\\event\\sco_launched is not validating "loadedcontent" properly');
  235. }
  236. /**
  237. * Tests for tracks viewed event.
  238. *
  239. * There is no api involved so the best we can do is test validations by triggering event manually.
  240. */
  241. public function test_tracks_viewed_event() {
  242. $this->resetAfterTest();
  243. $event = \mod_scorm\event\tracks_viewed::create(array(
  244. 'relateduserid' => 5,
  245. 'context' => context_module::instance($this->eventcm->id),
  246. 'courseid' => $this->eventcourse->id,
  247. 'other' => array('attemptid' => 2, 'instanceid' => $this->eventscorm->id, 'scoid' => 3)
  248. ));
  249. // Trigger and capture the event.
  250. $sink = $this->redirectEvents();
  251. $event->trigger();
  252. $events = $sink->get_events();
  253. $event = reset($events);
  254. // Check that the legacy log data is valid.
  255. $expected = array($this->eventcourse->id, 'scorm', 'userreporttracks', 'report/userreporttracks.php?id=' .
  256. $this->eventcm->id . '&user=5&attempt=' . 2 . '&scoid=3', $this->eventscorm->id, $this->eventcm->id);
  257. $this->assertEventLegacyLogData($expected, $event);
  258. $this->assertEventContextNotUsed($event);
  259. }
  260. /**
  261. * Tests for tracks viewed event validations.
  262. */
  263. public function test_tracks_viewed_event_validations() {
  264. $this->resetAfterTest();
  265. try {
  266. \mod_scorm\event\tracks_viewed::create(array(
  267. 'context' => context_module::instance($this->eventcm->id),
  268. 'courseid' => $this->eventcourse->id,
  269. 'other' => array('attemptid' => 2, 'scoid' => 2)
  270. ));
  271. $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
  272. other['instanceid']");
  273. } catch (Exception $e) {
  274. $this->assertInstanceOf('coding_exception', $e);
  275. }
  276. try {
  277. \mod_scorm\event\tracks_viewed::create(array(
  278. 'context' => context_module::instance($this->eventcm->id),
  279. 'courseid' => $this->eventcourse->id,
  280. 'other' => array('instanceid' => 2, 'scoid' => 2)
  281. ));
  282. $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
  283. other['attemptid']");
  284. } catch (Exception $e) {
  285. $this->assertInstanceOf('coding_exception', $e);
  286. }
  287. try {
  288. \mod_scorm\event\tracks_viewed::create(array(
  289. 'context' => context_module::instance($this->eventcm->id),
  290. 'courseid' => $this->eventcourse->id,
  291. 'other' => array('attemptid' => 2, 'instanceid' => 2)
  292. ));
  293. $this->fail("Event validation should not allow \\mod_scorm\\event\\tracks_viewed to be triggered without
  294. other['scoid']");
  295. } catch (Exception $e) {
  296. $this->assertInstanceOf('coding_exception', $e);
  297. }
  298. }
  299. /**
  300. * Tests for userreport viewed event.
  301. *
  302. * There is no api involved so the best we can do is test validations and legacy log by triggering event manually.
  303. */
  304. public function test_user_report_viewed_event() {
  305. $this->resetAfterTest();
  306. $event = \mod_scorm\event\user_report_viewed::create(array(
  307. 'relateduserid' => 5,
  308. 'context' => context_module::instance($this->eventcm->id),
  309. 'courseid' => $this->eventcourse->id,
  310. 'other' => array('attemptid' => 2, 'instanceid' => $this->eventscorm->id)
  311. ));
  312. // Trigger and capture the event.
  313. $sink = $this->redirectEvents();
  314. $event->trigger();
  315. $events = $sink->get_events();
  316. $event = reset($events);
  317. // Check that the legacy log data is valid.
  318. $expected = array($this->eventcourse->id, 'scorm', 'userreport', 'report/userreport.php?id=' .
  319. $this->eventcm->id . '&user=5&attempt=' . 2, $this->eventscorm->id, $this->eventcm->id);
  320. $this->assertEventLegacyLogData($expected, $event);
  321. $this->assertEventContextNotUsed($event);
  322. }
  323. /**
  324. * Tests for userreport viewed event validations.
  325. */
  326. public function test_user_report_viewed_event_validations() {
  327. $this->resetAfterTest();
  328. try {
  329. \mod_scorm\event\user_report_viewed::create(array(
  330. 'context' => context_module::instance($this->eventcm->id),
  331. 'courseid' => $this->eventcourse->id,
  332. 'other' => array('attemptid' => 2)
  333. ));
  334. $this->fail("Event validation should not allow \\mod_scorm\\event\\user_report_viewed to be triggered without
  335. other['instanceid']");
  336. } catch (Exception $e) {
  337. $this->assertInstanceOf('coding_exception', $e);
  338. }
  339. try {
  340. \mod_scorm\event\user_report_viewed::create(array(
  341. 'context' => context_module::instance($this->eventcm->id),
  342. 'courseid' => $this->eventcourse->id,
  343. 'other' => array('instanceid' => 2)
  344. ));
  345. $this->fail("Event validation should not allow \\mod_scorm\\event\\user_report_viewed to be triggered without
  346. other['attemptid']");
  347. } catch (Exception $e) {
  348. $this->assertInstanceOf('coding_exception', $e);
  349. }
  350. }
  351. /**
  352. * dataProvider for test_scoreraw_submitted_event().
  353. */
  354. public function get_scoreraw_submitted_event_provider() {
  355. return array(
  356. // SCORM 1.2.
  357. // - cmi.core.score.raw.
  358. 'cmi.core.score.raw => 100' => array('cmi.core.score.raw', '100'),
  359. 'cmi.core.score.raw => 90' => array('cmi.core.score.raw', '90'),
  360. 'cmi.core.score.raw => 50' => array('cmi.core.score.raw', '50'),
  361. 'cmi.core.score.raw => 10' => array('cmi.core.score.raw', '10'),
  362. // Check an edge case (PHP empty() vs isset()): score value equals to '0'.
  363. 'cmi.core.score.raw => 0' => array('cmi.core.score.raw', '0'),
  364. // SCORM 1.3 AKA 2004.
  365. // - cmi.score.raw.
  366. 'cmi.score.raw => 100' => array('cmi.score.raw', '100'),
  367. 'cmi.score.raw => 90' => array('cmi.score.raw', '90'),
  368. 'cmi.score.raw => 50' => array('cmi.score.raw', '50'),
  369. 'cmi.score.raw => 10' => array('cmi.score.raw', '10'),
  370. // Check an edge case (PHP empty() vs isset()): score value equals to '0'.
  371. 'cmi.score.raw => 0' => array('cmi.score.raw', '0'),
  372. );
  373. }
  374. /**
  375. * Tests for score submitted event.
  376. *
  377. * There is no api involved so the best we can do is test data by triggering event manually.
  378. *
  379. * @dataProvider get_scoreraw_submitted_event_provider
  380. *
  381. * @param string $cmielement a valid CMI raw score element
  382. * @param string $cmivalue a valid CMI raw score value
  383. */
  384. public function test_scoreraw_submitted_event($cmielement, $cmivalue) {
  385. $this->resetAfterTest();
  386. $event = \mod_scorm\event\scoreraw_submitted::create(array(
  387. 'other' => array('attemptid' => '2', 'cmielement' => $cmielement, 'cmivalue' => $cmivalue),
  388. 'objectid' => $this->eventscorm->id,
  389. 'context' => context_module::instance($this->eventcm->id),
  390. 'relateduserid' => $this->eventuser->id
  391. ));
  392. // Trigger and capture the event.
  393. $sink = $this->redirectEvents();
  394. $event->trigger();
  395. $events = $sink->get_events();
  396. $sink->close();
  397. $event = reset($events);
  398. $this->assertEquals(2, $event->other['attemptid']);
  399. $this->assertEquals($cmielement, $event->other['cmielement']);
  400. $this->assertEquals($cmivalue, $event->other['cmivalue']);
  401. // Check that no legacy log data is provided.
  402. $this->assertEventLegacyLogData(null, $event);
  403. $this->assertEventContextNotUsed($event);
  404. }
  405. /**
  406. * dataProvider for test_scoreraw_submitted_event_validations().
  407. */
  408. public function get_scoreraw_submitted_event_validations() {
  409. return array(
  410. 'scoreraw_submitted => missing cmielement' => array(
  411. null, '50',
  412. "Event validation should not allow \\mod_scorm\\event\\scoreraw_submitted " .
  413. "to be triggered without other['cmielement']",
  414. 'Coding error detected, it must be fixed by a programmer: ' .
  415. "The 'cmielement' must be set in other."
  416. ),
  417. 'scoreraw_submitted => missing cmivalue' => array(
  418. 'cmi.core.score.raw', null,
  419. "Event validation should not allow \\mod_scorm\\event\\scoreraw_submitted " .
  420. "to be triggered without other['cmivalue']",
  421. 'Coding error detected, it must be fixed by a programmer: ' .
  422. "The 'cmivalue' must be set in other."
  423. ),
  424. 'scoreraw_submitted => wrong CMI element' => array(
  425. 'cmi.core.lesson_status', '50',
  426. "Event validation should not allow \\mod_scorm\\event\\scoreraw_submitted " .
  427. 'to be triggered with a CMI element not representing a raw score',
  428. 'Coding error detected, it must be fixed by a programmer: ' .
  429. "The 'cmielement' must represents a valid CMI raw score (cmi.core.lesson_status)."
  430. ),
  431. );
  432. }
  433. /**
  434. * Tests for score submitted event validations.
  435. *
  436. * @dataProvider get_scoreraw_submitted_event_validations
  437. *
  438. * @param string $cmielement a valid CMI raw score element
  439. * @param string $cmivalue a valid CMI raw score value
  440. * @param string $failmessage the message used to fail the test in case of missing to violate a validation rule
  441. * @param string $excmessage the exception message when violating the validations rules
  442. */
  443. public function test_scoreraw_submitted_event_validations($cmielement, $cmivalue, $failmessage, $excmessage) {
  444. $this->resetAfterTest();
  445. try {
  446. $data = array(
  447. 'context' => context_module::instance($this->eventcm->id),
  448. 'courseid' => $this->eventcourse->id,
  449. 'other' => array('attemptid' => 2)
  450. );
  451. if ($cmielement != null) {
  452. $data['other']['cmielement'] = $cmielement;
  453. }
  454. if ($cmivalue != null) {
  455. $data['other']['cmivalue'] = $cmivalue;
  456. }
  457. \mod_scorm\event\scoreraw_submitted::create($data);
  458. $this->fail($failmessage);
  459. } catch (Exception $e) {
  460. $this->assertInstanceOf('coding_exception', $e);
  461. $this->assertEquals($excmessage, $e->getMessage());
  462. }
  463. }
  464. /**
  465. * dataProvider for test_status_submitted_event().
  466. */
  467. public function get_status_submitted_event_provider() {
  468. return array(
  469. // SCORM 1.2.
  470. // 1. Status: cmi.core.lesson_status.
  471. 'cmi.core.lesson_status => passed' => array('cmi.core.lesson_status', 'passed'),
  472. 'cmi.core.lesson_status => completed' => array('cmi.core.lesson_status', 'completed'),
  473. 'cmi.core.lesson_status => failed' => array('cmi.core.lesson_status', 'failed'),
  474. 'cmi.core.lesson_status => incomplete' => array('cmi.core.lesson_status', 'incomplete'),
  475. 'cmi.core.lesson_status => browsed' => array('cmi.core.lesson_status', 'browsed'),
  476. 'cmi.core.lesson_status => not attempted' => array('cmi.core.lesson_status', 'not attempted'),
  477. // SCORM 1.3 AKA 2004.
  478. // 1. Completion status: cmi.completion_status.
  479. 'cmi.completion_status => completed' => array('cmi.completion_status', 'completed'),
  480. 'cmi.completion_status => incomplete' => array('cmi.completion_status', 'incomplete'),
  481. 'cmi.completion_status => not attempted' => array('cmi.completion_status', 'not attempted'),
  482. 'cmi.completion_status => unknown' => array('cmi.completion_status', 'unknown'),
  483. // 2. Success status: cmi.success_status.
  484. 'cmi.success_status => passed' => array('cmi.success_status', 'passed'),
  485. 'cmi.success_status => failed' => array('cmi.success_status', 'failed'),
  486. 'cmi.success_status => unknown' => array('cmi.success_status', 'unknown')
  487. );
  488. }
  489. /**
  490. * Tests for status submitted event.
  491. *
  492. * There is no api involved so the best we can do is test data by triggering event manually.
  493. *
  494. * @dataProvider get_status_submitted_event_provider
  495. *
  496. * @param string $cmielement a valid CMI status element
  497. * @param string $cmivalue a valid CMI status value
  498. */
  499. public function test_status_submitted_event($cmielement, $cmivalue) {
  500. $this->resetAfterTest();
  501. $event = \mod_scorm\event\status_submitted::create(array(
  502. 'other' => array('attemptid' => '2', 'cmielement' => $cmielement, 'cmivalue' => $cmivalue),
  503. 'objectid' => $this->eventscorm->id,
  504. 'context' => context_module::instance($this->eventcm->id),
  505. 'relateduserid' => $this->eventuser->id
  506. ));
  507. // Trigger and capture the event.
  508. $sink = $this->redirectEvents();
  509. $event->trigger();
  510. $events = $sink->get_events();
  511. $sink->close();
  512. $event = reset($events);
  513. $this->assertEquals(2, $event->other['attemptid']);
  514. $this->assertEquals($cmielement, $event->other['cmielement']);
  515. $this->assertEquals($cmivalue, $event->other['cmivalue']);
  516. // Check that no legacy log data is provided.
  517. $this->assertEventLegacyLogData(null, $event);
  518. $this->assertEventContextNotUsed($event);
  519. }
  520. /**
  521. * dataProvider for test_status_submitted_event_validations().
  522. */
  523. public function get_status_submitted_event_validations() {
  524. return array(
  525. 'status_submitted => missing cmielement' => array(
  526. null, 'passed',
  527. "Event validation should not allow \\mod_scorm\\event\\status_submitted " .
  528. "to be triggered without other['cmielement']",
  529. 'Coding error detected, it must be fixed by a programmer: ' .
  530. "The 'cmielement' must be set in other."
  531. ),
  532. 'status_submitted => missing cmivalue' => array(
  533. 'cmi.core.lesson_status', null,
  534. "Event validation should not allow \\mod_scorm\\event\\status_submitted " .
  535. "to be triggered without other['cmivalue']",
  536. 'Coding error detected, it must be fixed by a programmer: ' .
  537. "The 'cmivalue' must be set in other."
  538. ),
  539. 'status_submitted => wrong CMI element' => array(
  540. 'cmi.core.score.raw', 'passed',
  541. "Event validation should not allow \\mod_scorm\\event\\status_submitted " .
  542. 'to be triggered with a CMI element not representing a valid CMI status element',
  543. 'Coding error detected, it must be fixed by a programmer: ' .
  544. "The 'cmielement' must represents a valid CMI status element (cmi.core.score.raw)."
  545. ),
  546. 'status_submitted => wrong CMI value' => array(
  547. 'cmi.core.lesson_status', 'blahblahblah',
  548. "Event validation should not allow \\mod_scorm\\event\\status_submitted " .
  549. 'to be triggered with a CMI element not representing a valid CMI status',
  550. 'Coding error detected, it must be fixed by a programmer: ' .
  551. "The 'cmivalue' must represents a valid CMI status value (blahblahblah)."
  552. ),
  553. );
  554. }
  555. /**
  556. * Tests for status submitted event validations.
  557. *
  558. * @dataProvider get_status_submitted_event_validations
  559. *
  560. * @param string $cmielement a valid CMI status element
  561. * @param string $cmivalue a valid CMI status value
  562. * @param string $failmessage the message used to fail the test in case of missing to violate a validation rule
  563. * @param string $excmessage the exception message when violating the validations rules
  564. */
  565. public function test_status_submitted_event_validations($cmielement, $cmivalue, $failmessage, $excmessage) {
  566. $this->resetAfterTest();
  567. try {
  568. $data = array(
  569. 'context' => context_module::instance($this->eventcm->id),
  570. 'courseid' => $this->eventcourse->id,
  571. 'other' => array('attemptid' => 2)
  572. );
  573. if ($cmielement != null) {
  574. $data['other']['cmielement'] = $cmielement;
  575. }
  576. if ($cmivalue != null) {
  577. $data['other']['cmivalue'] = $cmivalue;
  578. }
  579. \mod_scorm\event\status_submitted::create($data);
  580. $this->fail($failmessage);
  581. } catch (Exception $e) {
  582. $this->assertInstanceOf('coding_exception', $e);
  583. $this->assertEquals($excmessage, $e->getMessage());
  584. }
  585. }
  586. }