forked from OPUS4/application
/
FileTest.php
378 lines (261 loc) · 12.8 KB
/
FileTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the Licence, or any later version.
* OPUS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @category Application Unit Test
* @package Admin_Form
* @author Jens Schwidder <schwidder@zib.de>
* @author Maximilian Salomon <salomon@zib.de>
* @copyright Copyright (c) 2008-2018, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/
class Admin_Form_FileTest extends ControllerTestCase {
public function testConstructForm() {
$form = new Admin_Form_File();
$this->assertEquals(10, count($form->getElements()));
$elements = array('Id', 'FileLink', 'FileSize', 'Language', 'Label', 'Comment', 'VisibleIn', 'Roles', 'ServerDateSubmitted', 'SortOrder');
foreach ($elements as $element) {
$this->assertNotNull($form->getElement($element), "Element '$element' is missing.");
}
$this->assertEquals(1, count($form->getSubForms()));
$this->assertNotNull($form->getSubForm('Hashes'));
}
public function testPopulateFromModel() {
$this->useEnglish();
$form = new Admin_Form_File();
$file = new Opus_File(126); // hängt an Testdokument 146
$form->populateFromModel($file);
$this->assertEquals(126, $form->getElementValue('Id'));
$this->assertEquals($file, $form->getElementValue('FileLink'));
$this->assertEmpty($form->getElement('FileLink')->getErrorMessages()); // Datei existiert
$this->assertEquals(8817, $form->getElement('FileSize')->getValue());
$this->assertEquals('deu', $form->getElement('Language')->getValue());
$this->assertEquals('foo-pdf', $form->getElement('Label')->getValue());
$this->assertEquals('foo-pdf file', $form->getElement('Comment')->getValue());
$this->assertEquals('2013/12/10', $form->getElement('ServerDateSubmitted')->getValue());
$this->assertEquals('1', $form->getElement('SortOrder')->getValue());
$this->assertEquals(array('frontdoor', 'oai'), $form->getElement('VisibleIn')->getValue());
$this->assertEquals(array('administrator', 'guest', 'reviewer'), $form->getElement('Roles')->getValue());
$hashes = $form->getSubForm('Hashes');
// TODO hashes
}
public function testPopulateFromModelFileDoesNotExist() {
$form = new Admin_Form_File();
$file = new Opus_File(123); // von Dokument 122
$form->populateFromModel($file);
$this->assertFalse($file->exists(), 'Datei mit ID = 123 sollte in den Testdaten nicht existieren.');
$this->assertEquals(123, $form->getElementValue('Id'));
$errorMessages = $form->getElement('FileLink')->getErrorMessages();
$this->assertEquals(1, count($errorMessages));
$this->assertEquals('admin_filemanager_file_does_not_exist', $errorMessages[0]);
}
public function testUpdateModel() {
$form = new Admin_Form_File();
$form->getElement('Language')->setValue('fra');
$form->getElement('Label')->setValue('Testlabel');
$form->getElement('Comment')->setValue('Testkommentar');
$form->getElement('VisibleIn')->setValue(array('frontdoor', 'oai'));
$form->getElement('Roles')->setValue(array('reviewer', 'docsadmin'));
$document = $this->createTestDocument();
$file = $document->addFile();
$file->setPathName('test.pdf');
$document->store();
$form->updateModel($file);
$this->assertEquals('fra', $file->getLanguage());
$this->assertEquals('Testlabel', $file->getLabel());
$this->assertEquals('Testkommentar', $file->getComment());
$this->assertEquals(1, $file->getVisibleInFrontdoor());
$this->assertEquals(1, $file->getVisibleInOai());
$roles = $form->getRolesForFile($file->getId());
$this->assertEquals(2, count($roles));
$this->assertContains('reviewer', $roles);
$this->assertContains('docsadmin', $roles);
$form->getElement('VisibleIn')->setValue(array('oai'));
$form->getElement('Roles')->setValue(array('reviewer', 'guest'));
$form->updateModel($file);
$this->assertEquals(0, $file->getVisibleInFrontdoor());
$this->assertEquals(1, $file->getVisibleInOai());
$roles = $form->getRolesForFile($file->getId());
$this->assertEquals(2, count($roles));
$this->assertContains('reviewer', $roles);
$this->assertContains('guest', $roles);
}
public function testUpdateModelSingleValues() {
$form = new Admin_Form_File();
$form->getElement('VisibleIn')->setValue('frontdoor');
$form->getElement('Roles')->setValue('reviewer');
$document = $this->createTestDocument();
$file = $document->addFile();
$file->setPathName('test.pdf');
$document->store();
$form->updateModel($file);
$this->assertEquals(1, $file->getVisibleInFrontdoor());
$this->assertEquals(0, $file->getVisibleInOai());
$roles = $form->getRolesForFile($file->getId());
$this->assertInternalType('array', $roles);
$this->assertEquals(1, count($roles));
$this->assertContains('reviewer', $roles);
}
public function testGetModel() {
$form = new Admin_Form_File();
$form->getElement('Id')->setValue(126); // Datei 'test.pdf' von Dokument 146
$file = new Opus_File(126);
$form->populateFromModel($file);
$form->getElement('Comment')->setValue('Testkommentar');
$model = $form->getModel();
$this->assertInstanceOf('Opus_File', $model);
$this->assertEquals(126, $model->getId());
$this->assertEquals('Testkommentar', $model->getComment());
$roles = $form->getRolesForFile($model->getId());
$this->assertEquals(3, count($roles));
$this->assertContains('administrator', $roles);
$this->assertContains('guest', $roles);
$this->assertContains('reviewer', $roles);
}
/**
* @expectedException Application_Exception
* @expectedExceptionMessage Bad file ID = 'bla'.
*/
public function testGetModelBadId() {
$form = new Admin_Form_File();
$form->getElement('Id')->setValue('bla');
$form->getModel();
}
/**
* @expectedException Application_Exception
* @expectedExceptionMessage Unknown file ID = '8888'.
*/
public function testGetModelUnknownID() {
$form = new Admin_Form_File();
$form->getElement('Id')->setValue('8888');
$form->getModel();
}
public function testSetDefaults() {
$form = new Admin_Form_File();
$form->setName('File0');
$post = array(
'File0' => array(
'Id' => 116
)
);
$form->setDefaults($post);
$this->assertEquals(116, $form->getElementValue('Id'));
$this->assertEquals('6970', $form->getElementValue('FileSize'));
$hashes = $form->getSubForm('Hashes');
$this->assertEquals(2, count($hashes->getElements()));
}
public function testValidation() {
$form = new Admin_Form_File();
$post = array(
'FileLink' => 123,
'Language' => 'deu'
);
$result = $form->isValid($post);
$this->assertTrue($result);
}
/**
* @expectedException Application_Exception
* @expectedExceptionMessage File with ID = 5555 not found.
*/
public function testValidationUnknownFileLink() {
$form = new Admin_Form_File();
$post = array(
'FileLink' => '5555',
'Language' => 'eng'
);
$result = $form->isValid($post);
}
public function testUpdateFileRoles() {
$form = new Admin_Form_File();
$logger = new MockLogger();
$form->setLogger($logger);
$document = $this->createTestDocument();
$file = $document->addFile();
$file->setPathName('test.pdf');
$document->store(); // setzt automatisch 'guest' Zugriff für Datei
$form->updateFileRoles($file, array('administrator', 'reviewer'));
$fileId = $file->getId();
$messages = $logger->getMessages();
$this->assertEquals(3, count($messages));
$this->assertContains("File ID = $fileId access for role 'guest' removed.", $messages[0]);
$this->assertContains("File ID = $fileId access for role 'administrator' added.", $messages[1]);
$this->assertContains("File ID = $fileId access for role 'reviewer' added.", $messages[2]);
$roles = $form->getRolesForFile($fileId);
$this->assertEquals(2, count($roles));
$this->assertContains('administrator', $roles);
$this->assertContains('reviewer', $roles);
$logger->clear();
$form->updateFileRoles($file, array('guest', 'reviewer'));
$messages = $logger->getMessages();
$this->assertEquals(3, count($messages));
$this->assertContains("File ID = $fileId access for role 'administrator' removed.", $messages[0]);
$this->assertContains("File ID = $fileId access for role 'guest' added.", $messages[1]);
$this->assertContains("File ID = $fileId access for role 'reviewer' already permitted.", $messages[2]);
$roles = $form->getRolesForFile($fileId);
$this->assertEquals(2, count($roles));
$this->assertContains('guest', $roles);
$this->assertContains('reviewer', $roles);
$logger->clear();
$form->updateFileRoles($file, 'docsadmin');
$messages = $logger->getMessages();
$this->assertEquals(3, count($messages));
$this->assertContains("File ID = $fileId access for role 'guest' removed.", $messages[0]);
$this->assertContains("File ID = $fileId access for role 'reviewer' removed.", $messages[1]);
$this->assertContains("File ID = $fileId access for role 'docsadmin' added.", $messages[2]);
$roles = $form->getRolesForFile($fileId);
$this->assertEquals(1, count($roles));
$this->assertContains('docsadmin', $roles);
$logger->clear();
$form->updateFileRoles($file, null);
$messages = $logger->getMessages();
$this->assertEquals(1, count($messages));
$this->assertContains("File ID = $fileId access for role 'docsadmin' removed.", $messages[0]);
$roles = $form->getRolesForFile($fileId);
$this->assertEquals(0, count($roles));
$logger->clear();
$form->updateFileRoles($file, 'unknownrole');
$messages = $logger->getMessages();
$this->assertEquals(1, count($messages));
$this->assertContains("Unknown role 'unknownrole'.", $messages[0]);
$roles = $form->getRolesForFile($fileId);
$this->assertEquals(0, count($roles));
}
public function testUpdateModelSortOrderNull() {
$form = new Admin_Form_File();
$form->getElement('Language')->setValue('fra');
$form->getElement('SortOrder')->setValue(1);
$document = $this->createTestDocument();
$file = $document->addFile();
$file->setPathName('test.pdf');
$document->store();
$form->updateModel($file);
$this->assertEquals('fra', $file->getLanguage());
$this->assertEquals(1, $file->getSortOrder());
$form->getElement('SortOrder')->setValue(null);
$form->updateModel($file);
$this->assertNotNull($file->getSortOrder());
$document->store(); // triggered exception before fix
}
}