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;
18
 
19
use core_xapi\xapi_exception;
20
use core_xapi\local\statement;
21
use core_xapi\local\statement\item_agent;
22
use core_xapi\local\statement\item_verb;
23
use core_xapi\local\statement\item_activity;
24
use advanced_testcase;
25
use core_xapi\local\state;
26
use stdClass;
27
 
28
/**
29
 * Contains test cases for testing xAPI handler base methods.
30
 *
31
 * @package    core_xapi
32
 * @since      Moodle 3.9
33
 * @covers     \core_xapi\handler
34
 * @copyright  2020 Ferran Recio
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
1441 ariadna 37
final class handler_test extends advanced_testcase {
1 efrain 38
 
39
    /**
40
     * Setup to ensure that fixtures are loaded.
41
     */
42
    public static function setUpBeforeClass(): void {
43
        global $CFG;
44
        require_once($CFG->dirroot.'/lib/xapi/tests/helper.php');
1441 ariadna 45
        parent::setUpBeforeClass();
1 efrain 46
    }
47
 
48
    /**
49
     * Test handler creation.
50
     */
11 efrain 51
    public function test_handler_create(): void {
1 efrain 52
        // Get an existent handler.
53
        $handler = handler::create('fake_component');
54
        $this->assertEquals(get_class($handler), 'fake_component\\xapi\\handler');
55
 
56
        // Get a non existent handler.
57
        $this->expectException(xapi_exception::class);
58
        $value = handler::create('potato_omelette');
59
    }
60
 
61
    /**
62
     * Test xAPI support.
63
     */
11 efrain 64
    public function test_supports_xapi(): void {
1 efrain 65
        // Get an existent handler.
66
        $result = handler::supports_xapi('fake_component');
67
        $this->assertTrue($result);
68
 
69
        // Get a non existent handler.
70
        $result = handler::supports_xapi('potato_omelette');
71
        $this->assertFalse($result);
72
    }
73
 
74
    /**
75
     * Test support group.
76
     */
11 efrain 77
    public function test_support_group_actor(): void {
1 efrain 78
        global $CFG;
79
        // Get an existent handler.
80
        $this->resetAfterTest();
81
        $handler = handler::create('fake_component');
82
        $this->assertEquals(get_class($handler), 'fake_component\\xapi\\handler');
83
        $CFG->xapitestforcegroupactors = false;
84
        $this->assertEquals(false, $handler->supports_group_actors());
85
    }
86
 
87
    /**
88
     * Test for process_statements method.
89
     */
11 efrain 90
    public function test_process_statements(): void {
1 efrain 91
 
92
        $this->resetAfterTest();
93
        $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
94
 
95
        $user = $this->getDataGenerator()->create_user();
96
 
97
        $testhelper = new test_helper();
98
        $testhelper->init_log();
99
 
100
        // Generate a 2 statements array (one accepted one not).
101
        $statements = [];
102
 
103
        $statement = new statement();
104
        $statement->set_actor(item_agent::create_from_user($user));
105
        $statement->set_verb(item_verb::create_from_id('cook'));
106
        $statement->set_object(item_activity::create_from_id('paella'));
107
        $statements[] = $statement;
108
 
109
        $statement2 = new statement();
110
        $statement2->set_actor(item_agent::create_from_user($user));
111
        $statement2->set_verb(item_verb::create_from_id('invalid'));
112
        $statement2->set_object(item_activity::create_from_id('paella'));
113
        $statements[] = $statement2;
114
 
115
        $handler = handler::create('fake_component');
116
        $result = $handler->process_statements($statements);
117
 
118
        // Check results.
119
        $this->assertCount(2, $result);
120
        $this->assertEquals(true, $result[0]);
121
        $this->assertEquals(false, $result[1]);
122
 
123
        // Check log entries.
124
        /** @var \core_xapi\event\xapi_test_statement_post $log */
125
        $log = $testhelper->get_last_log_entry();
126
        $this->assertNotEmpty($log);
127
 
128
        // Validate statement information on log.
129
        $value = $log->get_name();
130
        $this->assertEquals($value, 'xAPI test statement');
131
        $value = $log->get_description();
132
        // Due to logstore limitation, event must use a real component (core_xapi).
133
        $this->assertEquals($value, 'User \''.$user->id.'\' send a statement to component \'core_xapi\'');
134
        $this->assertTrue($log->compare_statement($statement));
135
    }
