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_share_info_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_get_share_info_activity
  38   */
  39  class moodlenet_get_share_info_activity_test extends externallib_advanced_testcase {
  40  
  41      /**
  42       * Test the behaviour of moodlenet_get_share_info_activity().
  43       * @covers ::execute
  44       */
  45      public function test_moodlenet_get_share_info_activity() {
  46          global $CFG;
  47          $this->resetAfterTest();
  48          $this->setAdminUser();
  49          $CFG->enablesharingtomoodlenet = true;
  50  
  51          // Generate course and activities.
  52          $course = $this->getDataGenerator()->create_course();
  53          $activity1 = $this->getDataGenerator()->create_module('chat', ['course' => $course->id, 'name' => 'Chat activity']);
  54          $activity2 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'name' => 'Assign activity']);
  55          $activity3 = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'name' => 'Quiz activity']);
  56  
  57          // Create dummy enabled issuer.
  58          $issuer = \core\moodlenet\helpers::get_mock_issuer(1);
  59  
  60          // Test the 1st activity with no OAuth2 setup yet.
  61          $result = moodlenet_get_share_info_activity::execute($activity1->cmid);
  62          $result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
  63          $this->assertFalse($result['status']);
  64          $this->assertEmpty($result['name']);
  65          $this->assertEmpty($result['type']);
  66          $this->assertEmpty($result['server']);
  67          $this->assertEmpty($result['supportpageurl']);
  68          $this->assertNotEmpty($result['warnings']);
  69          $this->assertEquals(0, $result['warnings'][0]['item']);
  70          $this->assertEquals('errorissuernotset', $result['warnings'][0]['warningcode']);
  71          $this->assertEquals(get_string('moodlenet:issuerisnotset', 'moodle'), $result['warnings'][0]['message']);
  72  
  73          // Test the 1st activity with OAuth2 disabled.
  74          set_config('oauthservice', $issuer->get('id'), 'moodlenet');
  75          $issuer->set('enabled', 0);
  76          $irecord = $issuer->to_record();
  77          api::update_issuer($irecord);
  78  
  79          $result = moodlenet_get_share_info_activity::execute($activity1->cmid);
  80          $result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
  81          $this->assertFalse($result['status']);
  82          $this->assertEmpty($result['name']);
  83          $this->assertEmpty($result['type']);
  84          $this->assertEmpty($result['server']);
  85          $this->assertEmpty($result['supportpageurl']);
  86          $this->assertNotEmpty($result['warnings']);
  87          $this->assertEquals($issuer->get('id'), $result['warnings'][0]['item']);
  88          $this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
  89          $this->assertEquals(get_string('moodlenet:issuerisnotenabled', 'moodle'), $result['warnings'][0]['message']);
  90  
  91          // Test the 1st activity with support url is set to the internal contact site support page.
  92          $issuer->set('enabled', 1);
  93          $irecord = $issuer->to_record();
  94          api::update_issuer($irecord);
  95  
  96          $expectedsupporturl = $CFG->wwwroot . '/user/contactsitesupport.php';
  97          $result = moodlenet_get_share_info_activity::execute($activity1->cmid);
  98          $result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
  99          $this->assertTrue($result['status']);
 100          $this->assertEquals($activity1->name, $result['name']);
 101          $this->assertEquals(get_string('modulename', 'mod_chat'), $result['type']);
 102          $this->assertEquals($issuer->get_display_name(), $result['server']);
 103          $this->assertEquals($expectedsupporturl, $result['supportpageurl']);
 104  
 105          // Test the 2nd activity with support url is set to the external contact site support page.
 106          $expectedsupporturl = 'https://moodle.org/';
 107          $CFG->supportpage = $expectedsupporturl;
 108          $result = moodlenet_get_share_info_activity::execute($activity2->cmid);
 109          $result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
 110          $this->assertTrue($result['status']);
 111          $this->assertEquals($activity2->name, $result['name']);
 112          $this->assertEquals(get_string('modulename', 'mod_assign'), $result['type']);
 113          $this->assertEquals($expectedsupporturl, $result['supportpageurl']);
 114  
 115          // Test the 3rd activity with contact site support is disabled.
 116          $CFG->supportavailability = CONTACT_SUPPORT_DISABLED;
 117          $result = moodlenet_get_share_info_activity::execute($activity3->cmid);
 118          $result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
 119          $this->assertTrue($result['status']);
 120          $this->assertEquals($activity3->name, $result['name']);
 121          $this->assertEquals(get_string('modulename', 'mod_quiz'), $result['type']);
 122          $this->assertEmpty($result['supportpageurl']);
 123  
 124          // Test with an invalid activity.
 125          // Get a random cmid that not in the created activity list.
 126          $cmids = [$activity1->cmid, $activity2->cmid, $activity3->cmid];
 127          do {
 128              $randomcmid = random_int(5, 25);
 129          } while (in_array($randomcmid, $cmids));
 130          $result = moodlenet_get_share_info_activity::execute($randomcmid);
 131          $result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
 132          $this->assertFalse($result['status']);
 133          $this->assertEmpty($result['name']);
 134          $this->assertEmpty($result['type']);
 135          $this->assertNotEmpty($result['warnings']);
 136          $this->assertEquals($randomcmid, $result['warnings'][0]['item']);
 137          $this->assertEquals('errorgettingactivityinformation', $result['warnings'][0]['warningcode']);
 138          $this->assertEquals(get_string('invalidcoursemodule', 'error'), $result['warnings'][0]['message']);
 139      }
 140  }