mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
tree_test.php
718 lines (644 loc) · 28.7 KB
/
tree_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the condition tree class and related logic.
*
* @package core_availability
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use core_availability\capability_checker;
use \core_availability\tree;
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for the condition tree class and related logic.
*
* @package core_availability
* @copyright 2014 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tree_testcase extends \advanced_testcase {
public function setUp() {
// Load the mock classes so they can be used.
require_once(__DIR__ . '/fixtures/mock_condition.php');
require_once(__DIR__ . '/fixtures/mock_info.php');
}
/**
* Tests constructing a tree with errors.
*/
public function test_construct_errors() {
try {
new tree('frog');
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('not object', $e->getMessage());
}
try {
new tree((object)array());
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('missing ->op', $e->getMessage());
}
try {
new tree((object)array('op' => '*'));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('unknown ->op', $e->getMessage());
}
try {
new tree((object)array('op' => '|'));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('missing ->show', $e->getMessage());
}
try {
new tree((object)array('op' => '|', 'show' => 0));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('->show not bool', $e->getMessage());
}
try {
new tree((object)array('op' => '&'));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('missing ->showc', $e->getMessage());
}
try {
new tree((object)array('op' => '&', 'showc' => 0));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('->showc not array', $e->getMessage());
}
try {
new tree((object)array('op' => '&', 'showc' => array(0)));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('->showc value not bool', $e->getMessage());
}
try {
new tree((object)array('op' => '|', 'show' => true));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('missing ->c', $e->getMessage());
}
try {
new tree((object)array('op' => '|', 'show' => true,
'c' => 'side'));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('->c not array', $e->getMessage());
}
try {
new tree((object)array('op' => '|', 'show' => true,
'c' => array(3)));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('child not object', $e->getMessage());
}
try {
new tree((object)array('op' => '|', 'show' => true,
'c' => array((object)array('type' => 'doesnotexist'))));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('Unknown condition type: doesnotexist', $e->getMessage());
}
try {
new tree((object)array('op' => '|', 'show' => true,
'c' => array((object)array())));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('missing ->op', $e->getMessage());
}
try {
new tree((object)array('op' => '&',
'c' => array((object)array('op' => '&', 'c' => array())),
'showc' => array(true, true)
));
$this->fail();
} catch (coding_exception $e) {
$this->assertContains('->c, ->showc mismatch', $e->getMessage());
}
}
/**
* Tests constructing a tree with plugin that does not exist (ignored).
*/
public function test_construct_ignore_missing_plugin() {
// Construct a tree with & combination of one condition that doesn't exist.
$tree = new tree(tree::get_root_json(array(
(object)array('type' => 'doesnotexist')), tree::OP_OR), true);
// Expected result is an empty tree with | condition, shown.
$this->assertEquals('+|()', (string)$tree);
}
/**
* Tests constructing a tree with subtrees using all available operators.
*/
public function test_construct_just_trees() {
$structure = tree::get_root_json(array(
tree::get_nested_json(array(), tree::OP_OR),
tree::get_nested_json(array(
tree::get_nested_json(array(), tree::OP_NOT_OR)), tree::OP_NOT_AND)),
tree::OP_AND, array(true, true));
$tree = new tree($structure);
$this->assertEquals('&(+|(),+!&(!|()))', (string)$tree);
}
/**
* Tests constructing tree using the mock plugin.
*/
public function test_construct_with_mock_plugin() {
$structure = tree::get_root_json(array(
self::mock(array('a' => true, 'm' => ''))), tree::OP_OR);
$tree = new tree($structure);
$this->assertEquals('+|({mock:y,})', (string)$tree);
}
/**
* Tests the check_available and get_result_information functions.
*/
public function test_check_available() {
global $USER;
// Setup.
$this->resetAfterTest();
$info = new \core_availability\mock_info();
$this->setAdminUser();
$information = '';
// No conditions.
$structure = tree::get_root_json(array(), tree::OP_OR);
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertTrue($available);
// One condition set to yes.
$structure->c = array(
self::mock(array('a' => true)));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertTrue($available);
// One condition set to no.
$structure->c = array(
self::mock(array('a' => false, 'm' => 'no')));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertEquals('SA: no', $information);
// Two conditions, OR, resolving as true.
$structure->c = array(
self::mock(array('a' => false, 'm' => 'no')),
self::mock(array('a' => true)));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertTrue($available);
$this->assertEquals('', $information);
// Two conditions, OR, resolving as false.
$structure->c = array(
self::mock(array('a' => false, 'm' => 'no')),
self::mock(array('a' => false, 'm' => 'way')));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertRegExp('~any of.*no.*way~', $information);
// Two conditions, OR, resolving as false, no display.
$structure->show = false;
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertEquals('', $information);
// Two conditions, AND, resolving as true.
$structure->op = '&';
unset($structure->show);
$structure->showc = array(true, true);
$structure->c = array(
self::mock(array('a' => true)),
self::mock(array('a' => true)));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertTrue($available);
// Two conditions, AND, one false.
$structure->c = array(
self::mock(array('a' => false, 'm' => 'wom')),
self::mock(array('a' => true, 'm' => '')));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertEquals('SA: wom', $information);
// Two conditions, AND, both false.
$structure->c = array(
self::mock(array('a' => false, 'm' => 'wom')),
self::mock(array('a' => false, 'm' => 'bat')));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertRegExp('~wom.*bat~', $information);
// Two conditions, AND, both false, show turned off for one. When
// show is turned off, that means if you don't have that condition
// you don't get to see anything at all.
$structure->showc[0] = false;
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertEquals('', $information);
$structure->showc[0] = true;
// Two conditions, NOT OR, both false.
$structure->op = '!|';
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertTrue($available);
// Two conditions, NOT OR, one true.
$structure->c[0]->a = true;
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertEquals('SA: !wom', $information);
// Two conditions, NOT OR, both true.
$structure->c[1]->a = true;
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertRegExp('~!wom.*!bat~', $information);
// Two conditions, NOT AND, both true.
$structure->op = '!&';
unset($structure->showc);
$structure->show = true;
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertRegExp('~any of.*!wom.*!bat~', $information);
// Two conditions, NOT AND, one true.
$structure->c[1]->a = false;
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertTrue($available);
// Nested NOT conditions; true.
$structure->c = array(
tree::get_nested_json(array(
self::mock(array('a' => true, 'm' => 'no'))), tree::OP_NOT_AND));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertTrue($available);
// Nested NOT conditions; false (note no ! in message).
$structure->c[0]->c[0]->a = false;
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertEquals('SA: no', $information);
// Nested condition groups, message test.
$structure->op = '|';
$structure->c = array(
tree::get_nested_json(array(
self::mock(array('a' => false, 'm' => '1')),
self::mock(array('a' => false, 'm' => '2'))
), tree::OP_AND),
self::mock(array('a' => false, 'm' => 3)));
list ($available, $information) = $this->get_available_results(
$structure, $info, $USER->id);
$this->assertFalse($available);
$this->assertRegExp('~<ul.*<ul.*<li.*1.*<li.*2.*</ul>.*<li.*3~', $information);
}
/**
* Shortcut function to check availability and also get information.
*
* @param stdClass $structure Tree structure
* @param \core_availability\info $info Location info
* @param int $userid User id
*/
protected function get_available_results($structure, \core_availability\info $info, $userid) {
$tree = new tree($structure);
$result = $tree->check_available(false, $info, true, $userid);
return array($result->is_available(), $tree->get_result_information($info, $result));
}
/**
* Tests the is_available_for_all() function.
*/
public function test_is_available_for_all() {
// Empty tree is always available.
$structure = tree::get_root_json(array(), tree::OP_OR);
$tree = new tree($structure);
$this->assertTrue($tree->is_available_for_all());
// Tree with normal item in it, not always available.
$structure->c[0] = (object)array('type' => 'mock');
$tree = new tree($structure);
$this->assertFalse($tree->is_available_for_all());
// OR tree with one always-available item.
$structure->c[1] = self::mock(array('all' => true));
$tree = new tree($structure);
$this->assertTrue($tree->is_available_for_all());
// AND tree with one always-available and one not.
$structure->op = '&';
$structure->showc = array(true, true);
unset($structure->show);
$tree = new tree($structure);
$this->assertFalse($tree->is_available_for_all());
// Test NOT conditions (items not always-available).
$structure->op = '!&';
$structure->show = true;
unset($structure->showc);
$tree = new tree($structure);
$this->assertFalse($tree->is_available_for_all());
// Test again with one item always-available for NOT mode.
$structure->c[1]->allnot = true;
$tree = new tree($structure);
$this->assertTrue($tree->is_available_for_all());
}
/**
* Tests the get_full_information() function.
*/
public function test_get_full_information() {
// Setup.
$info = new \core_availability\mock_info();
// No conditions.
$structure = tree::get_root_json(array(), tree::OP_OR);
$tree = new tree($structure);
$this->assertEquals('', $tree->get_full_information($info));
// Condition (normal and NOT).
$structure->c = array(
self::mock(array('m' => 'thing')));
$tree = new tree($structure);
$this->assertEquals('SA: [FULL]thing',
$tree->get_full_information($info));
$structure->op = '!&';
$tree = new tree($structure);
$this->assertEquals('SA: ![FULL]thing',
$tree->get_full_information($info));
// Complex structure.
$structure->op = '|';
$structure->c = array(
tree::get_nested_json(array(
self::mock(array('m' => '1')),
self::mock(array('m' => '2'))), tree::OP_AND),
self::mock(array('m' => 3)));
$tree = new tree($structure);
$this->assertRegExp('~<ul.*<ul.*<li.*1.*<li.*2.*</ul>.*<li.*3~',
$tree->get_full_information($info));
// Test intro messages before list. First, OR message.
$structure->c = array(
self::mock(array('m' => '1')),
self::mock(array('m' => '2'))
);
$tree = new tree($structure);
$this->assertRegExp('~Not available unless any of:.*<ul>~',
$tree->get_full_information($info));
// Now, OR message when not shown.
$structure->show = false;
$tree = new tree($structure);
$this->assertRegExp('~hidden.*<ul>~',
$tree->get_full_information($info));
// AND message.
$structure->op = '&';
unset($structure->show);
$structure->showc = array(false, false);
$tree = new tree($structure);
$this->assertRegExp('~Not available unless:.*<ul>~',
$tree->get_full_information($info));
// Hidden markers on items.
$this->assertRegExp('~1.*hidden.*2.*hidden~',
$tree->get_full_information($info));
// Hidden markers on child tree and items.
$structure->c[1] = tree::get_nested_json(array(
self::mock(array('m' => '2')),
self::mock(array('m' => '3'))), tree::OP_AND);
$tree = new tree($structure);
$this->assertRegExp('~1.*hidden.*All of \(hidden.*2.*3~',
$tree->get_full_information($info));
$structure->c[1]->op = '|';
$tree = new tree($structure);
$this->assertRegExp('~1.*hidden.*Any of \(hidden.*2.*3~',
$tree->get_full_information($info));
// Hidden markers on single-item display, AND and OR.
$structure->showc = array(false);
$structure->c = array(
self::mock(array('m' => '1'))
);
$tree = new tree($structure);
$this->assertRegExp('~1.*hidden~',
$tree->get_full_information($info));
unset($structure->showc);
$structure->show = false;
$structure->op = '|';
$tree = new tree($structure);
$this->assertRegExp('~1.*hidden~',
$tree->get_full_information($info));
// Hidden marker if single item is tree.
$structure->c[0] = tree::get_nested_json(array(
self::mock(array('m' => '1')),
self::mock(array('m' => '2'))), tree::OP_AND);
$tree = new tree($structure);
$this->assertRegExp('~Not available \(hidden.*1.*2~',
$tree->get_full_information($info));
// Single item tree containing single item.
unset($structure->c[0]->c[1]);
$tree = new tree($structure);
$this->assertRegExp('~SA.*1.*hidden~',
$tree->get_full_information($info));
}
/**
* Tests the get_all_children() function.
*/
public function test_get_all_children() {
// Create a tree with nothing in.
$structure = tree::get_root_json(array(), tree::OP_OR);
$tree1 = new tree($structure);
// Create second tree with complex structure.
$structure->c = array(
tree::get_nested_json(array(
self::mock(array('m' => '1')),
self::mock(array('m' => '2'))
), tree::OP_OR),
self::mock(array('m' => 3)));
$tree2 = new tree($structure);
// Check list of conditions from both trees.
$this->assertEquals(array(), $tree1->get_all_children('core_availability\condition'));
$result = $tree2->get_all_children('core_availability\condition');
$this->assertEquals(3, count($result));
$this->assertEquals('{mock:n,1}', (string)$result[0]);
$this->assertEquals('{mock:n,2}', (string)$result[1]);
$this->assertEquals('{mock:n,3}', (string)$result[2]);
// Check specific type, should give same results.
$result2 = $tree2->get_all_children('availability_mock\condition');
$this->assertEquals($result, $result2);
}
/**
* Tests the update_dependency_id() function.
*/
public function test_update_dependency_id() {
// Create tree with structure of 3 mocks.
$structure = tree::get_root_json(array(
tree::get_nested_json(array(
self::mock(array('table' => 'frogs', 'id' => 9)),
self::mock(array('table' => 'zombies', 'id' => 9))
)),
self::mock(array('table' => 'frogs', 'id' => 9))));
// Get 'before' value.
$tree = new tree($structure);
$before = $tree->save();
// Try replacing a table or id that isn't used.
$this->assertFalse($tree->update_dependency_id('toads', 9, 13));
$this->assertFalse($tree->update_dependency_id('frogs', 7, 8));
$this->assertEquals($before, $tree->save());
// Replace the zombies one.
$this->assertTrue($tree->update_dependency_id('zombies', 9, 666));
$after = $tree->save();
$this->assertEquals(666, $after->c[0]->c[1]->id);
// And the frogs one.
$this->assertTrue($tree->update_dependency_id('frogs', 9, 3));
$after = $tree->save();
$this->assertEquals(3, $after->c[0]->c[0]->id);
$this->assertEquals(3, $after->c[1]->id);
}
/**
* Tests the filter_users function.
*/
public function test_filter_users() {
$info = new \core_availability\mock_info();
$checker = new capability_checker($info->get_context());
// Don't need to create real users in database, just use these ids.
$users = array(1 => null, 2 => null, 3 => null);
// Test basic tree with one condition that doesn't filter.
$structure = tree::get_root_json(array(self::mock(array())));
$tree = new tree($structure);
$result = $tree->filter_user_list($users, false, $info, $checker);
ksort($result);
$this->assertEquals(array(1, 2, 3), array_keys($result));
// Now a tree with one condition that filters.
$structure = tree::get_root_json(array(self::mock(array('filter' => array(2, 3)))));
$tree = new tree($structure);
$result = $tree->filter_user_list($users, false, $info, $checker);
ksort($result);
$this->assertEquals(array(2, 3), array_keys($result));
// Tree with two conditions that both filter (|).
$structure = tree::get_root_json(array(
self::mock(array('filter' => array(3))),
self::mock(array('filter' => array(1)))), tree::OP_OR);
$tree = new tree($structure);
$result = $tree->filter_user_list($users, false, $info, $checker);
ksort($result);
$this->assertEquals(array(1, 3), array_keys($result));
// Tree with two condition that both filter (&).
$structure = tree::get_root_json(array(
self::mock(array('filter' => array(2, 3))),
self::mock(array('filter' => array(1, 2)))));
$tree = new tree($structure);
$result = $tree->filter_user_list($users, false, $info, $checker);
ksort($result);
$this->assertEquals(array(2), array_keys($result));
// Tree with child tree with NOT condition.
$structure = tree::get_root_json(array(
tree::get_nested_json(array(
self::mock(array('filter' => array(1)))), tree::OP_NOT_AND)));
$tree = new tree($structure);
$result = $tree->filter_user_list($users, false, $info, $checker);
ksort($result);
$this->assertEquals(array(2, 3), array_keys($result));
}
/**
* Tests the get_json methods in tree (which are mainly for use in testing
* but might be used elsewhere).
*/
public function test_get_json() {
// Create a simple child object (fake).
$child = (object)array('type' => 'fake');
$childstr = json_encode($child);
// Minimal case.
$this->assertEquals(
(object)array('op' => '&', 'c' => array()),
tree::get_nested_json(array()));
// Children and different operator.
$this->assertEquals(
(object)array('op' => '|', 'c' => array($child, $child)),
tree::get_nested_json(array($child, $child), tree::OP_OR));
// Root empty.
$this->assertEquals('{"op":"&","c":[],"showc":[]}',
json_encode(tree::get_root_json(array(), tree::OP_AND)));
// Root with children (multi-show operator).
$this->assertEquals('{"op":"&","c":[' . $childstr . ',' . $childstr .
'],"showc":[true,true]}',
json_encode(tree::get_root_json(array($child, $child), tree::OP_AND)));
// Root with children (single-show operator).
$this->assertEquals('{"op":"|","c":[' . $childstr . ',' . $childstr .
'],"show":true}',
json_encode(tree::get_root_json(array($child, $child), tree::OP_OR)));
// Root with children (specified show boolean).
$this->assertEquals('{"op":"&","c":[' . $childstr . ',' . $childstr .
'],"showc":[false,false]}',
json_encode(tree::get_root_json(array($child, $child), tree::OP_AND, false)));
// Root with children (specified show array).
$this->assertEquals('{"op":"&","c":[' . $childstr . ',' . $childstr .
'],"showc":[true,false]}',
json_encode(tree::get_root_json(array($child, $child), tree::OP_AND, array(true, false))));
}
/**
* Tests get_user_list_sql.
*/
public function test_get_user_list_sql() {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator();
// Create a test course with 2 groups and users in each combination of them.
$course = $generator->create_course();
$group1 = $generator->create_group(array('courseid' => $course->id));
$group2 = $generator->create_group(array('courseid' => $course->id));
$userin1 = $generator->create_user();
$userin2 = $generator->create_user();
$userinboth = $generator->create_user();
$userinneither = $generator->create_user();
$studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
foreach (array($userin1, $userin2, $userinboth, $userinneither) as $user) {
$generator->enrol_user($user->id, $course->id, $studentroleid);
}
groups_add_member($group1, $userin1);
groups_add_member($group2, $userin2);
groups_add_member($group1, $userinboth);
groups_add_member($group2, $userinboth);
$info = new \core_availability\mock_info($course);
// Tree with single group condition.
$tree = new tree(tree::get_root_json(array(
\availability_group\condition::get_json($group1->id)
)));
list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals(array($userin1->id, $userinboth->id), $result);
// Tree with 'AND' of both group conditions.
$tree = new tree(tree::get_root_json(array(
\availability_group\condition::get_json($group1->id),
\availability_group\condition::get_json($group2->id)
)));
list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals(array($userinboth->id), $result);
// Tree with 'AND' of both group conditions.
$tree = new tree(tree::get_root_json(array(
\availability_group\condition::get_json($group1->id),
\availability_group\condition::get_json($group2->id)
), tree::OP_OR));
list($sql, $params) = $tree->get_user_list_sql(false, $info, false);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals(array($userin1->id, $userin2->id, $userinboth->id), $result);
// Check with flipped logic (NOT above level of tree).
list($sql, $params) = $tree->get_user_list_sql(true, $info, false);
$result = $DB->get_fieldset_sql($sql, $params);
sort($result);
$this->assertEquals(array($userinneither->id), $result);
}
/**
* Utility function to build the PHP structure representing a mock condition.
*
* @param array $params Mock parameters
* @return \stdClass Structure object
*/
protected static function mock(array $params) {
$params['type'] = 'mock';
return (object)$params;
}
}