Automatically generated installer lang files
[moodle.git] / privacy / tests / writer_test.php
blob47960afd270d3dd076d8040449200651a2c7ae72
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 Moodle Content Writer.
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\writer;
32 /**
33 * Tests for the \core_privacy API's moodle_content_writer functionality.
35 * Note: The \core_privacy\tests\request\content_writer will be used for these tests.
36 * This content writer has additional sugar methods for fetching infromation which are not part of the standard
37 * content_writer interface.
39 * @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 * @coversDefaultClass \core_privacy\local\request\writer
43 class writer_test extends advanced_testcase {
44 /**
45 * Ensure that the writer is cleared away as appropriate after each
46 * test.
48 public function tearDown(): void {
49 writer::reset();
52 /**
53 * Test that calling with_context multiple times will return the same write instance.
55 * @covers ::with_context
57 public function test_with_context() {
58 $writer = writer::with_context(\context_system::instance());
60 $this->assertSame($writer, writer::with_context(\context_system::instance()));
63 /**
64 * Test that calling with_context multiple times will return the same write instance.
66 * @covers ::with_context
68 public function test_with_context_different_context_same_instance() {
69 $writer = writer::with_context(\context_system::instance());
71 $this->assertSame($writer, writer::with_context(\context_user::instance(\core_user::get_user_by_username('admin')->id)));
74 /**
75 * Test that calling writer::reset() causes a new copy of the writer to be returned.
77 * @covers ::reset
79 public function test_reset() {
80 $writer = writer::with_context(\context_system::instance());
81 writer::reset();
83 $this->assertNotSame($writer, writer::with_context(\context_system::instance()));
86 /**
87 * Test that the export_user_preference calls the writer against the system context.
89 * @covers ::export_user_preference
91 public function test_export_user_preference_sets_system_context() {
92 $writer = writer::with_context(\context_user::instance(\core_user::get_user_by_username('admin')->id));
94 writer::export_user_preference('core_test', 'key', 'value', 'description');
96 $this->assertSame(\context_system::instance(), $writer->get_current_context());