Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   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  namespace mod_resource;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  
  31  /**
  32   * Unit tests for mod_resource lib
  33   *
  34   * @package    mod_resource
  35   * @category   external
  36   * @copyright  2015 Juan Leyva <juan@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   * @since      Moodle 3.0
  39   */
  40  class lib_test extends \advanced_testcase {
  41  
  42      /**
  43       * Prepares things before this test case is initialised
  44       * @return void
  45       */
  46      public static function setUpBeforeClass(): void {
  47          global $CFG;
  48          require_once($CFG->dirroot . '/mod/resource/lib.php');
  49      }
  50  
  51      /**
  52       * Test resource_view
  53       * @return void
  54       */
  55      public function test_resource_view() {
  56          global $CFG;
  57  
  58          $CFG->enablecompletion = 1;
  59          $this->resetAfterTest();
  60  
  61          $this->setAdminUser();
  62          // Setup test data.
  63          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  64          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
  65                                                              array('completion' => 2, 'completionview' => 1));
  66          $context = \context_module::instance($resource->cmid);
  67          $cm = get_coursemodule_from_instance('resource', $resource->id);
  68  
  69          // Trigger and capture the event.
  70          $sink = $this->redirectEvents();
  71  
  72          resource_view($resource, $course, $cm, $context);
  73  
  74          $events = $sink->get_events();
  75          // 2 additional events thanks to completion.
  76          $this->assertCount(3, $events);
  77          $event = array_shift($events);
  78  
  79          // Checking that the event contains the expected values.
  80          $this->assertInstanceOf('\mod_resource\event\course_module_viewed', $event);
  81          $this->assertEquals($context, $event->get_context());
  82          $moodleurl = new \moodle_url('/mod/resource/view.php', array('id' => $cm->id));
  83          $this->assertEquals($moodleurl, $event->get_url());
  84          $this->assertEventContextNotUsed($event);
  85          $this->assertNotEmpty($event->get_name());
  86  
  87          // Check completion status.
  88          $completion = new \completion_info($course);
  89          $completiondata = $completion->get_data($cm);
  90          $this->assertEquals(1, $completiondata->completionstate);
  91  
  92      }
  93  
  94      /**
  95       * Tests the resource_get_coursemodule_info function.
  96       *
  97       * Note: This currently doesn't test every aspect of the function, mainly focusing on the icon.
  98       */
  99      public function test_get_coursemodule_info() {
 100          global $DB, $USER;
 101  
 102          $this->resetAfterTest();
 103          $this->setAdminUser();
 104  
 105          // Create course.
 106          $generator = $this->getDataGenerator();
 107          $course = $generator->create_course();
 108  
 109          // Create a resource with no files.
 110          $draftid = file_get_unused_draft_itemid();
 111          $resource1 = $generator->create_module('resource', array('course' => $course->id,
 112                  'name' => 'R1', 'files' => $draftid));
 113  
 114          // Create a resource with one file.
 115          $draftid = file_get_unused_draft_itemid();
 116          $contextid = \context_user::instance($USER->id)->id;
 117          $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
 118                  'itemid' => $draftid, 'filename' => 'r2.txt', 'filepath' => '/');
 119          $fs = get_file_storage();
 120          $fs->create_file_from_string($filerecord, 'Test');
 121          $resource2 = $generator->create_module('resource', array('course' => $course->id,
 122                  'name' => 'R2', 'files' => $draftid));
 123  
 124          // Create a resource with two files.
 125          $draftid = file_get_unused_draft_itemid();
 126          $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
 127                  'itemid' => $draftid, 'filename' => 'r3.txt', 'filepath' => '/', 'sortorder' => 1);
 128          $fs->create_file_from_string($filerecord, 'Test');
 129          $filerecord['filename'] = 'r3.doc';
 130          $filerecord['sortorder'] = 2;
 131          $fs->create_file_from_string($filerecord, 'Test');
 132          $resource3 = $generator->create_module('resource', array('course' => $course->id,
 133                  'name' => 'R3', 'files' => $draftid));
 134  
 135          // Try get_coursemodule_info for first one.
 136          $info = resource_get_coursemodule_info(
 137                  $DB->get_record('course_modules', array('id' => $resource1->cmid)));
 138  
 139          // The name should be set. There is no overridden icon.
 140          $this->assertEquals('R1', $info->name);
 141          $this->assertEmpty($info->icon);
 142  
 143          // For second one, there should be an overridden icon.
 144          $info = resource_get_coursemodule_info(
 145                  $DB->get_record('course_modules', array('id' => $resource2->cmid)));
 146          $this->assertEquals('R2', $info->name);
 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      }
 153  
 154      public function test_resource_core_calendar_provide_event_action() {
 155          $this->resetAfterTest();
 156          $this->setAdminUser();
 157  
 158          // Create the activity.
 159          $course = $this->getDataGenerator()->create_course();
 160          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
 161  
 162          // Create a calendar event.
 163          $event = $this->create_action_event($course->id, $resource->id,
 164              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 165  
 166          // Create an action factory.
 167          $factory = new \core_calendar\action_factory();
 168  
 169          // Decorate action event.
 170          $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
 171  
 172          // Confirm the event was decorated.
 173          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
 174          $this->assertEquals(get_string('view'), $actionevent->get_name());
 175          $this->assertInstanceOf('moodle_url', $actionevent->get_url());
 176          $this->assertEquals(1, $actionevent->get_item_count());
 177          $this->assertTrue($actionevent->is_actionable());
 178      }
 179  
 180      public function test_resource_core_calendar_provide_event_action_already_completed() {
 181          global $CFG;
 182  
 183          $this->resetAfterTest();
 184          $this->setAdminUser();
 185  
 186          $CFG->enablecompletion = 1;
 187  
 188          // Create the activity.
 189          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 190          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
 191              array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
 192  
 193          // Get some additional data.
 194          $cm = get_coursemodule_from_instance('resource', $resource->id);
 195  
 196          // Create a calendar event.
 197          $event = $this->create_action_event($course->id, $resource->id,
 198              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 199  
 200          // Mark the activity as completed.
 201          $completion = new \completion_info($course);
 202          $completion->set_module_viewed($cm);
 203  
 204          // Create an action factory.
 205          $factory = new \core_calendar\action_factory();
 206  
 207          // Decorate action event.
 208          $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
 209  
 210          // Ensure result was null.
 211          $this->assertNull($actionevent);
 212      }
 213  
 214      /**
 215       * Test mod_resource_core_calendar_provide_event_action with user override
 216       */
 217      public function test_resource_core_calendar_provide_event_action_user_override() {
 218          global $CFG, $USER;
 219  
 220          $this->resetAfterTest();
 221          $this->setAdminUser();
 222          $user = $this->getDataGenerator()->create_user();
 223          $CFG->enablecompletion = 1;
 224  
 225          // Create the activity.
 226          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 227          $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
 228              array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
 229  
 230          // Get some additional data.
 231          $cm = get_coursemodule_from_instance('resource', $resource->id);
 232  
 233          // Create a calendar event.
 234          $event = $this->create_action_event($course->id, $resource->id,
 235              \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
 236  
 237          // Mark the activity as completed.
 238          $completion = new \completion_info($course);
 239          $completion->set_module_viewed($cm);
 240  
 241          // Create an action factory.
 242          $factory = new \core_calendar\action_factory();
 243  
 244          // Decorate action event.
 245          $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory, $USER->id);
 246  
 247          // Decorate action with a userid override.
 248          $actionevent2 = mod_resource_core_calendar_provide_event_action($event, $factory, $user->id);
 249  
 250          // Ensure result was null because it has been marked as completed for the associated user.
 251          // Logic was brought across from the "_already_completed" function.
 252          $this->assertNull($actionevent);
 253  
 254          // Confirm the event was decorated.
 255          $this->assertNotNull($actionevent2);
 256          $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
 257          $this->assertEquals(get_string('view'), $actionevent2->get_name());
 258          $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
 259          $this->assertEquals(1, $actionevent2->get_item_count());
 260          $this->assertTrue($actionevent2->is_actionable());
 261      }
 262  
 263      /**
 264       * Creates an action event.
 265       *
 266       * @param int $courseid The course id.
 267       * @param int $instanceid The instance id.
 268       * @param string $eventtype The event type.
 269       * @return bool|calendar_event
 270       */
 271      private function create_action_event($courseid, $instanceid, $eventtype) {
 272          $event = new \stdClass();
 273          $event->name = 'Calendar event';
 274          $event->modulename  = 'resource';
 275          $event->courseid = $courseid;
 276          $event->instance = $instanceid;
 277          $event->type = CALENDAR_EVENT_TYPE_ACTION;
 278          $event->eventtype = $eventtype;
 279          $event->timestart = time();
 280  
 281          return \calendar_event::create($event);
 282      }
 283  }