2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit Tests for the abstract contextlist Class
20 * @package core_privacy
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();
30 use \core_privacy\local\request\contextlist_base
;
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
{
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));
63 * Provider for the list of contextids.
67 public function get_contextids_provider() {
75 [1, 1, 2, 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],
88 * Ensure that get_contexts returns the correct list of contexts.
90 * @covers ::get_contexts
92 public function test_get_contexts(): void
{
96 $contexts[] = \context_system
::instance();
97 $contexts[] = \context_user
::instance(\core_user
::get_user_by_username('admin')->id
);
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
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.
139 public function test_context_iteration(): void
{
142 $allcontexts = $DB->get_records('context');
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.
161 public function test_current_context_one_context(): void
{
164 $this->resetAfterTest();
167 'contextlevel' => CONTEXT_BLOCK
,
169 'path' => '1/5/67/107',
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.
190 public function test_current_context_two_contexts(): void
{
193 $this->resetAfterTest();
196 'contextlevel' => CONTEXT_BLOCK
,
198 'path' => '1/5/67/107',
202 $contextid1 = $DB->insert_record('context', $data);
205 'contextlevel' => CONTEXT_BLOCK
,
207 'path' => '1/5/54/213',
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
{
230 $this->resetAfterTest();
233 'contextlevel' => CONTEXT_BLOCK
,
235 'path' => '1/5/67/107',
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
{
257 $this->resetAfterTest();
260 'contextlevel' => CONTEXT_BLOCK
,
262 'path' => '1/5/67/107',
266 $contextid1 = $DB->insert_record('context', $data);
269 'contextlevel' => CONTEXT_BLOCK
,
271 'path' => '1/5/54/213',
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
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);