Commit | Line | Data |
---|---|---|
943989c2 FM |
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 | * Course competency persistent class tests. | |
19 | * | |
e0b9ba28 | 20 | * @package core_competency |
943989c2 FM |
21 | * @copyright 2016 Frédéric Massart - FMCorz.net |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | defined('MOODLE_INTERNAL') || die(); | |
26 | global $CFG; | |
27 | ||
67bc0eaf | 28 | use core_competency\course_competency; |
943989c2 FM |
29 | |
30 | /** | |
31 | * Course competency persistent testcase. | |
32 | * | |
e0b9ba28 | 33 | * @package core_competency |
943989c2 FM |
34 | * @copyright 2016 Frédéric Massart - FMCorz.net |
35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
36 | */ | |
e0b9ba28 | 37 | class core_competency_course_competency_testcase extends advanced_testcase { |
943989c2 FM |
38 | |
39 | public function test_get_courses_with_competency_and_user() { | |
40 | global $CFG, $DB; | |
41 | ||
42 | $this->resetAfterTest(true); | |
43 | $dg = $this->getDataGenerator(); | |
922634d3 | 44 | $lpg = $dg->get_plugin_generator('core_competency'); |
943989c2 FM |
45 | |
46 | $c1 = $dg->create_course(); | |
47 | $c2 = $dg->create_course(); | |
48 | $c3 = $dg->create_course(); | |
49 | $c4 = $dg->create_course(); | |
50 | $u1 = $dg->create_user(); | |
51 | $u2 = $dg->create_user(); | |
52 | $u3 = $dg->create_user(); | |
53 | $u4 = $dg->create_user(); | |
54 | ||
55 | $flatfileplugin = enrol_get_plugin('flatfile'); | |
56 | $flatfileinstanceid = $flatfileplugin->add_instance($c2); | |
57 | ||
58 | $framework = $lpg->create_framework(); | |
59 | $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); // In C1, and C2. | |
60 | $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); // In C2. | |
61 | $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); // In None. | |
62 | $comp4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); // In C4. | |
63 | $lpg->create_course_competency(array('competencyid' => $comp1->get_id(), 'courseid' => $c1->id)); | |
64 | $lpg->create_course_competency(array('competencyid' => $comp1->get_id(), 'courseid' => $c2->id)); | |
65 | $lpg->create_course_competency(array('competencyid' => $comp2->get_id(), 'courseid' => $c2->id)); | |
66 | $lpg->create_course_competency(array('competencyid' => $comp4->get_id(), 'courseid' => $c4->id)); | |
67 | ||
68 | // Enrol the user 1 in C1, C2, and C3. | |
69 | $dg->enrol_user($u1->id, $c1->id); | |
70 | $dg->enrol_user($u1->id, $c2->id); | |
71 | $dg->enrol_user($u1->id, $c3->id); | |
72 | ||
73 | // Enrol the user 2 in C4. | |
74 | $dg->enrol_user($u2->id, $c4->id); | |
75 | ||
76 | // Enrol the user 3 in C1 and C2, but non active in C2. | |
77 | $dg->enrol_user($u3->id, $c1->id); | |
78 | $dg->enrol_user($u3->id, $c2->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED); | |
79 | ||
80 | // Enrol the user 4 with a plugin which will be enabled/disabled. | |
81 | $dg->enrol_user($u4->id, $c2->id, null, 'flatfile'); | |
82 | ||
83 | // Using the competency that is not used anywhere -> no courses. | |
84 | $this->assertCount(0, course_competency::get_courses_with_competency_and_user($comp3->get_id(), $u1->id)); | |
85 | ||
86 | // Using the competency that is used in a course where the user is not enrolled -> no courses. | |
87 | $this->assertCount(0, course_competency::get_courses_with_competency_and_user($comp4->get_id(), $u1->id)); | |
88 | ||
89 | // Using the competency that is used in a course where the user is enrolled -> one course. | |
90 | $courses = course_competency::get_courses_with_competency_and_user($comp2->get_id(), $u1->id); | |
91 | $this->assertCount(1, $courses); | |
92 | $this->assertArrayHasKey($c2->id, $courses); | |
93 | ||
94 | // Using the competency used multiple times. | |
95 | $courses = course_competency::get_courses_with_competency_and_user($comp1->get_id(), $u1->id); | |
96 | $this->assertCount(2, $courses); | |
97 | $this->assertArrayHasKey($c1->id, $courses); | |
98 | $this->assertArrayHasKey($c2->id, $courses); | |
99 | ||
100 | // Checking for another user where the competency is used twice, but not for them. | |
101 | $courses = course_competency::get_courses_with_competency_and_user($comp1->get_id(), $u2->id); | |
102 | $this->assertCount(0, $courses); | |
103 | ||
104 | // Checking for another user where the competency is used in their course. | |
105 | $courses = course_competency::get_courses_with_competency_and_user($comp4->get_id(), $u2->id); | |
106 | $this->assertCount(1, $courses); | |
107 | $this->assertArrayHasKey($c4->id, $courses); | |
108 | ||
109 | // Checking for a user who is suspended in a course. | |
110 | $courses = course_competency::get_courses_with_competency_and_user($comp1->get_id(), $u3->id); | |
111 | $this->assertCount(1, $courses); | |
112 | $this->assertArrayHasKey($c1->id, $courses); | |
113 | ||
114 | // Check for the user with plugin enabled. | |
115 | $enrolplugins = explode(',', $CFG->enrol_plugins_enabled); | |
116 | $enrolplugins[] = 'flatfile'; | |
117 | $CFG->enrol_plugins_enabled = implode(',', array_unique($enrolplugins)); | |
118 | $courses = course_competency::get_courses_with_competency_and_user($comp1->get_id(), $u4->id); | |
119 | $this->assertCount(1, $courses); | |
120 | $this->assertArrayHasKey($c2->id, $courses); | |
121 | ||
122 | // Check for the user with plugin enabled, but enrolment instance disabled. | |
123 | $flatfileinstance = $DB->get_record('enrol', array('id' => $flatfileinstanceid)); | |
124 | $flatfileplugin->update_status($flatfileinstance, ENROL_INSTANCE_DISABLED); | |
125 | $courses = course_competency::get_courses_with_competency_and_user($comp1->get_id(), $u4->id); | |
126 | $this->assertCount(0, $courses); | |
127 | $flatfileplugin->update_status($flatfileinstance, ENROL_INSTANCE_ENABLED); | |
128 | ||
129 | // Check for the user with plugin disabled. | |
130 | $enrolplugins = array_flip(explode(',', $CFG->enrol_plugins_enabled)); | |
131 | unset($enrolplugins['flatfile']); | |
132 | $CFG->enrol_plugins_enabled = implode(',', array_keys($enrolplugins)); | |
133 | $courses = course_competency::get_courses_with_competency_and_user($comp1->get_id(), $u4->id); | |
134 | $this->assertCount(0, $courses); | |
135 | } | |
136 | ||
137 | } |