Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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_page;
  18  
  19  use core_external\external_api;
  20  use externallib_advanced_testcase;
  21  use mod_page_external;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  
  27  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28  
  29  /**
  30   * External mod_page functions unit tests
  31   *
  32   * @package    mod_page
  33   * @category   external
  34   * @copyright  2015 Juan Leyva <juan@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @since      Moodle 3.0
  37   */
  38  class externallib_test extends externallib_advanced_testcase {
  39  
  40      /**
  41       * Test view_page
  42       */
  43      public function test_view_page() {
  44          global $DB;
  45  
  46          $this->resetAfterTest(true);
  47  
  48          // Setup test data.
  49          $course = $this->getDataGenerator()->create_course();
  50          $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
  51          $context = \context_module::instance($page->cmid);
  52          $cm = get_coursemodule_from_instance('page', $page->id);
  53  
  54          // Test invalid instance id.
  55          try {
  56              mod_page_external::view_page(0);
  57              $this->fail('Exception expected due to invalid mod_page 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_page_external::view_page($page->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_page_external::view_page($page->id);
  80          $result = external_api::clean_returnvalue(mod_page_external::view_page_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_page\event\course_module_viewed', $event);
  88          $this->assertEquals($context, $event->get_context());
  89          $moodlepage = new \moodle_url('/mod/page/view.php', array('id' => $cm->id));
  90          $this->assertEquals($moodlepage, $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/page: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_page_external::view_page($page->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      /**
 111       * Test test_mod_page_get_pages_by_courses
 112       */
 113      public function test_mod_page_get_pages_by_courses() {
 114          global $DB;
 115  
 116          $this->resetAfterTest(true);
 117  
 118          $course1 = self::getDataGenerator()->create_course();
 119          $course2 = self::getDataGenerator()->create_course();
 120  
 121          $student = self::getDataGenerator()->create_user();
 122          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 123          $this->getDataGenerator()->enrol_user($student->id, $course1->id, $studentrole->id);
 124  
 125          // First page.
 126          $record = new \stdClass();
 127          $record->course = $course1->id;
 128          $page1 = self::getDataGenerator()->create_module('page', $record);
 129  
 130          // Second page.
 131          $record = new \stdClass();
 132          $record->course = $course2->id;
 133          $page2 = self::getDataGenerator()->create_module('page', $record);
 134  
 135          // Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
 136          $enrol = enrol_get_plugin('manual');
 137          $enrolinstances = enrol_get_instances($course2->id, true);
 138          foreach ($enrolinstances as $courseenrolinstance) {
 139              if ($courseenrolinstance->enrol == "manual") {
 140                  $instance2 = $courseenrolinstance;
 141                  break;
 142              }
 143          }
 144          $enrol->enrol_user($instance2, $student->id, $studentrole->id);
 145  
 146          self::setUser($student);
 147  
 148          $returndescription = mod_page_external::get_pages_by_courses_returns();
 149  
 150          // Create what we expect to be returned when querying the two courses.
 151          $expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'lang',
 152                                  'content', 'contentformat', 'contentfiles', 'legacyfiles', 'legacyfileslast', 'display',
 153                                  'displayoptions', 'revision', 'timemodified', 'section', 'visible', 'groupmode', 'groupingid');
 154  
 155          // Add expected coursemodule and data.
 156          $page1->coursemodule = $page1->cmid;
 157          $page1->introformat = 1;
 158          $page1->contentformat = 1;
 159          $page1->section = 0;
 160          $page1->visible = true;
 161          $page1->groupmode = 0;
 162          $page1->groupingid = 0;
 163          $page1->introfiles = [];
 164          $page1->contentfiles = [];
 165          $page1->lang = '';
 166  
 167          $page2->coursemodule = $page2->cmid;
 168          $page2->introformat = 1;
 169          $page2->contentformat = 1;
 170          $page2->section = 0;
 171          $page2->visible = true;
 172          $page2->groupmode = 0;
 173          $page2->groupingid = 0;
 174          $page2->introfiles = [];
 175          $page2->contentfiles = [];
 176          $page2->lang = '';
 177  
 178          foreach ($expectedfields as $field) {
 179              $expected1[$field] = $page1->{$field};
 180              $expected2[$field] = $page2->{$field};
 181          }
 182  
 183          $expectedpages = array($expected2, $expected1);
 184  
 185          // Call the external function passing course ids.
 186          $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id));
 187          $result = external_api::clean_returnvalue($returndescription, $result);
 188  
 189          $this->assertEquals($expectedpages, $result['pages']);
 190          $this->assertCount(0, $result['warnings']);
 191  
 192          // Call the external function without passing course id.
 193          $result = mod_page_external::get_pages_by_courses();
 194          $result = external_api::clean_returnvalue($returndescription, $result);
 195          $this->assertEquals($expectedpages, $result['pages']);
 196          $this->assertCount(0, $result['warnings']);
 197  
 198          // Add a file to the intro.
 199          $filename = "file.txt";
 200          $filerecordinline = array(
 201              'contextid' => \context_module::instance($page2->cmid)->id,
 202              'component' => 'mod_page',
 203              'filearea'  => 'intro',
 204              'itemid'    => 0,
 205              'filepath'  => '/',
 206              'filename'  => $filename,
 207          );
 208          $fs = get_file_storage();
 209          $timepost = time();
 210          $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
 211  
 212          $result = mod_page_external::get_pages_by_courses(array($course2->id, $course1->id));
 213          $result = external_api::clean_returnvalue($returndescription, $result);
 214  
 215          $this->assertCount(1, $result['pages'][0]['introfiles']);
 216          $this->assertEquals($filename, $result['pages'][0]['introfiles'][0]['filename']);
 217  
 218          // Unenrol user from second course.
 219          $enrol->unenrol_user($instance2, $student->id);
 220          array_shift($expectedpages);
 221  
 222          // Call the external function without passing course id.
 223          $result = mod_page_external::get_pages_by_courses();
 224          $result = external_api::clean_returnvalue($returndescription, $result);
 225          $this->assertEquals($expectedpages, $result['pages']);
 226  
 227          // Call for the second course we unenrolled the user from, expected warning.
 228          $result = mod_page_external::get_pages_by_courses(array($course2->id));
 229          $this->assertCount(1, $result['warnings']);
 230          $this->assertEquals('1', $result['warnings'][0]['warningcode']);
 231          $this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
 232      }
 233  }