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]

   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   * External mod_url functions unit tests
  19   *
  20   * @package    mod_url
  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  global $CFG;
  30  
  31  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  32  
  33  /**
  34   * External mod_url functions unit tests
  35   *
  36   * @package    mod_url
  37   * @category   external
  38   * @copyright  2015 Juan Leyva <juan@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   * @since      Moodle 3.0
  41   */
  42  class mod_url_external_testcase extends externallib_advanced_testcase {
  43  
  44      /**
  45       * Test view_url
  46       */
  47      public function test_view_url() {
  48          global $DB;
  49  
  50          $this->resetAfterTest(true);
  51  
  52          // Setup test data.
  53          $course = $this->getDataGenerator()->create_course();
  54          $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id));
  55          $context = context_module::instance($url->cmid);
  56          $cm = get_coursemodule_from_instance('url', $url->id);
  57  
  58          // Test invalid instance id.
  59          try {
  60              mod_url_external::view_url(0);
  61              $this->fail('Exception expected due to invalid mod_url instance id.');
  62          } catch (moodle_exception $e) {
  63              $this->assertEquals('invalidrecord', $e->errorcode);
  64          }
  65  
  66          // Test not-enrolled user.
  67          $user = self::getDataGenerator()->create_user();
  68          $this->setUser($user);
  69          try {
  70              mod_url_external::view_url($url->id);
  71              $this->fail('Exception expected due to not enrolled user.');
  72          } catch (moodle_exception $e) {
  73              $this->assertEquals('requireloginerror', $e->errorcode);
  74          }
  75  
  76          // Test user with full capabilities.
  77          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  78          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
  79  
  80          // Trigger and capture the event.
  81          $sink = $this->redirectEvents();
  82  
  83          $result = mod_url_external::view_url($url->id);
  84          $result = external_api::clean_returnvalue(mod_url_external::view_url_returns(), $result);
  85  
  86          $events = $sink->get_events();
  87          $this->assertCount(1, $events);
  88          $event = array_shift($events);
  89  
  90          // Checking that the event contains the expected values.
  91          $this->assertInstanceOf('\mod_url\event\course_module_viewed', $event);
  92          $this->assertEquals($context, $event->get_context());
  93          $moodleurl = new \moodle_url('/mod/url/view.php', array('id' => $cm->id));
  94          $this->assertEquals($moodleurl, $event->get_url());
  95          $this->assertEventContextNotUsed($event);
  96          $this->assertNotEmpty($event->get_name());
  97  
  98          // Test user with no capabilities.
  99          // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
 100          assign_capability('mod/url:view', CAP_PROHIBIT, $studentrole->id, $context->id);
 101          // Empty all the caches that may be affected by this change.
 102          accesslib_clear_all_caches_for_unit_testing();
 103          course_modinfo::clear_instance_cache();
 104  
 105          try {
 106              mod_url_external::view_url($url->id);
 107              $this->fail('Exception expected due to missing capability.');
 108          } catch (moodle_exception $e) {
 109              $this->assertEquals('requireloginerror', $e->errorcode);
 110          }
 111  
 112      }
 113  
 114      /**
 115       * Test test_mod_url_get_urls_by_courses
 116       */
 117      public function test_mod_url_get_urls_by_courses() {
 118          global $DB;
 119  
 120          $this->resetAfterTest(true);
 121  
 122          $course1 = self::getDataGenerator()->create_course();
 123          $course2 = self::getDataGenerator()->create_course();
 124  
 125          $student = self::getDataGenerator()->create_user();
 126          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 127          $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id);
 128  
 129          // First url.
 130          $record = new stdClass();
 131          $record->course = $course1->id;
 132          $url1 = self::getDataGenerator()->create_module('url', $record);
 133  
 134          // Second url.
 135          $record = new stdClass();
 136          $record->course = $course2->id;
 137          $url2 = self::getDataGenerator()->create_module('url', $record);
 138  
 139          // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
 140          $enrol = enrol_get_plugin('manual');
 141          $enrolinstances = enrol_get_instances($course2->id, true);
 142          foreach ($enrolinstances as $courseenrolinstance) {
 143              if ($courseenrolinstance->enrol == "manual") {
 144                  $instance2 = $courseenrolinstance;
 145                  break;
 146              }
 147          }
 148          $enrol->enrol_user($instance2, $student->id, $studentrole->id);
 149  
 150          self::setUser($student);
 151  
 152          $returndescription = mod_url_external::get_urls_by_courses_returns();
 153  
 154          // Create what we expect to be returned when querying the two courses.
 155          $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'externalurl',
 156                                  'display', 'displayoptions', 'parameters', 'timemodified', 'section', 'visible', 'groupmode',
 157                                  'groupingid');
 158  
 159          // Add expected coursemodule and data.
 160          $url1->coursemodule = $url1->cmid;
 161          $url1->introformat = 1;
 162          $url1->section = 0;
 163          $url1->visible = true;
 164          $url1->groupmode = 0;
 165          $url1->groupingid = 0;
 166          $url1->introfiles = [];
 167  
 168          $url2->coursemodule = $url2->cmid;
 169          $url2->introformat = 1;
 170          $url2->section = 0;
 171          $url2->visible = true;
 172          $url2->groupmode = 0;
 173          $url2->groupingid = 0;
 174          $url2->introfiles = [];
 175  
 176          foreach ($expectedfields as $field) {
 177              $expected1[$field] = $url1->{$field};
 178              $expected2[$field] = $url2->{$field};
 179          }
 180  
 181          $expectedurls = array($expected2, $expected1);
 182  
 183          // Call the external function passing course ids.
 184          $result = mod_url_external::get_urls_by_courses(array($course2->id, $course1->id));
 185          $result = external_api::clean_returnvalue($returndescription, $result);
 186  
 187          $this->assertEquals($expectedurls, $result['urls']);
 188          $this->assertCount(0, $result['warnings']);
 189  
 190          // Call the external function without passing course id.
 191          $result = mod_url_external::get_urls_by_courses();
 192          $result = external_api::clean_returnvalue($returndescription, $result);
 193          $this->assertEquals($expectedurls, $result['urls']);
 194          $this->assertCount(0, $result['warnings']);
 195  
 196          // Add a file to the intro.
 197          $filename = "file.txt";
 198          $filerecordinline = array(
 199              'contextid' => context_module::instance($url2->cmid)->id,
 200              'component' => 'mod_url',
 201              'filearea'  => 'intro',
 202              'itemid'    => 0,
 203              'filepath'  => '/',
 204              'filename'  => $filename,
 205          );
 206          $fs = get_file_storage();
 207          $timepost = time();
 208          $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
 209  
 210          $result = mod_url_external::get_urls_by_courses(array($course2->id, $course1->id));
 211          $result = external_api::clean_returnvalue($returndescription, $result);
 212  
 213          $this->assertCount(1, $result['urls'][0]['introfiles']);
 214          $this->assertEquals($filename, $result['urls'][0]['introfiles'][0]['filename']);
 215  
 216          // Unenrol user from second course and alter expected urls.
 217          $enrol->unenrol_user($instance2, $student->id);
 218          array_shift($expectedurls);
 219  
 220          // Call the external function without passing course id.
 221          $result = mod_url_external::get_urls_by_courses();
 222          $result = external_api::clean_returnvalue($returndescription, $result);
 223          $this->assertEquals($expectedurls, $result['urls']);
 224  
 225          // Call for the second course we unenrolled the user from, expected warning.
 226          $result = mod_url_external::get_urls_by_courses(array($course2->id));
 227          $this->assertCount(1, $result['warnings']);
 228          $this->assertEquals('1', $result['warnings'][0]['warningcode']);
 229          $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
 230      }
 231  }