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_resource lib
  19   *
  20   * @package    mod_resource
  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_resource lib
  32   *
  33   * @package    mod_resource
  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_resource_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/resource/lib.php');
  48      }
  49  
  50      /**
  51       * Test resource_view
  52       * @return void
  53       */
  54      public function test_resource_view() {
  55          global $CFG;
  56  
  57          $CFG->enablecompletion = 1;
  58          $this->resetAfterTest();
  59  
  60          $this->setAdminUser();
  61          // Setup test data.
  62          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  63          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
  64                                                              array('completion' => 2, 'completionview' => 1));
  65          $context = context_module::instance($resource->cmid);
  66          $cm = get_coursemodule_from_instance('resource', $resource->id);
  67  
  68          // Trigger and capture the event.
  69          $sink = $this->redirectEvents();
  70  
  71          resource_view($resource, $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_resource\event\course_module_viewed', $event);
  80          $this->assertEquals($context, $event->get_context());
  81          $moodleurl = new \moodle_url('/mod/resource/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  
  93      /**
  94       * Tests the resource_get_coursemodule_info function.
  95       *
  96       * Note: This currently doesn't test every aspect of the function, mainly focusing on the icon.
  97       */
  98      public function test_get_coursemodule_info() {
  99          global $DB, $USER;
 100  
 101          $this->resetAfterTest();
 102          $this->setAdminUser();
 103  
 104          // Create course.
 105          $generator = $this->getDataGenerator();
 106          $course = $generator->create_course();
 107  
 108          // Create a resource with no files.
 109          $draftid = file_get_unused_draft_itemid();
 110          $resource1 = $generator->create_module('resource', array('course' => $course->id,
 111                  'name' => 'R1', 'files' => $draftid));
 112  
 113          // Create a resource with one file.
 114          $draftid = file_get_unused_draft_itemid();
 115          $contextid = context_user::instance($USER->id)->id;
 116          $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
 117                  'itemid' => $draftid, 'filename' => 'r2.txt', 'filepath' => '/');
 118          $fs = get_file_storage();
 119          $fs->create_file_from_string($filerecord, 'Test');
 120          $resource2 = $generator->create_module('resource', array('course' => $course->id,
 121                  'name' => 'R2', 'files' => $draftid));
 122  
 123          // Create a resource with two files.
 124          $draftid = file_get_unused_draft_itemid();
 125          $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
 126                  'itemid' => $draftid, 'filename' => 'r3.txt', 'filepath' => '/', 'sortorder' => 1);
 127          $fs->create_file_from_string($filerecord, 'Test');
 128          $filerecord['filename'] = 'r3.doc';
 129          $filerecord['sortorder'] = 2;
 130          $fs->create_file_from_string($filerecord, 'Test');
 131          $resource3 = $generator->create_module('resource', array('course' => $course->id,
 132                  'name' => 'R3', 'files' => $draftid));
 133  
 134          // Try get_coursemodule_info for first one.
 135          $info = resource_get_coursemodule_info(
 136                  $DB->get_record('course_modules', array('id' => $resource1->cmid)));
 137  
 138          // The name should be set. There is no overridden icon.
 139          $this->assertEquals('R1', $info->name);
 140          $this->assertEmpty($info->icon);
 141  
 142          // For second one, there should be an overridden icon.
 143          $info = resource_get_coursemodule_info(
 144                  $DB->get_record('course_modules', array('id' => $resource2->cmid)));
 145          $this->assertEquals('R2', $info->name);
 146          $this->assertEquals('f/text-24', $info->icon);
 147  
 148          // For third one, it should use the highest sortorder icon.
 149          $info = resource_get_coursemodule_info(
 150                  $DB->get_record('course_modules', array('id' => $resource3->cmid)));
 151          $this->assertEquals('R3', $info->name);
 152          $this->assertEquals('f/document-24', $info->icon);
 153      }
 154  
 155      public function test_resource_core_calendar_provide_event_action() {
 156          $this->resetAfterTest();
 157          $this->setAdminUser();
 158  
 159          // Create the activity.
 160          $course = $this->getDataGenerator()->create_course();
 161          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
 162  
 163          // Create a calendar event.
 164          $event = $this->create_action_event($course->id, $resource->id,
 165              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 166  
 167          // Create an action factory.
 168          $factory = new \core_calendar\action_factory();
 169  
 170          // Decorate action event.
 171          $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
 172  
 173          // Confirm the event was decorated.
 174          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
 175          $this->assertEquals(get_string('view'), $actionevent->get_name());
 176          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
 177          $this->assertEquals(1, $actionevent->get_item_count());
 178          $this->assertTrue($actionevent->is_actionable());
 179      }
 180  
 181      public function test_resource_core_calendar_provide_event_action_already_completed() {
 182          global $CFG;
 183  
 184          $this->resetAfterTest();
 185          $this->setAdminUser();
 186  
 187          $CFG->enablecompletion = 1;
 188  
 189          // Create the activity.
 190          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 191          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
 192              array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
 193  
 194          // Get some additional data.
 195          $cm = get_coursemodule_from_instance('resource', $resource->id);
 196  
 197          // Create a calendar event.
 198          $event = $this->create_action_event($course->id, $resource->id,
 199              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 200  
 201          // Mark the activity as completed.
 202          $completion = new completion_info($course);
 203          $completion->set_module_viewed($cm);
 204  
 205          // Create an action factory.
 206          $factory = new \core_calendar\action_factory();
 207  
 208          // Decorate action event.
 209          $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
 210  
 211          // Ensure result was null.
 212          $this->assertNull($actionevent);
 213      }
 214  
 215      /**
 216       * Test mod_resource_core_calendar_provide_event_action with user override
 217       */
 218      public function test_resource_core_calendar_provide_event_action_user_override() {
 219          global $CFG, $USER;
 220  
 221          $this->resetAfterTest();
 222          $this->setAdminUser();
 223          $user = $this->getDataGenerator()->create_user();
 224          $CFG->enablecompletion = 1;
 225  
 226          // Create the activity.
 227          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 228          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
 229              array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
 230  
 231          // Get some additional data.
 232          $cm = get_coursemodule_from_instance('resource', $resource->id);
 233  
 234          // Create a calendar event.
 235          $event = $this->create_action_event($course->id, $resource->id,
 236              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 237  
 238          // Mark the activity as completed.
 239          $completion = new completion_info($course);
 240          $completion->set_module_viewed($cm);
 241  
 242          // Create an action factory.
 243          $factory = new \core_calendar\action_factory();
 244  
 245          // Decorate action event.
 246          $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory, $USER->id);
 247  
 248          // Decorate action with a userid override.
 249          $actionevent2 = mod_resource_core_calendar_provide_event_action($event, $factory, $user->id);
 250  
 251          // Ensure result was null because it has been marked as completed for the associated user.
 252          // Logic was brought across from the "_already_completed" function.
 253          $this->assertNull($actionevent);
 254  
 255          // Confirm the event was decorated.
 256          $this->assertNotNull($actionevent2);
 257          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
 258          $this->assertEquals(get_string('view'), $actionevent2->get_name());
 259          $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
 260          $this->assertEquals(1, $actionevent2->get_item_count());
 261          $this->assertTrue($actionevent2->is_actionable());
 262      }
 263  
 264      /**
 265       * Creates an action event.
 266       *
 267       * @param int $courseid The course id.
 268       * @param int $instanceid The instance id.
 269       * @param string $eventtype The event type.
 270       * @return bool|calendar_event
 271       */
 272      private function create_action_event($courseid, $instanceid, $eventtype) {
 273          $event = new stdClass();
 274          $event->name = 'Calendar event';
 275          $event->modulename  = 'resource';
 276          $event->courseid = $courseid;
 277          $event->instance = $instanceid;
 278          $event->type = CALENDAR_EVENT_TYPE_ACTION;
 279          $event->eventtype = $eventtype;
 280          $event->timestart = time();
 281  
 282          return calendar_event::create($event);
 283      }
 284  }