Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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 core\external;
  18  
  19  use core\oauth2\api;
  20  use core_external\external_api;
  21  use externallib_advanced_testcase;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  
  27  require_once($CFG->dirroot . '/lib/tests/moodlenet/helpers.php');
  28  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  29  
  30  /**
  31   * External functions test for moodlenet_send_course.
  32   *
  33   * @package    core
  34   * @category   test
  35   * @copyright  2023 Safat Shahin <safat.shahin@gmail.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @coversDefaultClass \core\external\moodlenet_send_course
  38   */
  39  class moodlenet_send_course_test extends externallib_advanced_testcase {
  40  
  41      /**
  42       * Test the behaviour of moodlenet_send_course().
  43       *
  44       * @covers ::execute
  45       */
  46      public function test_moodlenet_send_course() {
  47          global $CFG;
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50  
  51          // Generate data.
  52          $generator = $this->getDataGenerator();
  53          $course = $generator->create_course();
  54          $user = $generator->create_user();
  55          $generator->enrol_user($user->id, $course->id, 'student');
  56  
  57          // Create dummy issuer.
  58          $issuer = \core\moodlenet\helpers::get_mock_issuer(0);
  59  
  60          // Test with the experimental flag off.
  61          $result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
  62          $result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
  63          $this->assertFalse($result['status']);
  64          $this->assertNotEmpty($result['warnings']);
  65          $this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
  66  
  67          $CFG->enablesharingtomoodlenet = true;
  68  
  69          // Test with invalid format.
  70          $result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 5);
  71          $result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
  72          $this->assertFalse($result['status']);
  73          $this->assertNotEmpty($result['warnings']);
  74          $this->assertEquals('errorinvalidformat', $result['warnings'][0]['warningcode']);
  75  
  76          // Test with invalid course module id.
  77          $result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0, [random_int(5, 30)]);
  78          $result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
  79          $this->assertFalse($result['status']);
  80          $this->assertNotEmpty($result['warnings']);
  81          $this->assertEquals('errorinvalidcmids', $result['warnings'][0]['warningcode']);
  82  
  83          // Test with the user does not have permission.
  84          $this->setUser($user);
  85          $result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
  86          $result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
  87          $this->assertFalse($result['status']);
  88          $this->assertNotEmpty($result['warnings']);
  89          $this->assertEquals('errorpermission', $result['warnings'][0]['warningcode']);
  90  
  91          $this->setAdminUser();
  92  
  93          // Test with the issuer is not enabled.
  94          $result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
  95          $result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
  96          $this->assertFalse($result['status']);
  97          $this->assertNotEmpty($result['warnings']);
  98          $this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
  99  
 100          // Test with the issuer is enabled but not set in the MN Outbound setting.
 101          $issuer->set('enabled', 1);
 102          $irecord = $issuer->to_record();
 103          api::update_issuer($irecord);
 104          $result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
 105          $result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
 106          $this->assertFalse($result['status']);
 107          $this->assertNotEmpty($result['warnings']);
 108          $this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
 109  
 110          set_config('oauthservice', $issuer->get('id'), 'moodlenet');
 111          // Test with the issuer not yet authorized.
 112          $result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
 113          $result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
 114          $this->assertFalse($result['status']);
 115          $this->assertNotEmpty($result['warnings']);
 116          $this->assertEquals('erroroauthclient', $result['warnings'][0]['warningcode']);
 117          $this->assertEquals($issuer->get('id'), $result['warnings'][0]['item']);
 118          $this->assertEquals(get_string('moodlenet:issuerisnotauthorized', 'moodle'), $result['warnings'][0]['message']);
 119      }
 120  
 121      /**
 122       * Test execute_returns() method.
 123       *
 124       * @dataProvider return_resource_url_provider
 125       * @covers ::execute_returns
 126       */
 127      public function test_moodlenet_send_course_return_resource_url(bool $state, string $resourceurl) {
 128          $this->resetAfterTest();
 129          // Create dummy result with the resourceurl.
 130          $result = [
 131              'status' => true,
 132              'resourceurl' => $resourceurl,
 133              'warnings' => [],
 134          ];
 135          if (!$state) {
 136              $this->expectException(\invalid_response_exception::class);
 137          }
 138          $result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
 139          if ($state) {
 140              $this->assertEquals($resourceurl, $result['resourceurl']);
 141          }
 142      }
 143  
 144      /**
 145       * Provider for test_moodlenet_send_course_return_resource_url().
 146       *
 147       * @return array Test data.
 148       */
 149      public function return_resource_url_provider(): array {
 150          return [
 151              'Success 1' => [
 152                  true,
 153                  'https://moodlenet.example.com/drafts/view/testcourse_backup.mbz',
 154              ],
 155              'Success 2' => [
 156                  true,
 157                  'https://moodlenet.example.com/drafts/view/testcourse_backup with spaces.mbz',
 158              ],
 159              'Success 3' => [
 160                  true,
 161                  'https://moodlenet.example.com/drafts/view/testcourse_backup with " character.mbz',
 162              ],
 163              'Success 4' => [
 164                  true,
 165                  "https://moodlenet.example.com/drafts/view/testcourse_backup with ' character.mbz",
 166              ],
 167              'Success 5' => [
 168                  true,
 169                  'https://moodlenet.example.com/drafts/view/testcourse_backup with < and > characters.mbz',
 170              ],
 171              'Fail 1' => [
 172                  false,
 173                  'https://moodlenet.example.com/drafts/view/testcourse_backupwith<lang lang="en">a<a</lang>html.mbz',
 174              ],
 175          ];
 176      }
 177  }