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 gradingform.
19 *
20 * @package     core_grading
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 Grading 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 gradeform_privacy_legacy_polyfill_test extends advanced_testcase {
35    /**
36     * Test that the core_grading\privacy\legacy_polyfill works and that the static _export_gradingform_instance_data can be called.
37     */
38    public function test_export_gradingform_instance_data() {
39        $context = context_system::instance();
40
41        $mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class);
42        $mock->expects($this->once())
43            ->method('get_return_value')
44            ->with('_export_gradingform_instance_data', [$context, 3, ['subcontext']]);
45
46        test_legacy_polyfill_gradingform_provider::$mock = $mock;
47        test_legacy_polyfill_gradingform_provider::export_gradingform_instance_data($context, 3, ['subcontext']);
48    }
49
50    /**
51     * Test for _get_metadata shim.
52     */
53    public function test_get_metadata() {
54        $collection = new \core_privacy\local\metadata\collection('core_gradingform');
55        $this->assertSame($collection, test_legacy_polyfill_gradingform_provider::get_metadata($collection));
56    }
57
58    /**
59     * Test the _delete_gradingform_for_instances shim.
60     */
61    public function test_delete_gradingform_for_instances() {
62        $context = context_system::instance();
63
64        $mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class);
65        $mock->expects($this->once())
66            ->method('get_return_value')
67            ->with('_delete_gradingform_for_instances', [[3, 17]]);
68
69        test_legacy_polyfill_gradingform_provider::$mock = $mock;
70        test_legacy_polyfill_gradingform_provider::delete_gradingform_for_instances([3, 17]);
71    }
72
73    /**
74     * Test the __get_gradingform_export_data shim.
75     */
76    public function test_get_gradingform_export_data() {
77        $userid = 476;
78        $context = context_system::instance();
79
80        $mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class);
81        $mock->expects($this->once())
82            ->method('get_return_value')
83            ->with('_get_gradingform_export_data', [$context, (object)[], $userid]);
84
85        test_legacy_polyfill_gradingform_provider::$mock = $mock;
86        test_legacy_polyfill_gradingform_provider::get_gradingform_export_data($context, (object)[], $userid);
87        $this->assertDebuggingCalled();
88    }
89
90    /**
91     * Test the _delete_gradingform_for_context shim.
92     */
93    public function test_delete_gradingform_for_context() {
94        $context = context_system::instance();
95
96        $mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class);
97        $mock->expects($this->once())
98            ->method('get_return_value')
99            ->with('_delete_gradingform_for_context', [$context]);
100
101        test_legacy_polyfill_gradingform_provider::$mock = $mock;
102        test_legacy_polyfill_gradingform_provider::delete_gradingform_for_context($context);
103        $this->assertDebuggingCalled();
104    }
105
106    /**
107     * Test the _delete_gradingform_for_userid shim.
108     */
109    public function test_delete_gradingform_for_user() {
110        $userid = 696;
111        $context = \context_system::instance();
112
113        $mock = $this->createMock(test_gradingform_legacy_polyfill_mock_wrapper::class);
114        $mock->expects($this->once())
115            ->method('get_return_value')
116            ->with('_delete_gradingform_for_userid', [$userid, $context]);
117
118        test_legacy_polyfill_gradingform_provider::$mock = $mock;
119        test_legacy_polyfill_gradingform_provider::delete_gradingform_for_userid($userid, $context);
120        $this->assertDebuggingCalled();
121    }
122}
123
124/**
125 * Legacy polyfill test class for the gradingform_provider.
126 *
127 * @copyright   2018 Jake Dallimore <jrhdallimore@gmail.com>
128 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
129 */
130class test_legacy_polyfill_gradingform_provider implements
131    \core_privacy\local\metadata\provider,
132    \core_grading\privacy\gradingform_provider,
133    \core_grading\privacy\gradingform_provider_v2 {
134
135    use \core_grading\privacy\gradingform_legacy_polyfill;
136    use \core_privacy\local\legacy_polyfill;
137
138    /**
139     * @var test_legacy_polyfill_gradingform_provider $mock.
140     */
141    public static $mock = null;
142
143    /**
144     * Export user data relating to an instance ID.
145     *
146     * @param  \context $context Context to use with the export writer.
147     * @param  int $instanceid The instance ID to export data for.
148     * @param  array $subcontext The directory to export this data to.
149     */
150    protected static function _export_gradingform_instance_data(\context $context, $instanceid, $subcontext) {
151        static::$mock->get_return_value(__FUNCTION__, func_get_args());
152    }
153
154    /**
155     * Deletes all user data related to the provided instance IDs.
156     *
157     * @param  array  $instanceids The instance IDs to delete information from.
158     */
159    protected static function _delete_gradingform_for_instances($instanceids) {
160        static::$mock->get_return_value(__FUNCTION__, func_get_args());
161    }
162
163    /**
164     * Returns metadata about this plugin.
165     *
166     * @param   \core_privacy\local\metadata\collection $collection The initialised collection to add items to.
167     * @return  \core_privacy\local\metadata\collection     A listing of user data stored through this system.
168     */
169    protected static function _get_metadata(\core_privacy\local\metadata\collection $collection) {
170        return $collection;
171    }
172
173    /**
174     * This method is used to export any user data this sub-plugin has using the object to get the context and userid.
175     *
176     * @deprecated Since Moodle 3.6 MDL-62535 Please use the methods in the gradingform_provider_v2 interface.
177     * @todo MDL-63167 remove this method.
178     *
179     * @param context $context Context owner of the data.
180     * @param stdClass $definition Grading definition entry to export.
181     * @param int $userid The user whose information is to be exported.
182     *
183     * @return stdClass The data to export.
184     */
185    protected static function _get_gradingform_export_data(\context $context, $definition, int $userid) {
186        static::$mock->get_return_value(__FUNCTION__, func_get_args());
187    }
188
189    /**
190     * Any call to this method should delete all user data for the context defined.
191     *
192     * @deprecated Since Moodle 3.6 MDL-62535 Please use the methods in the gradingform_provider_v2 interface.
193     * @todo MDL-63167 remove this method.
194     *
195     * @param context $context Context owner of the data.
196     */
197    protected static function _delete_gradingform_for_context(\context $context) {
198        static::$mock->get_return_value(__FUNCTION__, func_get_args());
199    }
200
201    /**
202     * A call to this method should delete user data (where practicle) from the userid and context.
203     *
204     * @deprecated Since Moodle 3.6 MDL-62535 Please use the methods in the gradingform_provider_v2 interface.
205     * @todo MDL-63167 remove this method.
206     *
207     * @param int $userid The user whose information is to be deleted.
208     * @param context $context Context owner of the data.
209     */
210    protected static function _delete_gradingform_for_userid(int $userid, \context $context) {
211        static::$mock->get_return_value(__FUNCTION__, func_get_args());
212    }
213}
214
215/**
216 * Called inside the polyfill methods in the test polyfill provider, allowing us to ensure these are called with correct params.
217 *
218 * @copyright   2018 Jake Dallimore <jrhdallimore@gmail.com>
219 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
220 */
221class test_gradingform_legacy_polyfill_mock_wrapper {
222    /**
223     * Get the return value for the specified item.
224     */
225    public function get_return_value() {
226    }
227}
228