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\external;
18
 
19
use core\context\module;
20
use core_external\external_api;
21
use core_xapi\iri;
22
use core_xapi\local\statement\item_activity;
23
use core_xapi\local\statement\item_agent;
24
use core_xapi\test_helper;
25
use core_xapi\xapi_exception;
26
use externallib_advanced_testcase;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
global $CFG;
31
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
 
33
/**
34
 * Unit tests for xAPI delete states webservice.
35
 *
36
 * @package    core_xapi
37
 * @covers     \core_xapi\external\delete_states
38
 * @since      Moodle 4.3
39
 * @copyright  2023 Laurent David <laurent.david@moodle.com>
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
1441 ariadna 42
final class delete_states_test extends externallib_advanced_testcase {
1 efrain 43
 
44
    /**
45
     * Setup to ensure that fixtures are loaded.
46
     */
47
    public static function setUpBeforeClass(): void {
48
        global $CFG;
49
        require_once($CFG->dirroot . '/lib/xapi/tests/helper.php');
1441 ariadna 50
        parent::setUpBeforeClass();
1 efrain 51
    }
52
 
53
    /**
54
     * Testing different component names on valid states.
55
     *
56
     * @dataProvider components_provider
57
     * @param string $component component name
58
     * @param object|null $expected expected results
59
     */
60
    public function test_component_names(string $component, ?object $expected): void {
61
        global $DB, $USER;
62
        $this->resetAfterTest();
63
 
64
        // Scenario.
65
        $this->setAdminUser();
66
 
67
        // Perform test.
68
        $info = [
69
            'agent' => item_agent::create_from_user($USER),
70
            'activity' => item_activity::create_from_id('12345'),
71
        ];
72
        test_helper::create_state($info, true);
73
        if (!empty($expected->exception)) {
74
            $this->expectException($expected->exception);
75
        }
76
        $this->execute($component,
77
            iri::generate($info['activity']->get_id(), 'activity'),
78
            json_encode($info['agent'])
79
        );
80
 
81
        if (isset($expected->expectedcount)) {
82
            $this->assertEquals($expected->expectedcount, $DB->record_exists('xapi_states', []));
83
        }
84
    }
85
 
86
    /**
87
     * This function execute the delete_states_data
88
     *
89
     * @param string $component component name
90
     * @param string $activityiri
91
     * @param string $agent
92
     * @param string|null $registration
93
     * @return array empty array
94
     */
95
    private function execute(string $component,
96
        string $activityiri,
97
        string $agent,
98
        ?string $registration = null
99
    ): void {
100
        $external = $this->get_external_class();
101
        $external::execute(
102
            $component,
103
            $activityiri,
104
            $agent,
105
            $registration
106
        );
107
    }
108
 
109
    /**
110
     * Return a xAPI external webservice class to operate.
111
     *
112
     * The test needs to fake a component in order to test without
113
     * using a real one. This way if in the future any component
114
     * implement it's xAPI handler this test will continue working.
115
     *
116
     * @return delete_states the external class
117
     */
118
    private function get_external_class(): delete_states {
119
        $ws = new class extends delete_states {
120
            /**
121
             * Method to override validate_component.
122
             *
123
             * @param string $component The component name in frankenstyle.
124
             */
125
            protected static function validate_component(string $component): void {
126
                if ($component != 'fake_component') {
127
                    parent::validate_component($component);
128
                }
129
            }
130
        };
131
        return $ws;
132
    }
133
 
134
    /**
135
     * Data provider for the test_component_names tests.
136
     *
137
     * @return  array
138
     */
1441 ariadna 139
    public static function components_provider(): array {
1 efrain 140
        return [
141
            'Inexistent component' => [
142
                'component' => 'inexistent_component',
143
                'expected' => (object) ['exception' => xapi_exception::class],
144
            ],
145
            'Compatible component' => [
146
                'component' => 'fake_component',
147
                'expected' => (object) ['expectedcount' => 0],
148
            ],
149
            'Incompatible component' => [
150
                'component' => 'core_xapi',
151
                'expected' => (object) ['exception' => xapi_exception::class],
152
            ],
153
        ];
154
    }
155
 
156
    /**
157
     * Testing invalid agent.
158
     *
159
     */
160
    public function test_invalid_agent(): void {
161
        $this->resetAfterTest();
162
 
163
        // Scenario.
164
        $this->setAdminUser();
165
        $other = $this->getDataGenerator()->create_user();
166
 
167
        // Invalid agent (use different user, instead of the current one).
168
        $info = [
169
            'agent' => item_agent::create_from_user($other),
170
            'activity' => item_activity::create_from_id('12345'),
171
        ];
172
        test_helper::create_state($info, true);
173
        $this->expectException(xapi_exception::class);
174
        $this->execute(
175
            'fake_component',
176
            iri::generate($info['activity']->get_id(), 'activity'),
177
            json_encode($info['agent'])
178
        );
179
    }
180
 
181
    /**
182
     * Testing deleting states
183
     *
184
     * @dataProvider states_provider
185
     * @param string $testedusername
186
     * @param string $testedcomponent
187
     * @param string $testedactivityname
188
     * @param array $states
189
     * @param array $expectedstates
190
     * @return void
191
     */
