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
namespace core_xapi;
18
 
19
use core_xapi\local\statement\item_agent;
20
use core_xapi\local\statement\item_activity;
21
use advanced_testcase;
22
 
23
/**
24
 * Contains test cases for testing xAPI state store methods.
25
 *
26
 * @package    core_xapi
27
 * @since      Moodle 4.2
28
 * @covers     \core_xapi\state_store
29
 * @copyright  2023 Sara Arjona (sara@moodle.com)
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
1441 ariadna 32
final class state_store_test extends advanced_testcase {
1 efrain 33
 
34
    /**
35
     * Setup to ensure that fixtures are loaded.
36
     */
37
    public static function setUpBeforeClass(): void {
38
        global $CFG;
39
        require_once($CFG->dirroot.'/lib/xapi/tests/helper.php');
1441 ariadna 40
        parent::setUpBeforeClass();
1 efrain 41
    }
42
 
43
    /**
44
     * Testing delete method.
45
     *
46
     * @dataProvider states_provider
47
     * @param array $info Array of overriden state data.
48
     * @param bool $expected Expected results.
49
     * @return void
50
     */
51
    public function test_state_store_delete(array $info, bool $expected): void {
52
        global $DB;
53
 
54
        $this->resetAfterTest();
55
 
56
        // Scenario.
57
        $this->setAdminUser();
58
        // Add, at least, one xAPI state record to database (with the default values).
59
        test_helper::create_state([], true);
60
 
61
        // Get current states in database.
62
        $currentstates = $DB->count_records('xapi_states');
63
 
64
        // Perform test.
65
        $component = $info['component'] ?? 'fake_component';
66
        $state = test_helper::create_state($info);
67
        $store = new state_store($component);
68
        $result = $store->delete($state);
69
 
70
        // Check the state has been removed.
71
        $records = $DB->get_records('xapi_states');
72
        $this->assertTrue($result);
73
        if ($expected) {
74
            $this->assertCount($currentstates - 1, $records);
75
        } else if ($expected === 'false') {
76
            $this->assertCount($currentstates, $records);
77
        }
78
    }
79
 
80
    /**
81
     * Testing get method.
82
     *
83
     * @dataProvider states_provider
84
     * @param array $info Array of overriden state data.
85
     * @param bool $expected Expected results.
86
     * @return void
87
     */
88
    public function test_state_store_get(array $info, bool $expected): void {
89
        $this->resetAfterTest();
90
 
91
        // Scenario.
92
        $this->setAdminUser();
93
        // Add, at least, one xAPI state record to database (with the default values).
94
        test_helper::create_state([], true);
95
 
96
        // Perform test.
97
        $component = $info['component'] ?? 'fake_component';
98
        $state = test_helper::create_state($info);
99
        // Remove statedata from the state object, to guarantee the get method is working as expected.
100
        $state->set_state_data(null);
101
        $store = new state_store($component);
102
        $result = $store->get($state);
103
 
104
        // Check the returned state has the expected values.
105
        if ($expected) {
106
            $this->assertEquals(json_encode($state->jsonSerialize()), json_encode($result->jsonSerialize()));
107
        } else {
108
            $this->assertNull($result);
109
        }
110
    }
111
 
112
    /**
113
     * Data provider for the test_state_store_delete and test_state_store_get tests.
114
     *
115
     * @return array
116
     */
1441 ariadna 117
    public static function states_provider(): array {
1 efrain 118
        return [
119
            'Existing and valid state' => [
120
                'info' => [],
121
                'expected' => true,
122
            ],
123
            'No state (wrong activityid)' => [
124
                'info' => ['activity' => item_activity::create_from_id('1')],
125
                'expected' => false,
126
            ],
127
            'No state (wrong stateid)' => [
128
                'info' => ['stateid' => 'food'],
129
                'expected' => false,
130
            ],
131
            'No state (wrong component)' => [
132
                'info' => ['component' => 'mod_h5pactivity'],
133
                'expected' => false,
134
            ],
135
        ];
136
    }
137
 
138
    /**
139
     * Testing put method.
140
     *
141
     * @dataProvider put_states_provider
142
     * @param array $info Array of overriden state data.
143
     * @param string $expected Expected results.
144
     * @return void
145
     */
