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
namespace core_xapi\privacy;
18
 
19
use core_privacy\tests\provider_testcase;
20
use core_privacy\local\request\transform;
21
use core_xapi\privacy\provider;
22
use core_xapi\local\statement\item_activity;
23
use core_xapi\test_helper;
24
 
25
/**
26
 * Privacy tests for core_xapi.
27
 *
28
 * @package    core_xapi
29
 * @category   test
30
 * @copyright  2023 Sara Arjona (sara@moodle.com)
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @covers     \core_xapi\privacy\provider
33
 */
1441 ariadna 34
final class provider_test extends provider_testcase {
1 efrain 35
 
36
    /**
37
     * Setup to ensure that fixtures are loaded.
38
     */
39
    public static function setUpBeforeClass(): void {
40
        global $CFG;
41
        require_once($CFG->dirroot.'/lib/xapi/tests/helper.php');
1441 ariadna 42
        parent::setUpBeforeClass();
1 efrain 43
    }
44
 
45
    /**
46
     * Helper to set up some sample data.
47
     *
48
     * @return array Array with the users that have been created.
49
     */
50
    protected function set_up_data(): array {
51
        $user1 = self::getDataGenerator()->create_user();
52
        $user2 = self::getDataGenerator()->create_user();
53
        $user3 = self::getDataGenerator()->create_user();
54
 
55
        // Add a few xAPI state records to database.
56
        $context = \context_system::instance();
57
        $cid = $context->id;
58
        $this->setUser($user1);
59
        test_helper::create_state(['activity' => item_activity::create_from_id($context->id)], true);
60
        test_helper::create_state(['activity' => item_activity::create_from_id('2')], true);
61
        test_helper::create_state(['activity' => item_activity::create_from_id('3'), 'component' => 'mod_h5pactivity'], true);
62
        $this->setUser($user2);
63
        test_helper::create_state(['activity' => item_activity::create_from_id($context->id)], true);
64
        test_helper::create_state(['activity' => item_activity::create_from_id('2')], true);
65
        test_helper::create_state(['activity' => item_activity::create_from_id('4')], true);
66
        test_helper::create_state(['activity' => item_activity::create_from_id('5')], true);
67
        $this->setUser($user3);
68
        test_helper::create_state(['activity' => item_activity::create_from_id($cid), 'component' => 'mod_h5pactivity'], true);
69
 
70
        return [$user1, $user2, $user3];
71
    }
72
 
73
    /**
74
     * Test confirming that contexts of xapi items can be added to the contextlist.
75
     */
76
    public function test_add_contexts_for_userid(): void {
77
        $this->resetAfterTest();
78
 
79
        // Scenario.
80
        list($user1, $user2) = $this->set_up_data();
81
 
82
        // Ask the xapi privacy api to export contexts for xapi of the type we just created, for user1.
83
        $contextlist = new \core_privacy\local\request\contextlist();
84
        provider::add_contexts_for_userid($contextlist, $user1->id, 'fake_component');
85
        $this->assertCount(2, $contextlist->get_contextids());
86
 
87
        $contextlist = new \core_privacy\local\request\contextlist();
88
        provider::add_contexts_for_userid($contextlist, $user1->id, 'mod_h5pactivity');
89
        $this->assertCount(1, $contextlist->get_contextids());
90
 
91
        // Ask the xapi privacy api to export contexts for xapi of the type we just created, for user2.
92
        $contextlist = new \core_privacy\local\request\contextlist();
93
        provider::add_contexts_for_userid($contextlist, $user2->id, 'fake_component');
94
        $this->assertCount(4, $contextlist->get_contextids());
95
 
96
        $contextlist = new \core_privacy\local\request\contextlist();
97
        provider::add_contexts_for_userid($contextlist, $user2->id, 'mod_h5pactivity');
98
        $this->assertCount(0, $contextlist->get_contextids());
99
    }
100
 
101
    /**
102
     * Test confirming that user ID's of xapi states can be added to the userlist.
103
     */
11 efrain 104
    public function test_add_userids_for_context(): void {
1 efrain 105
        global $DB;
106
 
107
        $this->resetAfterTest();
108
 
109
        // Scenario.
110
        list($user1, $user2, $user3) = $this->set_up_data();
111
        $this->assertEquals(3, $DB->count_records('xapi_states', ['userid' => $user1->id]));
112
        $this->assertEquals(4, $DB->count_records('xapi_states', ['userid' => $user2->id]));
113
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user3->id]));
114
        $systemcontext = \context_system::instance();
