Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

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