146
    public function test_state_store_put(array $info, string $expected): void {
147
        global $DB;
148
 
149
        $this->resetAfterTest();
150
 
151
        // Scenario.
152
        $this->setAdminUser();
153
        // Add, at least, one xAPI state record to database (with the default values).
154
        test_helper::create_state([], true);
155
 
156
        // Get current states in database.
157
        $currentstates = $DB->count_records('xapi_states');
158
 
159
        // Perform test.
160
        $component = $info['component'] ?? 'fake_component';
161
        $state = test_helper::create_state($info);
162
        $store = new state_store($component);
163
        $result = $store->put($state);
164
 
165
        // Check the state has been added/updated.
166
        $this->assertTrue($result);
167
        $recordsnum = $DB->count_records('xapi_states');
168
        $params = [
169
            'component' => $component,
170
            'userid' => $state->get_user()->id,
171
            'itemid' => $state->get_activity_id(),
172
            'stateid' => $state->get_state_id(),
173
            'registration' => $state->get_registration(),
174
        ];
175
        $records = $DB->get_records('xapi_states', $params);
176
        $record = reset($records);
177
        if ($expected === 'added') {
178
            $this->assertEquals($currentstates + 1, $recordsnum);
179
            $this->assertEquals($record->timecreated, $record->timemodified);
180
        } else if ($expected === 'updated') {
181
            $this->assertEquals($currentstates, $recordsnum);
182
            $this->assertGreaterThanOrEqual($record->timecreated, $record->timemodified);
183
        }
184
 
185
        $this->assertEquals($component, $record->component);
186
        $this->assertEquals($state->get_activity_id(), $record->itemid);
187
        $this->assertEquals($state->get_user()->id, $record->userid);
188
        $this->assertEquals(json_encode($state->jsonSerialize()), $record->statedata);
189
        $this->assertEquals($state->get_registration(), $record->registration);
190
    }
191
 
192
    /**
193
     * Data provider for the test_state_store_put tests.
194
     *
195
     * @return array
196
     */
1441 ariadna 197
    public static function put_states_provider(): array {
1 efrain 198
        return [
199
            'Update existing state' => [
200
                'info' => [],
201
                'expected' => 'updated',
202
            ],
203
            'Update existing state (change statedata)' => [
204
                'info' => ['statedata' => '{"progress":0,"answers":[[["BB"],[""]],[{"answers":[]}]],"answered":[true,false]}'],
205
                'expected' => 'updated',
206
            ],
207
            'Add state (with different itemid)' => [
208
                'info' => ['activity' => item_activity::create_from_id('1')],
209
                'expected' => 'added',
210
            ],
211
            'Add state (with different stateid)' => [
212
                'info' => ['stateid' => 'food'],
213
                'expected' => 'added',
214
            ],
215
            'Add state (with different component)' => [
216
                'info' => ['component' => 'mod_h5pactivity'],
217
                'expected' => 'added',
218
            ],
219
        ];
220
    }
221
 
222
    /**
223
     * Testing reset method.
224
     *
225
     * @dataProvider reset_wipe_states_provider
226
     * @param array $info Array of overriden state data.
227
     * @param int $expected The states that will be reset.
228
     * @return void
229
     */
230
    public function test_state_store_reset(array $info, int $expected): void {
231
        global $DB;
232
 
233
        $this->resetAfterTest();
234
 
235
        // Scenario.
236
        $this->setAdminUser();
237
        $other = $this->getDataGenerator()->create_user();
238
 
239
        // Add a few xAPI state records to database.
240
        test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
241
        test_helper::create_state(['activity' => item_activity::create_from_id('2'), 'stateid' => 'paella'], true);
242
        test_helper::create_state([
243
            'activity' => item_activity::create_from_id('3'),
244
            'agent' => item_agent::create_from_user($other),
245
            'stateid' => 'paella',
246
            'registration' => 'ABC',
247
        ], true);
248
        test_helper::create_state([
249
            'activity' => item_activity::create_from_id('4'),
250
            'agent' => item_agent::create_from_user($other),
251
        ], true);
252
        test_helper::create_state(['activity' => item_activity::create_from_id('5'), 'component' => 'my_component'], true);
253
        test_helper::create_state([
254
            'activity' => item_activity::create_from_id('6'),
255
            'component' => 'my_component',
256
            'stateid' => 'paella',
257
            'agent' => item_agent::create_from_user($other),
258
        ], true);
259
 
260
        // Get current states in database.
261
        $currentstates = $DB->count_records('xapi_states');
262
 
263
        // Perform test.
264
        $component = $info['component'] ?? 'fake_component';
265
        $itemid = $info['activity'] ?? null;
266
        $userid = (array_key_exists('agent', $info) && $info['agent'] === 'other') ? $other->id : null;
267
        $stateid = $info['stateid'] ?? null;
268
        $registration = $info['registration'] ?? null;
269
        $store = new state_store($component);
270
        $store->reset($itemid, $userid, $stateid, $registration);
271
 
272
        // Check the states haven't been removed.
273
        $this->assertCount($currentstates, $DB->get_records('xapi_states'));
274
        $records = $DB->get_records_select('xapi_states', 'statedata IS NULL');
275
        $this->assertCount($expected, $records);
276
    }
