Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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 mod_bigbluebuttonbn\external;
  18  
  19  use external_api;
  20  use mod_bigbluebuttonbn\instance;
  21  use mod_bigbluebuttonbn\test\testcase_helper_trait;
  22  use require_login_exception;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28  
  29  /**
  30   * Tests for the completion_validate class.
  31   *
  32   * @package    mod_bigbluebuttonbn
  33   * @category   test
  34   * @copyright  2021 - present, Blindside Networks Inc
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @author    Laurent David (laurent@call-learning.fr)
  37   * @covers \mod_bigbluebuttonbn\external\completion_validate
  38   */
  39  class completion_validate_test extends \externallib_advanced_testcase {
  40      use testcase_helper_trait;
  41  
  42      /**
  43       * Setup for test
  44       */
  45      public function setUp(): void {
  46          parent::setUp();
  47          $this->initialise_mock_server();
  48      }
  49  
  50      /**
  51       * Helper
  52       *
  53       * @param mixed ...$params
  54       * @return mixed
  55       */
  56      protected function completion_validate(...$params) {
  57          $returnvalue = completion_validate::execute(...$params);
  58  
  59          return external_api::clean_returnvalue(completion_validate::execute_returns(), $returnvalue);
  60      }
  61  
  62      /**
  63       * Test execute API CALL with no instance
  64       */
  65      public function test_execute_no_instance() {
  66          $result = $this->completion_validate(1234);
  67  
  68          $this->assertIsArray($result);
  69          $this->assertArrayHasKey('warnings', $result);
  70          $this->assertEquals([
  71              'item' => 'mod_bigbluebuttonbn',
  72              'itemid' => 1234,
  73              'warningcode' => 'indexerrorbbtn',
  74              'message' => 'BigBlueButton ID 1234 is incorrect',
  75          ], $result['warnings'][0]);
  76      }
  77  
  78      /**
  79       * Test execute API CALL without login
  80       */
  81      public function test_execute_without_login() {
  82          $this->resetAfterTest();
  83  
  84          $course = $this->getDataGenerator()->create_course();
  85          $record = $this->getDataGenerator()->create_module('bigbluebuttonbn', ['course' => $course->id]);
  86          $instance = instance::get_from_instanceid($record->id);
  87          $this->expectException(require_login_exception::class);
  88          $this->completion_validate($instance->get_instance_id());
  89      }
  90  
  91      /**
  92       * Test execute API CALL with invalid login
  93       */
  94      public function test_execute_with_invalid_login() {
  95          $this->resetAfterTest();
  96  
  97          $generator = $this->getDataGenerator();
  98          $course = $generator->create_course();
  99          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 100          $instance = instance::get_from_instanceid($record->id);
 101  
 102          $user = $generator->create_user($course);
 103          $this->setUser($user);
 104          $this->expectException(require_login_exception::class);
 105          $this->completion_validate($instance->get_instance_id());
 106      }
 107  
 108      /**
 109       * When login as a student
 110       */
 111      public function test_execute_with_valid_login_but_student() {
 112          $this->resetAfterTest();
 113  
 114          $generator = $this->getDataGenerator();
 115          $course = $generator->create_course();
 116          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 117          $instance = instance::get_from_instanceid($record->id);
 118  
 119          $user = $generator->create_and_enrol($course, 'student');
 120          $this->setUser($user);
 121  
 122          $returnvalue = $this->completion_validate($instance->get_instance_id());
 123  
 124          $this->assertArrayHasKey('warnings', $returnvalue);
 125  
 126          $this->assertEquals([
 127              'item' => 'mod_bigbluebuttonbn',
 128              'itemid' => $record->id,
 129              'warningcode' => 'nopermissions',
 130              'message' => 'Sorry, but you do not currently have permissions to do that (completion_validate).',
 131          ], $returnvalue['warnings'][0]);
 132      }
 133  
 134      /**
 135       * When login as a student
 136       */
 137      public function test_execute_with_valid_login_with_teacher() {
 138          $this->resetAfterTest();
 139  
 140          $generator = $this->getDataGenerator();
 141          $course = $generator->create_course();
 142          $record = $generator->create_module('bigbluebuttonbn', ['course' => $course->id]);
 143          $instance = instance::get_from_instanceid($record->id);
 144  
 145          $user = $generator->create_and_enrol($course, 'editingteacher');
 146          $this->setUser($user);
 147  
 148          $returnvalue = $this->completion_validate($instance->get_instance_id());
 149  
 150          $this->assertArrayHasKey('warnings', $returnvalue);
 151          $this->assertEmpty($returnvalue['warnings']);
 152      }
 153  }
 154