2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace core_competency
;
20 * Course competency persistent testcase.
22 * @package core_competency
23 * @copyright 2016 Frédéric Massart - FMCorz.net
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 class course_competency_test
extends \advanced_testcase
{
28 public function test_get_courses_with_competency_and_user() {
31 $this->resetAfterTest(true);
32 $dg = $this->getDataGenerator();
33 $lpg = $dg->get_plugin_generator('core_competency');
35 $c1 = $dg->create_course();
36 $c2 = $dg->create_course();
37 $c3 = $dg->create_course();
38 $c4 = $dg->create_course();
39 $u1 = $dg->create_user();
40 $u2 = $dg->create_user();
41 $u3 = $dg->create_user();
42 $u4 = $dg->create_user();
44 $flatfileplugin = enrol_get_plugin('flatfile');
45 $flatfileinstanceid = $flatfileplugin->add_instance($c2);
47 $framework = $lpg->create_framework();
48 $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); // In C1, and C2.
49 $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); // In C2.
50 $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); // In None.
51 $comp4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id'))); // In C4.
52 $lpg->create_course_competency(array('competencyid' => $comp1->get('id'), 'courseid' => $c1->id
));
53 $lpg->create_course_competency(array('competencyid' => $comp1->get('id'), 'courseid' => $c2->id
));
54 $lpg->create_course_competency(array('competencyid' => $comp2->get('id'), 'courseid' => $c2->id
));
55 $lpg->create_course_competency(array('competencyid' => $comp4->get('id'), 'courseid' => $c4->id
));
57 // Enrol the user 1 in C1, C2, and C3.
58 $dg->enrol_user($u1->id
, $c1->id
);
59 $dg->enrol_user($u1->id
, $c2->id
);
60 $dg->enrol_user($u1->id
, $c3->id
);
62 // Enrol the user 2 in C4.
63 $dg->enrol_user($u2->id
, $c4->id
);
65 // Enrol the user 3 in C1 and C2, but non active in C2.
66 $dg->enrol_user($u3->id
, $c1->id
);
67 $dg->enrol_user($u3->id
, $c2->id
, null, 'manual', 0, 0, ENROL_USER_SUSPENDED
);
69 // Enrol the user 4 with a plugin which will be enabled/disabled.
70 $dg->enrol_user($u4->id
, $c2->id
, null, 'flatfile');
72 // Using the competency that is not used anywhere -> no courses.
73 $this->assertCount(0, course_competency
::get_courses_with_competency_and_user($comp3->get('id'), $u1->id
));
75 // Using the competency that is used in a course where the user is not enrolled -> no courses.
76 $this->assertCount(0, course_competency
::get_courses_with_competency_and_user($comp4->get('id'), $u1->id
));
78 // Using the competency that is used in a course where the user is enrolled -> one course.
79 $courses = course_competency
::get_courses_with_competency_and_user($comp2->get('id'), $u1->id
);
80 $this->assertCount(1, $courses);
81 $this->assertArrayHasKey($c2->id
, $courses);
83 // Using the competency used multiple times.
84 $courses = course_competency
::get_courses_with_competency_and_user($comp1->get('id'), $u1->id
);
85 $this->assertCount(2, $courses);
86 $this->assertArrayHasKey($c1->id
, $courses);
87 $this->assertArrayHasKey($c2->id
, $courses);
89 // Checking for another user where the competency is used twice, but not for them.
90 $courses = course_competency
::get_courses_with_competency_and_user($comp1->get('id'), $u2->id
);
91 $this->assertCount(0, $courses);
93 // Checking for another user where the competency is used in their course.
94 $courses = course_competency
::get_courses_with_competency_and_user($comp4->get('id'), $u2->id
);
95 $this->assertCount(1, $courses);
96 $this->assertArrayHasKey($c4->id
, $courses);
98 // Checking for a user who is suspended in a course.
99 $courses = course_competency
::get_courses_with_competency_and_user($comp1->get('id'), $u3->id
);
100 $this->assertCount(1, $courses);
101 $this->assertArrayHasKey($c1->id
, $courses);
103 // Check for the user with plugin enabled.
104 $enrolplugins = explode(',', $CFG->enrol_plugins_enabled
);
105 $enrolplugins[] = 'flatfile';
106 $CFG->enrol_plugins_enabled
= implode(',', array_unique($enrolplugins));
107 $courses = course_competency
::get_courses_with_competency_and_user($comp1->get('id'), $u4->id
);
108 $this->assertCount(1, $courses);
109 $this->assertArrayHasKey($c2->id
, $courses);
111 // Check for the user with plugin enabled, but enrolment instance disabled.
112 $flatfileinstance = $DB->get_record('enrol', array('id' => $flatfileinstanceid));
113 $flatfileplugin->update_status($flatfileinstance, ENROL_INSTANCE_DISABLED
);
114 $courses = course_competency
::get_courses_with_competency_and_user($comp1->get('id'), $u4->id
);
115 $this->assertCount(0, $courses);
116 $flatfileplugin->update_status($flatfileinstance, ENROL_INSTANCE_ENABLED
);
118 // Check for the user with plugin disabled.
119 $enrolplugins = array_flip(explode(',', $CFG->enrol_plugins_enabled
));
120 unset($enrolplugins['flatfile']);
121 $CFG->enrol_plugins_enabled
= implode(',', array_keys($enrolplugins));
122 $courses = course_competency
::get_courses_with_competency_and_user($comp1->get('id'), $u4->id
);
123 $this->assertCount(0, $courses);