MDL-51965 tool_lp: Revisit capabilities to read/manage plans
[moodle.git] / admin / tool / lp / tests / plan_test.php
CommitLineData
5159d679
FM
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 * Plan persistent class tests.
19 *
20 * @package tool_lp
21 * @copyright 2015 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
28/**
29 * Plan persistent testcase.
30 *
31 * @package tool_lp
32 * @copyright 2015 Frédéric Massart - FMCorz.net
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class tool_lp_plan_testcase extends advanced_testcase {
36
37 public function test_can_manage_user() {
38 $this->resetAfterTest(true);
39
40 $manage = create_role('Manage', 'manage', 'Plan manager');
41 $manageown = create_role('Manageown', 'manageown', 'Own plan manager');
42
43 $u1 = $this->getDataGenerator()->create_user();
44 $u2 = $this->getDataGenerator()->create_user();
45 $u3 = $this->getDataGenerator()->create_user();
46
47 $syscontext = context_system::instance();
48 $u1context = context_user::instance($u1->id);
49 $u2context = context_user::instance($u2->id);
50 $u3context = context_user::instance($u3->id);
51
52 assign_capability('tool/lp:planmanage', CAP_ALLOW, $manage, $syscontext->id);
53 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageown, $u2context->id);
54
55 role_assign($manage, $u1->id, $syscontext->id);
56 role_assign($manageown, $u2->id, $syscontext->id);
57 role_assign($manage, $u3->id, $u2context->id);
58 accesslib_clear_all_caches_for_unit_testing();
59
60 $this->setUser($u1);
61 $this->assertTrue(\tool_lp\plan::can_manage_user($u1->id));
62 $this->assertTrue(\tool_lp\plan::can_manage_user($u2->id));
63 $this->assertTrue(\tool_lp\plan::can_manage_user($u3->id));
64
65 $this->setUser($u2);
66 $this->assertFalse(\tool_lp\plan::can_manage_user($u1->id));
67 $this->assertTrue(\tool_lp\plan::can_manage_user($u2->id));
68 $this->assertFalse(\tool_lp\plan::can_manage_user($u3->id));
69
70 $this->setUser($u3);
71 $this->assertFalse(\tool_lp\plan::can_manage_user($u1->id));
72 $this->assertTrue(\tool_lp\plan::can_manage_user($u2->id));
73 $this->assertFalse(\tool_lp\plan::can_manage_user($u3->id));
74 }
75
76 public function test_can_manage_user_draft() {
77 $this->resetAfterTest(true);
78
79 $manage = create_role('Manage', 'manage', 'Plan manager');
80 $manageown = create_role('Manageown', 'manageown', 'Own plan manager');
81 $managedraft = create_role('Managedraft', 'managedraft', 'Draft plan manager');
82 $manageowndraft = create_role('Manageowndraft', 'manageowndraft', 'Own draft plan manager');
83
84 $u1 = $this->getDataGenerator()->create_user();
85 $u2 = $this->getDataGenerator()->create_user();
86 $u3 = $this->getDataGenerator()->create_user();
87 $u4 = $this->getDataGenerator()->create_user();
88 $u5 = $this->getDataGenerator()->create_user();
89
90 $syscontext = context_system::instance();
91 $u1context = context_user::instance($u1->id);
92 $u2context = context_user::instance($u2->id);
93 $u3context = context_user::instance($u3->id);
94 $u4context = context_user::instance($u4->id);
95 $u5context = context_user::instance($u5->id);
96
97 assign_capability('tool/lp:planmanage', CAP_ALLOW, $manage, $syscontext->id);
98 assign_capability('tool/lp:planmanageown', CAP_ALLOW, $manageown, $syscontext->id);
99 assign_capability('tool/lp:planmanagedraft', CAP_ALLOW, $managedraft, $syscontext->id);
100 assign_capability('tool/lp:planmanageowndraft', CAP_ALLOW, $manageowndraft, $syscontext->id);
101
102 role_assign($manage, $u1->id, $syscontext->id);
103 role_assign($manageown, $u2->id, $syscontext->id);
104 role_assign($managedraft, $u3->id, $syscontext->id);
105 role_assign($managedraft, $u4->id, $u2context->id);
106 role_assign($manageowndraft, $u5->id, $syscontext->id);
107 accesslib_clear_all_caches_for_unit_testing();
108
109 $this->setUser($u1);
110 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u1->id));
111 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u2->id));
112 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u3->id));
113 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u4->id));
114 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u5->id));
115
116 $this->setUser($u2);
117 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u1->id));
118 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u2->id));
119 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u3->id));
120 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u4->id));
121 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u5->id));
122
123 $this->setUser($u3);
124 $this->assertTrue(\tool_lp\plan::can_manage_user_draft($u1->id));
125 $this->assertTrue(\tool_lp\plan::can_manage_user_draft($u2->id));
126 $this->assertTrue(\tool_lp\plan::can_manage_user_draft($u3->id));
127 $this->assertTrue(\tool_lp\plan::can_manage_user_draft($u4->id));
128 $this->assertTrue(\tool_lp\plan::can_manage_user_draft($u5->id));
129
130 $this->setUser($u4);
131 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u1->id));
132 $this->assertTrue(\tool_lp\plan::can_manage_user_draft($u2->id));
133 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u3->id));
134 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u4->id));
135 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u5->id));
136
137 $this->setUser($u5);
138 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u1->id));
139 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u2->id));
140 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u3->id));
141 $this->assertFalse(\tool_lp\plan::can_manage_user_draft($u4->id));
142 $this->assertTrue(\tool_lp\plan::can_manage_user_draft($u5->id));
143 }
144
145 public function test_can_read_user() {
146 $this->resetAfterTest(true);
147
148 $read = create_role('Read', 'read', 'Plan reader');
149 $readown = create_role('Readown', 'readown', 'Own plan reader');
150
151 $u1 = $this->getDataGenerator()->create_user();
152 $u2 = $this->getDataGenerator()->create_user();
153 $u3 = $this->getDataGenerator()->create_user();
154
155 $syscontext = context_system::instance();
156 $u1context = context_user::instance($u1->id);
157 $u2context = context_user::instance($u2->id);
158 $u3context = context_user::instance($u3->id);
159
160 assign_capability('tool/lp:planview', CAP_ALLOW, $read, $syscontext->id);
161 assign_capability('tool/lp:planviewown', CAP_ALLOW, $readown, $u2context->id);
162
163 role_assign($read, $u1->id, $syscontext->id);
164 role_assign($readown, $u2->id, $syscontext->id);
165 role_assign($read, $u3->id, $u2context->id);
166 accesslib_clear_all_caches_for_unit_testing();
167
168 $this->setUser($u1);
169 $this->assertTrue(\tool_lp\plan::can_read_user($u1->id));
170 $this->assertTrue(\tool_lp\plan::can_read_user($u2->id));
171 $this->assertTrue(\tool_lp\plan::can_read_user($u3->id));
172
173 $this->setUser($u2);
174 $this->assertFalse(\tool_lp\plan::can_read_user($u1->id));
175 $this->assertTrue(\tool_lp\plan::can_read_user($u2->id));
176 $this->assertFalse(\tool_lp\plan::can_read_user($u3->id));
177
178 $this->setUser($u3);
179 $this->assertFalse(\tool_lp\plan::can_read_user($u1->id));
180 $this->assertTrue(\tool_lp\plan::can_read_user($u2->id));
181 $this->assertTrue(\tool_lp\plan::can_read_user($u3->id)); // Due to the default capability.
182 }
183
184 public function test_can_read_user_draft() {
185 $this->resetAfterTest(true);
186
187 $read = create_role('Read', 'read', 'Plan readr');
188 $readown = create_role('Readown', 'readown', 'Own plan readr');
189 $readdraft = create_role('Readdraft', 'readdraft', 'Draft plan readr');
190 $readowndraft = create_role('Readowndraft', 'readowndraft', 'Own draft plan readr');
191
192 $u1 = $this->getDataGenerator()->create_user();
193 $u2 = $this->getDataGenerator()->create_user();
194 $u3 = $this->getDataGenerator()->create_user();
195 $u4 = $this->getDataGenerator()->create_user();
196 $u5 = $this->getDataGenerator()->create_user();
197
198 $syscontext = context_system::instance();
199 $u1context = context_user::instance($u1->id);
200 $u2context = context_user::instance($u2->id);
201 $u3context = context_user::instance($u3->id);
202 $u4context = context_user::instance($u4->id);
203 $u5context = context_user::instance($u5->id);
204
205 assign_capability('tool/lp:planview', CAP_ALLOW, $read, $syscontext->id);
206 assign_capability('tool/lp:planviewown', CAP_ALLOW, $readown, $syscontext->id);
207 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $readdraft, $syscontext->id);
208 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $readowndraft, $syscontext->id);
209 assign_capability('tool/lp:planviewown', CAP_PROHIBIT, $readowndraft, $syscontext->id);
210
211 role_assign($read, $u1->id, $syscontext->id);
212 role_assign($readown, $u2->id, $syscontext->id);
213 role_assign($readdraft, $u3->id, $syscontext->id);
214 role_assign($readdraft, $u4->id, $u2context->id);
215 role_assign($readowndraft, $u5->id, $syscontext->id);
216 accesslib_clear_all_caches_for_unit_testing();
217
218 $this->setUser($u1);
219 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u1->id));
220 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u2->id));
221 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u3->id));
222 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u4->id));
223 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u5->id));
224
225 $this->setUser($u2);
226 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u1->id));
227 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u2->id));
228 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u3->id));
229 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u4->id));
230 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u5->id));
231
232 $this->setUser($u3);
233 $this->assertTrue(\tool_lp\plan::can_read_user_draft($u1->id));
234 $this->assertTrue(\tool_lp\plan::can_read_user_draft($u2->id));
235 $this->assertTrue(\tool_lp\plan::can_read_user_draft($u3->id));
236 $this->assertTrue(\tool_lp\plan::can_read_user_draft($u4->id));
237 $this->assertTrue(\tool_lp\plan::can_read_user_draft($u5->id));
238
239 $this->setUser($u4);
240 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u1->id));
241 $this->assertTrue(\tool_lp\plan::can_read_user_draft($u2->id));
242 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u3->id));
243 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u4->id));
244 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u5->id));
245
246 $this->setUser($u5);
247 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u1->id));
248 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u2->id));
249 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u3->id));
250 $this->assertFalse(\tool_lp\plan::can_read_user_draft($u4->id));
251 $this->assertTrue(\tool_lp\plan::can_read_user_draft($u5->id));
252 }
253}