Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

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