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 enrol_guest\external;
  18  
  19  use core_external\external_api;
  20  
  21  /**
  22   * Tests for validate_password class.
  23   *
  24   * @package enrol_guest
  25   * @covers \enrol_guest\external\validate_password
  26   */
  27  class validate_password_test extends \advanced_testcase {
  28  
  29      public function test_execute(): void {
  30          global $DB;
  31  
  32          $this->resetAfterTest();
  33  
  34          $course = self::getDataGenerator()->create_course();
  35          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
  36          $student = self::getDataGenerator()->create_user();
  37          $pass = 'abc';
  38  
  39          // Add enrolment methods for course.
  40          $guestplugin = enrol_get_plugin('guest');
  41          $instanceid = $guestplugin->add_instance($course, [
  42              'status' => ENROL_INSTANCE_ENABLED,
  43              'name' => 'Test instance',
  44              'customint6' => 1,
  45              'password' => $pass,
  46              'roleid' => $studentrole->id,
  47          ]);
  48  
  49          $this->setUser($student);
  50  
  51          // Invalid password.
  52          $result = validate_password::execute($instanceid, 'z');
  53          $result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
  54          $this->assertFalse($result['validated']);
  55          $this->assertEmpty($result['hint']);
  56  
  57          // Set invalid password preference.
  58          set_user_preference('enrol_guest_ws_password_' . $instanceid, 'y');
  59  
  60          // Enable hint for invalid password.
  61          set_config('showhint', 1, 'enrol_guest');
  62          $result = validate_password::execute($instanceid, 'z');
  63          $result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
  64          $this->assertFalse($result['validated']);
  65          $this->assertNotEmpty($result['hint']); // Check hint.
  66          $this->assertNull(get_user_preferences('enrol_guest_ws_password_'. $instanceid));   // Check preference was reset.
  67  
  68          // Try valid password.
  69          $result = validate_password::execute($instanceid, $pass);
  70          $result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
  71          $this->assertTrue($result['validated']);
  72  
  73          // Check correct user preference.
  74          $this->assertEquals($pass, get_user_preferences('enrol_guest_ws_password_'. $instanceid));
  75  
  76          // Course hidden, expect exception.
  77          $DB->set_field('course', 'visible', 0, ['id' => $course->id]);
  78          $this->expectException(\moodle_exception::class);
  79          $result = validate_password::execute($instanceid, '');
  80      }
  81  }