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