MDL-53700 competency: Migrating data generator to core
[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
67bc0eaf 28use core_competency\course_competency;
767f66c0 29use core_competency\api;
67bc0eaf 30use core_competency\course_competency_settings;
644ff55b
DW
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();
922634d3 48 $lpg = $dg->get_plugin_generator('core_competency');
644ff55b 49 $role = create_role('Settings changer role', 'settingschanger', 'Someone who can change course competency settings');
b90e2205
FM
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);
644ff55b 54 $gradedrole = create_role('Graded role', 'graded', 'Someone who can be graded');
b90e2205 55 assign_capability('moodle/competency:coursecompetencygradable', CAP_ALLOW, $gradedrole, $syscontext->id);
644ff55b
DW
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
9919b4af 79 $coursesettings = course_competency_settings::get_by_courseid($c1->id);
644ff55b
DW
80 $this->assertTrue((boolean)$coursesettings->get_pushratingstouserplans());
81
82 set_config('pushcourseratingstouserplans', false, 'tool_lp');
83
9919b4af 84 $coursesettings = course_competency_settings::get_by_courseid($c1->id);
644ff55b
DW
85 $this->assertFalse((boolean)$coursesettings->get_pushratingstouserplans());
86
9785dd81 87 api::update_course_competency_settings($c1->id, (object) array('pushratingstouserplans' => true));
9919b4af 88 $coursesettings = course_competency_settings::get_by_courseid($c1->id);
644ff55b
DW
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));
9919b4af 93 $coursesettings = course_competency_settings::get_by_courseid($c1->id);
644ff55b
DW
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 );
67bc0eaf
FM
102 $usercompcourse = \core_competency\user_competency_course::get_record($filterparams);
103 $usercomp = \core_competency\user_competency::get_record($filterparams);
644ff55b
DW
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 );
67bc0eaf
FM
115 $usercompcourse = \core_competency\user_competency_course::get_record($filterparams);
116 $usercomp = \core_competency\user_competency::get_record($filterparams);
644ff55b
DW
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}