MDL-62463 mod_glossary: Fix SQL query
[moodle.git] / privacy / tests / contextlist_base_test.php
blobe60dcbcb406f20af745838a82f1031af550ee735
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.
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 the abstract contextlist Class
20 * @package core_privacy
21 * @category test
22 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 use \core_privacy\local\request\contextlist_base;
32 /**
33 * Tests for the \core_privacy API's contextlist base functionality.
35 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class contextlist_base_test extends advanced_testcase {
39 /**
40 * Ensure that get_contextids returns the list of unique contextids.
42 * @dataProvider get_contextids_provider
43 * @param array $input List of context IDs
44 * @param array $expected list of contextids
45 * @param int $count Expected count
47 public function test_get_contextids($input, $expected, $count) {
48 $uit = new test_contextlist_base();
49 $uit->set_contextids($input);
51 $result = $uit->get_contextids();
52 $this->assertCount($count, $result);
54 // Note: Array order is not guaranteed and should not matter.
55 foreach ($expected as $contextid) {
56 $this->assertNotFalse(array_search($contextid, $result));
60 /**
61 * Provider for the list of contextids.
63 * @return array
65 public function get_contextids_provider() {
66 return [
67 'basic' => [
68 [1, 2, 3, 4, 5],
69 [1, 2, 3, 4, 5],
72 'duplicates' => [
73 [1, 1, 2, 2, 3, 4, 5],
74 [1, 2, 3, 4, 5],
77 'Mixed order with duplicates' => [
78 [5, 4, 2, 5, 4, 1, 3, 4, 1, 5, 5, 5, 2, 4, 1, 2],
79 [1, 2, 3, 4, 5],
85 /**
86 * Ensure that get_contexts returns the correct list of contexts.
88 public function test_get_contexts() {
89 global $DB;
91 $contexts = [];
92 $contexts[] = \context_system::instance();
93 $contexts[] = \context_user::instance(\core_user::get_user_by_username('admin')->id);
95 $ids = [];
96 foreach ($contexts as $context) {
97 $ids[] = $context->id;
100 $uit = new test_contextlist_base();
101 $uit->set_contextids($ids);
103 $result = $uit->get_contexts();
104 $this->assertCount(count($contexts), $result);
105 foreach ($contexts as $context) {
106 $this->assertNotFalse(array_search($context, $result));
111 * Ensure that the contextlist_base is countable.
113 * @dataProvider get_contextids_provider
114 * @param array $input List of context IDs
115 * @param array $expected list of contextids
116 * @param int $count Expected count
118 public function test_countable($input, $expected, $count) {
119 $uit = new test_contextlist_base();
120 $uit->set_contextids($input);
122 $this->assertCount($count, $uit);
126 * Ensure that the contextlist_base iterates over the set of contexts.
128 public function test_context_iteration() {
129 global $DB;
131 $allcontexts = $DB->get_records('context');
132 $contexts = [];
133 foreach ($allcontexts as $context) {
134 $contexts[] = \context::instance_by_id($context->id);
137 $uit = new test_contextlist_base();
138 $uit->set_contextids(array_keys($allcontexts));
140 foreach ($uit as $key => $context) {
141 $this->assertNotFalse(array_search($context, $contexts));
147 * A test class extending the contextlist_base allowing setting of the
148 * contextids.
150 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
151 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
153 class test_contextlist_base extends contextlist_base {
155 * Set the contextids for the test class.
157 * @param int[] $contexids The list of contextids to use.
159 public function set_contextids(array $contextids) {
160 parent::set_contextids($contextids);