136
 
137
    /**
138
     * Testing save_state method.
139
     */
140
    public function test_save_state(): void {
141
        global $DB;
142
 
143
        $this->resetAfterTest();
144
 
145
        // Scenario.
146
        $this->setAdminUser();
147
        $component = 'fake_component';
148
        $handler = handler::create($component);
149
 
150
        // Check the state has been added.
151
        $state = test_helper::create_state();
152
        $this->assertEquals(0, $DB->count_records('xapi_states'));
153
        $result = $handler->save_state($state);
154
        $this->assertTrue($result);
155
        $records = $DB->get_records('xapi_states');
156
        $this->assertCount(1, $records);
157
        $record = reset($records);
158
        $this->check_state($component, $state, $record);
159
 
160
        // Check the state has been updated.
161
        $statedata = '{"progress":0,"answers":[[["BB"],[""]],[{"answers":[]}]],"answered":[true,false]}';
162
        $state->set_state_data(json_decode($statedata));
163
        $result = $handler->save_state($state);
164
        $this->assertTrue($result);
165
        $records = $DB->get_records('xapi_states');
166
        $this->assertCount(1, $records);
167
        $record = reset($records);
168
        $this->check_state($component, $state, $record);
169
 
170
        // Check an exception is thrown when the state is not valid.
171
        $this->expectException(xapi_exception::class);
172
        $state = test_helper::create_state(['stateid' => 'paella']);
173
        $result = $handler->save_state($state);
174
    }
175
 
176
    /**
177
     * Testing load_state method.
178
     */
179
    public function test_load_state(): void {
180
        global $DB;
181
 
182
        $this->resetAfterTest();
183
 
184
        // Scenario.
185
        $this->setAdminUser();
186
        $component = 'fake_component';
187
        $handler = handler::create($component);
188
 
189
        // Check the state is not found (when there are no states).
190
        $state = test_helper::create_state();
191
        $state->set_state_data(null);
192
        $this->assertEquals(0, $DB->count_records('xapi_states'));
193
        $result = $handler->load_state($state);
194
        $this->assertEquals(0, $DB->count_records('xapi_states'));
195
        $this->assertNull($result);
196
 
197
        // Add, at least, one xAPI state record to database (with the default values).
198
        test_helper::create_state([], true);
199
 
200
        // Check the state is found when it exists.
201
        $result = $handler->load_state($state);
202
        $records = $DB->get_records('xapi_states');
203
        $this->assertCount(1, $records);
204
        $record = reset($records);
205
        $this->check_state($component, $state, $record);
206
        $this->assertEquals($state->jsonSerialize(), $result->jsonSerialize());
207
 
208
        // Check the state is not found when it doesn't exist.
209
        $state = test_helper::create_state(['activity' => item_activity::create_from_id('1')]);
210
        $state->set_state_data(null);
211
        $result = $handler->load_state($state);
212
        $records = $DB->get_records('xapi_states');
213
        $this->assertCount(1, $DB->get_records('xapi_states'));
214
        $this->assertNull($result);
215
 
216
        // Check an exception is thrown when the state is not valid.
217
        $this->expectException(xapi_exception::class);
218
        $state = test_helper::create_state(['stateid' => 'paella']);
219
        $result = $handler->load_state($state);
220
    }
221
 
222
    /**
223
     * Testing delete_state method.
224
     */
