PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/imscp/tests/lib_test.php

https://github.com/mspall/moodle
PHP | 330 lines | 158 code | 78 blank | 94 comment | 1 complexity | d6849f537350bb06675813b1d645473b 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 (some of) mod/imscp/lib.php.
  18. *
  19. * @package mod_imscp
  20. * @category test
  21. * @copyright 2015 Juan Leyva <juan@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. namespace mod_imscp;
  25. defined('MOODLE_INTERNAL') || die();
  26. global $CFG;
  27. require_once($CFG->dirroot . '/mod/imscp/lib.php');
  28. /**
  29. * Unit tests for (some of) mod/imscp/lib.php.
  30. *
  31. * @package mod_imscp
  32. * @category test
  33. * @copyright 2015 Juan Leyva <juan@moodle.com>
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class lib_test extends \advanced_testcase {
  37. public function test_export_contents() {
  38. global $DB, $USER;
  39. $this->resetAfterTest(true);
  40. $user = $this->getDataGenerator()->create_user();
  41. $course = $this->getDataGenerator()->create_course();
  42. $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  43. $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  44. $this->setAdminUser();
  45. $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
  46. $cm = get_coursemodule_from_id('imscp', $imscp->cmid);
  47. $this->setUser($user);
  48. $contents = imscp_export_contents($cm, '');
  49. // The test package contains 47 files.
  50. $this->assertCount(47, $contents);
  51. // The structure is present.
  52. $this->assertEquals('structure', $contents[0]['filename']);
  53. // The structure is returned and it maches the expected one.
  54. $this->assertEquals(json_encode(unserialize($imscp->structure)), $contents[0]['content']);
  55. }
  56. /**
  57. * Test imscp_view
  58. * @return void
  59. */
  60. public function test_imscp_view() {
  61. global $CFG;
  62. $CFG->enablecompletion = 1;
  63. $this->resetAfterTest();
  64. $this->setAdminUser();
  65. // Setup test data.
  66. $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  67. $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id),
  68. array('completion' => 2, 'completionview' => 1));
  69. $context = \context_module::instance($imscp->cmid);
  70. $cm = get_coursemodule_from_instance('imscp', $imscp->id);
  71. // Trigger and capture the event.
  72. $sink = $this->redirectEvents();
  73. imscp_view($imscp, $course, $cm, $context);
  74. $events = $sink->get_events();
  75. // 2 additional events thanks to completion.
  76. $this->assertCount(3, $events);
  77. $event = array_shift($events);
  78. // Checking that the event contains the expected values.
  79. $this->assertInstanceOf('\mod_imscp\event\course_module_viewed', $event);
  80. $this->assertEquals($context, $event->get_context());
  81. $moodleurl = new \moodle_url('/mod/imscp/view.php', array('id' => $cm->id));
  82. $this->assertEquals($moodleurl, $event->get_url());
  83. $this->assertEventContextNotUsed($event);
  84. $this->assertNotEmpty($event->get_name());
  85. // Check completion status.
  86. $completion = new \completion_info($course);
  87. $completiondata = $completion->get_data($cm);
  88. $this->assertEquals(1, $completiondata->completionstate);
  89. }
  90. public function test_imscp_core_calendar_provide_event_action() {
  91. $this->resetAfterTest();
  92. $this->setAdminUser();
  93. // Create the activity.
  94. $course = $this->getDataGenerator()->create_course();
  95. $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
  96. // Create a calendar event.
  97. $event = $this->create_action_event($course->id, $imscp->id,
  98. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  99. // Create an action factory.
  100. $factory = new \core_calendar\action_factory();
  101. // Decorate action event.
  102. $actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory);
  103. // Confirm the event was decorated.
  104. $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
  105. $this->assertEquals(get_string('view'), $actionevent->get_name());
  106. $this->assertInstanceOf('moodle_url', $actionevent->get_url());
  107. $this->assertEquals(1, $actionevent->get_item_count());
  108. $this->assertTrue($actionevent->is_actionable());
  109. }
  110. public function test_imscp_core_calendar_provide_event_action_for_user() {
  111. global $CFG;
  112. $this->resetAfterTest();
  113. $this->setAdminUser();
  114. // Create a course.
  115. $course = $this->getDataGenerator()->create_course();
  116. // Create a student.
  117. $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  118. // Create the activity.
  119. $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
  120. // Create a calendar event.
  121. $event = $this->create_action_event($course->id, $imscp->id,
  122. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  123. // Now log out.
  124. $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
  125. $this->setUser();
  126. // Create an action factory.
  127. $factory = new \core_calendar\action_factory();
  128. // Decorate action event for the student.
  129. $actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory, $student->id);
  130. // Confirm the event was decorated.
  131. $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
  132. $this->assertEquals(get_string('view'), $actionevent->get_name());
  133. $this->assertInstanceOf('moodle_url', $actionevent->get_url());
  134. $this->assertEquals(1, $actionevent->get_item_count());
  135. $this->assertTrue($actionevent->is_actionable());
  136. }
  137. public function test_imscp_core_calendar_provide_event_action_as_non_user() {
  138. global $CFG;
  139. $this->resetAfterTest();
  140. $this->setAdminUser();
  141. // Create the activity.
  142. $course = $this->getDataGenerator()->create_course();
  143. $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
  144. // Create a calendar event.
  145. $event = $this->create_action_event($course->id, $imscp->id,
  146. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  147. // Now log out.
  148. $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
  149. $this->setUser();
  150. // Create an action factory.
  151. $factory = new \core_calendar\action_factory();
  152. // Decorate action event.
  153. $actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory);
  154. // Confirm the event is not shown at all.
  155. $this->assertNull($actionevent);
  156. }
  157. public function test_imscp_core_calendar_provide_event_action_in_hidden_section() {
  158. global $CFG;
  159. $this->resetAfterTest();
  160. $this->setAdminUser();
  161. // Create a course.
  162. $course = $this->getDataGenerator()->create_course();
  163. // Create a student.
  164. $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  165. // Create the activity.
  166. $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
  167. // Create a calendar event.
  168. $event = $this->create_action_event($course->id, $imscp->id,
  169. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  170. // Set sections 0 as hidden.
  171. set_section_visible($course->id, 0, 0);
  172. // Now log out.
  173. $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
  174. $this->setUser();
  175. // Create an action factory.
  176. $factory = new \core_calendar\action_factory();
  177. // Decorate action event for the student.
  178. $actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory, $student->id);
  179. // Confirm the event is not shown at all.
  180. $this->assertNull($actionevent);
  181. }
  182. public function test_imscp_core_calendar_provide_event_action_already_completed() {
  183. global $CFG;
  184. $this->resetAfterTest();
  185. $this->setAdminUser();
  186. $CFG->enablecompletion = 1;
  187. // Create the activity.
  188. $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  189. $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id),
  190. array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
  191. // Get some additional data.
  192. $cm = get_coursemodule_from_instance('imscp', $imscp->id);
  193. // Create a calendar event.
  194. $event = $this->create_action_event($course->id, $imscp->id,
  195. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  196. // Mark the activity as completed.
  197. $completion = new \completion_info($course);
  198. $completion->set_module_viewed($cm);
  199. // Create an action factory.
  200. $factory = new \core_calendar\action_factory();
  201. // Decorate action event.
  202. $actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory);
  203. // Ensure result was null.
  204. $this->assertNull($actionevent);
  205. }
  206. public function test_imscp_core_calendar_provide_event_action_already_completed_for_user() {
  207. global $CFG;
  208. $this->resetAfterTest();
  209. $this->setAdminUser();
  210. $CFG->enablecompletion = 1;
  211. // Create a course.
  212. $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  213. // Create a student.
  214. $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  215. // Create the activity.
  216. $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id),
  217. array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
  218. // Get some additional data.
  219. $cm = get_coursemodule_from_instance('imscp', $imscp->id);
  220. // Create a calendar event.
  221. $event = $this->create_action_event($course->id, $imscp->id,
  222. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  223. // Mark the activity as completed.
  224. $completion = new \completion_info($course);
  225. $completion->set_module_viewed($cm, $student->id);
  226. // Create an action factory.
  227. $factory = new \core_calendar\action_factory();
  228. // Decorate action event for the student.
  229. $actionevent = mod_imscp_core_calendar_provide_event_action($event, $factory, $student->id);
  230. // Ensure result was null.
  231. $this->assertNull($actionevent);
  232. }
  233. /**
  234. * Creates an action event.
  235. *
  236. * @param int $courseid The course id.
  237. * @param int $instanceid The instance id.
  238. * @param string $eventtype The event type.
  239. * @return bool|calendar_event
  240. */
  241. private function create_action_event($courseid, $instanceid, $eventtype) {
  242. $event = new \stdClass();
  243. $event->name = 'Calendar event';
  244. $event->modulename = 'imscp';
  245. $event->courseid = $courseid;
  246. $event->instance = $instanceid;
  247. $event->type = CALENDAR_EVENT_TYPE_ACTION;
  248. $event->eventtype = $eventtype;
  249. $event->timestart = time();
  250. return \calendar_event::create($event);
  251. }
  252. }