Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 plugin 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
use advanced_testcase;
29
use stdClass;
30
 
31
/**
32
 * coursecompleted enrolment plugin tests.
33
 *
34
 * @package   enrol_coursecompleted
35
 * @copyright 2017 eWallah (www.eWallah.net)
36
 * @author    Renaat Debleu <info@eWallah.net>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @coversDefaultClass \enrol_coursecompleted_plugin
39
 */
40
final class enrol_test extends advanced_testcase {
41
    /** @var stdClass Instance. */
42
    private $instance;
43
 
44
    /** @var stdClass Student. */
45
    private $student;
46
 
47
    /** @var stdClass First course. */
48
    private $course1;
49
 
50
    /** @var stdClass Second course. */
51
    private $course2;
52
 
53
    /** @var stdClass Third course. */
54
    private $course3;
55
 
56
    /** @var stdClass Last course. */
57
    private $course4;
58
 
59
    /** @var stdClass Plugin. */
60
    private $plugin;
61
 
62
    /**
63
     * Setup to ensure that forms and locallib are loaded.
64
     */
65
    public static function setUpBeforeClass(): void {
66
        global $CFG;
67
        require_once($CFG->libdir . '/formslib.php');
68
        require_once($CFG->dirroot . '/enrol/locallib.php');
69
    }
70
 
71
    /**
72
     * Tests initial setup.
73
     */
74
    protected function setUp(): void {
75
        global $CFG, $DB;
76
 
77
        $CFG->enablecompletion = true;
78
        $this->resetAfterTest(true);
79
        $enabled = enrol_get_plugins(true);
80
        unset($enabled['guest']);
81
        unset($enabled['self']);
82
        $enabled['coursecompleted'] = true;
83
        set_config('enrol_plugins_enabled', implode(',', array_keys($enabled)));
84
        $generator = $this->getDataGenerator();
85
        $this->course1 = $generator->create_course(['shortname' => 'A1', 'enablecompletion' => 1]);
86
        $this->course2 = $generator->create_course(['shortname' => 'A2', 'enablecompletion' => 1]);
87
        $this->course3 = $generator->create_course(['shortname' => 'A3', 'enablecompletion' => 1]);
88
        $this->course4 = $generator->create_course(['shortname' => 'A4', 'enablecompletion' => 1]);
89
        $studentrole = $DB->get_field('role', 'id', ['shortname' => 'student']);
90
        $this->setAdminUser();
91
        $this->plugin = enrol_get_plugin('coursecompleted');
92
        $id = $this->plugin->add_instance(
93
            $this->course2,
94
            ['customint1' => $this->course1->id, 'customint2' => 1, 'roleid' => $studentrole]
95
        );
96
        $this->instance = $DB->get_record('enrol', ['id' => $id]);
97
        $this->plugin->add_instance($this->course4, ['customint1' => $this->course3->id, 'roleid' => $studentrole]);
98
        $this->plugin->add_instance($this->course3, ['customint1' => $this->course2->id, 'roleid' => $studentrole]);
99
        $this->instance = $DB->get_record('enrol', ['id' => $id]);
100
        $this->student = $generator->create_and_enrol($this->course1, 'student');
101
    }
102
 
103
    /**
104
     * Test if user is enrolled after completing a course.
105
     * @covers \enrol_coursecompleted\observer
106
     */
107
    public function test_event_enrolled(): void {
108
        global $PAGE;
109
        $PAGE->set_url('/enrol/editinstance.php');
110
        $manager1 = new \course_enrolment_manager($PAGE, $this->course1);
111
        $this->assertCount(1, $manager1->get_user_enrolments($this->student->id));
112
        $this->assertfalse($this->plugin->has_bulk_operations($manager1));
113
        $this->assertCount(0, $this->plugin->get_bulk_operations($manager1));
114
        $manager2 = new \course_enrolment_manager($PAGE, $this->course2);
115
        $this->assertCount(0, $manager2->get_user_enrolments($this->student->id));
116
        $this->assertTrue($this->plugin->has_bulk_operations($manager2));
117
        $this->assertCount(2, $this->plugin->get_bulk_operations($manager2));
118
        $compevent = \core\event\course_completed::create(
119
            [
120
                'objectid' => $this->course2->id,
121
                'relateduserid' => $this->student->id,
122
                'context' => \context_course::instance($this->course1->id),
123
                'courseid' => $this->course1->id,
124
                'other' => ['relateduserid' => $this->student->id],
125
            ]
126
        );
127
        $observer = new observer();
128
        $observer->enroluser($compevent);
129
        $this->assertTrue(is_enrolled(\context_course::instance($this->course1->id), $this->student->id));
130
        $this->assertTrue(is_enrolled(\context_course::instance($this->course2->id), $this->student->id));
131
        $this->assertCount(1, $manager1->get_user_enrolments($this->student->id));
132
        $this->assertCount(1, $manager2->get_user_enrolments($this->student->id));
133
    }
134
 
135
    /**
136
     * Test if user is enrolled after completing a course.
137
     * @covers \enrol_coursecompleted_plugin
138
     */
139
    public function test_enrolled_after_completion(): void {
140
        global $PAGE;
141
        $manager = new \course_enrolment_manager($PAGE, $this->course2);
142
        $this->assertCount(0, $manager->get_user_enrolments($this->student->id));
143
        $ccompletion = new \completion_completion(['course' => $this->course1->id, 'userid' => $this->student->id]);
144
        $ccompletion->mark_complete(time());
145
        $this->assertEquals(
146
            '100',
147
            \core_completion\progress::get_course_progress_percentage($this->course1, $this->student->id)
148
        );
149
        $this->runAdhocTasks();
150
        $manager = new \course_enrolment_manager($PAGE, $this->course2);
151
        $this->assertCount(1, $manager->get_user_enrolments($this->student->id));
152
    }
153
 
154
    /**
155
     * Test ue.
156
     * @covers \enrol_coursecompleted_plugin
157
     */
158
    public function test_user_edit(): void {
159
        global $PAGE;
160
        $ccompletion = new \completion_completion(['course' => $this->course1->id, 'userid' => $this->student->id]);
161
        $ccompletion->mark_complete(time());
162
        $this->assertEquals(
163
            '100',
164
            \core_completion\progress::get_course_progress_percentage($this->course1, $this->student->id)
165
        );
166
        $this->runAdhocTasks();
167
        $this->setAdminUser();
168
        $context = \context_course::instance($this->course1->id);
169
        $this->assertTrue(has_capability('report/completion:view', $context));
170
        $url = new \moodle_url('/user/index.php', ['id' => $this->course2->id]);
171
        $PAGE->set_url($url);
172
        $manager = new \course_enrolment_manager($PAGE, $this->course2);
173
        $enrolments = $manager->get_user_enrolments($this->student->id);
174
        $this->assertCount(1, $enrolments);
175
        foreach ($enrolments as $enrolment) {
176
            if ($enrolment->enrolmentinstance->enrol == 'coursecompleted') {
177
                $actions = $this->plugin->get_user_enrolment_actions($manager, $enrolment);
178
                $this->assertCount(3, $actions);
179
                $this->assertEquals('Edit enrolment', $actions[0]->get_title());
180
                $this->assertEquals('Unenrol', $actions[1]->get_title());
181
                $this->assertEquals('Course completion', $actions[2]->get_title());
182
                $this->assertTrue($this->plugin->has_bulk_operations($manager));
183
                $operations = $this->plugin->get_bulk_operations($manager, null);
184
                $this->assertCount(2, $operations);
185
            }
186
        }
187
    }
188
 
189
    /**
190
     * Test builld course path.
191
     * @covers \enrol_coursecompleted_plugin
192
     */
193
    public function test_build_course_path(): void {
194
        global $DB;
195
        $records = $DB->get_records('enrol', ['enrol' => 'coursecompleted']);
196
        foreach ($records as $record) {
197
            $this->assertCount(4, $this->plugin->build_course_path($record));
198
        }
199
    }
200
 
201
    /**
202
     * Test library.
203
     * @covers \enrol_coursecompleted_plugin
204
     * @covers \enrol_coursecompleted\observer
205
     */
206
    public function test_library_functions(): void {
207
        global $DB;
208
        $studentrole = $DB->get_field('role', 'id', ['shortname' => 'student']);
209
        $this->assertEquals($this->plugin->get_name(), 'coursecompleted');
210
        $this->assertEquals($this->plugin->get_config('enabled'), null);
211
        $this->assertTrue($this->plugin->roles_protected());
212
        $this->assertTrue($this->plugin->can_add_instance($this->course2->id));
213
        $this->assertTrue($this->plugin->allow_unenrol($this->instance));
214
        $this->assertTrue($this->plugin->allow_manage($this->instance));
215
        $this->assertTrue($this->plugin->can_hide_show_instance($this->instance));
216
        $this->assertTrue($this->plugin->can_delete_instance($this->instance));
217
        $this->assertTrue($this->plugin->show_enrolme_link($this->instance));
218
        $this->assertCount(4, $this->plugin->build_course_path($this->instance));
219
        $this->assertCount(1, $this->plugin->get_info_icons([$this->instance]));
220
        $this->assertCount(2, $this->plugin->get_action_icons($this->instance));
221
        $this->assertEquals('After completing course: A1', $this->plugin->get_instance_name($this->instance));
222
        $this->assertEquals(
223
            'Enrolment by completion of course with id ' . $this->course1->id,
224
            $this->plugin->get_description_text($this->instance)
225
        );
226
        $this->assertStringContainsString('Test course 1', $this->plugin->enrol_page_hook($this->instance));
227
        $arr = ['status' => 0, 'enrolenddate' => time(), 'enrolstartdate' => time() + 10000];
228
        $tmp = $this->plugin->edit_instance_validation($arr, null, $this->instance, null);
229
        $this->assertEquals('The specified course does not exist', $tmp['customint']);
230
        $this->assertEquals('The enrolment end date cannot be earlier than the start date.', $tmp['enrolenddate']);
231
        $generator = $this->getDataGenerator();
232
        $course = $generator->create_course(['shortname' => 'c1', 'enablecompletion' => 1]);
233
        $tmp = $this->plugin->edit_instance_validation(['status' => 0, 'customint1' => $course->id], null, $this->instance, null);
234
        $this->assertEquals([], $tmp);
235
        $this->setUser(1);
236
        $this->assertStringContainsString('Test course 1', $this->plugin->enrol_page_hook($this->instance));
237
        $this->assertCount(1, $this->plugin->get_info_icons([$this->instance]));
238
        $this->setUser($this->student);
239
        $this->assertCount(1, $this->plugin->get_info_icons([$this->instance]));
240
        $page = new \moodle_page();
241
        $page->set_context(\context_course::instance($this->course1->id));
242
        $page->set_course($this->course1);
243
        $page->set_pagelayout('standard');
244
        $page->set_pagetype('course-view');
245
        $page->set_url('/enrol/index.php?id=' . $this->course1->id);
246
        $this->assertfalse($this->plugin->can_add_instance($this->course1->id));
247
        $this->assertfalse($this->plugin->allow_unenrol($this->instance));
248
        $this->assertfalse($this->plugin->allow_manage($this->instance));
249
        $this->assertfalse($this->plugin->can_hide_show_instance($this->instance));
250
        $this->assertfalse($this->plugin->can_delete_instance($this->instance));
251
        $this->assertStringContainsString('Test course 1', $this->plugin->enrol_page_hook($this->instance));
252
        $compevent = \core\event\course_completed::create(
253
            [
254
                'objectid' => $this->course2->id,
255
                'relateduserid' => $this->student->id,
256
                'context' => \context_course::instance($this->course2->id),
257
                'courseid' => $this->course2->id,
258
                'other' => ['relateduserid' => $this->student->id],
259
            ]
260
        );
261
        $observer = new observer();
262
        $observer->enroluser($compevent);
263
        $tmp = $this->plugin->enrol_page_hook($this->instance);
264
        $this->assertStringContainsString('Test course 1', $tmp);
265
        $this->assertStringContainsString('You will be enrolled in this course when you complete course', $tmp);
266
        $this->assertCount(1, $this->plugin->get_info_icons([$this->instance]));
267
        $student = $generator->create_user();
268
        $generator->enrol_user($student->id, $this->course2->id, $studentrole);
269
        mark_user_dirty($student->id);
270
        $this->setUser($student);
271
        $this->assertCount(1, $this->plugin->get_info_icons([$this->instance]));
272
        $tmp = $this->plugin->enrol_page_hook($this->instance);
273
        $this->assertStringContainsString('Test course 1', $tmp);
274
        $this->assertStringContainsString('You will be enrolled in this course when you complete course', $tmp);
275
    }
276
 
277
    /**
278
     * Test form.
279
     * @covers \enrol_coursecompleted_plugin
280
     */
281
    public function test_form(): void {
282
        $page = new \moodle_page();
283
        $context = \context_course::instance($this->course1->id);
284
        $page->set_context($context);
285
        $page->set_course($this->course1);
286
        $page->set_pagelayout('standard');
287
        $page->set_pagetype('course-view');
288
        $page->set_url('/enrol/coursecompleted/manage.php?enrolid=' . $this->instance->id);
289
        $form = $this->tempform();
290
        $mform = $form->getform();
291
        $this->plugin->edit_instance_form($this->instance, $mform, $context);
292
        $this->assertStringContainsString('Required field', $mform->getReqHTML());
293
        ob_start();
294
        $mform->display();
295
        $html = ob_get_clean();
296
        $strm = get_string_manager();
297
        $arr = ['compcourse', 'customwelcome', 'enrolenddate', 'enrolstartdate', 'group'];
298
        foreach ($arr as $value) {
299
            if ($strm->string_exists($value, 'enrol_coursecompleted')) {
300
                $this->assertStringContainsString(get_string($value, 'enrol_coursecompleted'), $html);
301
            }
302
            if ($strm->string_exists($value . '_desc', 'enrol_coursecompleted')) {
303
                $this->assertStringContainsString(get_string($value . '_desc', 'enrol_coursecompleted'), $html);
304
            }
305
        }
306
    }
307
 
308
    /**
309
     * Test deleted course.
310
     * @covers \enrol_coursecompleted\observer
311
     * @covers \enrol_coursecompleted_plugin
312
     */
313
    public function test_deleted_course(): void {
314
        global $DB;
315
        $start = $DB->count_records('course');
316
        $sink = $this->redirectEvents();
317
        ob_start();
318
        delete_course($this->course1->id, false);
319
        ob_end_clean();
320
        $events = $sink->get_events();
321
        $sink->close();
322
        $this->assertEquals('Deleted course ' . $this->course1->id, $this->plugin->get_instance_name($this->instance));
323
        $this->assertEquals(
324
            'Enrolment by completion of course with id ' . $this->course1->id,
325
            $this->plugin->get_description_text($this->instance)
326
        );
327
        $event = array_pop($events);
328
        $this->assertInstanceOf('\core\event\course_deleted', $event);
329
        $observer = new \enrol_coursecompleted\observer();
330
        $observer->coursedeleted($event);
331
        $this->assertEquals($start - 1, $DB->count_records('course'));
332
    }
333
 
334
    /**
335
     * Test form.
336
     * @covers \enrol_coursecompleted_plugin
337
     * @return \moodleform
338
     */
339
    private function tempform() {
340
        /**
341
         * coursecompleted enrolment form tests.
342
         *
343
         * @package   enrol_coursecompleted
344
         * @copyright 2017 eWallah (www.eWallah.net)
345
         * @author    Renaat Debleu <info@eWallah.net>
346
         * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
347
         */
348
        return new class extends \moodleform {
349
            /**
350
             * Form definition.
351
             */
352
            public function definition() {
353
                // No definition required.
354
            }
355
            /**
356
             * Returns form reference
357
             * @return MoodleQuickForm
358
             */
359
            public function getform() {
360
                $mform = $this->_form;
361
                // Simulate submission.
362
                $mform->_flagSubmitted = true;
363
                return $mform;
364
            }
365
        };
366
    }
367
}