115
 
116
        // Ask the xapi privacy api to export userids for xapi states of the type we just created, in the system context.
117
        $userlist = new \core_privacy\local\request\userlist($systemcontext, 'fake_component');
118
        provider::add_userids_for_context($userlist, 'fake_component');
119
        // Only user1 and user2 should be returned, because user3 has a different component for the system context.
120
        $this->assertCount(2, $userlist->get_userids());
121
        $expected = [
122
            $user1->id,
123
            $user2->id,
124
        ];
125
        $this->assertEqualsCanonicalizing($expected, $userlist->get_userids());
126
 
127
        // Ask the xapi privacy api to export userids for xapi states of the type we just created for a different component.
128
        $userlist = new \core_privacy\local\request\userlist($systemcontext, 'mod_h5pactivity');
129
        provider::add_userids_for_context($userlist, 'mod_h5pactivity');
130
        // Only user3 should be returned, because the others have a different component for the system context.
131
        $this->assertCount(1, $userlist->get_userids());
132
        $expected = [$user3->id];
133
        $this->assertEqualsCanonicalizing($expected, $userlist->get_userids());
134
 
135
        // Ask the xapi privacy api to export userids xapi states for an empty component.
136
        $userlist = new \core_privacy\local\request\userlist($systemcontext, 'empty_component');
137
        provider::add_userids_for_context($userlist, 'empty_component');
138
        $this->assertCount(0, $userlist->get_userids());
139
    }
140
 
141
    /**
142
     * Test fetching the xapi state data for a specified user in a specified component and itemid.
143
     */
11 efrain 144
    public function test_get_xapi_states_for_user(): void {
1 efrain 145
        global $DB;
146
 
147
        $this->resetAfterTest();
148
 
149
        // Scenario.
150
        list($user1, $user2, $user3) = $this->set_up_data();
151
        $this->assertEquals(3, $DB->count_records('xapi_states', ['userid' => $user1->id]));
152
        $this->assertEquals(4, $DB->count_records('xapi_states', ['userid' => $user2->id]));
153
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user3->id]));
154
        $systemcontext = \context_system::instance();
155
 
156
        // Get the states info for user1 in the system context.
157
        $result = provider::get_xapi_states_for_user($user1->id, 'fake_component', $systemcontext->id);
158
        $info = (object) reset($result);
159
        // Ensure the correct data has been returned.
160
        $this->assertNotEmpty($info->statedata);
161
 
1441 ariadna 162
        $this->assertNotEmpty($info->timecreated);
163
        $this->assertNotEmpty($info->timemodified);
164
 
1 efrain 165
        // Get the states info for user2 in the system context.
166
        $result = provider::get_xapi_states_for_user($user2->id, 'fake_component', $systemcontext->id);
167
        $info = (object) reset($result);
168
        // Ensure the correct data has been returned.
169
        $this->assertNotEmpty($info->statedata);
1441 ariadna 170
        $this->assertNotEmpty($info->timecreated);
171
        $this->assertNotEmpty($info->timemodified);
1 efrain 172
 
173
        // Get the states info for user3 in the system context (it should be empty).
174
        $info = provider::get_xapi_states_for_user($user3->id, 'fake_component', $systemcontext->id);
175
        // Ensure the correct data has been returned.
176
        $this->assertEmpty($info);
177
    }
178
 
179
    /**
180
     * Test deletion of user xapi states based on an approved_contextlist and component area.
181
     */
182
    public function test_delete_states_for_user(): void {
183
        global $DB;
184
 
185
        $this->resetAfterTest();
186
 
187
        // Scenario.
188
        list($user1, $user2, $user3) = $this->set_up_data();
189
        $this->assertEquals(3, $DB->count_records('xapi_states', ['userid' => $user1->id]));
190
        $this->assertEquals(4, $DB->count_records('xapi_states', ['userid' => $user2->id]));
191
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user3->id]));
192
 
193
        // Now, delete the xapistates for user1 only.
