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_analytics\privacy;
18
 
19
use core_privacy\local\request\approved_userlist;
1441 ariadna 20
use core_analytics\tests\mlbackend_helper_trait;
1 efrain 21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
require_once(__DIR__ . '/../fixtures/test_indicator_max.php');
25
require_once(__DIR__ . '/../fixtures/test_indicator_min.php');
26
require_once(__DIR__ . '/../fixtures/test_target_site_users.php');
27
require_once(__DIR__ . '/../fixtures/test_target_course_users.php');
28
 
29
/**
30
 * Unit tests for privacy.
31
 *
32
 * @package   core_analytics
33
 * @copyright 2018 David Monllaó {@link http://www.davidmonllao.com}
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class provider_test extends \core_privacy\tests\provider_testcase {
37
    use mlbackend_helper_trait;
1 efrain 38
 
39
    /** @var \core_analytics\model Store Model 1. */
40
    protected $model1;
41
 
42
    /** @var \core_analytics\model Store Model 2. */
43
    protected $model2;
44
 
45
    /** @var \stdClass $modelobj1 Store Model 1 object. */
46
    protected $modelobj1;
47
 
48
    /** @var \stdClass $modelobj2 Store Model 2 object. */
49
    protected $modelobj2;
50
 
51
    /** @var \stdClass $u1 User 1 record. */
52
    protected $u1;
53
 
54
    /** @var \stdClass $u2 User 2 record. */
55
    protected $u2;
56
 
57
    /** @var \stdClass $u3 User 3 record. */
58
    protected $u3;
59
 
60
    /** @var \stdClass $u4 User 4 record. */
61
    protected $u4;
62
 
63
    /** @var \stdClass $u5 User 5 record. */
64
    protected $u5;
65
 
66
    /** @var \stdClass $u6 User 6 record. */
67
    protected $u6;
68
 
69
    /** @var \stdClass $u7 User 7 record. */
70
    protected $u7;
71
 
72
    /** @var \stdClass $u8 User 8 record. */
73
    protected $u8;
74
 
1441 ariadna 75
    /** @var \stdClass $u9 User 9 record. */
76
    protected $u9;
77
 
78
    /** @var \stdClass $u10 User 10 record. */
79
    protected $u10;
80
 
1 efrain 81
    /** @var \stdClass $c1 Course 1 record. */
82
    protected $c1;
83
 
84
    /** @var \stdClass $c2 Course 2 record. */
85
    protected $c2;
86
 
