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\repository;
18
use enrol_lti\local\ltiadvantage\entity\context;
19
use enrol_lti\local\ltiadvantage\entity\application_registration;
20
 
21
/**
22
 * Tests for context_repository.
23
 *
24
 * @package enrol_lti
25
 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @coversDefaultClass \enrol_lti\local\ltiadvantage\repository\context_repository
28
 */
1441 ariadna 29
final class context_repository_test extends \advanced_testcase {
1 efrain 30
    /**
31
     * Helper to create test context objects for use with the repository tests.
32
     *
33
     * @return context the context.
34
     */
35
    protected function create_test_context(): context {
36
        $registration = application_registration::create(
37
            'Test',
38
            'a2c94a2c94',
39
            new \moodle_url('http://lms.example.org'),
40
            'clientid_123',
41
            new \moodle_url('https://example.org/authrequesturl'),
42
            new \moodle_url('https://example.org/jwksurl'),
43
            new \moodle_url('https://example.org/accesstokenurl')
44
        );
45
        $registrationrepo = new application_registration_repository();
46
        $createdregistration = $registrationrepo->save($registration);
47
 
48
        $deployment = $createdregistration->add_tool_deployment('Deployment 1', 'DeployID123');
49
        $deploymentrepo = new deployment_repository();
50
        $saveddeployment = $deploymentrepo->save($deployment);
51
 
52
        return $saveddeployment->add_context('CTX123', ['http://purl.imsglobal.org/vocab/lis/v2/course#CourseSection']);
53
    }
54
 
55
    /**
56
     * Helper to assert that all the key elements of two contexts (i.e. excluding id) are equal.
57
     *
58
     * @param context $expected the context whose values are deemed correct.
59
     * @param context $check the context to check.
60
     */
61
    protected function assert_same_context_values(context $expected, context $check): void {
62
        $this->assertEquals($expected->get_deploymentid(), $check->get_deploymentid());
63
        $this->assertEquals($expected->get_contextid(), $check->get_contextid());
64
        $this->assertEquals($expected->get_types(), $check->get_types());
65
    }
66
 
67
    /**
68
     * Helper to assert that all the key elements of a context are present in the DB.
69
     *
70
     * @param context $expected the context whose values are deemed correct.
71
     */
72
    protected function assert_context_db_values(context $expected) {
73
        global $DB;
74
        $checkrecord = $DB->get_record('enrol_lti_context', ['id' => $expected->get_id()]);
75
        $this->assertEquals($expected->get_id(), $checkrecord->id);
76
        $this->assertEquals($expected->get_deploymentid(), $checkrecord->ltideploymentid);
77
        $this->assertEquals($expected->get_contextid(), $checkrecord->contextid);
78
        $this->assertEquals(json_encode($expected->get_types()), $checkrecord->type);
79
        $this->assertNotEmpty($checkrecord->timecreated);
80
        $this->assertNotEmpty($checkrecord->timemodified);
81
    }
82
 
83
    /**
84
     * Test saving a new context.
85
     *
86
     * @covers ::save
87
     */
11 efrain 88
    public function test_save_new(): void {
1 efrain 89
        $this->resetAfterTest();
90
        $context = $this->create_test_context();
91
        $contextrepo = new context_repository();
92
        $saved = $contextrepo->save($context);
93
 
94
        $this->assertIsInt($saved->get_id());
95
        $this->assert_same_context_values($context, $saved);
96
        $this->assert_context_db_values($saved);
97
    }
98
 
99
    /**
100
     * Test saving an existing context.
101
     *
102
     * @covers ::save
103
     */
11 efrain 104
    public function test_save_existing(): void {
1 efrain 105
        $this->resetAfterTest();
106
        $context = $this->create_test_context();
107
        $contextrepo = new context_repository();
108
        $saved = $contextrepo->save($context);
109
 
110
        $context2 = $context::create(
111
            $saved->get_deploymentid(),
112
            $saved->get_contextid(),
113
            $saved->get_types(),
114
            $saved->get_id()
115
        );
116
        $saved2 = $contextrepo->save($saved);
117
 
118
        $this->assertEquals($saved->get_id(), $saved2->get_id());
119
        $this->assert_same_context_values($saved, $saved2);
120
        $this->assert_context_db_values($saved2);
121
    }
122
 
123
    /**
124
     * Test trying to save two contexts with the same id for the same deployment.
125
     *
126
     * @covers ::save
127
     */
11 efrain 128
    public function test_save_unique_constraints_not_met(): void {
1 efrain 129
        $this->resetAfterTest();
130
        $context = $this->create_test_context();
131
        $context2 = clone $context;
132
 
133
        $contextrepo = new context_repository();
134
        $saved = $contextrepo->save($context);
135
        $this->assertInstanceOf(context::class, $saved);
136
 
137
        $this->expectException(\dml_exception::class);
138
        $contextrepo->save($context2);
139
    }
140
 
141
    /**
142
     * Test existence of a context within the repository.
143
     *
144
     * @covers ::exists
145
     */
11 efrain 146
    public function test_exists(): void {
1 efrain 147
        $this->resetAfterTest();
148
        $contextrepo = new context_repository();
149
        $context = $this->create_test_context();
150
        $savedcontext = $contextrepo->save($context);
151
 
152
        $this->assertTrue($contextrepo->exists($savedcontext->get_id()));
153
        $this->assertFalse($contextrepo->exists(0));
154
    }
155
 
156
    /**
157
     * Test finding a context in the repository.
158
     *
159
     * @covers ::find
160
     */
11 efrain 161
    public function test_find(): void {
1 efrain 162
        $this->resetAfterTest();
163
        $context = $this->create_test_context();
164
        $contextrepo = new context_repository();
165
        $savedcontext = $contextrepo->save($context);
166
 
167
        $foundcontext = $contextrepo->find($savedcontext->get_id());
168
        $this->assertEquals($savedcontext->get_id(), $foundcontext->get_id());
169
        $this->assert_same_context_values($savedcontext, $foundcontext);
170
        $this->assertNull($contextrepo->find(0));
171
    }
172
 
173
    /**
174
     * Test finding a context by contextid within the deployment.
175
     *
176
     * @covers ::find_by_contextid
177
     */
11 efrain 178
    public function test_find_by_contextid(): void {
1 efrain 179
        $this->resetAfterTest();
180
        $context = $this->create_test_context();
181
        $contextrepo = new context_repository();
182
        $savedcontext = $contextrepo->save($context);
183
 
184
        $foundcontext = $contextrepo->find_by_contextid($savedcontext->get_contextid(),
185
            $savedcontext->get_deploymentid());
186
        $this->assertEquals($savedcontext->get_id(), $foundcontext->get_id());
187
        $this->assert_same_context_values($savedcontext, $foundcontext);
188
        $this->assertNull($contextrepo->find_by_contextid(0, $savedcontext->get_deploymentid()));
189
    }
190
 
191
    /**
192
     * Test deleting a context from the repository.
193
     *
194
     * @covers ::delete
195
     */
11 efrain 196
    public function test_delete(): void {
1 efrain 197
        $this->resetAfterTest();
198
        $context = $this->create_test_context();
199
        $contextrepo = new context_repository();
200
        $savedcontext = $contextrepo->save($context);
201
        $this->assertTrue($contextrepo->exists($savedcontext->get_id()));
202
 
203
        $contextrepo->delete($savedcontext->get_id());
204
        $this->assertFalse($contextrepo->exists($savedcontext->get_id()));
205
 
206
        $this->assertNull($contextrepo->delete($savedcontext->get_id()));
207
    }
208
 
209
    /**
210
     * Test deleting a context from the repository, by deployment.
211
     *
212
     * @covers ::delete_by_deployment
213
     */
11 efrain 214
    public function test_delete_by_deployment(): void {
1 efrain 215
        $this->resetAfterTest();
216
        $context = $this->create_test_context();
217
        $contextrepo = new context_repository();
218
        $savedcontext = $contextrepo->save($context);
219
        $context2 = context::create($savedcontext->get_deploymentid(), 'new-context-345', ['CourseSection']);
220
        $savedcontext2 = $contextrepo->save($context2);
221
        $context3 = context::create($savedcontext->get_deploymentid() + 1, 'new-context-567', ['CourseSection']);
222
        $savedcontext3 = $contextrepo->save($context3);
223
        $this->assertTrue($contextrepo->exists($savedcontext->get_id()));
224
        $this->assertTrue($contextrepo->exists($savedcontext2->get_id()));
225
        $this->assertTrue($contextrepo->exists($savedcontext3->get_id()));
226
 
227
        $contextrepo->delete_by_deployment($savedcontext->get_deploymentid());
228
        $this->assertFalse($contextrepo->exists($savedcontext->get_id()));
229
        $this->assertFalse($contextrepo->exists($savedcontext2->get_id()));
230
        $this->assertTrue($contextrepo->exists($savedcontext3->get_id()));
231
 
232
        $this->assertNull($contextrepo->delete_by_deployment($savedcontext->get_id()));
233
    }
234
}