MDL-53452 tool_lp: Change update_course_competency_settings param to stdClass
[moodle.git] / admin / tool / lp / tests / course_competency_settings_test.php
CommitLineData
644ff55b
DW
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 *
20 * @package tool_lp
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
25defined('MOODLE_INTERNAL') || die();
26global $CFG;
27
28use tool_lp\course_competency;
29use tool_lp\api;
30use tool_lp\course_competency_settings;
31
32/**
33 * This test ensures that the course competency settings are applied and work correctly.
34 *
35 * @package tool_lp
36 * @copyright 2016 Frédéric Massart - FMCorz.net
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class tool_lp_course_competency_settings_testcase extends advanced_testcase {
40
41 public function test_who_can_change_settings() {
42 global $CFG, $DB;
43
44 $this->resetAfterTest(true);
45
46 $syscontext = context_system::instance();
47 $dg = $this->getDataGenerator();
48 $lpg = $dg->get_plugin_generator('tool_lp');
49 $role = create_role('Settings changer role', 'settingschanger', 'Someone who can change course competency settings');
50 assign_capability('tool/lp:coursecompetencyconfigure', CAP_ALLOW, $role, $syscontext->id);
51 assign_capability('tool/lp:competencygrade', CAP_ALLOW, $role, $syscontext->id);
52 assign_capability('tool/lp:coursecompetencyview', CAP_ALLOW, $role, $syscontext->id);
53 assign_capability('tool/lp:planview', CAP_ALLOW, $role, $syscontext->id);
54 $gradedrole = create_role('Graded role', 'graded', 'Someone who can be graded');
55 assign_capability('tool/lp:coursecompetencygradable', CAP_ALLOW, $gradedrole, $syscontext->id);
56
57 $c1 = $dg->create_course();
58 $u1 = $dg->create_user();
59 $u2 = $dg->create_user();
60 $u3 = $dg->create_user();
61
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));
67
68 // Enrol the user
69 $dg->enrol_user($u1->id, $c1->id);
70 role_assign($gradedrole, $u1->id, $syscontext->id);
71
72 // Assign roles.
73 role_assign($role, $u2->id, $syscontext->id);
74
75 $this->setUser($u2);
76
77 set_config('pushcourseratingstouserplans', true, 'tool_lp');
78
79 $coursesettings = course_competency_settings::get_course_settings($c1->id);
80 $this->assertTrue((boolean)$coursesettings->get_pushratingstouserplans());
81
82 set_config('pushcourseratingstouserplans', false, 'tool_lp');
83
84 $coursesettings = course_competency_settings::get_course_settings($c1->id);
85 $this->assertFalse((boolean)$coursesettings->get_pushratingstouserplans());
86
9785dd81 87 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
644ff55b
DW
88 $coursesettings = course_competency_settings::get_course_settings($c1->id);
89 $this->assertTrue((boolean)$coursesettings->get_pushratingstouserplans());
90
91 set_config('pushcourseratingstouserplans', true, 'tool_lp');
9785dd81 92 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false));
644ff55b
DW
93 $coursesettings = course_competency_settings::get_course_settings($c1->id);
94 $this->assertFalse((boolean)$coursesettings->get_pushratingstouserplans());
95
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 = \tool_lp\user_competency_course::get_record($filterparams);
103 $usercomp = \tool_lp\user_competency::get_record($filterparams);
104
105 // No grade in plan - only a grade in the course.
106 $this->assertEmpty($usercomp->get_grade());
107 $this->assertEquals(1, $usercompcourse->get_grade());
108
9785dd81 109 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
644ff55b
DW
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 = \tool_lp\user_competency_course::get_record($filterparams);
116 $usercomp = \tool_lp\user_competency::get_record($filterparams);
117
118 // Updated grade in plan - updated grade in the course.
119 $this->assertEquals(2, $usercomp->get_grade());
120 $this->assertEquals(2, $usercompcourse->get_grade());
121
122 $this->setUser($u3);
123 $this->setExpectedException('required_capability_exception');
9785dd81 124 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => false));
644ff55b
DW
125 }
126
127}