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_activity.
  32   *
  33   * @package    core
  34   * @category   test
  35   * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @coversDefaultClass \core\external\moodlenet_send_activity
  38   */
  39  class moodlenet_send_activity_test extends externallib_advanced_testcase {
  40  
  41      /**
  42       * Test the behaviour of moodlenet_send_activity().
  43       *
  44       * @covers ::execute
  45       */
  46      public function test_moodlenet_send_activity() {
  47          global $CFG;
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50  
  51          // Generate data.
  52          $generator = $this->getDataGenerator();
  53          $course = $generator->create_course();
  54          $moduleinstance = $generator->create_module('assign', ['course' => $course->id]);
  55          $user = $generator->create_user();
  56          $generator->enrol_user($user->id, $course->id, 'student');
  57  
  58          // Create dummy issuer.
  59          $issuer = \core\moodlenet\helpers::get_mock_issuer(0);
  60  
  61          // Test with the experimental flag off.
  62          $result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
  63          $result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
  64          $this->assertFalse($result['status']);
  65          $this->assertNotEmpty($result['warnings']);
  66          $this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
  67  
  68          $CFG->enablesharingtomoodlenet = true;
  69  
  70          // Test with invalid format.
  71          $result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 5);
  72          $result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
  73          $this->assertFalse($result['status']);
  74          $this->assertNotEmpty($result['warnings']);
  75          $this->assertEquals('errorinvalidformat', $result['warnings'][0]['warningcode']);
  76  
  77          // Test with the user does not have permission.
  78          $this->setUser($user);
  79          $result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
  80          $result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
  81          $this->assertFalse($result['status']);
  82          $this->assertNotEmpty($result['warnings']);
  83          $this->assertEquals('errorpermission', $result['warnings'][0]['warningcode']);
  84  
  85          $this->setAdminUser();
  86  
  87          // Test with the issuer is not enabled.
  88          $result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
  89          $result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
  90          $this->assertFalse($result['status']);
  91          $this->assertNotEmpty($result['warnings']);
  92          $this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
  93  
  94          // Test with the issuer is enabled but not set in the MN Outbound setting.
  95          $issuer->set('enabled', 1);
  96          $irecord = $issuer->to_record();
  97          api::update_issuer($irecord);
  98          $result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
  99          $result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
 100          $this->assertFalse($result['status']);
 101          $this->assertNotEmpty($result['warnings']);
 102          $this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
 103  
 104          set_config('oauthservice', $issuer->get('id'), 'moodlenet');
 105          // Test with the issuer not yet authorized.
 106          $result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
 107          $result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
 108          $this->assertFalse($result['status']);
 109          $this->assertNotEmpty($result['warnings']);
 110          $this->assertEquals('erroroauthclient', $result['warnings'][0]['warningcode']);
 111          $this->assertEquals($issuer->get('id'), $result['warnings'][0]['item']);
 112          $this->assertEquals(get_string('moodlenet:issuerisnotauthorized', 'moodle'), $result['warnings'][0]['message']);
 113      }
 114  
 115      /**
 116       * Test execute_returns() method.
 117       *
 118       * @dataProvider return_resource_url_provider
 119       * @covers ::execute_returns
 120       */
 121      public function test_moodlenet_send_activity_return_resource_url(bool $state, string $resourceurl) {
 122          $this->resetAfterTest();
 123          // Create dummy result with the resourceurl.
 124          $result = [
 125              'status' => true,
 126              'resourceurl' => $resourceurl,
 127              'warnings' => [],
 128          ];
 129          if (!$state) {
 130              $this->expectException(\invalid_response_exception::class);
 131          }
 132          $result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
 133          if ($state) {
 134              $this->assertEquals($resourceurl, $result['resourceurl']);
 135          }
 136      }
 137  
 138      /**
 139       * Provider for test_moodlenet_send_activity_return_resource_url().
 140       *
 141       * @return array Test data.
 142       */
 143      public function return_resource_url_provider(): array {
 144          return [
 145              'Success 1' => [
 146                  true,
 147                  'https://moodlenet.example.com/drafts/view/testactivity_backup.mbz',
 148              ],
 149              'Success 2' => [
 150                  true,
 151                  'https://moodlenet.example.com/drafts/view/testactivity_backup with spaces.mbz',
 152              ],
 153              'Success 3' => [
 154                  true,
 155                  'https://moodlenet.example.com/drafts/view/testactivity_backup with " character.mbz',
 156              ],
 157              'Success 4' => [
 158                  true,
 159                  "https://moodlenet.example.com/drafts/view/testactivity_backup with ' character.mbz",
 160              ],
 161              'Success 5' => [
 162                  true,
 163                  'https://moodlenet.example.com/drafts/view/testactivity_backup with < and > characters.mbz',
 164              ],
 165              'Fail 1' => [
 166                  false,
 167                  'https://moodlenet.example.com/drafts/view/testactivity_backupwith<lang lang="en">a<a</lang>html.mbz',
 168              ],
 169          ];
 170      }
 171  }