NOBUG: Fixed SVG browser compatibility
[moodle.git] / competency / tests / course_competency_settings_test.php
blobb825af743257364653d2128b7f30ea3b36171adc
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.
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;
19 /**
20 * This test ensures that the course competency settings are applied and work correctly.
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_settings_test extends \advanced_testcase {
28 public function test_who_can_change_settings() {
29 global $CFG, $DB;
31 $this->resetAfterTest(true);
33 $syscontext = \context_system::instance();
34 $dg = $this->getDataGenerator();
35 $lpg = $dg->get_plugin_generator('core_competency');
36 $role = create_role('Settings changer role', 'settingschanger', 'Someone who can change course competency settings');
37 assign_capability('moodle/competency:coursecompetencyconfigure', CAP_ALLOW, $role, $syscontext->id);
38 assign_capability('moodle/competency:competencygrade', CAP_ALLOW, $role, $syscontext->id);
39 assign_capability('moodle/competency:coursecompetencyview', CAP_ALLOW, $role, $syscontext->id);
40 assign_capability('moodle/competency:planview', CAP_ALLOW, $role, $syscontext->id);
41 $gradedrole = create_role('Graded role', 'graded', 'Someone who can be graded');
42 assign_capability('moodle/competency:coursecompetencygradable', CAP_ALLOW, $gradedrole, $syscontext->id);
44 $c1 = $dg->create_course();
45 $u1 = $dg->create_user();
46 $u2 = $dg->create_user();
47 $u3 = $dg->create_user();
49 $framework = $lpg->create_framework();
50 $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
51 $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get('id')));
52 $lpg->create_course_competency(array('competencyid' => $comp1->get('id'), 'courseid' => $c1->id));
53 $lpg->create_course_competency(array('competencyid' => $comp2->get('id'), 'courseid' => $c1->id));
55 // Enrol the user.
56 $dg->enrol_user($u1->id, $c1->id);
57 role_assign($gradedrole, $u1->id, $syscontext->id);
59 // Assign roles.
60 role_assign($role, $u2->id, $syscontext->id);
62 $this->setUser($u2);
64 set_config('pushcourseratingstouserplans', true, 'core_competency');
66 $coursesettings = course_competency_settings::get_by_courseid($c1->id);
67 $this->assertTrue((boolean)$coursesettings->get('pushratingstouserplans'));
69 set_config('pushcourseratingstouserplans', false, 'core_competency');
71 $coursesettings = course_competency_settings::get_by_courseid($c1->id);
72 $this->assertFalse((boolean)$coursesettings->get('pushratingstouserplans'));
74 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
75 $coursesettings = course_competency_settings::get_by_courseid($c1->id);
76 $this->assertTrue((boolean)$coursesettings->get('pushratingstouserplans'));
78 set_config('pushcourseratingstouserplans', true, 'core_competency');
79 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false));
80 $coursesettings = course_competency_settings::get_by_courseid($c1->id);
81 $this->assertFalse((boolean)$coursesettings->get('pushratingstouserplans'));
83 // Right now the setting is false.
84 api::grade_competency_in_course($c1->id, $u1->id, $comp1->get('id'), 1, 'Note');
85 $filterparams = array(
86 'userid' => $u1->id,
87 'competencyid' => $comp1->get('id'),
89 $usercompcourse = \core_competency\user_competency_course::get_record($filterparams);
90 $usercomp = \core_competency\user_competency::get_record($filterparams);
92 // No grade in plan - only a grade in the course.
93 $this->assertEmpty($usercomp->get('grade'));
94 $this->assertEquals(1, $usercompcourse->get('grade'));
96 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
97 api::grade_competency_in_course($c1->id, $u1->id, $comp1->get('id'), 2, 'Note 2');
98 $filterparams = array(
99 'userid' => $u1->id,
100 'competencyid' => $comp1->get('id'),
102 $usercompcourse = \core_competency\user_competency_course::get_record($filterparams);
103 $usercomp = \core_competency\user_competency::get_record($filterparams);
105 // Updated grade in plan - updated grade in the course.
106 $this->assertEquals(2, $usercomp->get('grade'));
107 $this->assertEquals(2, $usercompcourse->get('grade'));
109 $this->setUser($u3);
110 $this->expectException('required_capability_exception');
111 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false));