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 |
* Tests for tool_licensemanager manager class.
|
|
|
19 |
*
|
|
|
20 |
* @package tool_licensemanager
|
|
|
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 |
global $CFG;
|
|
|
28 |
require_once($CFG->libdir . '/formslib.php');
|
|
|
29 |
require_once($CFG->libdir . '/licenselib.php');
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Tests for tool_licensemanager manager class.
|
|
|
33 |
*
|
|
|
34 |
* @package tool_licensemanager
|
|
|
35 |
* @copyright 2020 Tom Dickman <tom.dickman@catalyst-au.net>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
* @group tool_licensemanager
|
|
|
38 |
*/
|
|
|
39 |
class manager_test extends advanced_testcase {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Test editing a license.
|
|
|
43 |
*/
|
|
|
44 |
public function test_edit_existing_license() {
|
|
|
45 |
$this->resetAfterTest();
|
|
|
46 |
|
|
|
47 |
// Create initial custom license to edit.
|
|
|
48 |
$testlicense = new stdClass();
|
|
|
49 |
$testlicense->shortname = 'my-lic';
|
|
|
50 |
$testlicense->fullname = 'My License';
|
|
|
51 |
$testlicense->source = 'https://fakeurl.net';
|
|
|
52 |
$testlicense->version = date('Ymd', time()) . '00';
|
|
|
53 |
$testlicense->custom = license_manager::CUSTOM_LICENSE;
|
|
|
54 |
|
|
|
55 |
license_manager::save($testlicense);
|
|
|
56 |
license_manager::enable($testlicense->shortname);
|
|
|
57 |
|
|
|
58 |
$manager = new \tool_licensemanager\manager();
|
|
|
59 |
|
|
|
60 |
// Attempt to submit form data with altered details.
|
|
|
61 |
$formdata = [
|
|
|
62 |
'shortname' => 'new-value',
|
|
|
63 |
'fullname' => 'New License Name',
|
|
|
64 |
'source' => 'https://updatedfakeurl.net',
|
|
|
65 |
'version' => time()
|
|
|
66 |
];
|
|
|
67 |
|
|
|
68 |
// Attempt to submit form data with an altered shortname.
|
|
|
69 |
\tool_licensemanager\form\edit_license::mock_submit($formdata);
|
|
|
70 |
|
|
|
71 |
// We're testing a private method, so we need to setup reflector magic.
|
|
|
72 |
$method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
|
|
|
73 |
$method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, $testlicense->shortname);
|
|
|
74 |
|
|
|
75 |
// Should not create a new license when updating an existing license.
|
|
|
76 |
$this->assertEmpty(license_manager::get_license_by_shortname($formdata['shortname']));
|
|
|
77 |
|
|
|
78 |
$actual = license_manager::get_license_by_shortname('my-lic');
|
|
|
79 |
// Should not be able to update the shortname of the license.
|
|
|
80 |
$this->assertNotSame($formdata['shortname'], $actual->shortname);
|
|
|
81 |
// Should be able to update other details of the license.
|
|
|
82 |
$this->assertSame($formdata['fullname'], $actual->fullname);
|
|
|
83 |
$this->assertSame($formdata['source'], $actual->source);
|
|
|
84 |
$this->assertSame(date('Ymd', $formdata['version']) . '00', $actual->version);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
public function test_edit_license_not_exists() {
|
|
|
88 |
$manager = new \tool_licensemanager\manager();
|
|
|
89 |
|
|
|
90 |
// We're testing a private method, so we need to setup reflector magic.
|
|
|
91 |
$method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
|
|
|
92 |
|
|
|
93 |
// Attempt to update a license that doesn't exist.
|
|
|
94 |
$formdata = [
|
|
|
95 |
'shortname' => 'new-value',
|
|
|
96 |
'fullname' => 'New License Name',
|
|
|
97 |
'source' => 'https://updatedfakeurl.net',
|
|
|
98 |
'version' => time()
|
|
|
99 |
];
|
|
|
100 |
\tool_licensemanager\form\edit_license::mock_submit($formdata);
|
|
|
101 |
|
|
|
102 |
// Should not be able to update a license with a shortname that doesn't exist.
|
|
|
103 |
$this->expectException('moodle_exception');
|
|
|
104 |
$method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, $formdata['shortname']);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
public function test_edit_license_no_shortname() {
|
|
|
108 |
$manager = new \tool_licensemanager\manager();
|
|
|
109 |
|
|
|
110 |
// We're testing a private method, so we need to setup reflector magic.
|
|
|
111 |
$method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
|
|
|
112 |
|
|
|
113 |
// Attempt to update a license without passing license shortname.
|
|
|
114 |
$formdata = [
|
|
|
115 |
'fullname' => 'New License Name',
|
|
|
116 |
'source' => 'https://updatedfakeurl.net',
|
|
|
117 |
'version' => time()
|
|
|
118 |
];
|
|
|
119 |
\tool_licensemanager\form\edit_license::mock_submit($formdata);
|
|
|
120 |
|
|
|
121 |
// Should not be able to update empty license shortname.
|
|
|
122 |
$this->expectException('moodle_exception');
|
|
|
123 |
$method->invoke($manager, \tool_licensemanager\manager::ACTION_UPDATE, '');
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
/**
|
|
|
127 |
* Test creating a new license.
|
|
|
128 |
*/
|
|
|
129 |
public function test_edit_create_license() {
|
|
|
130 |
$this->resetAfterTest();
|
|
|
131 |
|
|
|
132 |
$licensecount = count(license_manager::get_licenses());
|
|
|
133 |
|
|
|
134 |
$manager = new \tool_licensemanager\manager();
|
|
|
135 |
|
|
|
136 |
$formdata = [
|
|
|
137 |
'shortname' => 'new-value',
|
|
|
138 |
'fullname' => 'My License',
|
|
|
139 |
'source' => 'https://fakeurl.net',
|
|
|
140 |
'version' => time()
|
|
|
141 |
];
|
|
|
142 |
|
|
|
143 |
// Attempt to submit form data for a new license.
|
|
|
144 |
\tool_licensemanager\form\edit_license::mock_submit($formdata);
|
|
|
145 |
|
|
|
146 |
// We're testing a private method, so we need to setup reflector magic.
|
|
|
147 |
$method = new ReflectionMethod('\tool_licensemanager\manager', 'edit');
|
|
|
148 |
$method->invoke($manager, \tool_licensemanager\manager::ACTION_CREATE, $formdata['shortname']);
|
|
|
149 |
|
|
|
150 |
// Should create a new license in database.
|
|
|
151 |
$this->assertCount($licensecount + 1, license_manager::get_licenses());
|
|
|
152 |
$actual = license_manager::get_license_by_shortname($formdata['shortname']);
|
|
|
153 |
$this->assertSame($formdata['shortname'], $actual->shortname);
|
|
|
154 |
$this->assertSame($formdata['fullname'], $actual->fullname);
|
|
|
155 |
$this->assertSame($formdata['source'], $actual->source);
|
|
|
156 |
$this->assertSame(date('Ymd', $formdata['version']) . '00', $actual->version);
|
|
|
157 |
|
|
|
158 |
// Attempt to submit form data for a duplicate license.
|
|
|
159 |
\tool_licensemanager\form\edit_license::mock_submit($formdata);
|
|
|
160 |
|
|
|
161 |
// Should not be able to create duplicate licenses.
|
|
|
162 |
$this->expectException('moodle_exception');
|
|
|
163 |
$method->invoke($manager, \tool_licensemanager\manager::ACTION_CREATE, $formdata['shortname']);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Test changing the order of licenses.
|
|
|
168 |
*/
|
|
|
169 |
public function test_change_license_order() {
|
|
|
170 |
$this->resetAfterTest();
|
|
|
171 |
|
|
|
172 |
$licenseorder = array_keys(license_manager::get_licenses());
|
|
|
173 |
$initialposition = array_search('cc-nc-4.0', $licenseorder);
|
|
|
174 |
|
|
|
175 |
$manager = new tool_licensemanager\manager();
|
|
|
176 |
|
|
|
177 |
// We're testing a private method, so we need to setup reflector magic.
|
|
|
178 |
$method = new ReflectionMethod('\tool_licensemanager\manager', 'change_license_order');
|
|
|
179 |
$method->invoke($manager, \tool_licensemanager\manager::ACTION_MOVE_UP, 'cc-nc-4.0');
|
|
|
180 |
|
|
|
181 |
$licenseorder = array_keys(license_manager::get_licenses());
|
|
|
182 |
$newposition = array_search('cc-nc-4.0', $licenseorder);
|
|
|
183 |
|
|
|
184 |
$this->assertLessThan($initialposition, $newposition);
|
|
|
185 |
|
|
|
186 |
$initialposition = array_search('allrightsreserved', $licenseorder);
|
|
|
187 |
$method->invoke($manager, \tool_licensemanager\manager::ACTION_MOVE_DOWN, 'allrightsreserved');
|
|
|
188 |
$licenseorder = array_keys(license_manager::get_licenses());
|
|
|
189 |
$newposition = array_search('cc-nc-4.0', $licenseorder);
|
|
|
190 |
|
|
|
191 |
$this->assertGreaterThan($initialposition, $newposition);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
}
|