1 |
efrain |
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 |
/**
|
|
|
18 |
* Guest enrolment tests.
|
|
|
19 |
*
|
|
|
20 |
* @package enrol_guest
|
|
|
21 |
* @category phpunit
|
|
|
22 |
* @copyright 2023 Ilya Tregubov <ilya.a.tregubov@gmail.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
namespace enrol_guest;
|
|
|
26 |
|
|
|
27 |
class lib_test extends \advanced_testcase {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* Test the behaviour of validate_enrol_plugin_data().
|
|
|
31 |
*
|
|
|
32 |
* @covers ::validate_enrol_plugin_data
|
|
|
33 |
*/
|
|
|
34 |
public function test_validate_enrol_plugin_data(): void {
|
|
|
35 |
global $CFG;
|
|
|
36 |
|
|
|
37 |
$this->resetAfterTest();
|
|
|
38 |
|
|
|
39 |
$guestplugin = enrol_get_plugin('guest');
|
|
|
40 |
|
|
|
41 |
$guestplugin->set_config('usepasswordpolicy', false);
|
|
|
42 |
$enrolmentdata = [];
|
|
|
43 |
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
|
|
|
44 |
$this->assertEmpty($errors);
|
|
|
45 |
|
|
|
46 |
// Now enable some controls, and check that the policy responds with policy text.
|
|
|
47 |
$guestplugin->set_config('usepasswordpolicy', true);
|
|
|
48 |
$CFG->minpasswordlength = 8;
|
|
|
49 |
$CFG->minpassworddigits = 1;
|
|
|
50 |
$CFG->minpasswordlower = 1;
|
|
|
51 |
$CFG->minpasswordupper = 1;
|
|
|
52 |
$CFG->minpasswordnonalphanum = 1;
|
|
|
53 |
$CFG->maxconsecutiveidentchars = 1;
|
|
|
54 |
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
|
|
|
55 |
// If password is omitted it will be autocreated so nothing to validate.
|
|
|
56 |
$this->assertEmpty($errors);
|
|
|
57 |
|
|
|
58 |
$enrolmentdata = ['password' => 'test'];
|
|
|
59 |
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
|
|
|
60 |
$this->assertCount(4, $errors);
|
|
|
61 |
$this->assertEquals(get_string('errorminpasswordlength', 'auth', $CFG->minpasswordlength), $errors['enrol_guest0']);
|
|
|
62 |
$this->assertEquals(get_string('errorminpassworddigits', 'auth', $CFG->minpassworddigits), $errors['enrol_guest1']);
|
|
|
63 |
$this->assertEquals(get_string('errorminpasswordupper', 'auth', $CFG->minpasswordupper), $errors['enrol_guest2']);
|
|
|
64 |
$this->assertEquals(get_string('errorminpasswordnonalphanum', 'auth', $CFG->minpasswordnonalphanum), $errors['enrol_guest3']);
|
|
|
65 |
|
|
|
66 |
$enrolmentdata = ['password' => 'Testingtest123@'];
|
|
|
67 |
$errors = $guestplugin->validate_enrol_plugin_data($enrolmentdata);
|
|
|
68 |
$this->assertEmpty($errors);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Test the behaviour of update_enrol_plugin_data().
|
|
|
73 |
*
|
|
|
74 |
* @covers ::update_enrol_plugin_data
|
|
|
75 |
*/
|
|
|
76 |
public function test_update_enrol_plugin_data(): void {
|
|
|
77 |
global $DB;
|
|
|
78 |
$this->resetAfterTest();
|
|
|
79 |
$manualplugin = enrol_get_plugin('guest');
|
|
|
80 |
|
|
|
81 |
$admin = get_admin();
|
|
|
82 |
$this->setUser($admin);
|
|
|
83 |
|
|
|
84 |
$enrolmentdata = [];
|
|
|
85 |
|
|
|
86 |
$cat = $this->getDataGenerator()->create_category();
|
|
|
87 |
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
|
|
|
88 |
$instance = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'guest'], '*', MUST_EXIST);
|
|
|
89 |
|
|
|
90 |
$expectedinstance = $instance;
|
|
|
91 |
$modifiedinstance = $manualplugin->update_enrol_plugin_data($course->id, $enrolmentdata, $instance);
|
|
|
92 |
$this->assertEquals($expectedinstance, $modifiedinstance);
|
|
|
93 |
|
|
|
94 |
$enrolmentdata['password'] = 'test';
|
|
|
95 |
$expectedinstance->password = 'test';
|
|
|
96 |
$modifiedinstance = $manualplugin->update_enrol_plugin_data($course->id, $enrolmentdata, $instance);
|
|
|
97 |
$this->assertEquals($expectedinstance, $modifiedinstance);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Test the behaviour of find_instance().
|
|
|
102 |
*
|
|
|
103 |
* @covers ::find_instance
|
|
|
104 |
*/
|
|
|
105 |
public function test_find_instance() {
|
|
|
106 |
global $DB;
|
|
|
107 |
$this->resetAfterTest();
|
|
|
108 |
|
|
|
109 |
$cat = $this->getDataGenerator()->create_category();
|
|
|
110 |
// When we create a course, a guest enrolment instance is also created.
|
|
|
111 |
$course = $this->getDataGenerator()->create_course(['category' => $cat->id, 'shortname' => 'ANON']);
|
|
|
112 |
|
|
|
113 |
$guestplugin = enrol_get_plugin('guest');
|
|
|
114 |
|
|
|
115 |
$expected = $DB->get_record('enrol', ['courseid' => $course->id, 'enrol' => 'guest']);
|
|
|
116 |
|
|
|
117 |
// Let's try to add second instance - only 1 guest instance is possible.
|
|
|
118 |
$instanceid2 = null;
|
|
|
119 |
// Have to do this check since add_instance doesn't block adding second instance for guest plugin.
|
|
|
120 |
if ($guestplugin->can_add_instance($course->id)) {
|
|
|
121 |
$instanceid2 = $guestplugin->add_instance($course, []);
|
|
|
122 |
}
|
|
|
123 |
$this->assertNull($instanceid2);
|
|
|
124 |
|
|
|
125 |
$enrolmentdata = [];
|
|
|
126 |
$actual = $guestplugin->find_instance($enrolmentdata, $course->id);
|
|
|
127 |
$this->assertEquals($expected->id, $actual->id);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
}
|