Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
 * Base class for unit tests for message_airnotifier.
18
 *
19
 * @package    message_airnotifier
20
 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
namespace message_airnotifier\privacy;
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
use core_privacy\tests\provider_testcase;
28
use message_airnotifier\privacy\provider;
29
use core_privacy\local\request\approved_userlist;
30
 
31
/**
32
 * Unit tests for message\output\airnotifier\classes\privacy\provider.php
33
 *
34
 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
class provider_test extends provider_testcase {
38
 
39
    /**
40
     * Basic setup for these tests.
41
     */
42
    public function setUp(): void {
43
        $this->resetAfterTest(true);
44
    }
45
 
46
    /**
47
     * /
48
     * @param object $user User object
49
     * @param string $pushid unique string
50
     */
51
    protected function add_device($user, $pushid) {
52
        global $DB;
53
 
54
        // Add fake core device.
55
        $device = array(
56
            'appid' => 'com.moodle.moodlemobile',
57
            'name' => 'occam',
58
            'model' => 'Nexus 4',
59
            'platform' => 'Android',
60
            'version' => '4.2.2',
61
            'pushid' => $pushid,
62
            'uuid' => 'asdnfl348qlksfaasef859',
63
            'userid' => $user->id,
64
            'timecreated' => time(),
65
            'timemodified' => time(),
66
        );
67
        $coredeviceid = $DB->insert_record('user_devices', (object) $device);
68
 
69
        $airnotifierdev = array(
70
            'userdeviceid' => $coredeviceid,
71
            'enable' => 1
72
        );
73
        $airnotifierdevid = $DB->insert_record('message_airnotifier_devices', (object) $airnotifierdev);
74
    }
75
 
76
    /**
77
     * Test returning metadata.
78
     */
11 efrain 79
    public function test_get_metadata(): void {
1 efrain 80
        $collection = new \core_privacy\local\metadata\collection('message_airnotifier');
81
        $collection = \message_airnotifier\privacy\provider::get_metadata($collection);
82
        $this->assertNotEmpty($collection);
83
    }
84
 
85
    /**
86
     * Test getting the context for the user ID related to this plugin.
87
     */
11 efrain 88
    public function test_get_contexts_for_userid(): void {
1 efrain 89
 
90
        $user = $this->getDataGenerator()->create_user();
91
        $context = \context_user::instance($user->id);
92
 
93
        $this->add_device($user, 'apuJih874kj');
94
        $this->add_device($user, 'bdu09Ikjjsu');
95
 
96
        $contextlist = \message_airnotifier\privacy\provider::get_contexts_for_userid($user->id);
97
        $this->assertEquals($context->id, $contextlist->current()->id);
98
    }
99
 
100
    /**
101
     * Test that data is exported correctly for this plugin.
102
     */
11 efrain 103
    public function test_export_user_data(): void {
1 efrain 104
        $user = $this->getDataGenerator()->create_user();
105
        $context = \context_user::instance($user->id);
106
 
107
        $this->add_device($user, 'apuJih874kj');
108
        $this->add_device($user, 'bdu09Ikjjsu');
109
 
110
        $writer = \core_privacy\local\request\writer::with_context($context);
111
        $this->assertFalse($writer->has_any_data());
112
        $this->export_context_data_for_user($user->id, $context, 'message_airnotifier');
113
 
114
        // First device.
115
        $data = $writer->get_data([get_string('privacy:subcontext', 'message_airnotifier'), 'Nexus 4_apuJih874kj']);
116
        $this->assertEquals('com.moodle.moodlemobile', $data->appid);
117
 
118
        // Second device.
119
        $data = $writer->get_data([get_string('privacy:subcontext', 'message_airnotifier'), 'Nexus 4_bdu09Ikjjsu']);
120
        $this->assertEquals('bdu09Ikjjsu', $data->pushid);
121
    }
122
 
123
    /**
124
     * Test that user data is deleted using the context.
125
     */
11 efrain 126
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 127
        global $DB;
128
 
129
        $user = $this->getDataGenerator()->create_user();
130
        $context = \context_user::instance($user->id);
131
 
132
        $this->add_device($user, 'apuJih874kj');
133
 
134
        // Check that we have an entry.
135
        $devices = $DB->get_records('message_airnotifier_devices');
136
        $this->assertCount(1, $devices);
137
 
138
        \message_airnotifier\privacy\provider::delete_data_for_all_users_in_context($context);
