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 enrol_lti\local\ltiadvantage\entity;
18
 
19
/**
20
 * Tests for resource_link.
21
 *
22
 * @package enrol_lti
23
 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @coversDefaultClass \enrol_lti\local\ltiadvantage\entity\resource_link
26
 */
1441 ariadna 27
final class resource_link_test extends \advanced_testcase {
1 efrain 28
    /**
29
     * Test creation of the object instances.
30
     *
31
     * @dataProvider instantiation_data_provider
32
     * @param array $args the arguments to the creation method.
33
     * @param array $expectations various expectations for the test cases.
34
     * @covers ::create
35
     */
11 efrain 36
    public function test_create(array $args, array $expectations): void {
1 efrain 37
        if (!$expectations['valid']) {
38
            $this->expectException($expectations['exception']);
39
            $this->expectExceptionMessage($expectations['exceptionmessage']);
40
            resource_link::create(...array_values($args));
41
        } else {
42
            $reslink = resource_link::create(...array_values($args));
43
            $this->assertEquals($args['resourcelinkid'], $reslink->get_resourcelinkid());
44
            $this->assertEquals($args['resourceid'], $reslink->get_resourceid());
45
            $this->assertEquals($args['deploymentid'], $reslink->get_deploymentid());
46
            $this->assertEquals($args['contextid'], $reslink->get_contextid());
47
            $this->assertEquals($args['id'], $reslink->get_id());
48
            $this->assertEquals(null, $reslink->get_grade_service());
49
            $this->assertEquals(null, $reslink->get_names_and_roles_service());
50
        }
51
    }
52
 
53
    /**
54
     * Data provider for testing object instantiation.
55
     * @return array the data for testing.
56
     */
1441 ariadna 57
    public static function instantiation_data_provider(): array {
1 efrain 58
        return [
59
            'Valid creation, no context or id provided' => [
60
                'args' => [
61
                    'resourcelinkid' => 'res-link-id-123',
62
                    'deploymentid' => 45,
63
                    'resourceid' => 24,
64
                    'contextid' => null,
65
                    'id' => null
66
                ],
67
                'expectations' => [
68
                    'valid' => true
69
                ]
70
 
71
            ],
72
            'Valid creation, context id provided, no id provided' => [
73
                'args' => [
74
                    'resourcelinkid' => 'res-link-id-123',
75
                    'deploymentid' => 45,
76
                    'resourceid' => 24,
77
                    'contextid' => 777,
78
                    'id' => null
79
                ],
80
                'expectations' => [
81
                    'valid' => true
82
                ]
83
 
84
            ],
85
            'Valid creation, context id and id provided' => [
86
                'args' => [
87
                    'resourcelinkid' => 'res-link-id-123',
88
                    'deploymentid' => 45,
89
                    'resourceid' => 24,
90
                    'contextid' => 777,
91
                    'id' => 33
92
                ],
93
                'expectations' => [
94
                    'valid' => true
95
                ]
96
            ],
97
            'Invlid creation, empty resource link id string' => [
98
                'args' => [
99
                    'resourcelinkid' => '',
100
                    'deploymentid' => 45,
101
                    'resourceid' => 24,
102
                    'contextid' => null,
103
                    'id' => null
104
                ],
105
                'expectations' => [
106
                    'valid' => false,
107
                    'exception' => \coding_exception::class,
108
                    'exceptionmessage' => 'Error: resourcelinkid cannot be an empty string'
109
                ]
110
 
111
            ]
112
        ];
113
    }
114
 
115
    /**
116
     * Test confirming that a grade service instance can be added to the object instance.
117
     *
118
     * @param array $args the array of method arguments
119
     * @param array $expected the array of expectations
120
     * @dataProvider add_grade_service_provider
121
     * @covers ::add_grade_service
122
     */
11 efrain 123
    public function test_add_grade_service(array $args, array $expected): void {
1 efrain 124
        $reslink = resource_link::create('res-link-id-123', 24, 44);
125
        $this->assertNull($reslink->get_grade_service());
126
 
127
        if (!$expected['valid']) {
128
            $this->expectException($expected['exception']);
129
        }
130
        $reslink->add_grade_service(...array_values($args));
131
        $gradeservice = $reslink->get_grade_service();
132
        $this->assertInstanceOf(ags_info::class, $gradeservice);
133
        $this->assertEquals($args['lineitemsurl'], $gradeservice->get_lineitemsurl());
134
        $this->assertEquals($args['lineitemurl'], $gradeservice->get_lineitemurl());
135
        $this->assertEquals($args['scope'], $gradeservice->get_scopes());
136
    }
137
 
138
    /**
139
     * Data provider for testing the add_grade_service method.
140
     *
141
     * @return array the array of test case data.
142
     */
1441 ariadna 143
    public static function add_grade_service_provider(): array {
1 efrain 144
        return [
145
            'Valid, both URLs, some scopes' => [
146
                'args' => [
147
                    'lineitemsurl' => new \moodle_url('https://platform.example.org/10/lineitems'),
148
                    'lineitemurl' => new \moodle_url('https://platform.example.org/10/lineitems/4/lineitem'),
149
                    'scope' => [
150
                        'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem',
151
                        'https://purl.imsglobal.org/spec/lti-ags/scope/score'
152
                    ]
153
                ],
154
                'expected' => [
155
                    'valid' => true,
156
                ]
157
            ],
158
            'Valid, only coupled line item URL, some scopes' => [
159
                'args' => [
160
                    'lineitemsurl' => null,
161
                    'lineitemurl' => new \moodle_url('https://platform.example.org/10/lineitems/4/lineitem'),
162
                    'scope' => [
163
                        'https://purl.imsglobal.org/spec/lti-ags/scope/score'
164
                    ]
165
                ],
166
                'expected' => [
167
                    'valid' => true,
168
                ]
169
            ],
170
            'Valid, only decoupled line items URL, some scopes' => [
171
                'args' => [
172
                    'lineitemsurl' => new \moodle_url('https://platform.example.org/10/lineitems'),
173
                    'lineitemurl' => null,
174
                    'scope' => [
175
                        'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem',
176
                        'https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly',
177
                        'https://purl.imsglobal.org/spec/lti-ags/scope/score',
178
                    ]
179
                ],
180
                'expected' => [
181
                    'valid' => true,
182
                ]
183
            ],
184
            'Valid, URLs without any scopes' => [
185
                'args' => [
186
                    'lineitemsurl' => new \moodle_url('https://platform.example.org/10/lineitems'),
187
                    'lineitemurl' => new \moodle_url('https://platform.example.org/10/lineitems/4/lineitem'),
188
                    'scope' => []
189
                ],
190
                'expected' => [
191
                    'valid' => true,
192
                ]
193
            ],
194
            'Invalid, missing both URLs' => [
195
                'args' => [
196
                    'lineitemsurl' => null,
197
                    'lineitemurl' => null,
198
                    'scope' => [
199
                        'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem'
200
                    ]
201
                ],
202
                'expected' => [
203
                    'valid' => false,
204
                    'exception' => \coding_exception::class
205
                ]
206
            ],
207
        ];
208
    }
209
 
210
    /**
211
     * Test confirming that a names and roles service instance can be added to the object instance.
212
     *
213
     * @covers ::add_names_and_roles_service
214
     */
11 efrain 215
    public function test_add_names_and_roles_service(): void {
1 efrain 216
        $reslink = resource_link::create('res-link-id-123', 24, 44);
217
        $this->assertNull($reslink->get_names_and_roles_service());
218
        $reslink->add_names_and_roles_service(new \moodle_url('https://lms.example.com/10/memberships'), ['2.0']);
219
        $nrps = $reslink->get_names_and_roles_service();
220
        $this->assertInstanceOf(nrps_info::class, $nrps);
221
        $this->assertEquals(new \moodle_url('https://lms.example.com/10/memberships'),
222
            $nrps->get_context_memberships_url());
223
        $this->assertEquals(['2.0'], $nrps->get_service_versions());
224
    }
225
 
226
    /**
227
     * Verify that a user can be created from a resource link that has an id.
228
     *
229
     * @covers ::add_user
230
     */
11 efrain 231
    public function test_add_user(): void {
1 efrain 232
        global $CFG;
233
        $reslinkwithid = resource_link::create('res-link-id-123', 24, 44, 66, 33);
234
        $user = $reslinkwithid->add_user(2, 'platform-user-id-123', $CFG->lang, 'Sydney', 'AU', 'Test university', '99');
235
        $this->assertInstanceOf(user::class, $user);
236
        $this->assertEquals(33, $user->get_resourcelinkid());
237
 
238
        $reslinkwithoutid = resource_link::create('res-link-id-123', 24, 44);
239
        $this->expectException(\coding_exception::class);
240
        $this->expectExceptionMessage("Can't add user to a resource_link that hasn't first been saved");
241
        $reslinkwithoutid->add_user(2, 'platform-user-id-123', $CFG->lang, 'Sydney', 'Australia', 'Test university', '99');
242
    }
243
 
244
    /**
245
     * Test confirming that the resourceid can be changed on the object.
246
     *
247
     * @covers ::set_resourceid
248
     */
11 efrain 249
    public function test_set_resource_id(): void {
1 efrain 250
        $reslink = resource_link::create('res-link-id-123', 24, 44);
251
        $this->assertEquals(44, $reslink->get_resourceid());
252
        $reslink->set_resourceid(333);
253
        $this->assertEquals(333, $reslink->get_resourceid());
254
        $this->expectException(\coding_exception::class);
255
        $this->expectExceptionMessage('Resource id must be a positive int');
256
        $reslink->set_resourceid(0);
257
    }
258
 
259
    /**
260
     * Test confirming that the contextid can be changed on the object.
261
     *
262
     * @covers ::set_contextid
263
     */
11 efrain 264
    public function test_set_context_id(): void {
1 efrain 265
        $reslink = resource_link::create('res-link-id-123', 24, 44);
266
        $this->assertEquals(null, $reslink->get_contextid());
267
        $reslink->set_contextid(333);
268
        $this->assertEquals(333, $reslink->get_contextid());
269
        $this->expectException(\coding_exception::class);
270
        $this->expectExceptionMessage('Context id must be a positive int');
271
        $reslink->set_contextid(0);
272
    }
273
}