MDL-64506 phpunit: Update unit tests to use classic
[moodle.git] / cohort / tests / externallib_test.php
CommitLineData
088645e2
AB
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 * External cohort API
19 *
20 * @package core_cohort
21 * @category external
22 * @copyright MediaTouch 2000 srl
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31require_once($CFG->dirroot . '/cohort/externallib.php');
32
8252b7c2 33class core_cohort_externallib_testcase extends externallib_advanced_testcase {
088645e2
AB
34
35 /**
36 * Test create_cohorts
52f3e060
RT
37 *
38 * @expectedException required_capability_exception
088645e2
AB
39 */
40 public function test_create_cohorts() {
41 global $USER, $CFG, $DB;
42
43 $this->resetAfterTest(true);
44
88cb8b78
SA
45 set_config('allowcohortthemes', 1);
46
088645e2 47 $contextid = context_system::instance()->id;
80f98467 48 $category = $this->getDataGenerator()->create_category();
088645e2
AB
49
50 $cohort1 = array(
80f98467 51 'categorytype' => array('type' => 'id', 'value' => $category->id),
088645e2
AB
52 'name' => 'cohort test 1',
53 'idnumber' => 'cohorttest1',
88cb8b78 54 'description' => 'This is a description for cohorttest1',
e00f1c66 55 'theme' => 'classic'
088645e2
AB
56 );
57
58 $cohort2 = array(
66d6221f 59 'categorytype' => array('type' => 'system', 'value' => ''),
088645e2
AB
60 'name' => 'cohort test 2',
61 'idnumber' => 'cohorttest2',
80f98467
MG
62 'description' => 'This is a description for cohorttest2',
63 'visible' => 0
088645e2 64 );
66d6221f
DW
65
66 $cohort3 = array(
80f98467 67 'categorytype' => array('type' => 'id', 'value' => $category->id),
66d6221f
DW
68 'name' => 'cohort test 3',
69 'idnumber' => 'cohorttest3',
70 'description' => 'This is a description for cohorttest3'
71 );
088645e2
AB
72 $roleid = $this->assignUserCapability('moodle/cohort:manage', $contextid);
73
88cb8b78
SA
74 $cohort4 = array(
75 'categorytype' => array('type' => 'id', 'value' => $category->id),
76 'name' => 'cohort test 4',
77 'idnumber' => 'cohorttest4',
78 'description' => 'This is a description for cohorttest4',
e00f1c66 79 'theme' => 'classic'
88cb8b78
SA
80 );
81
088645e2 82 // Call the external function.
80f98467 83 $this->setCurrentTimeStart();
66d6221f 84 $createdcohorts = core_cohort_external::create_cohorts(array($cohort1, $cohort2));
52ab1076 85 $createdcohorts = external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts);
088645e2
AB
86
87 // Check we retrieve the good total number of created cohorts + no error on capability.
66d6221f 88 $this->assertEquals(2, count($createdcohorts));
088645e2
AB
89
90 foreach ($createdcohorts as $createdcohort) {
80f98467 91 $dbcohort = $DB->get_record('cohort', array('id' => $createdcohort['id']));
66d6221f 92 if ($createdcohort['idnumber'] == $cohort1['idnumber']) {
66d6221f
DW
93 $conid = $DB->get_field('context', 'id', array('instanceid' => $cohort1['categorytype']['value'],
94 'contextlevel' => CONTEXT_COURSECAT));
95 $this->assertEquals($dbcohort->contextid, $conid);
96 $this->assertEquals($dbcohort->name, $cohort1['name']);
66d6221f 97 $this->assertEquals($dbcohort->description, $cohort1['description']);
80f98467 98 $this->assertEquals($dbcohort->visible, 1); // Field was not specified, ensure it is visible by default.
88cb8b78
SA
99 // As $CFG->allowcohortthemes is enabled, theme must be initialised.
100 $this->assertEquals($dbcohort->theme, $cohort1['theme']);
80f98467
MG
101 } else if ($createdcohort['idnumber'] == $cohort2['idnumber']) {
102 $this->assertEquals($dbcohort->contextid, context_system::instance()->id);
103 $this->assertEquals($dbcohort->name, $cohort2['name']);
104 $this->assertEquals($dbcohort->description, $cohort2['description']);
105 $this->assertEquals($dbcohort->visible, $cohort2['visible']);
88cb8b78
SA
106 // Although $CFG->allowcohortthemes is enabled, no theme is defined for this cohort.
107 $this->assertEquals($dbcohort->theme, '');
80f98467
MG
108 } else {
109 $this->fail('Unrecognised cohort found');
66d6221f 110 }
80f98467
MG
111 $this->assertTimeCurrent($dbcohort->timecreated);
112 $this->assertTimeCurrent($dbcohort->timemodified);
088645e2
AB
113 }
114
88cb8b78
SA
115 // Call when $CFG->allowcohortthemes is disabled.
116 set_config('allowcohortthemes', 0);
117 $createdcohorts = core_cohort_external::create_cohorts(array($cohort4));
118 $createdcohorts = external_api::clean_returnvalue(core_cohort_external::create_cohorts_returns(), $createdcohorts);
119 foreach ($createdcohorts as $createdcohort) {
120 $dbcohort = $DB->get_record('cohort', array('id' => $createdcohort['id']));
121 if ($createdcohort['idnumber'] == $cohort4['idnumber']) {
122 $conid = $DB->get_field('context', 'id', array('instanceid' => $cohort4['categorytype']['value'],
123 'contextlevel' => CONTEXT_COURSECAT));
124 $this->assertEquals($dbcohort->contextid, $conid);
125 $this->assertEquals($dbcohort->name, $cohort4['name']);
126 $this->assertEquals($dbcohort->description, $cohort4['description']);
127 $this->assertEquals($dbcohort->visible, 1); // Field was not specified, ensure it is visible by default.
128 $this->assertEquals($dbcohort->theme, ''); // As $CFG->allowcohortthemes is disabled, theme must be empty.
129 }
130 }
131
088645e2
AB
132 // Call without required capability.
133 $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid);
66d6221f 134 $createdcohorts = core_cohort_external::create_cohorts(array($cohort3));
088645e2
AB
135 }
136
137 /**
138 * Test delete_cohorts
52f3e060
RT
139 *
140 * @expectedException required_capability_exception
088645e2
AB
141 */
142 public function test_delete_cohorts() {
143 global $USER, $CFG, $DB;
144
145 $this->resetAfterTest(true);
146
147 $cohort1 = self::getDataGenerator()->create_cohort();
148 $cohort2 = self::getDataGenerator()->create_cohort();
149 // Check the cohorts were correctly created.
150 $this->assertEquals(2, $DB->count_records_select('cohort', ' (id = :cohortid1 OR id = :cohortid2)',
151 array('cohortid1' => $cohort1->id, 'cohortid2' => $cohort2->id)));
152
153 $contextid = $cohort1->contextid;
154 $roleid = $this->assignUserCapability('moodle/cohort:manage', $contextid);
155
156 // Call the external function.
157 core_cohort_external::delete_cohorts(array($cohort1->id, $cohort2->id));
158
159 // Check we retrieve no cohorts + no error on capability.
160 $this->assertEquals(0, $DB->count_records_select('cohort', ' (id = :cohortid1 OR id = :cohortid2)',
161 array('cohortid1' => $cohort1->id, 'cohortid2' => $cohort2->id)));
162
163 // Call without required capability.
164 $cohort1 = self::getDataGenerator()->create_cohort();
165 $cohort2 = self::getDataGenerator()->create_cohort();
166 $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid);
088645e2
AB
167 core_cohort_external::delete_cohorts(array($cohort1->id, $cohort2->id));
168 }
169
170 /**
171 * Test get_cohorts
172 */
173 public function test_get_cohorts() {
174 global $USER, $CFG;
175
176 $this->resetAfterTest(true);
177
88cb8b78
SA
178 set_config('allowcohortthemes', 1);
179
088645e2
AB
180 $cohort1 = array(
181 'contextid' => 1,
182 'name' => 'cohortnametest1',
183 'idnumber' => 'idnumbertest1',
88cb8b78 184 'description' => 'This is a description for cohort 1',
e00f1c66 185 'theme' => 'classic'
088645e2
AB
186 );
187 $cohort1 = self::getDataGenerator()->create_cohort($cohort1);
188 $cohort2 = self::getDataGenerator()->create_cohort();
189
190 $context = context_system::instance();
191 $roleid = $this->assignUserCapability('moodle/cohort:view', $context->id);
192
193 // Call the external function.
194 $returnedcohorts = core_cohort_external::get_cohorts(array(
195 $cohort1->id, $cohort2->id));
52ab1076 196 $returnedcohorts = external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts);
088645e2
AB
197
198 // Check we retrieve the good total number of enrolled cohorts + no error on capability.
199 $this->assertEquals(2, count($returnedcohorts));
200
088645e2
AB
201 foreach ($returnedcohorts as $enrolledcohort) {
202 if ($enrolledcohort['idnumber'] == $cohort1->idnumber) {
203 $this->assertEquals($cohort1->name, $enrolledcohort['name']);
204 $this->assertEquals($cohort1->description, $enrolledcohort['description']);
80f98467 205 $this->assertEquals($cohort1->visible, $enrolledcohort['visible']);
88cb8b78 206 $this->assertEquals($cohort1->theme, $enrolledcohort['theme']);
088645e2
AB
207 }
208 }
66d6221f
DW
209
210 // Check that a user with cohort:manage can see the cohort.
211 $this->unassignUserCapability('moodle/cohort:view', $context->id, $roleid);
212 $roleid = $this->assignUserCapability('moodle/cohort:manage', $context->id, $roleid);
213 // Call the external function.
214 $returnedcohorts = core_cohort_external::get_cohorts(array(
215 $cohort1->id, $cohort2->id));
52ab1076 216 $returnedcohorts = external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts);
66d6221f
DW
217
218 // Check we retrieve the good total number of enrolled cohorts + no error on capability.
219 $this->assertEquals(2, count($returnedcohorts));
88cb8b78
SA
220
221 // Check when allowcohortstheme is disabled, theme is not returned.
222 set_config('allowcohortthemes', 0);
223 $returnedcohorts = core_cohort_external::get_cohorts(array(
224 $cohort1->id));
225 $returnedcohorts = external_api::clean_returnvalue(core_cohort_external::get_cohorts_returns(), $returnedcohorts);
226 foreach ($returnedcohorts as $enrolledcohort) {
227 if ($enrolledcohort['idnumber'] == $cohort1->idnumber) {
228 $this->assertNull($enrolledcohort['theme']);
229 }
230 }
088645e2
AB
231 }
232
233 /**
234 * Test update_cohorts
52f3e060
RT
235 *
236 * @expectedException required_capability_exception
088645e2
AB
237 */
238 public function test_update_cohorts() {
239 global $USER, $CFG, $DB;
240
241 $this->resetAfterTest(true);
242
88cb8b78
SA
243 set_config('allowcohortthemes', 0);
244
80f98467 245 $cohort1 = self::getDataGenerator()->create_cohort(array('visible' => 0));
088645e2
AB
246
247 $cohort1 = array(
248 'id' => $cohort1->id,
249 'categorytype' => array('type' => 'id', 'value' => '1'),
250 'name' => 'cohortnametest1',
251 'idnumber' => 'idnumbertest1',
88cb8b78 252 'description' => 'This is a description for cohort 1',
e00f1c66 253 'theme' => 'classic'
088645e2
AB
254 );
255
256 $context = context_system::instance();
257 $roleid = $this->assignUserCapability('moodle/cohort:manage', $context->id);
258
259 // Call the external function.
260 core_cohort_external::update_cohorts(array($cohort1));
261
262 $dbcohort = $DB->get_record('cohort', array('id' => $cohort1['id']));
263 $contextid = $DB->get_field('context', 'id', array('instanceid' => $cohort1['categorytype']['value'],
264 'contextlevel' => CONTEXT_COURSECAT));
265 $this->assertEquals($dbcohort->contextid, $contextid);
266 $this->assertEquals($dbcohort->name, $cohort1['name']);
267 $this->assertEquals($dbcohort->idnumber, $cohort1['idnumber']);
268 $this->assertEquals($dbcohort->description, $cohort1['description']);
80f98467 269 $this->assertEquals($dbcohort->visible, 0);
88cb8b78 270 $this->assertEmpty($dbcohort->theme);
80f98467
MG
271
272 // Since field 'visible' was added in 2.8, make sure that update works correctly with and without this parameter.
273 core_cohort_external::update_cohorts(array($cohort1 + array('visible' => 1)));
274 $dbcohort = $DB->get_record('cohort', array('id' => $cohort1['id']));
275 $this->assertEquals(1, $dbcohort->visible);
276 core_cohort_external::update_cohorts(array($cohort1));
277 $dbcohort = $DB->get_record('cohort', array('id' => $cohort1['id']));
278 $this->assertEquals(1, $dbcohort->visible);
088645e2 279
88cb8b78
SA
280 // Call when $CFG->allowcohortthemes is enabled.
281 set_config('allowcohortthemes', 1);
e00f1c66 282 core_cohort_external::update_cohorts(array($cohort1 + array('theme' => 'classic')));
88cb8b78 283 $dbcohort = $DB->get_record('cohort', array('id' => $cohort1['id']));
e00f1c66 284 $this->assertEquals('classic', $dbcohort->theme);
88cb8b78
SA
285
286 // Call when $CFG->allowcohortthemes is disabled.
287 set_config('allowcohortthemes', 0);
e00f1c66 288 core_cohort_external::update_cohorts(array($cohort1 + array('theme' => 'boost')));
88cb8b78 289 $dbcohort = $DB->get_record('cohort', array('id' => $cohort1['id']));
e00f1c66 290 $this->assertEquals('classic', $dbcohort->theme);
88cb8b78 291
088645e2
AB
292 // Call without required capability.
293 $this->unassignUserCapability('moodle/cohort:manage', $context->id, $roleid);
088645e2
AB
294 core_cohort_external::update_cohorts(array($cohort1));
295 }
296
fb5ce7d3
DP
297 /**
298 * Verify handling of 'id' param.
299 */
300 public function test_update_cohorts_invalid_id_param() {
301 $this->resetAfterTest(true);
302 $cohort = self::getDataGenerator()->create_cohort();
303
304 $cohort1 = array(
305 'id' => 'THIS IS NOT AN ID',
306 'name' => 'Changed cohort name',
307 'categorytype' => array('type' => 'id', 'value' => '1'),
308 'idnumber' => $cohort->idnumber,
309 );
310
311 try {
312 core_cohort_external::update_cohorts(array($cohort1));
313 $this->fail('Expecting invalid_parameter_exception exception, none occured');
314 } catch (invalid_parameter_exception $e1) {
315 $this->assertContains('Invalid external api parameter: the value is "THIS IS NOT AN ID"', $e1->debuginfo);
316 }
317
318 $cohort1['id'] = 9.999; // Also not a valid id of a cohort.
319 try {
320 core_cohort_external::update_cohorts(array($cohort1));
321 $this->fail('Expecting invalid_parameter_exception exception, none occured');
322 } catch (invalid_parameter_exception $e2) {
323 $this->assertContains('Invalid external api parameter: the value is "9.999"', $e2->debuginfo);
324 }
325 }
326
66d6221f
DW
327 /**
328 * Test update_cohorts without permission on the dest category.
52f3e060
RT
329 *
330 * @expectedException required_capability_exception
66d6221f
DW
331 */
332 public function test_update_cohorts_missing_dest() {
333 global $USER, $CFG, $DB;
334
335 $this->resetAfterTest(true);
336
337 $category1 = self::getDataGenerator()->create_category(array(
338 'name' => 'Test category 1'
339 ));
340 $category2 = self::getDataGenerator()->create_category(array(
341 'name' => 'Test category 2'
342 ));
343 $context1 = context_coursecat::instance($category1->id);
344 $context2 = context_coursecat::instance($category2->id);
345
346 $cohort = array(
347 'contextid' => $context1->id,
348 'name' => 'cohortnametest1',
349 'idnumber' => 'idnumbertest1',
350 'description' => 'This is a description for cohort 1'
351 );
352 $cohort1 = self::getDataGenerator()->create_cohort($cohort);
353
354 $roleid = $this->assignUserCapability('moodle/cohort:manage', $context1->id);
355
356 $cohortupdate = array(
357 'id' => $cohort1->id,
358 'categorytype' => array('type' => 'id', 'value' => $category2->id),
359 'name' => 'cohort update',
360 'idnumber' => 'idnumber update',
361 'description' => 'This is a description update'
362 );
363
364 // Call the external function.
365 // Should fail because we don't have permission on the dest category
66d6221f
DW
366 core_cohort_external::update_cohorts(array($cohortupdate));
367 }
368
369 /**
370 * Test update_cohorts without permission on the src category.
52f3e060
RT
371 *
372 * @expectedException required_capability_exception
66d6221f
DW
373 */
374 public function test_update_cohorts_missing_src() {
375 global $USER, $CFG, $DB;
376
377 $this->resetAfterTest(true);
378
379 $category1 = self::getDataGenerator()->create_category(array(
380 'name' => 'Test category 1'
381 ));
382 $category2 = self::getDataGenerator()->create_category(array(
383 'name' => 'Test category 2'
384 ));
385 $context1 = context_coursecat::instance($category1->id);
386 $context2 = context_coursecat::instance($category2->id);
387
388 $cohort = array(
389 'contextid' => $context1->id,
390 'name' => 'cohortnametest1',
391 'idnumber' => 'idnumbertest1',
392 'description' => 'This is a description for cohort 1'
393 );
394 $cohort1 = self::getDataGenerator()->create_cohort($cohort);
395
396 $roleid = $this->assignUserCapability('moodle/cohort:manage', $context2->id);
397
398 $cohortupdate = array(
399 'id' => $cohort1->id,
400 'categorytype' => array('type' => 'id', 'value' => $category2->id),
401 'name' => 'cohort update',
402 'idnumber' => 'idnumber update',
403 'description' => 'This is a description update'
404 );
405
406 // Call the external function.
407 // Should fail because we don't have permission on the src category
66d6221f
DW
408 core_cohort_external::update_cohorts(array($cohortupdate));
409 }
410
088645e2
AB
411 /**
412 * Test add_cohort_members
52f3e060
RT
413 *
414 * @expectedException required_capability_exception
088645e2
AB
415 */
416 public function test_add_cohort_members() {
417 global $DB;
418
419 $this->resetAfterTest(true); // Reset all changes automatically after this test.
420
421 $contextid = context_system::instance()->id;
422
423 $cohort = array(
424 'contextid' => $contextid,
425 'name' => 'cohortnametest1',
426 'idnumber' => 'idnumbertest1',
427 'description' => 'This is a description for cohort 1'
428 );
429 $cohort0 = self::getDataGenerator()->create_cohort($cohort);
430 // Check the cohorts were correctly created.
431 $this->assertEquals(1, $DB->count_records_select('cohort', ' (id = :cohortid0)',
432 array('cohortid0' => $cohort0->id)));
433
434 $cohort1 = array(
435 'cohorttype' => array('type' => 'id', 'value' => $cohort0->id),
436 'usertype' => array('type' => 'id', 'value' => '1')
437 );
438
439 $roleid = $this->assignUserCapability('moodle/cohort:assign', $contextid);
440
441 // Call the external function.
442 $addcohortmembers = core_cohort_external::add_cohort_members(array($cohort1));
52ab1076 443 $addcohortmembers = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $addcohortmembers);
088645e2
AB
444
445 // Check we retrieve the good total number of created cohorts + no error on capability.
446 $this->assertEquals(1, count($addcohortmembers));
447
448 foreach ($addcohortmembers as $addcohortmember) {
449 $dbcohort = $DB->get_record('cohort_members', array('cohortid' => $cohort0->id));
450 $this->assertEquals($dbcohort->cohortid, $cohort1['cohorttype']['value']);
451 $this->assertEquals($dbcohort->userid, $cohort1['usertype']['value']);
452 }
453
454 // Call without required capability.
455 $cohort2 = array(
456 'cohorttype' => array('type' => 'id', 'value' => $cohort0->id),
457 'usertype' => array('type' => 'id', 'value' => '2')
458 );
459 $this->unassignUserCapability('moodle/cohort:assign', $contextid, $roleid);
088645e2
AB
460 $addcohortmembers = core_cohort_external::add_cohort_members(array($cohort2));
461 }
462
463 /**
464 * Test delete_cohort_members
52f3e060
RT
465 *
466 * @expectedException required_capability_exception
088645e2
AB
467 */
468 public function test_delete_cohort_members() {
469 global $DB;
470
471 $this->resetAfterTest(true); // Reset all changes automatically after this test.
472
473 $cohort1 = self::getDataGenerator()->create_cohort();
474 $user1 = self::getDataGenerator()->create_user();
475 $cohort2 = self::getDataGenerator()->create_cohort();
476 $user2 = self::getDataGenerator()->create_user();
477
478 $context = context_system::instance();
479 $roleid = $this->assignUserCapability('moodle/cohort:assign', $context->id);
480
481 $cohortaddmember1 = array(
482 'cohorttype' => array('type' => 'id', 'value' => $cohort1->id),
483 'usertype' => array('type' => 'id', 'value' => $user1->id)
484 );
485 $cohortmembers1 = core_cohort_external::add_cohort_members(array($cohortaddmember1));
52ab1076
JL
486 $cohortmembers1 = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers1);
487
088645e2
AB
488 $cohortaddmember2 = array(
489 'cohorttype' => array('type' => 'id', 'value' => $cohort2->id),
490 'usertype' => array('type' => 'id', 'value' => $user2->id)
491 );
492 $cohortmembers2 = core_cohort_external::add_cohort_members(array($cohortaddmember2));
52ab1076 493 $cohortmembers2 = external_api::clean_returnvalue(core_cohort_external::add_cohort_members_returns(), $cohortmembers2);
088645e2
AB
494
495 // Check we retrieve no cohorts + no error on capability.
496 $this->assertEquals(2, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1)
497 OR (cohortid = :idcohort2 AND userid = :iduser2))',
498 array('idcohort1' => $cohort1->id, 'iduser1' => $user1->id, 'idcohort2' => $cohort2->id, 'iduser2' => $user2->id)));
499
500 // Call the external function.
501 $cohortdel1 = array(
502 'cohortid' => $cohort1->id,
503 'userid' => $user1->id
504 );
505 $cohortdel2 = array(
506 'cohortid' => $cohort2->id,
507 'userid' => $user2->id
508 );
509 core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2));
510
511 // Check we retrieve no cohorts + no error on capability.
512 $this->assertEquals(0, $DB->count_records_select('cohort_members', ' ((cohortid = :idcohort1 AND userid = :iduser1)
513 OR (cohortid = :idcohort2 AND userid = :iduser2))',
514 array('idcohort1' => $cohort1->id, 'iduser1' => $user1->id, 'idcohort2' => $cohort2->id, 'iduser2' => $user2->id)));
515
516 // Call without required capability.
517 $this->unassignUserCapability('moodle/cohort:assign', $context->id, $roleid);
088645e2
AB
518 core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2));
519 }
a60e8ba5
DW
520
521 /**
522 * Search cohorts.
523 */
524 public function test_search_cohorts() {
525 $this->resetAfterTest(true);
526
527 $creator = $this->getDataGenerator()->create_user();
528 $user = $this->getDataGenerator()->create_user();
529 $catuser = $this->getDataGenerator()->create_user();
530 $catcreator = $this->getDataGenerator()->create_user();
d406ddca 531 $courseuser = $this->getDataGenerator()->create_user();
a60e8ba5
DW
532 $category = $this->getDataGenerator()->create_category();
533 $othercategory = $this->getDataGenerator()->create_category();
d406ddca 534 $course = $this->getDataGenerator()->create_course();
a60e8ba5
DW
535 $syscontext = context_system::instance();
536 $catcontext = context_coursecat::instance($category->id);
d406ddca 537 $coursecontext = context_course::instance($course->id);
a60e8ba5
DW
538
539 // Fetching default authenticated user role.
540 $userroles = get_archetype_roles('user');
541 $this->assertCount(1, $userroles);
542 $authrole = array_pop($userroles);
543
544 // Reset all default authenticated users permissions.
545 unassign_capability('moodle/cohort:manage', $authrole->id);
546
547 // Creating specific roles.
548 $creatorrole = create_role('Creator role', 'creatorrole', 'creator role description');
549 $userrole = create_role('User role', 'userrole', 'user role description');
d406ddca 550 $courserole = create_role('Course user role', 'courserole', 'course user role description');
a60e8ba5
DW
551
552 assign_capability('moodle/cohort:manage', CAP_ALLOW, $creatorrole, $syscontext->id);
d406ddca 553 assign_capability('moodle/cohort:view', CAP_ALLOW, $courserole, $syscontext->id);
a60e8ba5
DW
554
555 // Check for parameter $includes = 'parents'.
556 role_assign($creatorrole, $creator->id, $syscontext->id);
557 role_assign($creatorrole, $catcreator->id, $catcontext->id);
558 role_assign($userrole, $user->id, $syscontext->id);
559 role_assign($userrole, $catuser->id, $catcontext->id);
560
d406ddca
DM
561 // Enrol user in the course.
562 $this->getDataGenerator()->enrol_user($courseuser->id, $course->id, 'courserole');
563
a60e8ba5
DW
564 $syscontext = array('contextid' => context_system::instance()->id);
565 $catcontext = array('contextid' => context_coursecat::instance($category->id)->id);
566 $othercatcontext = array('contextid' => context_coursecat::instance($othercategory->id)->id);
d406ddca 567 $coursecontext = array('contextid' => context_course::instance($course->id)->id);
a60e8ba5
DW
568
569 $cohort1 = $this->getDataGenerator()->create_cohort(array_merge($syscontext, array('name' => 'Cohortsearch 1')));
570 $cohort2 = $this->getDataGenerator()->create_cohort(array_merge($catcontext, array('name' => 'Cohortsearch 2')));
571 $cohort3 = $this->getDataGenerator()->create_cohort(array_merge($othercatcontext, array('name' => 'Cohortsearch 3')));
572
573 // A user without permission in the system.
574 $this->setUser($user);
575 try {
576 $result = core_cohort_external::search_cohorts("Cohortsearch", $syscontext, 'parents');
577 $this->fail('Invalid permissions in system');
578 } catch (required_capability_exception $e) {
579 // All good.
580 }
581
582 // A user without permission in a category.
583 $this->setUser($catuser);
584 try {
585 $result = core_cohort_external::search_cohorts("Cohortsearch", $catcontext, 'parents');
586 $this->fail('Invalid permissions in category');
587 } catch (required_capability_exception $e) {
588 // All good.
589 }
590
591 // A user with permissions in the system.
592 $this->setUser($creator);
593 $result = core_cohort_external::search_cohorts("Cohortsearch", $syscontext, 'parents');
594 $this->assertEquals(1, count($result['cohorts']));
595 $this->assertEquals('Cohortsearch 1', $result['cohorts'][$cohort1->id]->name);
596
597 // A user with permissions in the category.
598 $this->setUser($catcreator);
599 $result = core_cohort_external::search_cohorts("Cohortsearch", $catcontext, 'parents');
600 $this->assertEquals(2, count($result['cohorts']));
601 $cohorts = array();
602 foreach ($result['cohorts'] as $cohort) {
603 $cohorts[] = $cohort->name;
604 }
605 $this->assertTrue(in_array('Cohortsearch 1', $cohorts));
606
607 // Check for parameter $includes = 'self'.
608 $this->setUser($creator);
609 $result = core_cohort_external::search_cohorts("Cohortsearch", $othercatcontext, 'self');
610 $this->assertEquals(1, count($result['cohorts']));
611 $this->assertEquals('Cohortsearch 3', $result['cohorts'][$cohort3->id]->name);
612
613 // Check for parameter $includes = 'all'.
614 $this->setUser($creator);
615 $result = core_cohort_external::search_cohorts("Cohortsearch", $syscontext, 'all');
616 $this->assertEquals(3, count($result['cohorts']));
617
d406ddca
DM
618 // A user in the course context with the system cohort:view capability. Check that all the system cohorts are returned.
619 $this->setUser($courseuser);
620 $result = core_cohort_external::search_cohorts("Cohortsearch", $coursecontext, 'all');
621 $this->assertEquals(1, count($result['cohorts']));
622 $this->assertEquals('Cohortsearch 1', $result['cohorts'][$cohort1->id]->name);
623
a60e8ba5
DW
624 // Detect invalid parameter $includes.
625 $this->setUser($creator);
626 try {
627 $result = core_cohort_external::search_cohorts("Cohortsearch", $syscontext, 'invalid');
628 $this->fail('Invalid parameter includes');
629 } catch (coding_exception $e) {
630 // All good.
631 }
632 }
66d6221f 633}