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 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_imscp;
  18  
  19  use externallib_advanced_testcase;
  20  use mod_imscp_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_imscp functions unit tests
  30   *
  31   * @package    mod_imscp
  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_imscp
  41       */
  42      public function test_view_imscp() {
  43          global $DB;
  44  
  45          $this->resetAfterTest(true);
  46  
  47          $this->setAdminUser();
  48          // Setup test data.
  49          $course = $this->getDataGenerator()->create_course();
  50          $imscp = $this->getDataGenerator()->create_module('imscp', array('course' => $course->id));
  51          $context = \context_module::instance($imscp->cmid);
  52          $cm = get_coursemodule_from_instance('imscp', $imscp->id);
  53  
  54          // Test invalid instance id.
  55          try {
  56              mod_imscp_external::view_imscp(0);
  57              $this->fail('Exception expected due to invalid mod_imscp 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_imscp_external::view_imscp($imscp->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_imscp_external::view_imscp($imscp->id);
  80          $result = \external_api::clean_returnvalue(mod_imscp_external::view_imscp_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_imscp\event\course_module_viewed', $event);
  88          $this->assertEquals($context, $event->get_context());
  89          $moodleurl = new \moodle_url('/mod/imscp/view.php', array('id' => $cm->id));
  90          $this->assertEquals($moodleurl, $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/imscp: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_imscp_external::view_imscp($imscp->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 get_imscps_by_courses
 112       */
 113      public function test_get_imscps_by_courses() {
 114          global $DB, $USER;
 115          $this->resetAfterTest(true);
 116          // As admin.
 117          $this->setAdminUser();
 118          $course1 = self::getDataGenerator()->create_course();
 119          $imscpoptions1 = array(
 120            'course' => $course1->id,
 121            'name' => 'First IMSCP'
 122          );
 123          $imscp1 = self::getDataGenerator()->create_module('imscp', $imscpoptions1);
 124          $course2 = self::getDataGenerator()->create_course();
 125  
 126          $imscpoptions2 = array(
 127            'course' => $course2->id,
 128            'name' => 'Second IMSCP'
 129          );
 130          $imscp2 = self::getDataGenerator()->create_module('imscp', $imscpoptions2);
 131          $student1 = $this->getDataGenerator()->create_user();
 132  
 133          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 134  
 135          // Enroll Student1 in Course1.
 136          self::getDataGenerator()->enrol_user($student1->id,  $course1->id, $studentrole->id);
 137  
 138          $this->setUser($student1);
 139          $imscps = mod_imscp_external::get_imscps_by_courses(array());
 140          $imscps = \external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps);
 141          $this->assertCount(1, $imscps['imscps']);
 142          $this->assertEquals('First IMSCP', $imscps['imscps'][0]['name']);
 143          // As Student you cannot see some IMSCP properties like 'section'.
 144          $this->assertFalse(isset($imscps['imscps'][0]['section']));
 145  
 146          // Student1 is not enrolled in this Course.
 147          // The webservice will give a warning!
 148          $imscps = mod_imscp_external::get_imscps_by_courses(array($course2->id));
 149          $imscps = \external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps);
 150          $this->assertCount(0, $imscps['imscps']);
 151          $this->assertEquals(1, $imscps['warnings'][0]['warningcode']);
 152  
 153          // Now as admin.
 154          $this->setAdminUser();
 155          // As Admin we can see this IMSCP.
 156          $imscps = mod_imscp_external::get_imscps_by_courses(array($course2->id));
 157          $imscps = \external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps);
 158          $this->assertCount(1, $imscps['imscps']);
 159          $this->assertEquals('Second IMSCP', $imscps['imscps'][0]['name']);
 160          // As an Admin you can see some IMSCP properties like 'section'.
 161          $this->assertEquals(0, $imscps['imscps'][0]['section']);
 162  
 163          // Now, prohibit capabilities.
 164          $this->setUser($student1);
 165          $contextcourse1 = \context_course::instance($course1->id);
 166          // Prohibit capability = mod:imscp:view on Course1 for students.
 167          assign_capability('mod/imscp:view', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id);
 168          // Empty all the caches that may be affected by this change.
 169          accesslib_clear_all_caches_for_unit_testing();
 170          \course_modinfo::clear_instance_cache();
 171  
 172          $imscps = mod_imscp_external::get_imscps_by_courses(array($course1->id));
 173          $imscps = \external_api::clean_returnvalue(mod_imscp_external::get_imscps_by_courses_returns(), $imscps);
 174          $this->assertCount(0, $imscps['imscps']);
 175      }
 176  }