87
    public function setUp(): void {
1441 ariadna 88
        if (!self::is_mlbackend_python_configured()) {
89
            $this->markTestSkipped('mlbackend_python is not configured.');
90
        }
1 efrain 91
 
1441 ariadna 92
        parent::setUp();
93
 
1 efrain 94
        $this->resetAfterTest(true);
95
        $this->setAdminUser();
96
 
97
        $timesplittingid = '\core\analytics\time_splitting\single_range';
98
        $target = \core_analytics\manager::get_target('test_target_site_users');
99
        $indicators = array('test_indicator_max');
100
        foreach ($indicators as $key => $indicator) {
101
            $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
102
        }
103
        $this->model1 = \core_analytics\model::create($target, $indicators, $timesplittingid);
104
        $this->modelobj1 = $this->model1->get_model_obj();
105
 
106
        $target = \core_analytics\manager::get_target('test_target_course_users');
107
        $indicators = array('test_indicator_min');
108
        foreach ($indicators as $key => $indicator) {
109
            $indicators[$key] = \core_analytics\manager::get_indicator($indicator);
110
        }
111
        $this->model2 = \core_analytics\model::create($target, $indicators, $timesplittingid);
112
        $this->modelobj2 = $this->model1->get_model_obj();
113
 
114
        $this->u1 = $this->getDataGenerator()->create_user(['firstname' => 'a111111111111', 'lastname' => 'a']);
115
        $this->u2 = $this->getDataGenerator()->create_user(['firstname' => 'a222222222222', 'lastname' => 'a']);
116
        $this->u3 = $this->getDataGenerator()->create_user(['firstname' => 'b333333333333', 'lastname' => 'b']);
117
        $this->u4 = $this->getDataGenerator()->create_user(['firstname' => 'b444444444444', 'lastname' => 'b']);
118
        $this->u5 = $this->getdatagenerator()->create_user(['firstname' => 'a555555555555', 'lastname' => 'a']);
119
        $this->u6 = $this->getdatagenerator()->create_user(['firstname' => 'a666666666666', 'lastname' => 'a']);
120
        $this->u7 = $this->getdatagenerator()->create_user(['firstname' => 'b777777777777', 'lastname' => 'b']);
121
        $this->u8 = $this->getDataGenerator()->create_user(['firstname' => 'b888888888888', 'lastname' => 'b']);
1441 ariadna 122
        $this->u9 = $this->getDataGenerator()->create_user(['firstname' => 'a999999999999', 'lastname' => 'a']);
123
        $this->u10 = $this->getDataGenerator()->create_user(['firstname' => 'b000000000000', 'lastname' => 'a']);
1 efrain 124
 
125
        $this->c1 = $this->getDataGenerator()->create_course(['visible' => false]);
126
        $this->c2 = $this->getDataGenerator()->create_course();
127
 
1441 ariadna 128
        // Enrol users to course 1.
1 efrain 129
        $this->getDataGenerator()->enrol_user($this->u1->id, $this->c1->id, 'student');
130
        $this->getDataGenerator()->enrol_user($this->u2->id, $this->c1->id, 'student');
131
        $this->getDataGenerator()->enrol_user($this->u3->id, $this->c1->id, 'student');
132
        $this->getDataGenerator()->enrol_user($this->u4->id, $this->c1->id, 'student');
133
        $this->getDataGenerator()->enrol_user($this->u5->id, $this->c1->id, 'student');
134
        $this->getDataGenerator()->enrol_user($this->u6->id, $this->c1->id, 'student');
135
        $this->getDataGenerator()->enrol_user($this->u7->id, $this->c1->id, 'student');
136
        $this->getDataGenerator()->enrol_user($this->u8->id, $this->c1->id, 'student');
1441 ariadna 137
        $this->getDataGenerator()->enrol_user($this->u9->id, $this->c1->id, 'student');
138
        $this->getDataGenerator()->enrol_user($this->u10->id, $this->c1->id, 'student');
139
        // Enrol users to course 2.
1 efrain 140
        $this->getDataGenerator()->enrol_user($this->u1->id, $this->c2->id, 'student');
141
        $this->getDataGenerator()->enrol_user($this->u2->id, $this->c2->id, 'student');
142
        $this->getDataGenerator()->enrol_user($this->u3->id, $this->c2->id, 'student');
143
        $this->getDataGenerator()->enrol_user($this->u4->id, $this->c2->id, 'student');
144
        $this->getDataGenerator()->enrol_user($this->u5->id, $this->c2->id, 'student');
145
        $this->getDataGenerator()->enrol_user($this->u6->id, $this->c2->id, 'student');
146
        $this->getDataGenerator()->enrol_user($this->u7->id, $this->c2->id, 'student');
147
        $this->getDataGenerator()->enrol_user($this->u8->id, $this->c2->id, 'student');
1441 ariadna 148
        $this->getDataGenerator()->enrol_user($this->u9->id, $this->c2->id, 'student');
149
        $this->getDataGenerator()->enrol_user($this->u10->id, $this->c2->id, 'student');
1 efrain 150
 
151
        $this->setAdminUser();
152
 
153
        $this->model1->enable();
154
        $this->model1->train();
155
        $this->model1->predict();
156
        $this->model2->enable();
157
        $this->model2->train();
158
        $this->model2->predict();
159
 
160
        list($total, $predictions) = $this->model2->get_predictions(\context_course::instance($this->c1->id));
161
 
162
        $this->setUser($this->u3);
163
        $prediction = reset($predictions);
164
        $prediction->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $this->model2->get_target());
