MDL-53700 competency: Migrating data generator to core
[moodle.git] / competency / tests / course_competency_settings_test.php
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/>.
17 /**
18  * Course competency persistent class tests.
19  *
20  * @package    core_competency
21  * @copyright  2016 Frédéric Massart - FMCorz.net
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
28 use core_competency\course_competency;
29 use core_competency\api;
30 use core_competency\course_competency_settings;
32 /**
33  * This test ensures that the course competency settings are applied and work correctly.
34  *
35  * @package    core_competency
36  * @copyright  2016 Frédéric Massart - FMCorz.net
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class core_competency_course_competency_settings_testcase extends advanced_testcase {
41     public function test_who_can_change_settings() {
42         global $CFG, $DB;
44         $this->resetAfterTest(true);
46         $syscontext = context_system::instance();
47         $dg = $this->getDataGenerator();
48         $lpg = $dg->get_plugin_generator('core_competency');
49         $role = create_role('Settings changer role', 'settingschanger', 'Someone who can change course competency settings');
50         assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $role, $syscontext->id);
51         assign_capability('moodle/competency:competencygrade', CAP_ALLOW, $role, $syscontext->id);
52         assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW, $role, $syscontext->id);
53         assign_capability('moodle/competency:planview', CAP_ALLOW, $role, $syscontext->id);
54         $gradedrole = create_role('Graded role', 'graded', 'Someone who can be graded');
55         assign_capability('moodle/competency:coursecompetencygradable', CAP_ALLOW, $gradedrole, $syscontext->id);
57         $c1 = $dg->create_course();
58         $u1 = $dg->create_user();
59         $u2 = $dg->create_user();
60         $u3 = $dg->create_user();
62         $framework = $lpg->create_framework();
63         $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
64         $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
65         $lpg->create_course_competency(array('competencyid' => $comp1->get_id(), 'courseid' => $c1->id));
66         $lpg->create_course_competency(array('competencyid' => $comp2->get_id(), 'courseid' => $c1->id));
68         // Enrol the user
69         $dg->enrol_user($u1->id, $c1->id);
70         role_assign($gradedrole, $u1->id, $syscontext->id);
72         // Assign roles.
73         role_assign($role, $u2->id, $syscontext->id);
75         $this->setUser($u2);
77         set_config('pushcourseratingstouserplans', true, 'tool_lp');
79         $coursesettings = course_competency_settings::get_by_courseid($c1->id);
80         $this->assertTrue((boolean)$coursesettings->get_pushratingstouserplans());
82         set_config('pushcourseratingstouserplans', false, 'tool_lp');
84         $coursesettings = course_competency_settings::get_by_courseid($c1->id);
85         $this->assertFalse((boolean)$coursesettings->get_pushratingstouserplans());
87         api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
88         $coursesettings = course_competency_settings::get_by_courseid($c1->id);
89         $this->assertTrue((boolean)$coursesettings->get_pushratingstouserplans());
91         set_config('pushcourseratingstouserplans', true, 'tool_lp');
92         api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false));
93         $coursesettings = course_competency_settings::get_by_courseid($c1->id);
94         $this->assertFalse((boolean)$coursesettings->get_pushratingstouserplans());
96         // Right now the setting is false.
97         api::grade_competency_in_course($c1->id, $u1->id, $comp1->get_id(), 1, 'Note');
98         $filterparams = array(
99             'userid' => $u1->id,
100             'competencyid' => $comp1->get_id(),
101         );
102         $usercompcourse = \core_competency\user_competency_course::get_record($filterparams);
103         $usercomp = \core_competency\user_competency::get_record($filterparams);
105         // No grade in plan - only a grade in the course.
106         $this->assertEmpty($usercomp->get_grade());
107         $this->assertEquals(1, $usercompcourse->get_grade());
109         api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
110         api::grade_competency_in_course($c1->id, $u1->id, $comp1->get_id(), 2, 'Note 2');
111         $filterparams = array(
112             'userid' => $u1->id,
113             'competencyid' => $comp1->get_id(),
114         );
115         $usercompcourse = \core_competency\user_competency_course::get_record($filterparams);
116         $usercomp = \core_competency\user_competency::get_record($filterparams);
118         // Updated grade in plan - updated grade in the course.
119         $this->assertEquals(2, $usercomp->get_grade());
120         $this->assertEquals(2, $usercompcourse->get_grade());
122         $this->setUser($u3);
123         $this->setExpectedException('required_capability_exception');
124         api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false));
125     }