MDL-62384 privacy: Modify user contexts query for auth_oauth2
[moodle.git] / completion / tests / progress_test.php
blobb2e5b97e56f57fef609d5a6f2c1c3f379596258b
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 /**
18 * Test completion progress API.
20 * @package core_completion
21 * @category test
22 * @copyright 2017 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Test completion progress API.
31 * @package core_completion
32 * @category test
33 * @copyright 2017 Mark Nelson <markn@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_completion_progress_testcase extends advanced_testcase {
38 /**
39 * Test setup.
41 public function setUp() {
42 global $CFG;
44 $CFG->enablecompletion = true;
45 $this->resetAfterTest();
48 /**
49 * Tests that the course progress percentage is returned correctly when we have only activity completion.
51 public function test_course_progress_percentage_with_just_activities() {
52 global $DB;
54 // Add a course that supports completion.
55 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
57 // Enrol a user in the course.
58 $user = $this->getDataGenerator()->create_user();
59 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
60 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
62 // Add four activities that use completion.
63 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
64 array('completion' => 1));
65 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
66 array('completion' => 1));
67 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
68 array('completion' => 1));
69 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
70 array('completion' => 1));
72 // Add an activity that does *not* use completion.
73 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
75 // Mark two of them as completed for a user.
76 $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
77 $cmdata = get_coursemodule_from_id('data', $data->cmid);
78 $completion = new completion_info($course);
79 $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
80 $completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
82 // Check we have received valid data.
83 // Note - only 4 out of the 5 activities support completion, and the user has completed 2 of those.
84 $this->assertEquals('50', \core_completion\progress::get_course_progress_percentage($course, $user->id));
87 /**
88 * Tests that the course progress percentage is returned correctly when we have a course and activity completion.
90 public function test_course_progress_percentage_with_activities_and_course() {
91 global $DB;
93 // Add a course that supports completion.
94 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
96 // Enrol a user in the course.
97 $user = $this->getDataGenerator()->create_user();
98 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
99 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
101 // Add four activities that use completion.
102 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id),
103 array('completion' => 1));
104 $data = $this->getDataGenerator()->create_module('data', array('course' => $course->id),
105 array('completion' => 1));
106 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
107 array('completion' => 1));
108 $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
109 array('completion' => 1));
111 // Add an activity that does *not* use completion.
112 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
114 // Mark two of them as completed for a user.
115 $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
116 $cmdata = get_coursemodule_from_id('data', $data->cmid);
117 $completion = new completion_info($course);
118 $completion->update_state($cmassign, COMPLETION_COMPLETE, $user->id);
119 $completion->update_state($cmdata, COMPLETION_COMPLETE, $user->id);
121 // Now, mark the course as completed.
122 $ccompletion = new completion_completion(array('course' => $course->id, 'userid' => $user->id));
123 $ccompletion->mark_complete();
125 // Check we have received valid data.
126 // The course completion takes priority, so should return 100.
127 $this->assertEquals('100', \core_completion\progress::get_course_progress_percentage($course, $user->id));
131 * Tests that the course progress returns null when the course does not support it.
133 public function test_course_progress_course_not_using_completion() {
134 // Create a course that does not use completion.
135 $course = $this->getDataGenerator()->create_course();
137 // Check that the result was null.
138 $this->assertNull(\core_completion\progress::get_course_progress_percentage($course));
142 * Tests that the course progress returns null when there are no activities that support it.
144 public function test_course_progress_no_activities_using_completion() {
145 // Create a course that does support completion.
146 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
148 // Add an activity that does *not* support completion.
149 $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
151 // Check that the result was null.
152 $this->assertNull(\core_completion\progress::get_course_progress_percentage($course));