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 Moodle Content Writer.
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\writer
;
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
{
45 * Ensure that the writer is cleared away as appropriate after each
48 public function tearDown(): void
{
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()));
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
)));
75 * Test that calling writer::reset() causes a new copy of the writer to be returned.
79 public function test_reset() {
80 $writer = writer
::with_context(\context_system
::instance());
83 $this->assertNotSame($writer, writer
::with_context(\context_system
::instance()));
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());