testBulkActions.php
11.6 KB
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
<?php
require_once (KT_DIR . '/tests/test.php');
require_once (KT_DIR . '/ktapi/ktapi.inc.php');
/**
* Unit tests for the KTAPI_BulkActions class
*
* @author KnowledgeTree Team
* @package KnowledgeTree API
* @version 0.9
*/
class APIBulkActionsTestCase extends KTUnitTestCase {
/**
* @var KTAPI
*/
var $ktapi;
/**
* @var KTAPI_Folder
*/
var $root;
/**
* @var KTAPI_Session
*/
var $session;
/**
* @var KTAPI_BulkActions
*/
var $bulk;
/**
* Create a ktapi session
*/
function setUp() {
$this->ktapi = new KTAPI();
$this->session = $this->ktapi->start_system_session();
$this->root = $this->ktapi->get_root_folder();
$this->bulk = new KTAPI_BulkActions($this->ktapi);
}
/**
* End the ktapi session
*/
function tearDown() {
$this->session->logout();
}
/**
* Test the bulk copy functionality
*/
function testCopy()
{
// Create documents
$doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
$doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
$doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
$folder1 = $this->root->add_folder("New copy folder");
$this->assertNotError($newFolder);
if(PEAR::isError($newFolder)) return;
$doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
// Add a folder
$targetFolder = $this->root->add_folder("New target folder");
$this->assertNotError($newFolder);
if(PEAR::isError($newFolder)) return;
$aItems = array($doc1, $doc2, $doc3, $folder1);
// Copy documents and folder into target folder
$res = $this->bulk->copy($aItems, $targetFolder, 'Testing bulk copy');
$this->assertTrue(empty($res));
// Check the documents copied
$listDocs = $targetFolder->get_listing(1, 'D');
$this->assertTrue(count($listDocs) == 3);
// Check the folder copied
$listFolders = $targetFolder->get_listing(1, 'F');
$this->assertTrue(count($listFolders) == 1);
// Check the document contained in the folder copied
$newFolderId = $listFolders[0]['id'];
$newFolder = $this->ktapi->get_folder_by_id($newFolderId);
$listSubDocs = $newFolder->get_listing(1, 'D');
$this->assertTrue(count($listSubDocs) == 1);
// Delete and expunge documents and folder
$this->deleteDocument($doc1);
$this->deleteDocument($doc2);
$this->deleteDocument($doc3);
$this->deleteDocument($doc4);
$targetFolder->delete('Testing bulk copy');
$folder1->delete('Testing bulk copy');
}
/**
* Test the bulk move functionality
*/
function testMove()
{
// Create documents
$doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
$doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
$doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
$folder1 = $this->root->add_folder("New move folder");
$this->assertNotError($newFolder);
if(PEAR::isError($newFolder)) return;
$doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
// Add a folder
$targetFolder = $this->root->add_folder("New target folder");
$this->assertNotError($newFolder);
if(PEAR::isError($newFolder)) return;
$aItems = array($doc1, $doc2, $doc3, $folder1);
// Copy documents and folder into target folder
$res = $this->bulk->move($aItems, $targetFolder, 'Testing bulk move');
$this->assertTrue(empty($res));
// Check document has been moved not copied
$detail = $doc1->get_detail();
$this->assertFalse($detail['folder_id'] == $this->root->get_folderid());
$this->assertTrue($detail['folder_id'] == $targetFolder->get_folderid());
// Check folder has been moved not copied
$this->assertFalse($folder1->get_parent_folder_id() == $this->root->get_folderid());
$this->assertTrue($folder1->get_parent_folder_id() == $targetFolder->get_folderid());
// Check the documents copied
$listDocs = $targetFolder->get_listing(1, 'D');
$this->assertTrue(count($listDocs) == 3);
// Check the folder copied
$listFolders = $targetFolder->get_listing(1, 'F');
$this->assertTrue(count($listFolders) == 1);
// Check the document contained in the folder copied
$newFolderId = $listFolders[0]['id'];
$newFolder = $this->ktapi->get_folder_by_id($newFolderId);
$listSubDocs = $newFolder->get_listing(1, 'D');
$this->assertTrue(count($listSubDocs) == 1);
// Delete and expunge documents and folder
$this->deleteDocument($doc1);
$this->deleteDocument($doc2);
$this->deleteDocument($doc3);
$this->deleteDocument($doc4);
$targetFolder->delete('Testing bulk copy');
$folder1->delete('Testing bulk copy');
}
/**
* Test the bulk checkout and cancel checkout functionality
*/
function testCheckout()
{
// Create documents
$doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
$doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
$doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
$folder1 = $this->root->add_folder("New test folder");
$this->assertNotError($newFolder);
if(PEAR::isError($newFolder)) return;
$doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
$aItems = array($doc1, $doc2, $doc3, $folder1);
// Checkout documents and folder
$res = $this->bulk->checkout($aItems, 'Testing bulk checkout');
$this->assertTrue(empty($res));
$this->assertTrue($doc1->is_checked_out());
$this->assertTrue($doc2->is_checked_out());
$this->assertTrue($doc3->is_checked_out());
$this->assertTrue($doc4->is_checked_out());
$res = $this->bulk->undo_checkout($aItems, 'Testing bulk undo / cancel checkout');
$this->assertTrue(empty($res));
$this->assertFalse($doc1->is_checked_out());
$this->assertFalse($doc2->is_checked_out());
$this->assertFalse($doc3->is_checked_out());
$this->assertFalse($doc4->is_checked_out());
// Delete and expunge documents and folder
$this->deleteDocument($doc1);
$this->deleteDocument($doc2);
$this->deleteDocument($doc3);
$this->deleteDocument($doc4);
$folder1->delete('Testing bulk checkout');
}
/**
* Test the bulk immute functionality
*/
function testImmute()
{
// Create documents
$doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
$doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
$doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
$folder1 = $this->root->add_folder("New test folder");
$this->assertNotError($newFolder);
if(PEAR::isError($newFolder)) return;
$doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
$aItems = array($doc1, $doc2, $doc3, $folder1);
// Immute documents
$res = $this->bulk->immute($aItems);
$this->assertTrue(empty($res));
$this->assertTrue($doc1->isImmutable());
$this->assertTrue($doc2->isImmutable());
$this->assertTrue($doc3->isImmutable());
$this->assertTrue($doc4->isImmutable());
// remove immutability for deletion
$doc1->unimmute();
$doc2->unimmute();
$doc3->unimmute();
$doc4->unimmute();
// Delete and expunge documents and folder
$this->deleteDocument($doc1);
$this->deleteDocument($doc2);
$this->deleteDocument($doc3);
$this->deleteDocument($doc4);
$folder1->delete('Testing bulk checkout');
}
/**
* Test the bulk delete functionality
*/
function testDelete()
{
// Create documents
$doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
$doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
$doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
$folder1 = $this->root->add_folder("New test folder");
$this->assertNotError($newFolder);
if(PEAR::isError($newFolder)) return;
$doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
$aItems = array($doc1, $doc2, $doc3, $folder1);
// Delete documents and folder
$res = $this->bulk->delete($aItems, 'Testing bulk delete');
$this->assertTrue(empty($res));
// Check documents have been deleted
$this->assertTrue($doc1->is_deleted());
$this->assertTrue($doc2->is_deleted());
$this->assertTrue($doc3->is_deleted());
$this->assertTrue($doc4->is_deleted());
// Check folder has been deleted
$folder = $this->ktapi->get_folder_by_name('New test folder');
$this->assertError($folder);
// Expunge documents
$doc1->expunge();
$doc2->expunge();
$doc3->expunge();
$doc4->expunge();
}
/**
* Test the bulk archive functionality
*/
function testArchive()
{
// Create documents
$doc1 = $this->createDocument('Test Doc One', 'testdoc1.txt');
$doc2 = $this->createDocument('Test Doc Two', 'testdoc2.txt');
$doc3 = $this->createDocument('Test Doc Three', 'testdoc3.txt');
$folder1 = $this->root->add_folder("New test folder");
$this->assertNotError($newFolder);
if(PEAR::isError($newFolder)) return;
$doc4 = $this->createDocument('Test Doc Four', 'testdoc4.txt', $folder1);
$aItems = array($doc1, $doc2, $doc3, $folder1);
// Archive documents and folder
$res = $this->bulk->archive($aItems, 'Testing bulk archive');
$this->assertTrue(empty($res));
$document1 = $doc1->getObject();
$this->assertTrue($document1->getStatusID() == 4);
$document4 = $doc4->getObject();
$this->assertTrue($document4->getStatusID() == 4);
// Restore for deletion
$doc1->restore();
$doc2->restore();
$doc3->restore();
$doc4->restore();
// Delete and expunge documents and folder
$this->deleteDocument($doc1);
$this->deleteDocument($doc2);
$this->deleteDocument($doc3);
$this->deleteDocument($doc4);
$folder1->delete('Testing bulk archive');
}
/**
* Helper function to delete docs
*/
function deleteDocument($document)
{
$document->delete('Testing bulk actions');
$document->expunge();
}
/**
* Helper function to create a document
*/
function createDocument($title, $filename, $folder = null)
{
if(is_null($folder)){
$folder = $this->root;
}
// Create a new document
$randomFile = $this->createRandomFile();
$this->assertTrue(is_file($randomFile));
$document = $folder->add_document($title, $filename, 'Default', $randomFile);
$this->assertNotError($document);
@unlink($randomFile);
if(PEAR::isError($document)) return false;
return $document;
}
/**
* Helper function to create a file
*
* @param unknown_type $content
* @return unknown
*/
function createRandomFile($content = 'this is some text') {
$temp = tempnam(dirname(__FILE__), 'myfile');
file_put_contents($temp, $content);
return $temp;
}
}
?>