MDL-52777 usertours: Remove empty styles file
[moodle.git] / admin / tool / usertours / tests / role_filter_test.php
CommitLineData
001fc061
AN
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 * Tests for role filter.
19 *
20 * @package tool_usertours
21 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Tests for role filter.
29 *
30 * @package tool_usertours
31 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class tool_usertours_role_filter_testcase extends advanced_testcase {
35
36 /**
37 * @var $course Test course
38 */
39 protected $course;
40
41 /**
42 * @var $student Test student
43 */
44 protected $student;
45
46 /**
47 * @var $teacher Test teacher
48 */
49 protected $teacher;
50
51 /**
52 * @var $editingteacher Test editor
53 */
54 protected $editingteacher;
55
56 /**
57 * @var $roles List of all roles
58 */
59 protected $roles;
60
61 public function setUp() {
62 global $DB;
63
64 $this->resetAfterTest(true);
65 $generator = $this->getDataGenerator();
66
67 $this->course = $generator->create_course();
68 $this->roles = $DB->get_records_menu('role', [], null, 'shortname, id');
69 $this->testroles = ['student', 'teacher', 'editingteacher'];
70
71 foreach ($this->testroles as $role) {
72 $user = $this->$role = $generator->create_user();
73 $generator->enrol_user($user->id, $this->course->id, $this->roles[$role]);
74 }
75 }
76
77 /**
78 * Test the filter_matches function when any is set.
79 */
80 public function test_filter_matches_any() {
81 $context = \context_course::instance($this->course->id);
82
83 // Note: No need to persist this tour.
84 $tour = new \tool_usertours\tour();
85 $tour->set_filter_values('role', []);
86
87 // Note: The role filter does not use the context.
88 foreach ($this->testroles as $role) {
89 $this->setUser($this->$role);
90 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context));
91 }
92
93 // The admin should always be able to view too.
94 $this->setAdminUser();
95 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context));
96 }
97
98 /**
99 * Test the filter_matches function when one role is set.
100 */
101 public function test_filter_matches_single_role() {
102 $context = \context_course::instance($this->course->id);
103
104 $roles = [
105 $this->roles['student'],
106 ];
107
108 // Note: No need to persist this tour.
109 $tour = new \tool_usertours\tour();
110 $tour->set_filter_values('role', $roles);
111
112 // Note: The role filter does not use the context.
113 foreach ($this->testroles as $role) {
114 $this->setUser($this->$role);
115 if ($role === 'student') {
116 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context));
117 } else {
118 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context));
119 }
120 }
121
122 // The admin should always be able to view too.
123 $this->setAdminUser();
124 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context));
125 }
126
127 /**
128 * Test the filter_matches function when multiple roles are set.
129 */
130 public function test_filter_matches_multiple_role() {
131 $context = \context_course::instance($this->course->id);
132
133 $roles = [
134 $this->roles['teacher'],
135 $this->roles['editingteacher'],
136 ];
137
138 // Note: No need to persist this tour.
139 $tour = new \tool_usertours\tour();
140 $tour->set_filter_values('role', $roles);
141
142 // Note: The role filter does not use the context.
143 foreach ($this->testroles as $role) {
144 $this->setUser($this->$role);
145 if ($role === 'student') {
146 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context));
147 } else {
148 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context));
149 }
150 }
151
152 // The admin should always be able to view too.
153 $this->setAdminUser();
154 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context));
155 }
156
157 /**
158 * Test the filter_matches function when one user has multiple roles.
159 */
160 public function test_filter_matches_multiple_role_one_user() {
161 $context = \context_course::instance($this->course->id);
162
163 $roles = [
164 $this->roles['student'],
165 ];
166
167 $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->roles['teacher']);
168
169 // Note: No need to persist this tour.
170 $tour = new \tool_usertours\tour();
171 $tour->set_filter_values('role', $roles);
172
173
174 // Note: The role filter does not use the context.
175 foreach ($this->testroles as $role) {
176 $this->setUser($this->$role);
177 if ($role === 'student') {
178 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context));
179 } else {
180 $this->assertFalse(\tool_usertours\local\filter\role::filter_matches($tour, $context));
181 }
182 }
183
184 // The admin should always be able to view too.
185 $this->setAdminUser();
186 $this->assertTrue(\tool_usertours\local\filter\role::filter_matches($tour, $context));
187 }
188}