139
 
140
        // Check that it has now been deleted.
141
        $devices = $DB->get_records('message_airnotifier_devices');
142
        $this->assertCount(0, $devices);
143
    }
144
 
145
    /**
146
     * Test that user data is deleted for this user.
147
     */
11 efrain 148
    public function test_delete_data_for_user(): void {
1 efrain 149
        global $DB;
150
 
151
        $user = $this->getDataGenerator()->create_user();
152
        $context = \context_user::instance($user->id);
153
 
154
        $this->add_device($user, 'apuJih874kj');
155
 
156
        // Check that we have an entry.
157
        $devices = $DB->get_records('message_airnotifier_devices');
158
        $this->assertCount(1, $devices);
159
 
160
        $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'message_airnotifier', [$context->id]);
161
        \message_airnotifier\privacy\provider::delete_data_for_user($approvedlist);
162
 
163
        // Check that it has now been deleted.
164
        $devices = $DB->get_records('message_airnotifier_devices');
165
        $this->assertCount(0, $devices);
166
    }
167
 
168
    /**
169
     * Test that only users with a user context are fetched.
170
     */
11 efrain 171
    public function test_get_users_in_context(): void {
1 efrain 172
        $component = 'message_airnotifier';
173
 
174
        // Create user.
175
        $user = $this->getDataGenerator()->create_user();
176
        $usercontext = \context_user::instance($user->id);
177
 
178
        // The lists of users for the user context should be empty.
179
        // Related user data have not been created yet.
180
        $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
181
        provider::get_users_in_context($userlist);
182
        $this->assertCount(0, $userlist);
183
 
184
        $this->add_device($user, 'apuJih874kj');
185
        $this->add_device($user, 'bdu09Ikjjsu');
186
 
187
        // The list of users for userlist should return one user (user).
188
        provider::get_users_in_context($userlist);
189
        $this->assertCount(1, $userlist);
190
        $expected = [$user->id];
191
        $actual = $userlist->get_userids();
192
        $this->assertEquals($expected, $actual);
193
 
194
        // The list of users should only return users in the user context.
195
        $systemcontext = \context_system::instance();
196
        $userlist1 = new \core_privacy\local\request\userlist($systemcontext, $component);
197
        provider::get_users_in_context($userlist1);
198
        $this->assertCount(0, $userlist1);
199
    }
200
 
201
    /**
202
     * Test that data for users in approved userlist is deleted.
203
     */
11 efrain 204
    public function test_delete_data_for_users(): void {
1 efrain 205
        $component = 'message_airnotifier';
206
 
207
        // Create user1.
208
        $user1 = $this->getDataGenerator()->create_user();
209
        $usercontext1 = \context_user::instance($user1->id);
210
        // Create user2.
211
        $user2 = $this->getDataGenerator()->create_user();
212
        $usercontext2 = \context_user::instance($user2->id);
213
 
214
        $this->add_device($user1, 'apuJih874kj');
215
        $this->add_device($user1, 'cpuJih874kp');
216
        $this->add_device($user2, 'bdu09Ikjjsu');
217
 
218
        // The list of users for usercontext1 should return one user (user1).
219
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
220
        provider::get_users_in_context($userlist1);
221
        $this->assertCount(1, $userlist1);
222
 
223
        // The list of users for usercontext2 should return one user (user2).
224
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
225
        provider::get_users_in_context($userlist2);
226
        $this->assertCount(1, $userlist2);
227
 
228
        $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
229
        // Delete using delete_data_for_user.
230
        provider::delete_data_for_users($approvedlist);
231
 
232
        // Re-fetch users in usercontext1 - the user data should now be empty.
233
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
234
        provider::get_users_in_context($userlist1);
235
        $this->assertCount(0, $userlist1);
236
 
237
        // The list of users for usercontext2 should still return one user (user2).
238
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
239
        provider::get_users_in_context($userlist2);
240
        $this->assertCount(1, $userlist2);
241
 
242
        // User data should only be removed in the user context.
243
        $systemcontext = \context_system::instance();
244
        $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
245
        // Delete using delete_data_for_user.
246
        provider::delete_data_for_users($approvedlist);
247
        // Re-fetch users in usercontext2 - the user data should still be present.
248
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
249
        provider::get_users_in_context($userlist2);
250
        $this->assertCount(1, $userlist2);
251
    }
252
}