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_page functions unit tests
  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  global $CFG;
  30  
  31  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  32  
  33  /**
  34   * External mod_page functions unit tests
  35   *
  36   * @package    mod_page
  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_page_external_testcase extends externallib_advanced_testcase {
  43  
  44      /**
  45       * Test view_page
  46       */
  47      public function test_view_page() {
  48          global $DB;
  49  
  50          $this->resetAfterTest(true);
  51  
  52          // Setup test data.
  53          $course = $this->getDataGenerator()->create_course();
  54          $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
  55          $context = context_module::instance($page->cmid);
  56          $cm = get_coursemodule_from_instance('page', $page->id);
  57  
  58          // Test invalid instance id.
  59          try {
  60              mod_page_external::view_page(0);
  61              $this->fail('Exception expected due to invalid mod_page 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_page_external::view_page($page->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_page_external::view_page($page->id);
  84          $result = external_api::clean_returnvalue(mod_page_external::view_page_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_page\event\course_module_viewed', $event);
  92          $this->assertEquals($context, $event->get_context());
  93          $moodlepage = new \moodle_url('/mod/page/view.php', array('id' => $cm->id));
  94          $this->assertEquals($moodlepage, $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/page: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_page_external::view_page($page->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_page_get_pages_by_courses
 116       */
 117      public function test_mod_page_get_pages_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 page.
 130          $record = new stdClass();
 131          $record->course = $course1->id;
 132          $page1 = self::getDataGenerator()->create_module('page', $record);
 133  
 134          // Second page.
 135          $record = new stdClass();
 136          $record->course = $course2->id;
 137          $page2 = self::getDataGenerator()->create_module('page', $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_page_external::get_pages_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',
 156                                  'content', 'contentformat', 'contentfiles', 'legacyfiles', 'legacyfileslast', 'display',
 157                                  'displayoptions', 'revision', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid');
 158  
 159          // Add expected coursemodule and data.
 160          $page1->coursemodule = $page1->cmid;
 161          $page1->introformat = 1;
 162          $page1->contentformat = 1;
 163          $page1->section = 0;
 164          $page1->visible = true;
 165          $page1->groupmode = 0;
 166          $page1->groupingid = 0;
 167          $page1->introfiles = [];
 168          $page1->contentfiles = [];
 169  
 170          $page2->coursemodule = $page2->cmid;
 171          $page2->introformat = 1;
 172          $page2->contentformat = 1;
 173          $page2->section = 0;
 174          $page2->visible = true;
 175          $page2->groupmode = 0;
 176          $page2->groupingid = 0;
 177          $page2->introfiles = [];
 178          $page2->contentfiles = [];
 179  
 180          foreach ($expectedfields as $field) {
 181              $expected1[$field] = $page1->{$field};
 182              $expected2[$field] = $page2->{$field};
 183          }
 184  
 185          $expectedpages = array($expected2, $expected1);
 186  
 187          // Call the external function passing course ids.
 188          $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id));
 189          $result = external_api::clean_returnvalue($returndescription, $result);
 190  
 191          $this->assertEquals($expectedpages, $result['pages']);
 192          $this->assertCount(0, $result['warnings']);
 193  
 194          // Call the external function without passing course id.
 195          $result = mod_page_external::get_pages_by_courses();
 196          $result = external_api::clean_returnvalue($returndescription, $result);
 197          $this->assertEquals($expectedpages, $result['pages']);
 198          $this->assertCount(0, $result['warnings']);
 199  
 200          // Add a file to the intro.
 201          $filename = "file.txt";
 202          $filerecordinline = array(
 203              'contextid' => context_module::instance($page2->cmid)->id,
 204              'component' => 'mod_page',
 205              'filearea'  => 'intro',
 206              'itemid'    => 0,
 207              'filepath'  => '/',
 208              'filename'  => $filename,
 209          );
 210          $fs = get_file_storage();
 211          $timepost = time();
 212          $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
 213  
 214          $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id));
 215          $result = external_api::clean_returnvalue($returndescription, $result);
 216  
 217          $this->assertCount(1, $result['pages'][0]['introfiles']);
 218          $this->assertEquals($filename, $result['pages'][0]['introfiles'][0]['filename']);
 219  
 220          // Unenrol user from second course.
 221          $enrol->unenrol_user($instance2, $student->id);
 222          array_shift($expectedpages);
 223  
 224          // Call the external function without passing course id.
 225          $result = mod_page_external::get_pages_by_courses();
 226          $result = external_api::clean_returnvalue($returndescription, $result);
 227          $this->assertEquals($expectedpages, $result['pages']);
 228  
 229          // Call for the second course we unenrolled the user from, expected warning.
 230          $result = mod_page_external::get_pages_by_courses(array($course2->id));
 231          $this->assertCount(1, $result['warnings']);
 232          $this->assertEquals('1', $result['warnings'][0]['warningcode']);
 233          $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
 234      }
 235  }