Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
13
//
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/>.
16
 
17
/**
18
 * Unit Tests for the Moodle Content Writer.
19
 *
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
24
 */
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
 
30
use \core_privacy\local\request\writer;
31
 
32
/**
33
 * Tests for the \core_privacy API's moodle_content_writer functionality.
34
 *
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.
38
 *
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
42
 */
1441 ariadna 43
final class writer_test extends advanced_testcase {
1 efrain 44
    /**
45
     * Ensure that the writer is cleared away as appropriate after each
46
     * test.
47
     */
48
    public function tearDown(): void {
49
        writer::reset();
1441 ariadna 50
        parent::tearDown();
1 efrain 51
    }
52
 
53
    /**
54
     * Test that calling with_context multiple times will return the same write instance.
55
     *
56
     * @covers ::with_context
57
     */
11 efrain 58
    public function test_with_context(): void {
1 efrain 59
        $writer = writer::with_context(\context_system::instance());
60
 
61
        $this->assertSame($writer, writer::with_context(\context_system::instance()));
62
    }
63
 
64
    /**
65
     * Test that calling with_context multiple times will return the same write instance.
66
     *
67
     * @covers ::with_context
68
     */
11 efrain 69
    public function test_with_context_different_context_same_instance(): void {
1 efrain 70
        $writer = writer::with_context(\context_system::instance());
71
 
72
        $this->assertSame($writer, writer::with_context(\context_user::instance(\core_user::get_user_by_username('admin')->id)));
73
    }
74
 
75
    /**
76
     * Test that calling writer::reset() causes a new copy of the writer to be returned.
77
     *
78
     * @covers ::reset
79
     */
11 efrain 80
    public function test_reset(): void {
1 efrain 81
        $writer = writer::with_context(\context_system::instance());
82
        writer::reset();
83
 
84
        $this->assertNotSame($writer, writer::with_context(\context_system::instance()));
85
    }
86
 
87
    /**
88
     * Test that the export_user_preference calls the writer against the system context.
89
     *
90
     * @covers ::export_user_preference
91
     */
11 efrain 92
    public function test_export_user_preference_sets_system_context(): void {
1 efrain 93
        $writer = writer::with_context(\context_user::instance(\core_user::get_user_by_username('admin')->id));
94
 
95
        writer::export_user_preference('core_test', 'key', 'value', 'description');
96
 
97
        $this->assertSame(\context_system::instance(), $writer->get_current_context());
98
    }
99
}