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 |
/**
|
|
|
18 |
* licenselib tests.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
defined('MOODLE_INTERNAL') || die();
|
|
|
26 |
|
|
|
27 |
require_once(__DIR__.'/../licenselib.php');
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* licenselib tests.
|
|
|
31 |
*
|
|
|
32 |
* @package core
|
|
|
33 |
* @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
|
|
|
34 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
35 |
*/
|
|
|
36 |
class licenselib_test extends advanced_testcase {
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Test getting licenses from database or cache.
|
|
|
40 |
*/
|
11 |
efrain |
41 |
public function test_get_licenses(): void {
|
1 |
efrain |
42 |
$this->resetAfterTest();
|
|
|
43 |
|
|
|
44 |
// Reset the cache, to make sure we are not getting cached licenses.
|
|
|
45 |
$cache = \cache::make('core', 'license');
|
|
|
46 |
$cache->delete('licenses');
|
|
|
47 |
|
|
|
48 |
$licenses = license_manager::get_licenses();
|
|
|
49 |
|
|
|
50 |
$this->assertArrayHasKey('unknown', $licenses);
|
|
|
51 |
$this->assertArrayHasKey('allrightsreserved', $licenses);
|
|
|
52 |
$this->assertArrayHasKey('public', $licenses);
|
|
|
53 |
$this->assertArrayHasKey('cc-4.0', $licenses);
|
|
|
54 |
$this->assertArrayHasKey('cc-nd-4.0', $licenses);
|
|
|
55 |
$this->assertArrayHasKey('cc-nc-nd-4.0', $licenses);
|
|
|
56 |
$this->assertArrayHasKey('cc-nc-4.0', $licenses);
|
|
|
57 |
$this->assertArrayHasKey('cc-nc-sa-4.0', $licenses);
|
|
|
58 |
$this->assertArrayHasKey('cc-sa-4.0', $licenses);
|
|
|
59 |
|
|
|
60 |
// Get the licenses from cache and check again.
|
|
|
61 |
$licenses = license_manager::get_licenses();
|
|
|
62 |
|
|
|
63 |
$this->assertArrayHasKey('unknown', $licenses);
|
|
|
64 |
$this->assertArrayHasKey('allrightsreserved', $licenses);
|
|
|
65 |
$this->assertArrayHasKey('public', $licenses);
|
|
|
66 |
$this->assertArrayHasKey('cc-4.0', $licenses);
|
|
|
67 |
$this->assertArrayHasKey('cc-nd-4.0', $licenses);
|
|
|
68 |
$this->assertArrayHasKey('cc-nc-nd-4.0', $licenses);
|
|
|
69 |
$this->assertArrayHasKey('cc-nc-4.0', $licenses);
|
|
|
70 |
$this->assertArrayHasKey('cc-nc-sa-4.0', $licenses);
|
|
|
71 |
$this->assertArrayHasKey('cc-sa-4.0', $licenses);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* Test saving a license.
|
|
|
76 |
*/
|
11 |
efrain |
77 |
public function test_save(): void {
|
1 |
efrain |
78 |
global $DB;
|
|
|
79 |
|
|
|
80 |
$this->resetAfterTest();
|
|
|
81 |
|
|
|
82 |
$license = new stdClass();
|
|
|
83 |
$license->shortname = 'mit';
|
|
|
84 |
$license->fullname = 'MIT';
|
|
|
85 |
$license->source = 'https://opensource.org/licenses/MIT';
|
|
|
86 |
$license->version = '2020020200';
|
|
|
87 |
$license->custom = license_manager::CUSTOM_LICENSE;
|
|
|
88 |
|
|
|
89 |
license_manager::save($license);
|
|
|
90 |
|
|
|
91 |
$license = $DB->get_record('license', ['shortname' => 'mit']);
|
|
|
92 |
$this->assertNotEmpty($license);
|
|
|
93 |
$this->assertEquals('mit', $license->shortname);
|
|
|
94 |
|
|
|
95 |
// Attempting to update a core license should only update sortorder.
|
|
|
96 |
$license->shortname = 'cc-4.0';
|
|
|
97 |
$license->sortorder = 33;
|
|
|
98 |
license_manager::save($license);
|
|
|
99 |
|
|
|
100 |
$record = $DB->get_record('license', ['id' => $license->id]);
|
|
|
101 |
$this->assertNotEquals('cc', $record->shortname);
|
|
|
102 |
$record = $DB->get_record('license', ['shortname' => 'cc-4.0']);
|
|
|
103 |
$this->assertEquals(33, $record->sortorder);
|
|
|
104 |
|
|
|
105 |
// Adding a license with existing custom license shortname should update existing license.
|
|
|
106 |
$updatelicense = new stdClass();
|
|
|
107 |
$updatelicense->shortname = 'mit';
|
|
|
108 |
$updatelicense->fullname = 'MIT updated';
|
|
|
109 |
$updatelicense->source = 'https://en.wikipedia.org/wiki/MIT_License';
|
|
|
110 |
|
|
|
111 |
license_manager::save($updatelicense);
|
|
|
112 |
$actual = $DB->get_record('license', ['shortname' => 'mit']);
|
|
|
113 |
|
|
|
114 |
$this->assertEquals($updatelicense->fullname, $actual->fullname);
|
|
|
115 |
$this->assertEquals($updatelicense->source, $actual->source);
|
|
|
116 |
// Fields not updated should remain the same.
|
|
|
117 |
$this->assertEquals($license->version, $actual->version);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Test ability to get a license by it's short name.
|
|
|
122 |
*/
|
11 |
efrain |
123 |
public function test_get_license_by_shortname(): void {
|
1 |
efrain |
124 |
|
|
|
125 |
$license = license_manager::get_license_by_shortname('cc-nc-4.0');
|
|
|
126 |
$actual = $license->fullname;
|
|
|
127 |
|
|
|
128 |
$this->assertEquals('Creative Commons - NonCommercial 4.0 International', $actual);
|
|
|
129 |
$this->assertNull(license_manager::get_license_by_shortname('somefakelicense'));
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Test disabling a license.
|
|
|
134 |
*/
|
11 |
efrain |
135 |
public function test_disable_license(): void {
|
1 |
efrain |
136 |
global $DB;
|
|
|
137 |
|
|
|
138 |
$this->resetAfterTest();
|
|
|
139 |
|
|
|
140 |
// Manually set license record to enabled for testing.
|
|
|
141 |
$DB->set_field('license', 'enabled', license_manager::LICENSE_ENABLED, ['shortname' => 'cc-nc-4.0']);
|
|
|
142 |
|
|
|
143 |
$this->assertTrue(license_manager::disable('cc-nc-4.0'));
|
|
|
144 |
|
|
|
145 |
$license = license_manager::get_license_by_shortname('cc-nc-4.0');
|
|
|
146 |
$actual = $license->enabled;
|
|
|
147 |
|
|
|
148 |
$this->assertEquals(license_manager::LICENSE_DISABLED, $actual);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Test enabling a license.
|
|
|
153 |
*/
|
11 |
efrain |
154 |
public function test_enable_license(): void {
|
1 |
efrain |
155 |
global $DB;
|
|
|
156 |
|
|
|
157 |
$this->resetAfterTest();
|
|
|
158 |
|
|
|
159 |
// Manually set license record to disabled for testing.
|
|
|
160 |
$DB->set_field('license', 'enabled', license_manager::LICENSE_DISABLED, ['shortname' => 'cc-nc-4.0']);
|
|
|
161 |
|
|
|
162 |
$this->assertTrue(license_manager::enable('cc-nc-4.0'));
|
|
|
163 |
|
|
|
164 |
$license = license_manager::get_license_by_shortname('cc-nc-4.0');
|
|
|
165 |
$actual = $license->enabled;
|
|
|
166 |
|
|
|
167 |
$this->assertEquals(license_manager::LICENSE_ENABLED, $actual);
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* Test deleting a custom license.
|
|
|
172 |
*/
|
11 |
efrain |
173 |
public function test_delete(): void {
|
1 |
efrain |
174 |
$this->resetAfterTest();
|
|
|
175 |
|
|
|
176 |
// Create a custom license.
|
|
|
177 |
$license = new stdClass();
|
|
|
178 |
$license->shortname = 'mit';
|
|
|
179 |
$license->fullname = 'MIT';
|
|
|
180 |
$license->source = 'https://opensource.org/licenses/MIT';
|
|
|
181 |
$license->version = '2020020200';
|
|
|
182 |
$license->custom = license_manager::CUSTOM_LICENSE;
|
|
|
183 |
|
|
|
184 |
license_manager::save($license);
|
|
|
185 |
|
|
|
186 |
// Should be able to delete a custom license.
|
|
|
187 |
license_manager::delete($license->shortname);
|
|
|
188 |
$this->assertNull(license_manager::get_license_by_shortname($license->shortname));
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Test trying to delete a license currently in use by a file.
|
|
|
193 |
*/
|
11 |
efrain |
194 |
public function test_delete_license_in_use_by_file(): void {
|
1 |
efrain |
195 |
$this->resetAfterTest();
|
|
|
196 |
|
|
|
197 |
// Create a custom license.
|
|
|
198 |
$license = new stdClass();
|
|
|
199 |
$license->shortname = 'mit';
|
|
|
200 |
$license->fullname = 'MIT';
|
|
|
201 |
$license->source = 'https://opensource.org/licenses/MIT';
|
|
|
202 |
$license->version = '2020020200';
|
|
|
203 |
$license->custom = license_manager::CUSTOM_LICENSE;
|
|
|
204 |
|
|
|
205 |
license_manager::save($license);
|
|
|
206 |
|
|
|
207 |
// Create a test file with custom license selected.
|
|
|
208 |
$fs = get_file_storage();
|
|
|
209 |
$syscontext = context_system::instance();
|
|
|
210 |
$filerecord = array(
|
|
|
211 |
'contextid' => $syscontext->id,
|
|
|
212 |
'component' => 'tool_metadata',
|
|
|
213 |
'filearea' => 'unittest',
|
|
|
214 |
'itemid' => 0,
|
|
|
215 |
'filepath' => '/',
|
|
|
216 |
'filename' => 'test.doc',
|
|
|
217 |
);
|
|
|
218 |
$file = $fs->create_file_from_string($filerecord, 'Test file');
|
|
|
219 |
$file->set_license($license->shortname);
|
|
|
220 |
|
|
|
221 |
// Should not be able to delete a license when in use by a file.
|
|
|
222 |
$this->expectException(moodle_exception::class);
|
|
|
223 |
license_manager::delete($license->shortname);
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
/**
|
|
|
227 |
* Test trying to delete a core license.
|
|
|
228 |
*/
|
11 |
efrain |
229 |
public function test_delete_license_core(): void {
|
1 |
efrain |
230 |
// Should not be able to delete a standard/core license.
|
|
|
231 |
$this->expectException(moodle_exception::class);
|
|
|
232 |
license_manager::delete('cc-nc-4.0');
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* Test trying to delete a license which doesn't exist.
|
|
|
237 |
*/
|
11 |
efrain |
238 |
public function test_delete_license_not_exists(): void {
|
1 |
efrain |
239 |
// Should throw an exception if license with shortname doesn't exist.
|
|
|
240 |
$this->expectException(moodle_exception::class);
|
|
|
241 |
license_manager::delete('somefakelicense');
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* Test setting active licenses.
|
|
|
246 |
*/
|
11 |
efrain |
247 |
public function test_set_active_licenses(): void {
|
1 |
efrain |
248 |
$this->resetAfterTest();
|
|
|
249 |
|
|
|
250 |
// Private method used internally, test through disable and enable public methods.
|
|
|
251 |
license_manager::disable('allrightsreserved');
|
|
|
252 |
$this->assertStringNotContainsString('allrightsreserved', get_config('', 'licenses'));
|
|
|
253 |
|
|
|
254 |
license_manager::enable('allrightsreserved');
|
|
|
255 |
$this->assertStringContainsString('allrightsreserved', get_config('', 'licenses'));
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
/**
|
|
|
259 |
* Test getting active licenses.
|
|
|
260 |
*/
|
11 |
efrain |
261 |
public function test_get_active_licenses(): void {
|
1 |
efrain |
262 |
$this->resetAfterTest();
|
|
|
263 |
|
|
|
264 |
license_manager::disable('allrightsreserved');
|
|
|
265 |
license_manager::reset_license_cache();
|
|
|
266 |
|
|
|
267 |
$licenses = license_manager::get_active_licenses();
|
|
|
268 |
$this->assertArrayNotHasKey('allrightsreserved', $licenses);
|
|
|
269 |
|
|
|
270 |
license_manager::enable('allrightsreserved');
|
|
|
271 |
license_manager::reset_license_cache();
|
|
|
272 |
|
|
|
273 |
$licenses = license_manager::get_active_licenses();
|
|
|
274 |
$this->assertArrayHasKey('allrightsreserved', $licenses);
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Test getting active licenses as array.
|
|
|
279 |
*/
|
11 |
efrain |
280 |
public function test_get_active_licenses_as_array(): void {
|
1 |
efrain |
281 |
$this->resetAfterTest();
|
|
|
282 |
|
|
|
283 |
license_manager::disable('allrightsreserved');
|
|
|
284 |
license_manager::reset_license_cache();
|
|
|
285 |
|
|
|
286 |
$licenses = license_manager::get_active_licenses_as_array();
|
|
|
287 |
$this->assertIsArray($licenses);
|
|
|
288 |
$this->assertNotContains('All rights reserved', $licenses);
|
|
|
289 |
|
|
|
290 |
license_manager::enable('allrightsreserved');
|
|
|
291 |
license_manager::reset_license_cache();
|
|
|
292 |
|
|
|
293 |
$licenses = license_manager::get_active_licenses_as_array();
|
|
|
294 |
$this->assertIsArray($licenses);
|
|
|
295 |
$this->assertContains('All rights reserved', $licenses);
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Test resetting the license cache.
|
|
|
300 |
*/
|
11 |
efrain |
301 |
public function test_reset_license_cache(): void {
|
1 |
efrain |
302 |
global $DB;
|
|
|
303 |
|
|
|
304 |
$this->resetAfterTest();
|
|
|
305 |
|
|
|
306 |
$licenses = license_manager::get_licenses();
|
|
|
307 |
|
|
|
308 |
$cache = \cache::make('core', 'license');
|
|
|
309 |
$cachedlicenses = $cache->get('licenses');
|
|
|
310 |
|
|
|
311 |
$this->assertNotFalse($cachedlicenses);
|
|
|
312 |
$this->assertEquals($licenses, $cachedlicenses);
|
|
|
313 |
|
|
|
314 |
// Manually delete a license to see if cache persists.
|
|
|
315 |
$DB->delete_records('license', ['shortname' => 'cc-nc-4.0']);
|
|
|
316 |
$licenses = license_manager::get_licenses();
|
|
|
317 |
|
|
|
318 |
$this->assertArrayHasKey('cc-nc-4.0', $licenses);
|
|
|
319 |
|
|
|
320 |
license_manager::reset_license_cache();
|
|
|
321 |
|
|
|
322 |
$licenses = license_manager::get_licenses();
|
|
|
323 |
$this->assertArrayNotHasKey('cc-nc-4.0', $licenses);
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* Test that all licenses are installed correctly.
|
|
|
328 |
*/
|
11 |
efrain |
329 |
public function test_install_licenses(): void {
|
1 |
efrain |
330 |
global $DB;
|
|
|
331 |
|
|
|
332 |
$this->resetAfterTest();
|
|
|
333 |
|
|
|
334 |
$DB->delete_records('license');
|
|
|
335 |
|
|
|
336 |
license_manager::install_licenses();
|
|
|
337 |
|
|
|
338 |
$expectedshortnames = [
|
|
|
339 |
'allrightsreserved', 'public', 'unknown',
|
|
|
340 |
'cc-4.0', 'cc-nc-4.0', 'cc-nc-nd-4.0', 'cc-nc-sa-4.0', 'cc-nd-4.0', 'cc-sa-4.0',
|
|
|
341 |
];
|
|
|
342 |
$actualshortnames = $DB->get_records_menu('license', null, '', 'id, shortname');
|
|
|
343 |
|
|
|
344 |
foreach ($expectedshortnames as $expectedshortname) {
|
|
|
345 |
$this->assertContains($expectedshortname, $actualshortnames);
|
|
|
346 |
}
|
|
|
347 |
}
|
|
|
348 |
}
|