194
        $user1context = \context_user::instance($user1->id);
195
        $approvedcontextlist = new \core_privacy\local\request\approved_contextlist($user1, 'fake_component', [$user1context->id]);
196
        provider::delete_states_for_user($approvedcontextlist, 'fake_component');
197
 
198
        // Verify that we have no xapi states for user1 for the fake_component but that the rest of records are intact.
199
        $this->assertEquals(0, $DB->count_records('xapi_states', ['userid' => $user1->id, 'component' => 'fake_component']));
200
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user1->id, 'component' => 'mod_h5pactivity']));
201
        $this->assertEquals(4, $DB->count_records('xapi_states', ['userid' => $user2->id]));
202
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user3->id]));
203
    }
204
 
205
    /**
206
     * Test deletion of all user xapi states.
207
     */
208
    public function test_delete_states_for_all_users(): void {
209
        global $DB;
210
 
211
        $this->resetAfterTest();
212
 
213
        // Scenario.
214
        list($user1, $user2, $user3) = $this->set_up_data();
215
        $this->assertEquals(3, $DB->count_records('xapi_states', ['userid' => $user1->id]));
216
        $this->assertEquals(4, $DB->count_records('xapi_states', ['userid' => $user2->id]));
217
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user3->id]));
218
 
219
        // Now, delete all course module xapi states in the 'fake_component' context only.
220
        provider::delete_states_for_all_users(\context_system::instance(), 'fake_component');
221
 
222
        // Verify that only content with the context_system for the fake_component have been removed.
223
        $this->assertEquals(2, $DB->count_records('xapi_states', ['userid' => $user1->id]));
224
        $this->assertEquals(3, $DB->count_records('xapi_states', ['userid' => $user2->id]));
225
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user3->id]));
226
    }
227
 
228
    /**
229
     * Test deletion of user xapi states based on an approved_userlist and component area.
230
     */
11 efrain 231
    public function test_delete_states_for_userlist(): void {
1 efrain 232
        global $DB;
233
 
234
        $this->resetAfterTest();
235
 
236
        // Scenario.
237
        list($user1, $user2, $user3) = $this->set_up_data();
238
        $this->assertEquals(3, $DB->count_records('xapi_states', ['userid' => $user1->id]));
239
        $this->assertEquals(4, $DB->count_records('xapi_states', ['userid' => $user2->id]));
240
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user3->id]));
241
        $systemcontext = \context_system::instance();
242
 
243
        // Ask the xapi privacy api to export userids for states of the type we just created, in the system context.
244
        $userlist1 = new \core_privacy\local\request\userlist($systemcontext, 'fake_component');
245
        provider::add_userids_for_context($userlist1);
246
        // Verify we have two userids in the list for system context.
247
        $this->assertCount(2, $userlist1->get_userids());
248
 
249
        // Now, delete the states for user1 only in the system context.
250
        $approveduserlist = new \core_privacy\local\request\approved_userlist($systemcontext, 'fake_component', [$user1->id]);
251
        provider::delete_states_for_userlist($approveduserlist);
252
        // Ensure user1's data was deleted and user2 is still returned for system context.
253
        $userlist1 = new \core_privacy\local\request\userlist($systemcontext, 'fake_component');
254
        provider::add_userids_for_context($userlist1);
255
        $this->assertCount(1, $userlist1->get_userids());
256
        // Verify that user2 is still in the list for system context.
257
        $expected = [$user2->id];
258
        $this->assertEquals($expected, $userlist1->get_userids());
259
        // Verify that the data of user1 in other contexts was not deleted.
260
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user3->id]));
261
        $this->assertEquals(1, $DB->count_records('xapi_states', ['userid' => $user1->id]));
262
        $this->assertEquals(2, $DB->count_records('xapi_states', ['itemid' => $systemcontext->id]));
263
 
264
        // Verify that no data is removed if the component is empty.
265
        $userlist3 = new \core_privacy\local\request\userlist($systemcontext, 'empty_component');
266
        provider::add_userids_for_context($userlist3);
267
        $this->assertCount(0, $userlist3->get_userids());
268
        $this->assertEquals(2, $DB->count_records('xapi_states', ['itemid' => $systemcontext->id]));
269
    }
270
}