first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,44 @@
@enrol @enrol_guest
Feature: Guest users can auto-enrol themself in courses where guest access is allowed
In order to access courses contents
As a guest
I need to access courses as a guest
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
| student1 | Student | 1 | student1@example.com |
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | topics |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And the following "activity" exists:
| activity | forum |
| course | C1 |
| idnumber | 0001 |
| name | Test forum name |
And I am on the "Course 1" "enrolment methods" page logged in as teacher1
Scenario: Allow guest access without password
Given I click on "Edit" "link" in the "Guest access" "table_row"
And I set the following fields to these values:
| Allow guest access | Yes |
And I press "Save changes"
When I am on the "Test forum name" "forum activity" page logged in as student1
Then I should not see "Subscribe to this forum"
Scenario: Allow guest access with password
Given I click on "Edit" "link" in the "Guest access" "table_row"
And I set the following fields to these values:
| Allow guest access | Yes |
| Password | moodle_rules |
And I press "Save changes"
When I am on the "Course 1" course page logged in as student1
Then I should see "Guest access"
And I set the following fields to these values:
| Password | moodle_rules |
And I press "Submit"
And I should see "Test forum name"
+122
View File
@@ -0,0 +1,122 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Self enrol external PHPunit tests
*
* @package enrol_guest
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
namespace enrol_guest\external;
use core_external\external_api;
use enrol_guest_external;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Guest enrolment external functions tests
*
* @package enrol_guest
* @category external
* @copyright 2015 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
class external_test extends externallib_advanced_testcase {
/**
* Test get_instance_info
*/
public function test_get_instance_info(): void {
global $DB;
$this->resetAfterTest(true);
// Check if guest enrolment plugin is enabled.
$guestplugin = enrol_get_plugin('guest');
$this->assertNotEmpty($guestplugin);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$coursedata = new \stdClass();
$coursedata->visible = 0;
$course = self::getDataGenerator()->create_course($coursedata);
$student = self::getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
// Add enrolment methods for course.
$instance = $guestplugin->add_instance($course, array('status' => ENROL_INSTANCE_ENABLED,
'name' => 'Test instance',
'customint6' => 1,
'roleid' => $studentrole->id));
$this->setAdminUser();
$result = enrol_guest_external::get_instance_info($instance);
$result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
$this->assertEquals($instance, $result['instanceinfo']['id']);
$this->assertEquals($course->id, $result['instanceinfo']['courseid']);
$this->assertEquals('guest', $result['instanceinfo']['type']);
$this->assertEquals('Test instance', $result['instanceinfo']['name']);
$this->assertTrue($result['instanceinfo']['status']);
$this->assertFalse($result['instanceinfo']['passwordrequired']);
$DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id' => $instance));
$result = enrol_guest_external::get_instance_info($instance);
$result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
$this->assertEquals($instance, $result['instanceinfo']['id']);
$this->assertEquals($course->id, $result['instanceinfo']['courseid']);
$this->assertEquals('guest', $result['instanceinfo']['type']);
$this->assertEquals('Test instance', $result['instanceinfo']['name']);
$this->assertFalse($result['instanceinfo']['status']);
$this->assertFalse($result['instanceinfo']['passwordrequired']);
$DB->set_field('enrol', 'status', ENROL_INSTANCE_ENABLED, array('id' => $instance));
// Try to retrieve information using a normal user for a hidden course.
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
try {
enrol_guest_external::get_instance_info($instance);
} catch (\moodle_exception $e) {
$this->assertEquals('coursehidden', $e->errorcode);
}
// Student user.
$DB->set_field('course', 'visible', 1, array('id' => $course->id));
$this->setUser($student);
$result = enrol_guest_external::get_instance_info($instance);
$result = external_api::clean_returnvalue(enrol_guest_external::get_instance_info_returns(), $result);
$this->assertEquals($instance, $result['instanceinfo']['id']);
$this->assertEquals($course->id, $result['instanceinfo']['courseid']);
$this->assertEquals('guest', $result['instanceinfo']['type']);
$this->assertEquals('Test instance', $result['instanceinfo']['name']);
$this->assertTrue($result['instanceinfo']['status']);
$this->assertFalse($result['instanceinfo']['passwordrequired']);
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace enrol_guest\external;
use core_external\external_api;
/**
* Tests for validate_password class.
*
* @package enrol_guest
* @covers \enrol_guest\external\validate_password
*/
class validate_password_test extends \advanced_testcase {
public function test_execute(): void {
global $DB;
$this->resetAfterTest();
$course = self::getDataGenerator()->create_course();
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
$student = self::getDataGenerator()->create_user();
$pass = 'abc';
// Add enrolment methods for course.
$guestplugin = enrol_get_plugin('guest');
$instanceid = $guestplugin->add_instance($course, [
'status' => ENROL_INSTANCE_ENABLED,
'name' => 'Test instance',
'customint6' => 1,
'password' => $pass,
'roleid' => $studentrole->id,
]);
$this->setUser($student);
// Invalid password.
$result = validate_password::execute($instanceid, 'z');
$result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
$this->assertFalse($result['validated']);
$this->assertEmpty($result['hint']);
// Set invalid password preference.
set_user_preference('enrol_guest_ws_password_' . $instanceid, 'y');
// Enable hint for invalid password.
set_config('showhint', 1, 'enrol_guest');
$result = validate_password::execute($instanceid, 'z');
$result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
$this->assertFalse($result['validated']);
$this->assertNotEmpty($result['hint']); // Check hint.
$this->assertNull(get_user_preferences('enrol_guest_ws_password_'. $instanceid)); // Check preference was reset.
// Try valid password.
$result = validate_password::execute($instanceid, $pass);
$result = external_api::clean_returnvalue(validate_password::execute_returns(), $result);
$this->assertTrue($result['validated']);
// Check correct user preference.
$this->assertEquals($pass, get_user_preferences('enrol_guest_ws_password_'. $instanceid));
// Course hidden, expect exception.
$DB->set_field('course', 'visible', 0, ['id' => $course->id]);
$this->expectException(\moodle_exception::class);
$result = validate_password::execute($instanceid, '');
}
}
+130
View File
@@ -0,0 +1,130 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Guest enrolment tests.
*
* @package enrol_guest
* @category phpunit
* @copyright 2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_guest;
class lib_test extends \advanced_testcase {
/**
* Test the behaviour of validate_enrol_plugin_data().
*
* @covers ::validate_enrol_plugin_data
*/
public function test_validate_enrol_plugin_data(): void {
global $CFG;
$this->resetAfterTest();
$guestplugin = enrol_get_plugin('guest');
$guestplugin->set_config('usepasswordpolicy', false);
$enrolmentdata = [];
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
$this->assertEmpty($errors);
// Now enable some controls, and check that the policy responds with policy text.
$guestplugin->set_config('usepasswordpolicy', true);
$CFG->minpasswordlength = 8;
$CFG->minpassworddigits = 1;
$CFG->minpasswordlower = 1;
$CFG->minpasswordupper = 1;
$CFG->minpasswordnonalphanum = 1;
$CFG->maxconsecutiveidentchars = 1;
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
// If password is omitted it will be autocreated so nothing to validate.
$this->assertEmpty($errors);
$enrolmentdata = ['password' => 'test'];
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
$this->assertCount(4, $errors);
$this->assertEquals(get_string('errorminpasswordlength', 'auth', $CFG->minpasswordlength), $errors['enrol_guest0']);
$this->assertEquals(get_string('errorminpassworddigits', 'auth', $CFG->minpassworddigits), $errors['enrol_guest1']);
$this->assertEquals(get_string('errorminpasswordupper', 'auth', $CFG->minpasswordupper), $errors['enrol_guest2']);
$this->assertEquals(get_string('errorminpasswordnonalphanum', 'auth', $CFG->minpasswordnonalphanum), $errors['enrol_guest3']);
$enrolmentdata = ['password' => 'Testingtest123@'];
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
$this->assertEmpty($errors);
}
/**
* Test the behaviour of update_enrol_plugin_data().
*
* @covers ::update_enrol_plugin_data
*/
public function test_update_enrol_plugin_data(): void {
global $DB;
$this->resetAfterTest();
$manualplugin = enrol_get_plugin('guest');
$admin = get_admin();
$this->setUser($admin);
$enrolmentdata = [];
$cat = $this->getDataGenerator()->create_category();
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
$instance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'guest'], '*', MUST_EXIST);
$expectedinstance = $instance;
$modifiedinstance = $manualplugin->update_enrol_plugin_data($course->id, $enrolmentdata, $instance);
$this->assertEquals($expectedinstance, $modifiedinstance);
$enrolmentdata['password'] = 'test';
$expectedinstance->password = 'test';
$modifiedinstance = $manualplugin->update_enrol_plugin_data($course->id, $enrolmentdata, $instance);
$this->assertEquals($expectedinstance, $modifiedinstance);
}
/**
* Test the behaviour of find_instance().
*
* @covers ::find_instance
*/
public function test_find_instance(): void {
global $DB;
$this->resetAfterTest();
$cat = $this->getDataGenerator()->create_category();
// When we create a course, a guest enrolment instance is also created.
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
$guestplugin = enrol_get_plugin('guest');
$expected = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'guest']);
// Let's try to add second instance - only 1 guest instance is possible.
$instanceid2 = null;
// Have to do this check since add_instance doesn't block adding second instance for guest plugin.
if ($guestplugin->can_add_instance($course->id)) {
$instanceid2 = $guestplugin->add_instance($course, []);
}
$this->assertNull($instanceid2);
$enrolmentdata = [];
$actual = $guestplugin->find_instance($enrolmentdata, $course->id);
$this->assertEquals($expected->id, $actual->id);
}
}