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_get_shared_course_info.
  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_get_shared_course_info
  38   */
  39  class moodlenet_get_shared_course_info_test extends externallib_advanced_testcase {
  40  
  41      /**
  42       * Test the behaviour of moodlenet_get_shared_course_info().
  43       *
  44       * @covers ::execute
  45       */
  46      public function test_moodlenet_get_shared_course_info() {
  47          global $CFG;
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50          $CFG->enablesharingtomoodlenet = true;
  51  
  52          // Generate course and activities.
  53          $course = $this->getDataGenerator()->create_course();
  54  
  55          // Create dummy enabled issuer.
  56          $issuer = \core\moodlenet\helpers::get_mock_issuer(1);
  57  
  58          // Test the course with no OAuth2 setup yet.
  59          $result = moodlenet_get_shared_course_info::execute($course->id);
  60          $result = external_api::clean_returnvalue(moodlenet_get_shared_course_info::execute_returns(), $result);
  61          $this->assertFalse($result['status']);
  62          $this->assertEmpty($result['name']);
  63          $this->assertEmpty($result['type']);
  64          $this->assertEmpty($result['server']);
  65          $this->assertEmpty($result['supportpageurl']);
  66          $this->assertNotEmpty($result['warnings']);
  67          $this->assertEquals(0, $result['warnings'][0]['item']);
  68          $this->assertEquals('errorissuernotset', $result['warnings'][0]['warningcode']);
  69          $this->assertEquals(get_string('moodlenet:issuerisnotset', 'moodle'), $result['warnings'][0]['message']);
  70  
  71          // Test the course with OAuth2 disabled.
  72          set_config('oauthservice', $issuer->get('id'), 'moodlenet');
  73          $issuer->set('enabled', 0);
  74          $irecord = $issuer->to_record();
  75          api::update_issuer($irecord);
  76  
  77          $result = moodlenet_get_shared_course_info::execute($course->id);
  78          $result = external_api::clean_returnvalue(moodlenet_get_shared_course_info::execute_returns(), $result);
  79          $this->assertFalse($result['status']);
  80          $this->assertEmpty($result['name']);
  81          $this->assertEmpty($result['type']);
  82          $this->assertEmpty($result['server']);
  83          $this->assertEmpty($result['supportpageurl']);
  84          $this->assertNotEmpty($result['warnings']);
  85          $this->assertEquals($issuer->get('id'), $result['warnings'][0]['item']);
  86          $this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
  87          $this->assertEquals(get_string('moodlenet:issuerisnotenabled', 'moodle'), $result['warnings'][0]['message']);
  88  
  89          // Test the course with support url is set to the internal contact site support page.
  90          $issuer->set('enabled', 1);
  91          $irecord = $issuer->to_record();
  92          api::update_issuer($irecord);
  93  
  94          $expectedsupporturl = $CFG->wwwroot . '/user/contactsitesupport.php';
  95          $result = moodlenet_get_shared_course_info::execute($course->id);
  96          $result = external_api::clean_returnvalue(moodlenet_get_shared_course_info::execute_returns(), $result);
  97          $this->assertTrue($result['status']);
  98          $this->assertEquals($course->fullname, $result['name']);
  99          $this->assertEquals(get_string('course'), $result['type']);
 100          $this->assertEquals($issuer->get_display_name(), $result['server']);
 101          $this->assertEquals($expectedsupporturl, $result['supportpageurl']);
 102      }
 103  }