Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 and 403]

   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\moodlenet;
  18  
  19  use context_course;
  20  use stdClass;
  21  use testing_data_generator;
  22  
  23  /**
  24   * Unit tests for {@see utilities}.
  25   *
  26   * @coversDefaultClass \core\moodlenet\utilities
  27   * @package core
  28   * @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
  29   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class utilities_test extends \advanced_testcase {
  32  
  33      /** @var testing_data_generator Data generator. */
  34      private testing_data_generator $generator;
  35  
  36      /** @var stdClass Activity object, */
  37      private stdClass $course;
  38  
  39      /** @var context_course Course context instance. */
  40      private context_course $coursecontext;
  41  
  42      /**
  43       * Set up function for tests.
  44       */
  45      protected function setUp(): void {
  46          parent::setUp();
  47  
  48          $this->resetAfterTest();
  49          $this->generator = $this->getDataGenerator();
  50          $this->course = $this->generator->create_course();
  51          $this->coursecontext = context_course::instance($this->course->id);
  52      }
  53  
  54      /**
  55       * Test is_valid_instance method.
  56       *
  57       * @covers ::is_valid_instance
  58       * @return void
  59       */
  60      public function test_is_valid_instance() {
  61          global $CFG;
  62          $this->setAdminUser();
  63  
  64          // Create dummy issuer.
  65          $issuer = new \core\oauth2\issuer(0);
  66          $issuer->set('enabled', 0);
  67          $issuer->set('servicetype', 'google');
  68  
  69          // Can not share if the experimental flag it set to false.
  70          $CFG->enablesharingtomoodlenet = false;
  71          $this->assertFalse(utilities::is_valid_instance($issuer));
  72  
  73          // Enable the experimental flag.
  74          $CFG->enablesharingtomoodlenet = true;
  75  
  76          // Can not share if the OAuth 2 service in the outbound setting is not matched the given one.
  77          set_config('oauthservice', random_int(1, 30), 'moodlenet');
  78          $this->assertFalse(utilities::is_valid_instance($issuer));
  79  
  80          // Can not share if the OAuth 2 service in the outbound setting is not enabled.
  81          set_config('oauthservice', $issuer->get('id'), 'moodlenet');
  82          $this->assertFalse(utilities::is_valid_instance($issuer));
  83  
  84          // Can not share if the OAuth 2 service type is not moodlenet.
  85          $issuer->set('enabled', 1);
  86          $this->assertFalse(utilities::is_valid_instance($issuer));
  87  
  88          // All good now.
  89          $issuer->set('servicetype', 'moodlenet');
  90          $this->assertTrue(utilities::is_valid_instance($issuer));
  91      }
  92  
  93  
  94      /**
  95       * Test can_user_share method.
  96       *
  97       * @covers ::can_user_share
  98       * @return void
  99       */
 100      public function test_can_user_share() {
 101          global $DB;
 102  
 103          // Generate data.
 104          $student1 = $this->generator->create_user();
 105          $teacher1 = $this->generator->create_user();
 106          $teacher2 = $this->generator->create_user();
 107          $manager1 = $this->generator->create_user();
 108  
 109          // Enrol users.
 110          $this->generator->enrol_user($student1->id, $this->course->id, 'student');
 111          $this->generator->enrol_user($teacher1->id, $this->course->id, 'teacher');
 112          $this->generator->enrol_user($teacher2->id, $this->course->id, 'editingteacher');
 113          $this->generator->enrol_user($manager1->id, $this->course->id, 'manager');
 114  
 115          // Get roles.
 116          $teacherrole = $DB->get_record('role', ['shortname' => 'teacher'], 'id', MUST_EXIST);
 117          $editingteacherrole = $DB->get_record('role', ['shortname' => 'editingteacher'], 'id', MUST_EXIST);
 118  
 119          // Test with default settings.
 120          // Student and Teacher cannot share the activity.
 121          $this->assertFalse(utilities::can_user_share($this->coursecontext, $student1->id));
 122          $this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher1->id));
 123          // Editing-teacher and Manager can share the activity.
 124          $this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher2->id));
 125          $this->assertTrue(utilities::can_user_share($this->coursecontext, $manager1->id));
 126  
 127          // Teacher who has the capabilities can share the activity.
 128          assign_capability('moodle/moodlenet:shareactivity', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
 129          assign_capability('moodle/backup:backupactivity', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
 130          $this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher1->id));
 131  
 132          // Editing-teacher who does not have the capabilities can not share the activity.
 133          assign_capability('moodle/moodlenet:shareactivity', CAP_PROHIBIT, $editingteacherrole->id, $this->coursecontext);
 134          $this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher2->id));
 135      }
 136  }