165
 
166
        $this->setAdminUser();
167
    }
168
 
169
    /**
170
     * Test fetching users within a context.
171
     */
11 efrain 172
    public function test_get_users_in_context(): void {
1 efrain 173
        global $CFG;
174
 
175
        $component = 'core_analytics';
176
        $course1context = \context_course::instance($this->c1->id);
177
        $course2context = \context_course::instance($this->c2->id);
178
        $systemcontext = \context_system::instance();
179
        $expected = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id, $this->u5->id, $this->u6->id,
1441 ariadna 180
            $this->u7->id, $this->u8->id, $this->u9->id, $this->u10->id];
1 efrain 181
 
182
        // Check users exist in the relevant contexts.
183
        $userlist = new \core_privacy\local\request\userlist($course1context, $component);
184
        provider::get_users_in_context($userlist);
185
        $actual = $userlist->get_userids();
186
        sort($actual);
187
        $this->assertEquals($expected, $actual);
188
 
189
        $userlist = new \core_privacy\local\request\userlist($course2context, $component);
190
        provider::get_users_in_context($userlist);
191
        $actual = $userlist->get_userids();
192
        sort($actual);
193
        $this->assertEquals($expected, $actual);
194
 
195
        // System context will also find guest and admin user, add to expected before testing.
196
        $expected = array_merge($expected, [$CFG->siteguest, get_admin()->id]);
197
        sort($expected);
198
 
199
        $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
200
        provider::get_users_in_context($userlist);
201
        $actual = $userlist->get_userids();
202
        sort($actual);
203
        $this->assertEquals($expected, $actual);
204
    }
205
 
206
    /**
207
     * Test delete a context.
208
     *
209
     * @return null
210
     */
11 efrain 211
    public function test_delete_context_data(): void {
1 efrain 212
        global $DB;
213
 
214
        // We have 4 predictions for model1 and 8 predictions for model2.
215
        $this->assertEquals(12, $DB->count_records('analytics_predictions'));
1441 ariadna 216
        $this->assertEquals(32, $DB->count_records('analytics_indicator_calc'));
1 efrain 217
 
218
        // We have 1 prediction action.
219
        $this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
220
 
221
        $coursecontext = \context_course::instance($this->c1->id);
222
 
223
        // Delete the course that was used for prediction.
224
        provider::delete_data_for_all_users_in_context($coursecontext);
225
 
226
        // The course1 predictions are deleted.
227
        $this->assertEquals(8, $DB->count_records('analytics_predictions'));
228
 
229
        // Calculations related to that context are deleted.
230
        $this->assertEmpty($DB->count_records('analytics_indicator_calc', ['contextid' => $coursecontext->id]));
231
 
232
        // The deleted context prediction actions are deleted as well.
233
        $this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));
234
    }
235
 
236
    /**
237
     * Test delete a user.
238
     *
239
     * @return null
240
     */
11 efrain 241
    public function test_delete_user_data(): void {
1 efrain 242
        global $DB;
243
 
244
        $usercontexts = provider::get_contexts_for_userid($this->u3->id);
245
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->u3, 'core_analytics',
246
                                                                            $usercontexts->get_contextids());
247
        provider::delete_data_for_user($contextlist);
248
 
249
        // The site level prediction for u3 was deleted.
250
        $this->assertEquals(9, $DB->count_records('analytics_predictions'));
251
        $this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));
252
 
253
        $usercontexts = provider::get_contexts_for_userid($this->u1->id);
254
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->u1, 'core_analytics',
255
                                                                            $usercontexts->get_contextids());
256
        provider::delete_data_for_user($contextlist);
257
        // We have nothing for u1.