277
 
278
    /**
279
     * Testing wipe method.
280
     *
281
     * @dataProvider reset_wipe_states_provider
282
     * @param array $info Array of overriden state data.
283
     * @param int $expected The removed states.
284
     * @return void
285
     */
286
    public function test_state_store_wipe(array $info, int $expected): void {
287
        global $DB;
288
 
289
        $this->resetAfterTest();
290
 
291
        // Scenario.
292
        $this->setAdminUser();
293
        $other = $this->getDataGenerator()->create_user();
294
 
295
        // Add a few xAPI state records to database.
296
        test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
297
        test_helper::create_state(['activity' => item_activity::create_from_id('2'), 'stateid' => 'paella'], true);
298
        test_helper::create_state([
299
            'activity' => item_activity::create_from_id('3'),
300
            'agent' => item_agent::create_from_user($other),
301
            'stateid' => 'paella',
302
            'registration' => 'ABC',
303
        ], true);
304
        test_helper::create_state([
305
            'activity' => item_activity::create_from_id('4'),
306
            'agent' => item_agent::create_from_user($other),
307
        ], true);
308
        test_helper::create_state(['activity' => item_activity::create_from_id('5'), 'component' => 'my_component'], true);
309
        test_helper::create_state([
310
            'activity' => item_activity::create_from_id('6'),
311
            'component' => 'my_component',
312
            'stateid' => 'paella',
313
            'agent' => item_agent::create_from_user($other),
314
        ], true);
315
 
316
        // Get current states in database.
317
        $currentstates = $DB->count_records('xapi_states');
318
 
319
        // Perform test.
320
        $component = $info['component'] ?? 'fake_component';
321
        $itemid = $info['activity'] ?? null;
322
        $userid = (array_key_exists('agent', $info) && $info['agent'] === 'other') ? $other->id : null;
323
        $stateid = $info['stateid'] ?? null;
324
        $registration = $info['registration'] ?? null;
325
        $store = new state_store($component);
326
        $store->wipe($itemid, $userid, $stateid, $registration);
327
 
328
        // Check the states have been removed.
329
        $records = $DB->get_records('xapi_states');
330
        $this->assertCount($currentstates - $expected, $records);
331
    }
332
 
333
    /**
334
     * Data provider for the test_state_store_reset and test_state_store_wipe tests.
335
     *
336
     * @return array
337
     */
1441 ariadna 338
    public static function reset_wipe_states_provider(): array {
1 efrain 339
        return [
340
            'With fake_component' => [
341
                'info' => [],
342
                'expected' => 4,
343
            ],
344
            'With my_component' => [
345
                'info' => ['component' => 'my_component'],
346
                'expected' => 2,
347
            ],
348
            'With unexisting_component' => [
349
                'info' => ['component' => 'unexisting_component'],
350
                'expected' => 0,
351
            ],
352
            'Existing activity' => [
353
                'info' => ['activity' => '1'],
354
                'expected' => 1,
355
            ],
356
            'Unexisting activity' => [
357
                'info' => ['activity' => '1111'],
358
                'expected' => 0,
359
            ],
360
            'Existing userid' => [
361
                'info' => ['agent' => 'other'],
362
                'expected' => 2,
363
            ],
364
            'Existing stateid' => [
365
                'info' => ['stateid' => 'paella'],
366
                'expected' => 2,
367
            ],
368
            'Unexisting stateid' => [
369
                'info' => ['stateid' => 'chorizo'],
370
                'expected' => 0,
371
            ],
372
            'Existing registration' => [
373
                'info' => ['registration' => 'ABC'],
374
                'expected' => 1,
375
            ],
376
            'Uxexisting registration' => [
377
                'info' => ['registration' => 'XYZ'],
378
                'expected' => 0,
379
            ],
380
            'Existing stateid combined with activity' => [
381
                'info' => ['activity' => '3', 'stateid' => 'paella'],
382
                'expected' => 1,
383
            ],
384
            'Uxexisting stateid combined with activity' => [
385
                'info' => ['activity' => '1', 'stateid' => 'paella'],
386
                'expected' => 0,
387
            ],
388
        ];
389
    }