225
    public function test_delete_state(): void {
226
        global $DB;
227
 
228
        $this->resetAfterTest();
229
 
230
        // Scenario.
231
        $this->setAdminUser();
232
        $component = 'fake_component';
233
        $handler = handler::create($component);
234
 
235
        // Check the state is not deleted (when there are no states).
236
        $state = test_helper::create_state();
237
        $this->assertEquals(0, $DB->count_records('xapi_states'));
238
        $result = $handler->delete_state($state);
239
        $this->assertTrue($result);
240
        $this->assertEquals(0, $DB->count_records('xapi_states'));
241
 
242
        // Add, at least, one xAPI state record to database (with the default values).
243
        test_helper::create_state([], true);
244
 
245
        // Check the state is not deleted if the given state doesn't meet its values.
246
        $state2 = test_helper::create_state(['activity' => item_activity::create_from_id('1')]);
247
        $result = $handler->delete_state($state2);
248
        $this->assertTrue($result);
249
        $this->assertCount(1, $DB->get_records('xapi_states'));
250
 
251
        // Check the state is deleted if it exists.
252
        $result = $handler->delete_state($state);
253
        $this->assertTrue($result);
254
        $this->assertCount(0, $DB->get_records('xapi_states'));
255
 
256
        // Check an exception is thrown when the state is not valid.
257
        $this->expectException(xapi_exception::class);
258
        $state = test_helper::create_state(['stateid' => 'paella']);
259
        $result = $handler->delete_state($state);
260
    }
261
 
262
    /**
263
     * Testing reset_states method.
264
     */
265
    public function test_reset_states(): void {
266
        global $DB;
267
 
268
        $this->resetAfterTest();
269
 
270
        // Scenario.
271
        $this->setAdminUser();
272
        $component = 'fake_component';
273
        $handler = handler::create($component);
274
 
275
        // Check the state is not reset (when there are no states).
276
        $this->assertCount(0, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
277
        $handler->reset_states();
278
        $this->assertCount(0, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
279
 
280
        // Add, at least, one xAPI state record to database (with the default values).
281
        test_helper::create_state([], true);
282
 
283
        // Check the state is not reset if the given state doesn't meet its values.
284
        $handler->reset_states('1');
285
        $this->assertCount(1, $DB->get_records('xapi_states'));
286
        $this->assertCount(0, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
287
 
288
        // Check the state is reset if it exists.
289
        $handler->reset_states();
290
        $this->assertCount(1, $DB->get_records('xapi_states'));
291
        $this->assertCount(1, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
292
 
293
        // Check the state is reset too when using some of the given parameters.
294
        test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
295
        $handler->reset_states('1');
296
        $this->assertCount(2, $DB->get_records('xapi_states'));
297
        $this->assertCount(2, $DB->get_records_select('xapi_states', 'statedata IS NULL'));
298
    }
299
 
300
    /**
301
     * Testing wipe_states method.
302
     */
303
    public function test_wipe_states(): void {
304
        global $DB;
305
 
306
        $this->resetAfterTest();
307
 
308
        // Scenario.
309
        $this->setAdminUser();
310
        $component = 'fake_component';
311
        $handler = handler::create($component);
312
 
313
        // Check the state is not wiped (when there are no states).
314
        $this->assertCount(0, $DB->get_records('xapi_states'));
315
        $handler->wipe_states();
316
        $this->assertCount(0, $DB->get_records('xapi_states'));
317
 
318
        // Add, at least, one xAPI state record to database (with the default values).
319
        test_helper::create_state([], true);
320
 
321
        // Check the state is not wiped if the given state doesn't meet its values.
322
        $handler->wipe_states('1');
323
        $this->assertCount(1, $DB->get_records('xapi_states'));
324
 
325
        // Check the state is wiped if it exists.
326
        $handler->wipe_states();
327
        $this->assertCount(0, $DB->get_records('xapi_states'));
328
 
329
        // Check the state is wiped too when using some of the given parameters.
330
        test_helper::create_state(['activity' => item_activity::create_from_id('1')], true);
331
        $this->assertCount(1, $DB->get_records('xapi_states'));
332
        $handler->wipe_states('1');
333
        $this->assertCount(0, $DB->get_records('xapi_states'));
334
    }
335
 
336
    /**
337
     * Check if the given state and record are equals.
338
     *
339
     * @param string $component The component name in frankenstyle.
340
     * @param state $state The state to check.
341
     * @param stdClass $record The record to be compared with the state.
342
     */
343
    private function check_state(string $component, state $state, stdClass $record): void {
344
        $this->assertEquals($component, $record->component);
345
        $this->assertEquals($state->get_activity_id(), $record->itemid);
346
        $this->assertEquals($state->get_user()->id, $record->userid);
347
        $this->assertEquals(json_encode($state->jsonSerialize()), $record->statedata);
348
        $this->assertEquals($state->get_registration(), $record->registration);
349
    }
350
 
351
}