258
        $this->assertEquals(9, $DB->count_records('analytics_predictions'));
259
 
260
        $usercontexts = provider::get_contexts_for_userid($this->u4->id);
261
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->u4, 'core_analytics',
262
                                                                            $usercontexts->get_contextids());
263
        provider::delete_data_for_user($contextlist);
264
        $this->assertEquals(6, $DB->count_records('analytics_predictions'));
265
    }
266
 
267
    /**
268
     * Test deleting multiple users in a context.
269
     */
11 efrain 270
    public function test_delete_data_for_users(): void {
1 efrain 271
        global $DB;
272
 
273
        $component = 'core_analytics';
274
        $course1context = \context_course::instance($this->c1->id);
275
        $course2context = \context_course::instance($this->c2->id);
276
        $systemcontext = \context_system::instance();
277
 
278
        // Ensure all records exist in expected contexts.
279
        $expectedcontexts = [$course1context->id, $course2context->id, $systemcontext->id];
280
        sort($expectedcontexts);
281
 
282
        $actualcontexts = [
283
            $this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(),
284
            $this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(),
285
            $this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(),
286
            $this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(),
287
            $this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(),
288
            $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(),
289
            $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(),
290
            $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(),
1441 ariadna 291
            $this->u9->id => provider::get_contexts_for_userid($this->u9->id)->get_contextids(),
292
            $this->u10->id => provider::get_contexts_for_userid($this->u10->id)->get_contextids(),
1 efrain 293
        ];
294
 
295
        foreach ($actualcontexts as $userid => $unused) {
296
            sort($actualcontexts[$userid]);
297
            $this->assertEquals($expectedcontexts, $actualcontexts[$userid]);
298
        }
299
 
300
        // Test initial record counts are as expected.
301
        $this->assertEquals(12, $DB->count_records('analytics_predictions'));
302
        $this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
1441 ariadna 303
        $this->assertEquals(32, $DB->count_records('analytics_indicator_calc'));
1 efrain 304
 
305
        // Delete u1 and u3 from system context.
306
        $approveduserids = [$this->u1->id, $this->u3->id];
307
        $approvedlist = new approved_userlist($systemcontext, $component, $approveduserids);
308
        provider::delete_data_for_users($approvedlist);
309
 
310
        // Ensure u1 and u3 system context data deleted only.
311
        $expectedcontexts = [
312
            $this->u1->id => [$course1context->id, $course2context->id],
313
            $this->u2->id => [$systemcontext->id, $course1context->id, $course2context->id],
314
            $this->u3->id => [$course1context->id, $course2context->id],
315
            $this->u4->id => [$systemcontext->id, $course1context->id, $course2context->id],
316
            $this->u5->id => [$systemcontext->id, $course1context->id, $course2context->id],
317
            $this->u6->id => [$systemcontext->id, $course1context->id, $course2context->id],
318
            $this->u7->id => [$systemcontext->id, $course1context->id, $course2context->id],
319
            $this->u8->id => [$systemcontext->id, $course1context->id, $course2context->id],
1441 ariadna 320
            $this->u9->id => [$systemcontext->id, $course1context->id, $course2context->id],
321
            $this->u10->id => [$systemcontext->id, $course1context->id, $course2context->id],
1 efrain 322
        ];
323
 
324
        $actualcontexts = [
325
            $this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(),
326
            $this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(),
327
            $this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(),
328
            $this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(),
329
            $this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(),
330
            $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(),
331
            $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(),
332
            $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(),
1441 ariadna 333
            $this->u9->id => provider::get_contexts_for_userid($this->u9->id)->get_contextids(),
334
            $this->u10->id => provider::get_contexts_for_userid($this->u10->id)->get_contextids(),
1 efrain 335
        ];
336
 
337
        foreach ($actualcontexts as $userid => $unused) {
338
            sort($expectedcontexts[$userid]);
339
            sort($actualcontexts[$userid]);
340
            $this->assertEquals($expectedcontexts[$userid], $actualcontexts[$userid]);
341
        }
342
 
343
        // Test expected number of records have been deleted.
344
        $this->assertEquals(11, $DB->count_records('analytics_predictions'));
345
        $this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
1441 ariadna 346
        $this->assertEquals(30, $DB->count_records('analytics_indicator_calc'));
1 efrain 347
 
348
        // Delete for all 8 users in course 2 context.
349
        $approveduserids = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id, $this->u5->id, $this->u6->id,
1441 ariadna 350
            $this->u7->id, $this->u8->id, $this->u9->id, $this->u10->id];
