weekly release 4.5dev
[moodle.git] / privacy / tests / contextlist_base_test.php
blob5f9797520403584f888ee4bc40dd7bc8975f4dac
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
37 * @coversDefaultClass \core_privacy\local\request\contextlist_base
39 class contextlist_base_test extends advanced_testcase {
40 /**
41 * Ensure that get_contextids returns the list of unique contextids.
43 * @dataProvider get_contextids_provider
44 * @param array $input List of context IDs
45 * @param array $expected list of contextids
46 * @param int $count Expected count
47 * @covers ::get_contextids
49 public function test_get_contextids($input, $expected, $count): void {
50 $uit = new test_contextlist_base();
51 $uit->set_contextids($input);
53 $result = $uit->get_contextids();
54 $this->assertCount($count, $result);
56 // Note: Array order is not guaranteed and should not matter.
57 foreach ($expected as $contextid) {
58 $this->assertNotFalse(array_search($contextid, $result));
62 /**
63 * Provider for the list of contextids.
65 * @return array
67 public function get_contextids_provider() {
68 return [
69 'basic' => [
70 [1, 2, 3, 4, 5],
71 [1, 2, 3, 4, 5],
74 'duplicates' => [
75 [1, 1, 2, 2, 3, 4, 5],
76 [1, 2, 3, 4, 5],
79 'Mixed order with duplicates' => [
80 [5, 4, 2, 5, 4, 1, 3, 4, 1, 5, 5, 5, 2, 4, 1, 2],
81 [1, 2, 3, 4, 5],
87 /**
88 * Ensure that get_contexts returns the correct list of contexts.
90 * @covers ::get_contexts
92 public function test_get_contexts(): void {
93 global $DB;
95 $contexts = [];
96 $contexts[] = \context_system::instance();
97 $contexts[] = \context_user::instance(\core_user::get_user_by_username('admin')->id);
99 $ids = [];
100 foreach ($contexts as $context) {
101 $ids[] = $context->id;
104 $uit = new test_contextlist_base();
105 $uit->set_contextids($ids);
107 $result = $uit->get_contexts();
108 $this->assertCount(count($contexts), $result);
109 foreach ($contexts as $context) {
110 $this->assertNotFalse(array_search($context, $result));
115 * Ensure that the contextlist_base is countable.
117 * @dataProvider get_contextids_provider
118 * @param array $input List of context IDs
119 * @param array $expected list of contextids
120 * @param int $count Expected count
121 * @covers ::count
123 public function test_countable($input, $expected, $count): void {
124 $uit = new test_contextlist_base();
125 $uit->set_contextids($input);
127 $this->assertCount($count, $uit);
131 * Ensure that the contextlist_base iterates over the set of contexts.
133 * @covers ::current
134 * @covers ::key
135 * @covers ::next
136 * @covers ::rewind
137 * @covers ::valid
139 public function test_context_iteration(): void {
140 global $DB;
142 $allcontexts = $DB->get_records('context');
143 $contexts = [];
144 foreach ($allcontexts as $context) {
145 $contexts[] = \context::instance_by_id($context->id);
148 $uit = new test_contextlist_base();
149 $uit->set_contextids(array_keys($allcontexts));
151 foreach ($uit as $key => $context) {
152 $this->assertNotFalse(array_search($context, $contexts));
157 * Test that deleting a context results in current returning nothing.
159 * @covers ::current
161 public function test_current_context_one_context(): void {
162 global $DB;
164 $this->resetAfterTest();
166 $data = (object) [
167 'contextlevel' => CONTEXT_BLOCK,
168 'instanceid' => 45,
169 'path' => '1/5/67/107',
170 'depth' => 4
173 $contextid = $DB->insert_record('context', $data);
175 $contextbase = new test_contextlist_base();
176 $contextbase->set_contextids([$contextid]);
177 $this->assertCount(1, $contextbase);
178 $currentcontext = $contextbase->current();
179 $this->assertEquals($contextid, $currentcontext->id);
180 $DB->delete_records('context', ['id' => $contextid]);
181 context_helper::reset_caches();
182 $this->assertEmpty($contextbase->current());
186 * Test that deleting a context results in the next record being returned.
188 * @covers ::current
190 public function test_current_context_two_contexts(): void {
191 global $DB;
193 $this->resetAfterTest();
195 $data = (object) [
196 'contextlevel' => CONTEXT_BLOCK,
197 'instanceid' => 45,
198 'path' => '1/5/67/107',
199 'depth' => 4
202 $contextid1 = $DB->insert_record('context', $data);
204 $data = (object) [
205 'contextlevel' => CONTEXT_BLOCK,
206 'instanceid' => 47,
207 'path' => '1/5/54/213',
208 'depth' => 4
211 $contextid2 = $DB->insert_record('context', $data);
213 $contextbase = new test_contextlist_base();
214 $contextbase->set_contextids([$contextid1, $contextid2]);
215 $this->assertCount(2, $contextbase);
216 $DB->delete_records('context', ['id' => $contextid1]);
217 context_helper::reset_caches();
218 // Current should return context 2.
219 $this->assertEquals($contextid2, $contextbase->current()->id);
223 * Test that if there are no non-deleted contexts that nothing is returned.
225 * @covers ::get_contexts
227 public function test_get_contexts_all_deleted(): void {
228 global $DB;
230 $this->resetAfterTest();
232 $data = (object) [
233 'contextlevel' => CONTEXT_BLOCK,
234 'instanceid' => 45,
235 'path' => '1/5/67/107',
236 'depth' => 4
239 $contextid = $DB->insert_record('context', $data);
241 $contextbase = new test_contextlist_base();
242 $contextbase->set_contextids([$contextid]);
243 $this->assertCount(1, $contextbase);
244 $DB->delete_records('context', ['id' => $contextid]);
245 context_helper::reset_caches();
246 $this->assertEmpty($contextbase->get_contexts());
250 * Test that get_contexts() returns only active contexts.
252 * @covers ::get_contexts
254 public function test_get_contexts_one_deleted(): void {
255 global $DB;
257 $this->resetAfterTest();
259 $data = (object) [
260 'contextlevel' => CONTEXT_BLOCK,
261 'instanceid' => 45,
262 'path' => '1/5/67/107',
263 'depth' => 4
266 $contextid1 = $DB->insert_record('context', $data);
268 $data = (object) [
269 'contextlevel' => CONTEXT_BLOCK,
270 'instanceid' => 47,
271 'path' => '1/5/54/213',
272 'depth' => 4
275 $contextid2 = $DB->insert_record('context', $data);
277 $contextbase = new test_contextlist_base();
278 $contextbase->set_contextids([$contextid1, $contextid2]);
279 $this->assertCount(2, $contextbase);
280 $DB->delete_records('context', ['id' => $contextid1]);
281 context_helper::reset_caches();
282 $contexts = $contextbase->get_contexts();
283 $this->assertCount(1, $contexts);
284 $context = array_shift($contexts);
285 $this->assertEquals($contextid2, $context->id);
290 * A test class extending the contextlist_base allowing setting of the
291 * contextids.
293 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
294 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
296 class test_contextlist_base extends contextlist_base {
298 * Set the contextids for the test class.
300 * @param int[] $contexids The list of contextids to use.
302 public function set_contextids(array $contextids) {
303 parent::set_contextids($contextids);