MDL-48273 singleview: unit tests of load_user method
[moodle.git] / grade / report / singleview / tests / screen_test.php
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/>.
17 /**
18  * Unit tests for gradereport_singleview screen class.
19  *
20  * @package    gradereport_singleview
21  * @category   test
22  * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 require_once(__DIR__ . '/fixtures/screen.php');
29 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Tests for screen class.
32  *
33  * Class gradereport_singleview_screen_testcase.
34  */
35 class gradereport_singleview_screen_testcase extends advanced_testcase {
37     /**
38      * Test load_users method.
39      */
40     public function test_load_users() {
41         global $DB;
43         $this->setAdminUser();
44         $this->resetAfterTest(true);
46         $roleteacher = $DB->get_record('role', array('shortname' => 'teacher'), '*', MUST_EXIST);
48         // Create a course, users and groups.
49         $course = $this->getDataGenerator()->create_course();
50         $coursecontext = context_course::instance($course->id);
51         $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
52         $teacher = $this->getDataGenerator()->create_user();
53         $user1 = $this->getDataGenerator()->create_user();
54         $user2 = $this->getDataGenerator()->create_user();
55         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
56         $this->getDataGenerator()->enrol_user($user1->id, $course->id);
57         $this->getDataGenerator()->enrol_user($user2->id, $course->id);
58         $this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $teacher->id));
59         $this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user1->id));
60         $this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user2->id));
62         $screentest = new gradereport_singleview_screen_testable($course->id, 0, $group->id);
63         $groupusers = $screentest->test_load_users();
64         $this->assertCount(2, $groupusers);
66         // Now, let's suspend the enrolment of a user. Should return only one user.
67         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleteacher->id, 'manual', 0, 0, ENROL_USER_SUSPENDED);
68         $users = $screentest->test_load_users();
69         $this->assertCount(1, $users);
71         // Change the viewsuspendedusers capabilities and set the user preference to display suspended users.
72         assign_capability('moodle/course:viewsuspendedusers', CAP_ALLOW, $roleteacher->id, $coursecontext, true);
73         set_user_preference('grade_report_showonlyactiveenrol', false, $teacher);
74         accesslib_clear_all_caches_for_unit_testing();
75         $this->setUser($teacher);
76         $screentest = new gradereport_singleview_screen_testable($course->id, 0, $group->id);
77         $users = $screentest->test_load_users();
78         $this->assertCount(2, $users);
80         // Change the capability again, now the user can't see the suspended enrolments.
81         assign_capability('moodle/course:viewsuspendedusers', CAP_PROHIBIT, $roleteacher->id, $coursecontext, true);
82         set_user_preference('grade_report_showonlyactiveenrol', false, $teacher);
83         accesslib_clear_all_caches_for_unit_testing();
84         $users = $screentest->test_load_users();
85         $this->assertCount(1, $users);
87         // Now, activate the user enrolment again. We shall get 2 users now.
88         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleteacher->id, 'manual', 0, 0, ENROL_USER_ACTIVE);
89         $users = $screentest->test_load_users();
90         $this->assertCount(2, $users);
91     }
92 }