1 efrain 351
        $approvedlist = new approved_userlist($course2context, $component, $approveduserids);
352
        provider::delete_data_for_users($approvedlist);
353
 
354
        // Ensure all course 2 context data deleted for all 4 users.
355
        $expectedcontexts = [
356
            $this->u1->id => [$course1context->id],
357
            $this->u2->id => [$systemcontext->id, $course1context->id],
358
            $this->u3->id => [$course1context->id],
359
            $this->u4->id => [$systemcontext->id, $course1context->id],
360
            $this->u5->id => [$systemcontext->id, $course1context->id],
361
            $this->u6->id => [$systemcontext->id, $course1context->id],
362
            $this->u7->id => [$systemcontext->id, $course1context->id],
363
            $this->u8->id => [$systemcontext->id, $course1context->id],
1441 ariadna 364
            $this->u9->id => [$systemcontext->id, $course1context->id],
365
            $this->u10->id => [$systemcontext->id, $course1context->id],
1 efrain 366
        ];
367
 
368
        $actualcontexts = [
369
            $this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(),
370
            $this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(),
371
            $this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(),
372
            $this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(),
373
            $this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(),
374
            $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(),
375
            $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(),
376
            $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(),
1441 ariadna 377
            $this->u9->id => provider::get_contexts_for_userid($this->u9->id)->get_contextids(),
378
            $this->u10->id => provider::get_contexts_for_userid($this->u10->id)->get_contextids(),
1 efrain 379
        ];
380
 
381
        foreach ($actualcontexts as $userid => $unused) {
382
            sort($actualcontexts[$userid]);
383
            sort($expectedcontexts[$userid]);
384
            $this->assertEquals($expectedcontexts[$userid], $actualcontexts[$userid]);
385
        }
386
 
387
        // Test expected number of records have been deleted.
388
        $this->assertEquals(7, $DB->count_records('analytics_predictions'));
389
        $this->assertEquals(1, $DB->count_records('analytics_prediction_actions'));
1441 ariadna 390
        $this->assertEquals(20, $DB->count_records('analytics_indicator_calc'));
1 efrain 391
 
392
        $approveduserids = [$this->u3->id];
393
        $approvedlist = new approved_userlist($course1context, $component, $approveduserids);
394
        provider::delete_data_for_users($approvedlist);
395
 
396
        // Ensure all course 1 context data deleted for u3.
397
        $expectedcontexts = [
398
            $this->u1->id => [$course1context->id],
399
            $this->u2->id => [$systemcontext->id, $course1context->id],
400
            $this->u3->id => [],
401
            $this->u4->id => [$systemcontext->id, $course1context->id],
402
            $this->u5->id => [$systemcontext->id, $course1context->id],
403
            $this->u6->id => [$systemcontext->id, $course1context->id],
404
            $this->u7->id => [$systemcontext->id, $course1context->id],
405
            $this->u8->id => [$systemcontext->id, $course1context->id],
1441 ariadna 406
            $this->u9->id => [$systemcontext->id, $course1context->id],
407
            $this->u10->id => [$systemcontext->id, $course1context->id],
1 efrain 408
        ];
409
 