390
 
391
    /**
392
     * Testing cleanup method.
393
     *
394
     * @return void
395
     */
396
    public function test_state_store_cleanup(): void {
397
        global $DB;
398
 
399
        $this->resetAfterTest();
400
 
401
        // Scenario.
402
        $this->setAdminUser();
403
        $other = $this->getDataGenerator()->create_user();
404
 
405
        // Add a few xAPI state records to database.
406
        test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
407
        test_helper::create_state(['activity' => item_activity::create_from_id('2')], true);
408
        test_helper::create_state(['activity' => item_activity::create_from_id('3')], true);
409
        test_helper::create_state(['activity' => item_activity::create_from_id('4')], true);
410
        test_helper::create_state(['activity' => item_activity::create_from_id('5'), 'component' => 'my_component'], true);
411
        test_helper::create_state(['activity' => item_activity::create_from_id('6'), 'component' => 'my_component'], true);
412
 
413
        // Get current states in database.
414
        $currentstates = $DB->count_records('xapi_states');
415
 
416
        // Perform test.
417
        $component = 'fake_component';
418
        $store = new state_store($component);
419
        $store->cleanup();
420
 
421
        // Check no state has been removed (because the entries are not old enough).
422
        $this->assertEquals($currentstates, $DB->count_records('xapi_states'));
423
 
424
        // Make the existing state entries older.
425
        $timepast = time() - 2;
426
        $DB->set_field('xapi_states', 'timecreated', $timepast);
427
        $DB->set_field('xapi_states', 'timemodified', $timepast);
428
 
429
        // Create 1 more state, that shouldn't be removed after the cleanup.
430
        test_helper::create_state(['activity' => item_activity::create_from_id('7')], true);
431
 
432
        // Set the config to remove states older than 1 second.
433
        set_config('xapicleanupperiod', 1);
434
 
435
        // Check old states for fake_component have been removed.
436
        $currentstates = $DB->count_records('xapi_states');
437
        $store->cleanup();
438
        $this->assertEquals($currentstates - 4, $DB->count_records('xapi_states'));
439
        $this->assertEquals(1, $DB->count_records('xapi_states', ['component' => $component]));
440
        $this->assertEquals(2, $DB->count_records('xapi_states', ['component' => 'my_component']));
441
    }
442
 
443
    /**
444
     * Testing get_state_ids method.
445
     *
446
     * @dataProvider get_state_ids_provider
447
     * @param string $component
448
     * @param string|null $itemid
449
     * @param string|null $registration
450
     * @param bool|null $since
451
     * @param array $expected the expected result
452
     * @return void
453
     */
454
    public function test_get_state_ids(
455
        string $component,
456
        ?string $itemid,
457
        ?string $registration,
458
        ?bool $since,
459
        array $expected,
460
    ): void {
461
        global $DB, $USER;
462
 
463
        $this->resetAfterTest();
464
 
465
        // Scenario.
466
        $this->setAdminUser();
467
        $other = $this->getDataGenerator()->create_user();
468
 
469
        // Add a few xAPI state records to database.
470
        $states = [
471
            ['activity' => item_activity::create_from_id('1'), 'stateid' => 'aa'],
472
            ['activity' => item_activity::create_from_id('1'), 'registration' => 'reg', 'stateid' => 'bb'],
473
            ['activity' => item_activity::create_from_id('1'), 'registration' => 'reg2', 'stateid' => 'cc'],
474
            ['activity' => item_activity::create_from_id('2'), 'registration' => 'reg', 'stateid' => 'dd'],
475
            ['activity' => item_activity::create_from_id('3'), 'stateid' => 'ee'],
476
            ['activity' => item_activity::create_from_id('4'), 'component' => 'other', 'stateid' => 'ff'],
477
        ];
478
        foreach ($states as $state) {
479
            test_helper::create_state($state, true);
480
        }
481
 
482
        // Make all existing state entries older except form two.
483
        $currenttime = time();
484
        $timepast = $currenttime - 5;
485
        $DB->set_field('xapi_states', 'timecreated', $timepast);
486
        $DB->set_field('xapi_states', 'timemodified', $timepast);
487
        $DB->set_field('xapi_states', 'timemodified', $currenttime, ['stateid' => 'aa']);
488
        $DB->set_field('xapi_states', 'timemodified', $currenttime, ['stateid' => 'bb']);
489
        $DB->set_field('xapi_states', 'timemodified', $currenttime, ['stateid' => 'dd']);
490
 
491
        // Perform test.
492
        $sincetime = ($since) ? $currenttime - 1 : null;
493
        $store = new state_store($component);
494
        $stateids = $store->get_state_ids($itemid, $USER->id, $registration, $sincetime);
495
        sort($stateids);
496
 
497
        $this->assertEquals($expected, $stateids);
498
    }
