MDL-53452 competencies: Add unit test for course competency settings

This commit is contained in:
Damyon Wiese 2016-03-21 13:25:03 +08:00 committed by Frederic Massart
parent fdd85edef9
commit 644ff55b3a
5 changed files with 177 additions and 17 deletions

View File

@ -4724,9 +4724,9 @@ class api {
$USER->id,
$note);
if ($result) {
$all = user_competency::get_multiple($userid, array($competency->get_id()));
$all = user_competency_course::get_multiple($userid, $course->id, array($competency->get_id()));
$uc = reset($all);
$event = \tool_lp\event\user_competency_grade_rated_in_course::create_from_user_competency($uc, $course->id);
$event = \tool_lp\event\user_competency_grade_rated_in_course::create_from_user_competency_course($uc);
$event->trigger();
}
return $result;

View File

@ -25,7 +25,7 @@
namespace tool_lp\event;
use core\event\base;
use tool_lp\user_competency;
use tool_lp\user_competency_course;
use context_course;
defined('MOODLE_INTERNAL') || die();
@ -49,29 +49,28 @@ class user_competency_grade_rated_in_course extends base {
/**
* Convenience method to instantiate the event.
*
* @param user_competency $usercompetency The user competency.
* @param int $courseid the course id.
* @param user_competency_course $usercompetencycourse The user competency course.
* @return self
*/
public static function create_from_user_competency(user_competency $usercompetency, $courseid) {
if (!$usercompetency->get_id()) {
throw new \coding_exception('The user competency ID must be set.');
public static function create_from_user_competency_course(user_competency_course $usercompetencycourse) {
if (!$usercompetencycourse->get_id()) {
throw new \coding_exception('The user competency course ID must be set.');
}
$params = array(
'objectid' => $usercompetency->get_id(),
'relateduserid' => $usercompetency->get_userid(),
'objectid' => $usercompetencycourse->get_id(),
'relateduserid' => $usercompetencycourse->get_userid(),
'other' => array(
'competencyid' => $usercompetency->get_competencyid(),
'grade' => $usercompetency->get_grade()
'competencyid' => $usercompetencycourse->get_competencyid(),
'grade' => $usercompetencycourse->get_grade()
)
);
$coursecontext = context_course::instance($courseid);
$coursecontext = context_course::instance($usercompetencycourse->get_courseid());
$params['contextid'] = $coursecontext->id;
$params['courseid'] = $courseid;
$params['courseid'] = $usercompetencycourse->get_courseid();
$event = static::create($params);
$event->add_record_snapshot(user_competency::TABLE, $usercompetency->to_record());
$event->add_record_snapshot(user_competency_course::TABLE, $usercompetencycourse->to_record());
return $event;
}
@ -116,7 +115,7 @@ class user_competency_grade_rated_in_course extends base {
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_TEACHING;
$this->data['objecttable'] = user_competency::TABLE;
$this->data['objecttable'] = user_competency_course::TABLE;
}
/**

View File

@ -194,4 +194,39 @@ class user_competency_course extends persistent {
return true;
}
/**
* Get multiple user_competency for a user.
*
* @param int $userid
* @param int $courseid
* @param array $competenciesorids Limit search to those competencies, or competency IDs.
* @return \tool_lp\user_competency_course[]
*/
public static function get_multiple($userid, $courseid, array $competenciesorids = null) {
global $DB;
$params = array();
$params['userid'] = $userid;
$params['courseid'] = $courseid;
$sql = '1 = 1';
if (!empty($competenciesorids)) {
$test = reset($competenciesorids);
if (is_number($test)) {
$ids = $competenciesorids;
} else {
$ids = array();
foreach ($competenciesorids as $comp) {
$ids[] = $comp->get_id();
}
}
list($insql, $inparams) = $DB->get_in_or_equal($ids, SQL_PARAMS_NAMED);
$params += $inparams;
$sql = "competencyid $insql";
}
return self::get_records_select("userid = :userid AND courseid = :courseid AND $sql", $params);
}
}

View File

@ -0,0 +1,127 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Course competency persistent class tests.
*
* @package tool_lp
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
use tool_lp\course_competency;
use tool_lp\api;
use tool_lp\course_competency_settings;
/**
* This test ensures that the course competency settings are applied and work correctly.
*
* @package tool_lp
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_lp_course_competency_settings_testcase extends advanced_testcase {
public function test_who_can_change_settings() {
global $CFG, $DB;
$this->resetAfterTest(true);
$syscontext = context_system::instance();
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('tool_lp');
$role = create_role('Settings changer role', 'settingschanger', 'Someone who can change course competency settings');
assign_capability('tool/lp:coursecompetencyconfigure', CAP_ALLOW, $role, $syscontext->id);
assign_capability('tool/lp:competencygrade', CAP_ALLOW, $role, $syscontext->id);
assign_capability('tool/lp:coursecompetencyview', CAP_ALLOW, $role, $syscontext->id);
assign_capability('tool/lp:planview', CAP_ALLOW, $role, $syscontext->id);
$gradedrole = create_role('Graded role', 'graded', 'Someone who can be graded');
assign_capability('tool/lp:coursecompetencygradable', CAP_ALLOW, $gradedrole, $syscontext->id);
$c1 = $dg->create_course();
$u1 = $dg->create_user();
$u2 = $dg->create_user();
$u3 = $dg->create_user();
$framework = $lpg->create_framework();
$comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
$comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
$lpg->create_course_competency(array('competencyid' => $comp1->get_id(), 'courseid' => $c1->id));
$lpg->create_course_competency(array('competencyid' => $comp2->get_id(), 'courseid' => $c1->id));
// Enrol the user
$dg->enrol_user($u1->id, $c1->id);
role_assign($gradedrole, $u1->id, $syscontext->id);
// Assign roles.
role_assign($role, $u2->id, $syscontext->id);
$this->setUser($u2);
set_config('pushcourseratingstouserplans', true, 'tool_lp');
$coursesettings = course_competency_settings::get_course_settings($c1->id);
$this->assertTrue((boolean)$coursesettings->get_pushratingstouserplans());
set_config('pushcourseratingstouserplans', false, 'tool_lp');
$coursesettings = course_competency_settings::get_course_settings($c1->id);
$this->assertFalse((boolean)$coursesettings->get_pushratingstouserplans());
api::update_course_competency_settings($c1->id, true);
$coursesettings = course_competency_settings::get_course_settings($c1->id);
$this->assertTrue((boolean)$coursesettings->get_pushratingstouserplans());
set_config('pushcourseratingstouserplans', true, 'tool_lp');
api::update_course_competency_settings($c1->id, false);
$coursesettings = course_competency_settings::get_course_settings($c1->id);
$this->assertFalse((boolean)$coursesettings->get_pushratingstouserplans());
// Right now the setting is false.
api::grade_competency_in_course($c1->id, $u1->id, $comp1->get_id(), 1, 'Note');
$filterparams = array(
'userid' => $u1->id,
'competencyid' => $comp1->get_id(),
);
$usercompcourse = \tool_lp\user_competency_course::get_record($filterparams);
$usercomp = \tool_lp\user_competency::get_record($filterparams);
// No grade in plan - only a grade in the course.
$this->assertEmpty($usercomp->get_grade());
$this->assertEquals(1, $usercompcourse->get_grade());
api::update_course_competency_settings($c1->id, true);
api::grade_competency_in_course($c1->id, $u1->id, $comp1->get_id(), 2, 'Note 2');
$filterparams = array(
'userid' => $u1->id,
'competencyid' => $comp1->get_id(),
);
$usercompcourse = \tool_lp\user_competency_course::get_record($filterparams);
$usercomp = \tool_lp\user_competency::get_record($filterparams);
// Updated grade in plan - updated grade in the course.
$this->assertEquals(2, $usercomp->get_grade());
$this->assertEquals(2, $usercompcourse->get_grade());
$this->setUser($u3);
$this->setExpectedException('required_capability_exception');
api::update_course_competency_settings($c1->id, false);
}
}

View File

@ -1550,7 +1550,6 @@ class tool_lp_event_testcase extends advanced_testcase {
// Check that the event data is valid.
$this->assertInstanceOf('\tool_lp\event\evidence_created', $evidencecreatedevent);
$this->assertInstanceOf('\tool_lp\event\user_competency_grade_rated_in_course', $event);
$this->assertEquals($uc->get_id(), $event->objectid);
$this->assertEquals(context_course::instance($course->id)->id, $event->contextid);
$this->assertEquals($course->id, $event->courseid);
$this->assertEquals($uc->get_userid(), $event->relateduserid);