410
        $actualcontexts = [
411
            $this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(),
412
            $this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(),
413
            $this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(),
414
            $this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(),
415
            $this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(),
416
            $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(),
417
            $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(),
418
            $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(),
1441 ariadna 419
            $this->u9->id => provider::get_contexts_for_userid($this->u9->id)->get_contextids(),
420
            $this->u10->id => provider::get_contexts_for_userid($this->u10->id)->get_contextids(),
1 efrain 421
        ];
422
        foreach ($actualcontexts as $userid => $unused) {
423
            sort($actualcontexts[$userid]);
424
            sort($expectedcontexts[$userid]);
425
            $this->assertEquals($expectedcontexts[$userid], $actualcontexts[$userid]);
426
        }
427
 
428
        // Test expected number of records have been deleted.
429
        $this->assertEquals(6, $DB->count_records('analytics_predictions'));
430
        $this->assertEquals(0, $DB->count_records('analytics_prediction_actions'));
1441 ariadna 431
        $this->assertEquals(19, $DB->count_records('analytics_indicator_calc'));
1 efrain 432
    }
433
 
434
    /**
435
     * Test export user data.
436
     *
437
     * @return null
438
     */
11 efrain 439
    public function test_export_data(): void {
1 efrain 440
        global $DB;
441
 
442
        $system = \context_system::instance();
443
        list($total, $predictions) = $this->model1->get_predictions($system);
444
        foreach ($predictions as $key => $prediction) {
445
            if ($prediction->get_prediction_data()->sampleid !== $this->u3->id) {
446
                $otheruserprediction = $prediction;
447
                break;
448
            }
449
        }
450
        $this->setUser($this->u3);
451
        $otheruserprediction->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $this->model1->get_target());
452
        $this->setAdminUser();
453
 
454
        $this->export_context_data_for_user($this->u3->id, $system, 'core_analytics');
455
        $writer = \core_privacy\local\request\writer::with_context($system);
456
        $this->assertTrue($writer->has_any_data());
457
 
458
        $u3prediction = $DB->get_record('analytics_predictions', ['contextid' => $system->id, 'sampleid' => $this->u3->id]);
459
        $data = $writer->get_data([get_string('analytics', 'analytics'),
460
            get_string('privacy:metadata:analytics:predictions', 'analytics'), $u3prediction->id]);
461
        $this->assertEquals(get_string('adminhelplogs'), $data->target);
462
        $this->assertEquals(get_string('coresystem'), $data->context);
463
        $this->assertEquals('firstname first char is not A', $data->prediction);
464
 
465
        $u3calculation = $DB->get_record('analytics_indicator_calc', ['contextid' => $system->id, 'sampleid' => $this->u3->id]);
466
        $data = $writer->get_data([get_string('analytics', 'analytics'),
467
            get_string('privacy:metadata:analytics:indicatorcalc', 'analytics'), $u3calculation->id]);
468
        $this->assertEquals('Allow stealth activities', $data->indicator);
469
        $this->assertEquals(get_string('coresystem'), $data->context);
470
        $this->assertEquals(get_string('yes'), $data->calculation);
471
 
472
        $sql = "SELECT apa.id FROM {analytics_prediction_actions} apa
473
                  JOIN {analytics_predictions} ap ON ap.id = apa.predictionid
474
                 WHERE ap.contextid = :contextid AND apa.userid = :userid AND ap.modelid = :modelid";
475
        $params = ['contextid' => $system->id, 'userid' => $this->u3->id, 'modelid' => $this->model1->get_id()];
476
        $u3action = $DB->get_record_sql($sql, $params);
477
        $data = $writer->get_data([get_string('analytics', 'analytics'),
478
            get_string('privacy:metadata:analytics:predictionactions', 'analytics'), $u3action->id]);
479
        $this->assertEquals(get_string('adminhelplogs'), $data->target);
480
        $this->assertEquals(get_string('coresystem'), $data->context);
481
        $this->assertEquals(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $data->action);
482
 
483
    }
484
}