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 |
* coursecompleted enrolment manager tests.
|
|
|
19 |
*
|
|
|
20 |
* @package enrol_coursecompleted
|
|
|
21 |
* @copyright 2017 eWallah (www.eWallah.net)
|
|
|
22 |
* @author Renaat Debleu <info@eWallah.net>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace enrol_coursecompleted;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* coursecompleted enrolment manager tests.
|
|
|
30 |
*
|
|
|
31 |
* @package enrol_coursecompleted
|
|
|
32 |
* @copyright 2017 eWallah (www.eWallah.net)
|
|
|
33 |
* @author Renaat Debleu <info@eWallah.net>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
* @coversDefaultClass \enrol_coursecompleted_plugin
|
|
|
36 |
*/
|
|
|
37 |
final class manager_test extends \advanced_testcase {
|
|
|
38 |
/** @var stdClass Instance. */
|
|
|
39 |
private $instance;
|
|
|
40 |
|
|
|
41 |
/** @var stdClass Student. */
|
|
|
42 |
private $student;
|
|
|
43 |
|
|
|
44 |
/** @var stdClass course. */
|
|
|
45 |
private $course;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Tests initial setup.
|
|
|
49 |
*/
|
|
|
50 |
protected function setUp(): void {
|
|
|
51 |
global $CFG, $DB;
|
|
|
52 |
$CFG->enablecompletion = true;
|
|
|
53 |
$this->resetAfterTest(true);
|
|
|
54 |
$enabled = enrol_get_plugins(true);
|
|
|
55 |
unset($enabled['guest']);
|
|
|
56 |
unset($enabled['self']);
|
|
|
57 |
$enabled['coursecompleted'] = true;
|
|
|
58 |
set_config('enrol_plugins_enabled', implode(',', array_keys($enabled)));
|
|
|
59 |
$generator = $this->getDataGenerator();
|
|
|
60 |
$course = $generator->create_course(['shortname' => 'A1', 'enablecompletion' => 1]);
|
|
|
61 |
$this->course = $generator->create_course(['shortname' => 'A2', 'enablecompletion' => 1]);
|
|
|
62 |
$studentrole = $DB->get_record('role', ['shortname' => 'student']);
|
|
|
63 |
$this->setAdminUser();
|
|
|
64 |
$plugin = enrol_get_plugin('coursecompleted');
|
|
|
65 |
$id = $plugin->add_instance($course, ['customint1' => $this->course->id, 'roleid' => $studentrole->id]);
|
|
|
66 |
$this->instance = $DB->get_record('enrol', ['id' => $id]);
|
|
|
67 |
$this->student = $generator->create_and_enrol($this->course, 'student');
|
|
|
68 |
mark_user_dirty($this->student->id);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Test missing enrolid param.
|
|
|
73 |
* @covers \enrol_coursecompleted_plugin
|
|
|
74 |
*/
|
|
|
75 |
public function test_manager_empty_param(): void {
|
|
|
76 |
global $CFG;
|
|
|
77 |
chdir($CFG->dirroot . '/enrol/coursecompleted');
|
|
|
78 |
$this->expectException(\moodle_exception::class);
|
|
|
79 |
$this->expectExceptionMessage('A required parameter (enrolid) was missing');
|
|
|
80 |
include($CFG->dirroot . '/enrol/coursecompleted/manage.php');
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Test manager without permission.
|
|
|
85 |
* @covers \enrol_coursecompleted_plugin
|
|
|
86 |
*/
|
|
|
87 |
public function test_manager_without_permission(): void {
|
|
|
88 |
global $CFG;
|
|
|
89 |
chdir($CFG->dirroot . '/enrol/coursecompleted');
|
|
|
90 |
$this->setUser($this->student);
|
|
|
91 |
$_POST['enrolid'] = $this->instance->id;
|
|
|
92 |
$this->expectException(\moodle_exception::class);
|
|
|
93 |
$this->expectExceptionMessage('Sorry, but you do not currently have permissions to do that (Enrol users).');
|
|
|
94 |
include($CFG->dirroot . '/enrol/coursecompleted/manage.php');
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Test manager wrong permission.
|
|
|
99 |
* @covers \enrol_coursecompleted_plugin
|
|
|
100 |
*/
|
|
|
101 |
public function test_manager_wrong_permission(): void {
|
|
|
102 |
global $CFG, $DB;
|
|
|
103 |
chdir($CFG->dirroot . '/enrol/coursecompleted');
|
|
|
104 |
$generator = $this->getDataGenerator();
|
|
|
105 |
$user = $generator->create_and_enrol($this->course, 'editingteacher');
|
|
|
106 |
$role = $DB->get_record('role', ['shortname' => 'editingteacher']);
|
|
|
107 |
$context = \context_course::instance($this->course->id);
|
|
|
108 |
assign_capability('enrol/coursecompleted:enrolpast', CAP_PROHIBIT, $role->id, $context);
|
|
|
109 |
assign_capability('enrol/coursecompleted:unenrol', CAP_PROHIBIT, $role->id, $context);
|
|
|
110 |
assign_capability('enrol/manual:enrol', CAP_ALLOW, $role->id, $context);
|
|
|
111 |
\core\session\manager::init_empty_session();
|
|
|
112 |
$this->setUser($user);
|
|
|
113 |
$_POST['enrolid'] = $this->instance->id;
|
|
|
114 |
$this->expectException(\moodle_exception::class);
|
|
|
115 |
$this->expectExceptionMessage('Sorry, but you do not currently have permissions to do that (Enrol users).');
|
|
|
116 |
include($CFG->dirroot . '/enrol/coursecompleted/manage.php');
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Test manager bare.
|
|
|
121 |
* @covers \enrol_coursecompleted_plugin
|
|
|
122 |
* @covers \enrol_coursecompleted\form\bulkedit
|
|
|
123 |
* @covers \enrol_coursecompleted\form\bulkdelete
|
|
|
124 |
*/
|
|
|
125 |
public function test_manager_bare(): void {
|
|
|
126 |
global $CFG;
|
|
|
127 |
chdir($CFG->dirroot . '/enrol/coursecompleted');
|
|
|
128 |
$_POST['enrolid'] = $this->instance->id;
|
|
|
129 |
ob_start();
|
|
|
130 |
include($CFG->dirroot . '/enrol/coursecompleted/manage.php');
|
|
|
131 |
$html = ob_get_clean();
|
|
|
132 |
$this->assertStringContainsString('No users found', $html);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Test manager old users.
|
|
|
137 |
* @covers \enrol_coursecompleted_plugin
|
|
|
138 |
*/
|
|
|
139 |
public function test_manager_old_users(): void {
|
|
|
140 |
global $CFG, $DB;
|
|
|
141 |
$this->preventResetByRollback();
|
|
|
142 |
$this->setAdminUser();
|
|
|
143 |
$cc = new \stdClass();
|
|
|
144 |
$cc->userid = $this->student->id;
|
|
|
145 |
$cc->course = $this->course->id;
|
|
|
146 |
$cc->timestarted = time() - 100;
|
|
|
147 |
$cc->timeenrolled = 0;
|
|
|
148 |
$cc->timecompleted = time() - 50;
|
|
|
149 |
$DB->insert_record('course_completions', $cc);
|
|
|
150 |
chdir($CFG->dirroot . '/enrol/coursecompleted');
|
|
|
151 |
$_POST['enrolid'] = $this->instance->id;
|
|
|
152 |
ob_start();
|
|
|
153 |
include($CFG->dirroot . '/enrol/coursecompleted/manage.php');
|
|
|
154 |
$html = ob_get_clean();
|
|
|
155 |
$this->assertStringNotContainsString('No users found', $html);
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
/**
|
|
|
159 |
* Test submit manager oldusers.
|
|
|
160 |
* @covers \enrol_coursecompleted_plugin
|
|
|
161 |
*/
|
|
|
162 |
public function test_manager_submit(): void {
|
|
|
163 |
global $CFG, $DB;
|
|
|
164 |
$this->preventResetByRollback();
|
|
|
165 |
$this->setAdminUser();
|
|
|
166 |
set_config('messaging', false);
|
|
|
167 |
$cc = new \stdClass();
|
|
|
168 |
$cc->userid = $this->student->id;
|
|
|
169 |
$cc->course = $this->course->id;
|
|
|
170 |
$cc->timestarted = time() - 100;
|
|
|
171 |
$cc->timeenrolled = 0;
|
|
|
172 |
$cc->timecompleted = time() - 50;
|
|
|
173 |
$DB->insert_record('course_completions', $cc);
|
|
|
174 |
|
|
|
175 |
chdir($CFG->dirroot . '/enrol/coursecompleted');
|
|
|
176 |
$_POST['enrolid'] = $this->instance->id;
|
|
|
177 |
$_POST['action'] = 'enrol';
|
|
|
178 |
$_POST['sesskey'] = sesskey();
|
|
|
179 |
ob_start();
|
|
|
180 |
include($CFG->dirroot . '/enrol/coursecompleted/manage.php');
|
|
|
181 |
$html = ob_get_clean();
|
|
|
182 |
$this->assertStringContainsString('1 Users enrolled', $html);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Tests settings.
|
|
|
187 |
* @covers \enrol_coursecompleted_plugin
|
|
|
188 |
*/
|
|
|
189 |
public function test_enrol_courescompleted_settings(): void {
|
|
|
190 |
global $ADMIN, $CFG;
|
|
|
191 |
require_once($CFG->dirroot . '/lib/adminlib.php');
|
|
|
192 |
$ADMIN = admin_get_root(true, true);
|
|
|
193 |
$this->assertTrue($ADMIN->fulltree);
|
|
|
194 |
}
|
|
|
195 |
}
|