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

/mod/page/tests/lib_test.php

https://github.com/ankitagarwal/moodle
PHP | 221 lines | 101 code | 42 blank | 78 comment | 1 complexity | 8be2bd1ca3d1e7a34e1bd82149735c04 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 mod_page lib
  18. *
  19. * @package mod_page
  20. * @category external
  21. * @copyright 2015 Juan Leyva <juan@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @since Moodle 3.0
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * Unit tests for mod_page lib
  28. *
  29. * @package mod_page
  30. * @category external
  31. * @copyright 2015 Juan Leyva <juan@moodle.com>
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. * @since Moodle 3.0
  34. */
  35. class mod_page_lib_testcase extends advanced_testcase {
  36. /**
  37. * Prepares things before this test case is initialised
  38. * @return void
  39. */
  40. public static function setUpBeforeClass(): void {
  41. global $CFG;
  42. require_once($CFG->dirroot . '/mod/page/lib.php');
  43. }
  44. /**
  45. * Test page_view
  46. * @return void
  47. */
  48. public function test_page_view() {
  49. global $CFG;
  50. $CFG->enablecompletion = 1;
  51. $this->resetAfterTest();
  52. // Setup test data.
  53. $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  54. $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
  55. array('completion' => 2, 'completionview' => 1));
  56. $context = context_module::instance($page->cmid);
  57. $cm = get_coursemodule_from_instance('page', $page->id);
  58. // Trigger and capture the event.
  59. $sink = $this->redirectEvents();
  60. $this->setAdminUser();
  61. page_view($page, $course, $cm, $context);
  62. $events = $sink->get_events();
  63. // 2 additional events thanks to completion.
  64. $this->assertCount(3, $events);
  65. $event = array_shift($events);
  66. // Checking that the event contains the expected values.
  67. $this->assertInstanceOf('\mod_page\event\course_module_viewed', $event);
  68. $this->assertEquals($context, $event->get_context());
  69. $moodleurl = new \moodle_url('/mod/page/view.php', array('id' => $cm->id));
  70. $this->assertEquals($moodleurl, $event->get_url());
  71. $this->assertEventContextNotUsed($event);
  72. $this->assertNotEmpty($event->get_name());
  73. // Check completion status.
  74. $completion = new completion_info($course);
  75. $completiondata = $completion->get_data($cm);
  76. $this->assertEquals(1, $completiondata->completionstate);
  77. }
  78. public function test_page_core_calendar_provide_event_action() {
  79. $this->resetAfterTest();
  80. $this->setAdminUser();
  81. // Create the activity.
  82. $course = $this->getDataGenerator()->create_course();
  83. $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
  84. // Create a calendar event.
  85. $event = $this->create_action_event($course->id, $page->id,
  86. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  87. // Create an action factory.
  88. $factory = new \core_calendar\action_factory();
  89. // Decorate action event.
  90. $actionevent = mod_page_core_calendar_provide_event_action($event, $factory);
  91. // Confirm the event was decorated.
  92. $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
  93. $this->assertEquals(get_string('view'), $actionevent->get_name());
  94. $this->assertInstanceOf('moodle_url', $actionevent->get_url());
  95. $this->assertEquals(1, $actionevent->get_item_count());
  96. $this->assertTrue($actionevent->is_actionable());
  97. }
  98. public function test_page_core_calendar_provide_event_action_already_completed() {
  99. global $CFG;
  100. $this->resetAfterTest();
  101. $this->setAdminUser();
  102. $CFG->enablecompletion = 1;
  103. // Create the activity.
  104. $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  105. $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
  106. array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
  107. // Get some additional data.
  108. $cm = get_coursemodule_from_instance('page', $page->id);
  109. // Create a calendar event.
  110. $event = $this->create_action_event($course->id, $page->id,
  111. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  112. // Mark the activity as completed.
  113. $completion = new completion_info($course);
  114. $completion->set_module_viewed($cm);
  115. // Create an action factory.
  116. $factory = new \core_calendar\action_factory();
  117. // Decorate action event.
  118. $actionevent = mod_page_core_calendar_provide_event_action($event, $factory);
  119. // Ensure result was null.
  120. $this->assertNull($actionevent);
  121. }
  122. /**
  123. * Test mod_page_core_calendar_provide_event_action with user override
  124. */
  125. public function test_page_core_calendar_provide_event_action_user_override() {
  126. global $CFG, $USER;
  127. $this->resetAfterTest();
  128. $this->setAdminUser();
  129. $user = $this->getDataGenerator()->create_user();
  130. $CFG->enablecompletion = 1;
  131. // Create the activity.
  132. $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  133. $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
  134. array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
  135. // Get some additional data.
  136. $cm = get_coursemodule_from_instance('page', $page->id);
  137. // Create a calendar event.
  138. $event = $this->create_action_event($course->id, $page->id,
  139. \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
  140. // Mark the activity as completed.
  141. $completion = new completion_info($course);
  142. $completion->set_module_viewed($cm);
  143. // Create an action factory.
  144. $factory = new \core_calendar\action_factory();
  145. // Decorate action event.
  146. $actionevent = mod_page_core_calendar_provide_event_action($event, $factory, $USER->id);
  147. // Decorate action with a userid override.
  148. $actionevent2 = mod_page_core_calendar_provide_event_action($event, $factory, $user->id);
  149. // Ensure result was null because it has been marked as completed for the associated user.
  150. // Logic was brought across from the "_already_completed" function.
  151. $this->assertNull($actionevent);
  152. // Confirm the event was decorated.
  153. $this->assertNotNull($actionevent2);
  154. $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
  155. $this->assertEquals(get_string('view'), $actionevent2->get_name());
  156. $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
  157. $this->assertEquals(1, $actionevent2->get_item_count());
  158. $this->assertTrue($actionevent2->is_actionable());
  159. }
  160. /**
  161. * Creates an action event.
  162. *
  163. * @param int $courseid The course id.
  164. * @param int $instanceid The instance id.
  165. * @param string $eventtype The event type.
  166. * @return bool|calendar_event
  167. */
  168. private function create_action_event($courseid, $instanceid, $eventtype) {
  169. $event = new stdClass();
  170. $event->name = 'Calendar event';
  171. $event->modulename = 'page';
  172. $event->courseid = $courseid;
  173. $event->instance = $instanceid;
  174. $event->type = CALENDAR_EVENT_TYPE_ACTION;
  175. $event->eventtype = $eventtype;
  176. $event->timestart = time();
  177. return calendar_event::create($event);
  178. }
  179. }