499
 
500
    /**
501
     * Data provider for the test_get_state_ids.
502
     *
503
     * @return array
504
     */
1441 ariadna 505
    public static function get_state_ids_provider(): array {
1 efrain 506
        return [
507
            'empty_component' => [
508
                'component' => 'empty_component',
509
                'itemid' => null,
510
                'registration' => null,
511
                'since' => null,
512
                'expected' => [],
513
            ],
514
            'filter_by_itemid' => [
515
                'component' => 'fake_component',
516
                'itemid' => '1',
517
                'registration' => null,
518
                'since' => null,
519
                'expected' => ['aa', 'bb', 'cc'],
520
            ],
521
            'filter_by_registration' => [
522
                'component' => 'fake_component',
523
                'itemid' => null,
524
                'registration' => 'reg',
525
                'since' => null,
526
                'expected' => ['bb', 'dd'],
527
            ],
528
            'filter_by_since' => [
529
                'component' => 'fake_component',
530
                'itemid' => null,
531
                'registration' => null,
532
                'since' => true,
533
                'expected' => ['aa', 'bb', 'dd'],
534
            ],
535
            'filter_by_itemid_and_registration' => [
536
                'component' => 'fake_component',
537
                'itemid' => '1',
538
                'registration' => 'reg',
539
                'since' => null,
540
                'expected' => ['bb'],
541
            ],
542
            'filter_by_itemid_registration_since' => [
543
                'component' => 'fake_component',
544
                'itemid' => '1',
545
                'registration' => 'reg',
546
                'since' => true,
547
                'expected' => ['bb'],
548
            ],
549
            'filter_by_registration_since' => [
550
                'component' => 'fake_component',
551
                'itemid' => null,
552
                'registration' => 'reg',
553
                'since' => true,
554
                'expected' => ['bb', 'dd'],
555
            ],
556
        ];
557
    }
558
 
559
    /**
560
     * Test delete with a non numeric activity id.
561
     *
562
     * The default state store only allows integer itemids.
563
     *
564
     * @dataProvider invalid_activityid_format_provider
565
     * @param string $operation the method to execute
566
     * @param bool $usestate if the param is a state or the activity id
567
     */
568
    public function test_invalid_activityid_format(string $operation, bool $usestate = false): void {
569
        $this->resetAfterTest();
570
        $this->setAdminUser();
571
 
572
        $state = test_helper::create_state([
573
            'activity' => item_activity::create_from_id('notnumeric'),
574
        ]);
575
        $param = ($usestate) ? $state : 'notnumeric';
576
 
577
        $this->expectException(xapi_exception::class);
578
        $store = new state_store('fake_component');
579
        $store->$operation($param);
580
    }
581
 
582
    /**
583
     * Data provider for test_invalid_activityid_format.
584
     *
585
     * @return array
586
     */
1441 ariadna 587
    public static function invalid_activityid_format_provider(): array {
1 efrain 588
        return [
589
            'delete' => [
590
                'operation' => 'delete',
591
                'usestate' => true,
592
            ],
593
            'get' => [
594
                'operation' => 'get',
595
                'usestate' => true,
596
            ],
597
            'put' => [
598
                'operation' => 'put',
599
                'usestate' => true,
600
            ],
601
            'reset' => [
602
                'operation' => 'reset',
603
                'usestate' => false,
604
            ],
605
            'wipe' => [
606
                'operation' => 'wipe',
607
                'usestate' => false,
608
            ],
609
            'get_state_ids' => [
610
                'operation' => 'get_state_ids',
611
                'usestate' => false,
612
            ],
613
        ];
614
    }
615
}