2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
21 * @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once(__DIR__
.'/../licenselib.php');
33 * @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class licenselib_test
extends advanced_testcase
{
39 * Test getting licenses from database or cache.
41 public function test_get_licenses() {
42 $this->resetAfterTest();
44 // Reset the cache, to make sure we are not getting cached licenses.
45 $cache = \cache
::make('core', 'license');
46 $cache->delete('licenses');
48 $licenses = license_manager
::get_licenses();
50 $this->assertArrayHasKey('unknown', $licenses);
51 $this->assertArrayHasKey('allrightsreserved', $licenses);
52 $this->assertArrayHasKey('public', $licenses);
53 $this->assertArrayHasKey('cc', $licenses);
54 $this->assertArrayHasKey('cc-nd', $licenses);
55 $this->assertArrayHasKey('cc-nc-nd', $licenses);
56 $this->assertArrayHasKey('cc-nc', $licenses);
57 $this->assertArrayHasKey('cc-nc-sa', $licenses);
58 $this->assertArrayHasKey('cc-sa', $licenses);
60 // Get the licenses from cache and check again.
61 $licenses = license_manager
::get_licenses();
63 $this->assertArrayHasKey('unknown', $licenses);
64 $this->assertArrayHasKey('allrightsreserved', $licenses);
65 $this->assertArrayHasKey('public', $licenses);
66 $this->assertArrayHasKey('cc', $licenses);
67 $this->assertArrayHasKey('cc-nd', $licenses);
68 $this->assertArrayHasKey('cc-nc-nd', $licenses);
69 $this->assertArrayHasKey('cc-nc', $licenses);
70 $this->assertArrayHasKey('cc-nc-sa', $licenses);
71 $this->assertArrayHasKey('cc-sa', $licenses);
75 * Test saving a license.
77 public function test_save() {
80 $this->resetAfterTest();
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
;
89 license_manager
::save($license);
91 $license = $DB->get_record('license', ['shortname' => 'mit']);
92 $this->assertNotEmpty($license);
93 $this->assertEquals('mit', $license->shortname
);
95 // Attempting to update a core license should only update sortorder.
96 $license->shortname
= 'cc';
97 $license->sortorder
= 33;
98 license_manager
::save($license);
100 $record = $DB->get_record('license', ['id' => $license->id
]);
101 $this->assertNotEquals('cc', $record->shortname
);
102 $record = $DB->get_record('license', ['shortname' => 'cc']);
103 $this->assertEquals(33, $record->sortorder
);
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';
111 license_manager
::save($updatelicense);
112 $actual = $DB->get_record('license', ['shortname' => 'mit']);
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
);
121 * Test ability to get a license by it's short name.
123 public function test_get_license_by_shortname() {
125 $license = license_manager
::get_license_by_shortname('cc-nc');
126 $actual = $license->fullname
;
128 $this->assertEquals('Creative Commons - No Commercial', $actual);
129 $this->assertNull(license_manager
::get_license_by_shortname('somefakelicense'));
133 * Test disabling a license.
135 public function test_disable_license() {
138 $this->resetAfterTest();
140 // Manually set license record to enabled for testing.
141 $DB->set_field('license', 'enabled', license_manager
::LICENSE_ENABLED
, ['shortname' => 'cc-nc']);
143 $this->assertTrue(license_manager
::disable('cc-nc'));
145 $license = license_manager
::get_license_by_shortname('cc-nc');
146 $actual = $license->enabled
;
148 $this->assertEquals(license_manager
::LICENSE_DISABLED
, $actual);
152 * Test enabling a license.
154 public function test_enable_license() {
157 $this->resetAfterTest();
159 // Manually set license record to disabled for testing.
160 $DB->set_field('license', 'enabled', license_manager
::LICENSE_DISABLED
, ['shortname' => 'cc-nc']);
162 $this->assertTrue(license_manager
::enable('cc-nc'));
164 $license = license_manager
::get_license_by_shortname('cc-nc');
165 $actual = $license->enabled
;
167 $this->assertEquals(license_manager
::LICENSE_ENABLED
, $actual);
171 * Test deleting a custom license.
173 public function test_delete() {
174 $this->resetAfterTest();
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
;
184 license_manager
::save($license);
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
));
192 * Test trying to delete a license currently in use by a file.
194 public function test_delete_license_in_use_by_file() {
195 $this->resetAfterTest();
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
;
205 license_manager
::save($license);
207 // Create a test file with custom license selected.
208 $fs = get_file_storage();
209 $syscontext = context_system
::instance();
211 'contextid' => $syscontext->id
,
212 'component' => 'tool_metadata',
213 'filearea' => 'unittest',
216 'filename' => 'test.doc',
218 $file = $fs->create_file_from_string($filerecord, 'Test file');
219 $file->set_license($license->shortname
);
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
);
227 * Test trying to delete a core license.
229 public function test_delete_license_core() {
230 // Should not be able to delete a standard/core license.
231 $this->expectException(moodle_exception
::class);
232 license_manager
::delete('cc-nc');
236 * Test trying to delete a license which doesn't exist.
238 public function test_delete_license_not_exists() {
239 // Should throw an exception if license with shortname doesn't exist.
240 $this->expectException(moodle_exception
::class);
241 license_manager
::delete('somefakelicense');
245 * Test setting active licenses.
247 public function test_set_active_licenses() {
248 $this->resetAfterTest();
250 // Private method used internally, test through disable and enable public methods.
251 license_manager
::disable('allrightsreserved');
252 $this->assertStringNotContainsString('allrightsreserved', get_config('', 'licenses'));
254 license_manager
::enable('allrightsreserved');
255 $this->assertStringContainsString('allrightsreserved', get_config('', 'licenses'));
259 * Test getting active licenses.
261 public function test_get_active_licenses() {
262 $this->resetAfterTest();
264 license_manager
::disable('allrightsreserved');
265 license_manager
::reset_license_cache();
267 $licenses = license_manager
::get_active_licenses();
268 $this->assertArrayNotHasKey('allrightsreserved', $licenses);
270 license_manager
::enable('allrightsreserved');
271 license_manager
::reset_license_cache();
273 $licenses = license_manager
::get_active_licenses();
274 $this->assertArrayHasKey('allrightsreserved', $licenses);
278 * Test getting active licenses as array.
280 public function test_get_active_licenses_as_array() {
281 $this->resetAfterTest();
283 license_manager
::disable('allrightsreserved');
284 license_manager
::reset_license_cache();
286 $licenses = license_manager
::get_active_licenses_as_array();
287 $this->assertIsArray($licenses);
288 $this->assertNotContains('All rights reserved', $licenses);
290 license_manager
::enable('allrightsreserved');
291 license_manager
::reset_license_cache();
293 $licenses = license_manager
::get_active_licenses_as_array();
294 $this->assertIsArray($licenses);
295 $this->assertContains('All rights reserved', $licenses);
299 * Test resetting the license cache.
301 public function test_reset_license_cache() {
304 $this->resetAfterTest();
306 $licenses = license_manager
::get_licenses();
308 $cache = \cache
::make('core', 'license');
309 $cachedlicenses = $cache->get('licenses');
311 $this->assertNotFalse($cachedlicenses);
312 $this->assertEquals($licenses, $cachedlicenses);
314 // Manually delete a license to see if cache persists.
315 $DB->delete_records('license', ['shortname' => 'cc-nc']);
316 $licenses = license_manager
::get_licenses();
318 $this->assertArrayHasKey('cc-nc', $licenses);
320 license_manager
::reset_license_cache();
322 $licenses = license_manager
::get_licenses();
323 $this->assertArrayNotHasKey('cc-nc', $licenses);
327 * Test that all licenses are installed correctly.
329 public function test_install_licenses() {
332 $this->resetAfterTest();
334 $DB->delete_records('license');
336 license_manager
::install_licenses();
338 $expectedshortnames = ['allrightsreserved', 'cc', 'cc-nc', 'cc-nc-nd', 'cc-nc-sa', 'cc-nd', 'cc-sa', 'public', 'unknown'];
339 $actualshortnames = $DB->get_records_menu('license', null, '', 'id, shortname');
341 foreach ($expectedshortnames as $expectedshortname) {
342 $this->assertContains($expectedshortname, $actualshortnames);