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 privacy legacy polyfill for portfolio.
19 *
20 * @package     core_privacy
21 * @category    test
22 * @copyright   2018 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28/**
29 * Unit tests for the Portfolio API's privacy legacy_polyfill.
30 *
31 * @copyright   2018 Jake Dallimore <jrhdallimore@gmail.com>
32 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34class core_portfolio_privacy_legacy_polyfill_test extends advanced_testcase {
35    /**
36     * Test that the core_portfolio\privacy\legacy_polyfill works and that the static _export_portfolio_user_data can be called.
37     */
38    public function test_export_portfolio_user_data() {
39        $userid = 476;
40        $context = context_system::instance();
41
42        $mock = $this->createMock(test_portfolio_legacy_polyfill_mock_wrapper::class);
43        $mock->expects($this->once())
44            ->method('get_return_value')
45            ->with('_export_portfolio_user_data', [$userid, $context, [], []]);
46
47        test_legacy_polyfill_portfolio_provider::$mock = $mock;
48        test_legacy_polyfill_portfolio_provider::export_portfolio_user_data($userid, $context, [], []);
49    }
50
51    /**
52     * Test for _get_metadata shim.
53     */
54    public function test_get_metadata() {
55        $collection = new \core_privacy\local\metadata\collection('core_portfolio');
56        $this->assertSame($collection, test_legacy_polyfill_portfolio_provider::get_metadata($collection));
57    }
58
59    /**
60     * Test the _delete_portfolio_for_context shim.
61     */
62    public function test_delete_portfolio_for_context() {
63        $context = context_system::instance();
64
65        $mock = $this->createMock(test_portfolio_legacy_polyfill_mock_wrapper::class);
66        $mock->expects($this->once())
67            ->method('get_return_value')
68            ->with('_delete_portfolio_for_context', [$context]);
69
70        test_legacy_polyfill_portfolio_provider::$mock = $mock;
71        test_legacy_polyfill_portfolio_provider::delete_portfolio_for_context($context);
72    }
73
74    /**
75     * Test the _delete_portfolio_for_context shim.
76     */
77    public function test_delete_portfolio_for_user() {
78        $userid = 696;
79        $context = \context_system::instance();
80
81        $mock = $this->createMock(test_portfolio_legacy_polyfill_mock_wrapper::class);
82        $mock->expects($this->once())
83            ->method('get_return_value')
84            ->with('_delete_portfolio_for_user', [$userid, $context]);
85
86        test_legacy_polyfill_portfolio_provider::$mock = $mock;
87        test_legacy_polyfill_portfolio_provider::delete_portfolio_for_user($userid, $context);
88    }
89}
90
91/**
92 * Legacy polyfill test class for the portfolio_provider.
93 *
94 * @copyright   2018 Jake Dallimore <jrhdallimore@gmail.com>
95 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
96 */
97class test_legacy_polyfill_portfolio_provider implements
98    \core_privacy\local\metadata\provider,
99    \core_portfolio\privacy\portfolio_provider {
100
101    use \core_portfolio\privacy\legacy_polyfill;
102    use \core_privacy\local\legacy_polyfill;
103
104    /**
105     * @var test_legacy_polyfill_portfolio_provider $mock.
106     */
107    public static $mock = null;
108
109    /**
110     * Export all user data for the portfolio plugin.
111     *
112     * @param int $userid
113     * @param context $context
114     * @param array $subcontext
115     * @param array $linkarray
116     */
117    protected static function _export_portfolio_user_data($userid, \context $context, array $subcontext, array $linkarray) {
118        static::$mock->get_return_value(__FUNCTION__, func_get_args());
119    }
120
121    /**
122     * Deletes all user data for the given context.
123     *
124     * @param context $context
125     */
126    protected static function _delete_portfolio_for_context(\context $context) {
127        static::$mock->get_return_value(__FUNCTION__, func_get_args());
128    }
129
130    /**
131     * Delete personal data for the given user and context.
132     *
133     * @param int $userid
134     * @param context $context
135     */
136    protected static function _delete_portfolio_for_user($userid, \context $context) {
137        static::$mock->get_return_value(__FUNCTION__, func_get_args());
138    }
139
140    /**
141     * Returns metadata about this plugin.
142     *
143     * @param   \core_privacy\local\metadata\collection $collection The initialised collection to add items to.
144     * @return  \core_privacy\local\metadata\collection     A listing of user data stored through this system.
145     */
146    protected static function _get_metadata(\core_privacy\local\metadata\collection $collection) {
147        return $collection;
148    }
149}
150
151/**
152 * Called inside the polyfill methods in the test polyfill provider, allowing us to ensure these are called with correct params.
153 *
154 * @copyright   2018 Jake Dallimore <jrhdallimore@gmail.com>
155 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
156 */
157class test_portfolio_legacy_polyfill_mock_wrapper {
158    /**
159     * Get the return value for the specified item.
160     */
161    public function get_return_value() {
162    }
163}
164