1441 ariadna 192
    public function test_delete_states(
193
        string $testedusername,
1 efrain 194
        string $testedcomponent,
195
        string $testedactivityname,
196
        array $states,
197
        array $expectedstates
198
    ): void {
199
        global $DB;
200
        $this->resetAfterTest();
201
 
202
        // Scenario.
203
        $this->setAdminUser();
204
        $course = $this->getDataGenerator()->create_course();
205
 
206
        $activities = [];
207
        $users = [];
208
        // Create a set of states for different users and components.
209
        foreach ($states as $stateinfo) {
210
            $params = [
211
                'component' => $stateinfo['component'] ?? 'mod_h5pactivity',
212
            ];
213
            $uname = $stateinfo['user'];
214
            $user = $users[$uname] ?? null;
215
            if (empty($user)) {
216
                $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
217
                $users[$uname] = $user;
218
            }
219
            $activityname = $stateinfo['activity'];
220
            $activity = $activities[$activityname] ?? null;
221
            if (empty($activity)) {
222
                if (empty($stateinfo['activityid'])) {
223
                    $activity = $this->getDataGenerator()->create_module('h5pactivity', ['course' => $course]);
224
                    $activitycontext = module::instance($activity->cmid);
225
                    $activities[$activityname] = item_activity::create_from_id($activitycontext->id);
226
                } else {
227
                    $activities[$activityname] = item_activity::create_from_id($stateinfo['activityid']);
228
                }
229
            }
230
            $params['activity'] = $activities[$activityname];
231
            $params['agent'] = item_agent::create_from_user($user);
232
            test_helper::create_state($params, true);
233
        }
234
        if (empty($users[$testedusername])) {
235
            $user = $this->getDataGenerator()->create_and_enrol($course, 'student');
236
            $users[$testedusername] = $user;
237
        }
238
        $this->setUser($users[$testedusername]);
239
        $activity = $activities[$testedactivityname];
240
        $activityiri = iri::generate($activity->get_id(), 'activity');
241
        $agent = json_encode(item_agent::create_from_user($users[$testedusername]));
242
        $this->execute($testedcomponent,
243
            $activityiri,
244
            $agent);
245
 
246
        $statesleft = $DB->get_records('xapi_states');
247
        // Check that we have the expected leftover records.
248
        $this->assertCount(count($expectedstates), $statesleft);
249
        foreach ($expectedstates as $expectedstate) {
250
            $expectedactivityid = $activities[$expectedstate['activity']]->get_id();
251
            $expecteduserid = $users[$expectedstate['user']]->id;
252
            $found = false;
253
            foreach ($statesleft as $state) {
254
                if ($state->userid == $expecteduserid && $state->itemid == $expectedactivityid) {
255
                    $found = true;
256
                    break;
257
                }
258
            }
259
            $this->assertTrue($found, 'State not found:' . json_encode($statesleft));
260
        }
261
    }
262
 
263
    /**
264
     * Data provider for the test_get_state tests.
265
     *
266
     * @return array
267
     */
1441 ariadna 268
    public static function states_provider(): array {
1 efrain 269
        return [
270
            'Activities with different users and components' => [
1441 ariadna 271
                'testedusername' => 'user1',
272
                'testedcomponent' => 'mod_h5pactivity',
273
                'testedactivityname' => 'Activity 1',
1 efrain 274
                'states' => [
275
                    [
276
                        'user' => 'user1',
277
                        'activity' => 'Activity 1',
278
                        'component' => 'mod_h5pactivity'
279
                    ],
280
                    [
281
                        'user' => 'user2',
282
                        'activity' => 'Activity 1',
283
                        'component' => 'mod_h5pactivity'
284
                    ],
285
                    [
286
                        'user' => 'user1',
287
                        'activity' => 'Activity 3',
288
                        'activityid' => '1',
289
                        'component' => 'core_xapi'
290
                    ],
291
                    [
292
                        'user' => 'user1',
293
                        'activity' => 'Activity 1',
294
                        'component' => 'mod_h5pactivity'
295
                    ],
296
                ],
1441 ariadna 297
                'expectedstates' => [
1 efrain 298
                    ['user' => 'user2', 'activity' => 'Activity 1'],
299
                    ['user' => 'user1', 'activity' => 'Activity 3']
300
                ]
301
            ],
302
            'Activities with one single user' => [
1441 ariadna 303
                'testedusername' => 'user1',
304
                'testedcomponent' => 'mod_h5pactivity',
305
                'testedactivityname' => 'Activity 1',
1 efrain 306
                'states' => [
307
                    [
308
                        'user' => 'user1',
309
                        'activity' => 'Activity 1',
310
                        'component' => 'mod_h5pactivity'
311
                    ],
312
                    [
313
                        'user' => 'user1',
314
                        'activity' => 'Activity 1',
315
                        'component' => 'mod_h5pactivity'
316
                    ],
317
                    [
318
                        'user' => 'user1',
319
                        'activity' => 'Activity 1',
320
                        'component' => 'mod_h5pactivity'
321
                    ],
322
                ],
1441 ariadna 323
                'expectedstates' => []
1 efrain 324
            ],
325
        ];
326
    }
327
}