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_xapi\xapi_exception;
20
use core_xapi\local\statement\item_agent;
21
use externallib_advanced_testcase;
22
use core_external\external_api;
23
use core_xapi\iri;
24
use core_xapi\local\state;
25
use core_xapi\test_helper;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31
 
32
/**
33
 * Unit tests for xAPI post state webservice.
34
 *
35
 * @package    core_xapi
36
 * @covers     \core_xapi\external\post_state
37
 * @since      Moodle 4.2
38
 * @copyright  2023 Sara Arjona (sara@moodle.com)
39
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40
 */
1441 ariadna 41
final class post_state_test extends externallib_advanced_testcase {
1 efrain 42
 
43
    /**
44
     * Setup to ensure that fixtures are loaded.
45
     */
46
    public static function setUpBeforeClass(): void {
47
        global $CFG;
48
        require_once($CFG->dirroot . '/lib/xapi/tests/helper.php');
1441 ariadna 49
        parent::setUpBeforeClass();
1 efrain 50
    }
51
 
52
    /**
53
     * Testing different component names on valid states.
54
     *
55
     * @dataProvider components_provider
56
     * @param string $component component name
57
     * @param string|null $expected expected results
58
     */
59
    public function test_component_names(string $component, ?string $expected): void {
60
 
61
        $this->resetAfterTest();
62
 
63
        // Scenario.
64
        $this->setAdminUser();
65
 
66
        // Perform test.
67
        $data = test_helper::create_state();
68
        $this->post_state_data($component, $data, $expected);
69
    }
70
 
71
    /**
72
     * Data provider for the test_component_names tests.
73
     *
74
     * @return  array
75
     */
1441 ariadna 76
    public static function components_provider(): array {
1 efrain 77
        return [
78
            'Inexistent component' => [
79
                'component' => 'inexistent_component',
80
                'expected' => null,
81
            ],
82
            'Compatible component' => [
83
                'component' => 'fake_component',
84
                'expected' => 'true',
85
            ],
86
            'Incompatible component' => [
87
                'component' => 'core_xapi',
88
                'expected' => null,
89
            ],
90
        ];
91
    }
92
 
93
    /**
94
     * Testing invalid agent.
95
     *
96
     */
97
    public function test_invalid_agent(): void {
98
        $this->resetAfterTest();
99
 
100
        // Scenario.
101
        $this->setAdminUser();
102
        $other = $this->getDataGenerator()->create_user();
103
 
104
        // Invalid agent (use different user, instead of the current one).
105
        $info = [
106
            'agent' => item_agent::create_from_user($other),
107
        ];
108
        $data = test_helper::create_state($info);
109
        $this->post_state_data('fake_component', $data, null);
110
    }
111
 
112
    /**
113
     * Testing valid/invalid state.
114
     *
115
     * @dataProvider states_provider
116
     * @param string $stateid The xAPI state id.
117
     * @param string|null $expected Expected results.
118
     * @return void
119
     */
120
    public function test_post_state(string $stateid, ?string $expected): void {
121
        $this->resetAfterTest();
122
 
123
        // Scenario.
124
        $this->setAdminUser();
125
 
126
        // Perform test.
127
        $info = [
128
            'stateid' => $stateid,
129
        ];
130
        $data = test_helper::create_state($info);
131
        $this->post_state_data('fake_component', $data, $expected);
132
    }
133
 
134
    /**
135
     * Data provider for the test_post_state tests.
136
     *
137
     * @return array
138
     */
1441 ariadna 139
    public static function states_provider(): array {
1 efrain 140
        return [
141
            'Empty stateid' => [
142
                'stateid' => '',
143
                'expected' => 'true',
144
            ],
145
            'Valid stateid (any value but paella)' => [
146
                'stateid' => 'sangria',
147
                'expected' => 'true',
148
            ],
149
            'Invalid stateid' => [
150
                'stateid' => 'paella',
151
                'expected' => null,
152
            ],
153
        ];
154
    }
155
 
156
    /**
157
     * Return a xAPI external webservice class to operate.
158
     *
159
     * The test needs to fake a component in order to test without
160
     * using a real one. This way if in the future any component
161
     * implement it's xAPI handler this test will continue working.
162
     *
163
     * @return post_state the external class
164
     */
165
    private function get_external_class(): post_state {
166
        $ws = new class extends post_state {
167
            /**
168
             * Method to override validate_component.
169
             *
170
             * @param string $component  The component name in frankenstyle.
171
             */
172
            protected static function validate_component(string $component): void {
173
                if ($component != 'fake_component') {
174
                    parent::validate_component($component);
175
                }
176
            }
177
        };
178
        return $ws;
179
    }
180
 
181
    /**
182
     * This function do all checks from a standard post_state request.
183
     *
184
     * The reason for this function is because states crafting (special in error
185
     * scenarios) is complicated to do via data providers because every test need a specific
186
     * testing conditions. For this reason alls tests creates a scenario and then uses this
187
     * function to check the results.
188
     *
189
     * @param string $component component name
190
     * @param state $data data to encode and send to post_state
191
     * @param string $expected expected results (if null an exception is expected)
192
     */
193
    private function post_state_data(string $component, state $data, ?string $expected): void {
194
        global $DB;
195
 
196
        // Get current states in database.
197
        $currentstates = $DB->count_records('xapi_states');
198
 
199
        // When no result is expected, an exception will be incurred.
200
        if (is_null($expected)) {
201
            $this->expectException(xapi_exception::class);
202
        }
203
 
204
        $external = $this->get_external_class();
205
        $result = $external::execute(
206
            $component,
207
            iri::generate($data->get_activity_id(), 'activity'),
208
            json_encode($data->get_agent()),
209
            $data->get_state_id(),
210
            json_encode($data->jsonSerialize()),
211
            $data->get_registration()
212
        );
213
        $result = external_api::clean_returnvalue($external::execute_returns(), $result);
214
 
215
        // Check the state has been saved with the expected values.
216
        $this->assertTrue($result);
217
        $records = $DB->get_records('xapi_states');
218
        $this->assertCount($currentstates + 1, $records);
219
        $record = reset($records);
220
        $this->assertEquals($component, $record->component);
221
        $this->assertEquals($data->get_activity_id(), $record->itemid);
222
        $this->assertEquals($data->get_user()->id, $record->userid);
223
        $this->assertEquals(json_encode($data->jsonSerialize()), $record->statedata);
224
        $this->assertEquals($data->get_registration(), $record->registration);
225
    }
226
}