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 |
* This file contains tests for the repository_equella class.
|
|
|
19 |
*
|
|
|
20 |
* @package repository_equella
|
|
|
21 |
*
|
|
|
22 |
* @author Guillaume BARAT <guillaumebarat@catalyst-au.net>
|
|
|
23 |
* @copyright Catalyst IT
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
namespace repository_equella;
|
|
|
28 |
|
|
|
29 |
use repository_equella;
|
|
|
30 |
use stdClass;
|
|
|
31 |
|
|
|
32 |
defined('MOODLE_INTERNAL') || die();
|
|
|
33 |
|
|
|
34 |
global $CFG;
|
|
|
35 |
require_once($CFG->dirroot . '/repository/lib.php');
|
|
|
36 |
require_once($CFG->libdir . '/webdavlib.php');
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Class repository_equella_lib_testcase
|
|
|
40 |
*
|
|
|
41 |
* @group repository_equella
|
|
|
42 |
* @copyright Catalyst IT
|
|
|
43 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
44 |
*/
|
|
|
45 |
class lib_test extends \advanced_testcase {
|
|
|
46 |
|
|
|
47 |
/** @var null|\repository_equella the repository_equella object, which the tests are run on. */
|
|
|
48 |
private $repo = null;
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Create some data for repository.
|
|
|
52 |
*
|
|
|
53 |
* @return stdClass
|
|
|
54 |
*/
|
|
|
55 |
private function create_new_form_data(): stdClass {
|
|
|
56 |
$record = new stdClass();
|
|
|
57 |
$record->equella_url = 'http://dummy.url.com';
|
|
|
58 |
$record->equella_userfield = 'default';
|
|
|
59 |
$record->equella_select_restriction = 'none';
|
|
|
60 |
$record->equella_options = '';
|
|
|
61 |
$record->equella_shareid = 'id';
|
|
|
62 |
$record->equella_sharedsecret = 'secret';
|
|
|
63 |
$record->equella_manager_shareid = '';
|
|
|
64 |
$record->equella_manager_sharedsecret = '';
|
|
|
65 |
$record->equella_editingteacher_shareid = '';
|
|
|
66 |
$record->equella_editingteacher_sharedsecret = '';
|
|
|
67 |
|
|
|
68 |
return $record;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Create repository for testing.
|
|
|
73 |
*
|
|
|
74 |
* @return repository_equella
|
|
|
75 |
*/
|
|
|
76 |
private function create_repository(): repository_equella {
|
|
|
77 |
$record = new \stdClass();
|
|
|
78 |
$this->getDataGenerator()->create_repository_type('equella', $record);
|
|
|
79 |
$generator = $this->getDataGenerator()->get_plugin_generator('repository_equella');
|
|
|
80 |
$instance = $generator->create_instance();
|
|
|
81 |
$this->repo = new repository_equella($instance->id);
|
|
|
82 |
return $this->repo;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Test that environment is created.
|
|
|
87 |
* @covers \repository_equella::get_repository_by_id
|
|
|
88 |
* @return void
|
|
|
89 |
*/
|
|
|
90 |
public function test_repository_is_created(): void {
|
|
|
91 |
$this->initialise_repository();
|
|
|
92 |
$actual = repository_equella::get_repository_by_id($this->repo->id, $this->repo->context);
|
|
|
93 |
$this->assertEquals($this->repo->options['equella_url'], $actual->get_option('equella_url'));
|
|
|
94 |
$this->assertEquals($this->repo->options['equella_userfield'], $actual->get_option('equella_userfield'));
|
|
|
95 |
$this->assertEquals($this->repo->options['equella_select_restriction'],
|
|
|
96 |
$actual->get_option('equella_select_restriction'));
|
|
|
97 |
$this->assertEquals($this->repo->options['equella_options'], $actual->get_option('equella_options'));
|
|
|
98 |
$this->assertEquals($this->repo->options['equella_shareid'], $actual->get_option('equella_shareid'));
|
|
|
99 |
$this->assertEquals($this->repo->options['equella_sharedsecret'], $actual->get_option('equella_sharedsecret'));
|
|
|
100 |
$this->assertEquals($this->repo->options['equella_manager_shareid'], $actual->get_option('equella_manager_shareid'));
|
|
|
101 |
$this->assertEquals($this->repo->options['equella_manager_sharedsecret'],
|
|
|
102 |
$actual->get_option('equella_manager_sharedsecret'));
|
|
|
103 |
$this->assertEquals($this->repo->options['equella_editingteacher_shareid'],
|
|
|
104 |
$actual->get_option('equella_editingteacher_shareid'));
|
|
|
105 |
$this->assertEquals($this->repo->options['equella_editingteacher_sharedsecret'],
|
|
|
106 |
$actual->get_option('equella_editingteacher_sharedsecret'));
|
|
|
107 |
$this->resetAfterTest(true);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Data provider for get_userfield_value.
|
|
|
112 |
*
|
|
|
113 |
* @return array
|
|
|
114 |
* @covers ::get_userfield_value
|
|
|
115 |
*/
|
|
|
116 |
public static function get_userfield_value_provider(): array {
|
|
|
117 |
return [
|
|
|
118 |
[
|
|
|
119 |
'input' => [
|
|
|
120 |
'userfield' => 'nickname',
|
|
|
121 |
'value' => 'administrator',
|
|
|
122 |
],
|
|
|
123 |
'expected' => [
|
|
|
124 |
'username' => 'administrator',
|
|
|
125 |
],
|
|
|
126 |
], [
|
|
|
127 |
'input' => [
|
|
|
128 |
'userfield' => 'default',
|
|
|
129 |
'value' => 'default',
|
|
|
130 |
],
|
|
|
131 |
'expected' => [
|
|
|
132 |
'username' => 'admin',
|
|
|
133 |
],
|
|
|
134 |
], [
|
|
|
135 |
'input' => [
|
|
|
136 |
'userfield' => 'test',
|
|
|
137 |
'value' => 'test',
|
|
|
138 |
],
|
|
|
139 |
'expected' => [
|
|
|
140 |
'username' => 'test',
|
|
|
141 |
],
|
|
|
142 |
],
|
|
|
143 |
];
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Test method get_userfield_value.
|
|
|
148 |
*
|
|
|
149 |
* @dataProvider get_userfield_value_provider
|
|
|
150 |
*
|
|
|
151 |
* @param array $input
|
|
|
152 |
* @param array $expected
|
|
|
153 |
* @covers ::get_userfield_value
|
|
|
154 |
*
|
|
|
155 |
* @return void
|
|
|
156 |
*/
|
|
|
157 |
public function test_get_userfield_value($input, $expected): void {
|
|
|
158 |
global $USER;
|
|
|
159 |
$this->initialise_repository();
|
|
|
160 |
$USER->profile[$input['userfield']] = $input['value'];
|
|
|
161 |
|
|
|
162 |
$this->repo->set_option(['equella_userfield' => $input['userfield']]);
|
|
|
163 |
$return = $this->repo->get_userfield_value();
|
|
|
164 |
$this->assertEquals($expected['username'], $return);
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Data provider for get_listing.
|
|
|
169 |
*
|
|
|
170 |
* @return array
|
|
|
171 |
* @covers ::get_listing
|
|
|
172 |
*/
|
|
|
173 |
public static function get_listing_provider(): array {
|
|
|
174 |
return [
|
|
|
175 |
[
|
|
|
176 |
'input' => [
|
|
|
177 |
'url' => 'http://dummy.url.com',
|
|
|
178 |
'userfield' => 'nickname',
|
|
|
179 |
'value' => 'administrator',
|
|
|
180 |
],
|
|
|
181 |
'expected' => [
|
|
|
182 |
'username' => 'administrator',
|
|
|
183 |
],
|
|
|
184 |
], [
|
|
|
185 |
'input' => [
|
|
|
186 |
'url' => 'http://dummy.url.com',
|
|
|
187 |
'userfield' => 'default',
|
|
|
188 |
'value' => '',
|
|
|
189 |
],
|
|
|
190 |
'expected' => [
|
|
|
191 |
'username' => 'admin',
|
|
|
192 |
],
|
|
|
193 |
], [
|
|
|
194 |
'input' => [
|
|
|
195 |
'url' => 'http://dummy.url.com',
|
|
|
196 |
'userfield' => 'test',
|
|
|
197 |
'value' => 'test',
|
|
|
198 |
],
|
|
|
199 |
'expected' => [
|
|
|
200 |
'username' => 'test',
|
|
|
201 |
],
|
|
|
202 |
],
|
|
|
203 |
];
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
/**
|
|
|
207 |
* Test that the method get_listing return the correct array.
|
|
|
208 |
*
|
|
|
209 |
* @dataProvider get_listing_provider
|
|
|
210 |
*
|
|
|
211 |
* @param array $input
|
|
|
212 |
* @param array $expected
|
|
|
213 |
* @covers ::get_listing
|
|
|
214 |
*
|
|
|
215 |
* @return void
|
|
|
216 |
*/
|
|
|
217 |
public function test_get_listing($input, $expected): void {
|
|
|
218 |
global $USER;
|
|
|
219 |
$this->initialise_repository();
|
|
|
220 |
$USER->profile[$input['userfield']] = $input['value'];
|
|
|
221 |
$this->repo->set_option(['url' => $input['url'],
|
|
|
222 |
'equella_userfield' => $input['userfield']]);
|
|
|
223 |
$listing = $this->repo->get_listing();
|
|
|
224 |
$this->assertArrayHasKey('manage', $listing);
|
|
|
225 |
$this->assertStringContainsString($expected['username'], $listing['manage']);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
* Create and initialise the repository for test.
|
|
|
230 |
* @return void
|
|
|
231 |
*/
|
|
|
232 |
public function initialise_repository(): void {
|
|
|
233 |
$this->resetAfterTest(true);
|
|
|
234 |
// Admin is neccessary to create repository.
|
|
|
235 |
$this->setAdminUser();
|
|
|
236 |
$this->create_repository();
|
|
|
237 |
$this->create_new_form_data();
|
|
|
238 |
}
|
|
|
239 |
}
|