testElectronicSignatures.php
10.5 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
<?php
require_once (KT_DIR . '/tests/test.php');
require_once (KT_DIR . '/ktapi/ktapi.inc.php');
/**
* These are the unit tests for the main KTAPI class
*
*/
class APIElectronicSignaturesTestCase extends KTUnitTestCase {
/**
* @var object $ktapi The main ktapi object
*/
var $ktapi;
/**
* @var object $session The KT session object
*/
var $session;
/**
* @var object $root The KT folder object
*/
var $root;
/**
* @var bool $esig_enabled True if electronic signatures for api are enabled | False if not enabled
*/
var $esig_enabled;
/**
* This method sets up the KT session
*
*/
public function setUp() {
$this->ktapi = new KTAPI();
$this->session = $this->ktapi->start_session('admin', 'admin');
$this->root = $this->ktapi->get_root_folder();
$this->assertTrue($this->root instanceof KTAPI_Folder);
$this->esig_enabled = $this->ktapi->electronic_sig_enabled();
$this->assertTrue($this->esig_enabled);
}
/**
* This method emds the KT session
*
*/
public function tearDown() {
$this->session->logout();
}
/* *** Test webservice functions *** */
/**
* Testing folder creation and deletion, add document, get folder contents, folder detail
* Folder shortcuts and actions
*/
public function testFolderApiFunctions()
{
// Create a folder
// test without authentication - should fail
$result1 = $this->ktapi->create_folder(1, 'New test api folder');
$this->assertEqual($result1['status_code'], 1);
// test with authentication
$result2 = $this->ktapi->create_folder(1, 'New test api folder', 'admin', 'admin', 'Testing API');
$folder_id = $result2['results']['id'];
$this->assertEqual($result2['status_code'], 0);
$this->assertTrue($result2['results']['parent_id'] == 1);
// Create a sub folder
// test without authentication - should fail
$result3 = $this->ktapi->create_folder($folder_id, 'New test api sub-folder');
$this->assertEqual($result3['status_code'], 1);
// test with authentication
$result4 = $this->ktapi->create_folder($folder_id, 'New test api sub-folder', 'admin', 'admin', 'Testing API');
$folder_id2 = $result4['results']['id'];
$this->assertEqual($result4['status_code'], 0);
// Add a document
global $default;
$dir = $default->uploadDirectory;
$tempfilename = $this->createRandomFile('some text', $dir);
// test without authentication - should fail
$doc = $this->ktapi->add_document($folder_id, 'New API test doc', 'testdoc1.txt', 'Default', $tempfilename);
$this->assertEqual($doc['status_code'], 1);
// test with authentication
$doc = $this->ktapi->add_document($folder_id, 'New API test doc', 'testdoc1.txt', 'Default', $tempfilename,
'admin', 'admin', 'Testing API');
$this->assertEqual($doc['status_code'], 0);
$doc_id = $doc['results']['document_id'];
$this->assertEqual($doc['results']['title'], 'New API test doc');
// Rename the folder
// test without authentication - should fail
$renamed = $this->ktapi->rename_folder($folder_id, 'Renamed test folder');
$this->assertEqual($renamed['status_code'], 1);
// test with authentication
$renamed = $this->ktapi->rename_folder($folder_id, 'Renamed test folder', 'admin', 'admin', 'Testing API');
$this->assertEqual($renamed['status_code'], 0);
/**
* Copy and move appear to fail in other parts of the code, so only going to test failure here
*
* Must be VERY careful here with skipping the valid submissions, 3 failed auth attempts in a row locks out the user!
*/
// Copy folder
// test without authentication - should fail
$copied = $this->ktapi->copy_folder($source_id, $target_id, $reason);
$this->assertEqual($copied['status_code'], 1);
// // test with authentication
// $copied = $this->ktapi->copy_folder($source_id, $target_id, $reason, 'admin', 'admin');
// echo $copied['status_code']."sd<BR>";
// $this->assertEqual($copied['status_code'], 0);
// Move folder
// test without authentication - should fail
$moved = $this->ktapi->move_folder($source_id, $target_id, $reason);
$this->assertEqual($moved['status_code'], 1);
// before we end up with 3 fails in a row (see note above the first copy attempt,) force a successful auth
$renamed = $this->ktapi->rename_folder($folder_id, 'A New Name', 'admin', 'admin', 'Testing API');
// // test with authentication
// $moved = $this->ktapi->move_folder($source_id, $target_id, $reason, 'admin', 'admin');
// $this->assertEqual($moved['status_code'], 0);
// before we end up with 3 fails in a row (see note above the first copy attempt,) force a successful auth
$renamed = $this->ktapi->rename_folder($folder_id, 'A New Name', 'admin', 'admin', 'Testing API');
// Clean up - delete the folder
// test without authentication - should fail
$deleted = $this->ktapi->delete_folder($folder_id, 'Testing API');
$this->assertEqual($deleted['status_code'], 1);
// test with authentication
$deleted = $this->ktapi->delete_folder($folder_id, 'Testing API', 'admin', 'admin');
$this->assertEqual($deleted['status_code'], 0);
}
/**
* Testing document get, update, actions, delete, shortcuts and detail
*/
public function testDocumentApiFunctions()
{
// Create a folder
// test without authentication - should fail
$result1 = $this->ktapi->create_folder(1, 'New test api folder');
$this->assertEqual($result1['status_code'], 1);
// test with authentication
$result2 = $this->ktapi->create_folder(1, 'New test api folder', 'admin', 'admin', 'Testing API');
$folder_id = $result2['results']['id'];
$this->assertEqual($result2['status_code'], 0);
// Create a sub folder
// test without authentication - should fail
$result3 = $this->ktapi->create_folder($folder_id, 'New test api sub-folder');
$this->assertEqual($result3['status_code'], 1);
// test with authentication
$result4 = $this->ktapi->create_folder($folder_id, 'New test api sub-folder', 'admin', 'admin', 'Testing API');
$folder_id2 = $result4['results']['id'];
$this->assertEqual($result4['status_code'], 0);
// Add a document
global $default;
$dir = $default->uploadDirectory;
$tempfilename = $this->createRandomFile('some text', $dir);
// test without authentication - should fail
$doc = $this->ktapi->add_document($folder_id, 'New API test doc', 'testdoc1.txt', 'Default', $tempfilename);
$this->assertEqual($doc['status_code'], 1);
// test with authentication
$doc = $this->ktapi->add_document($folder_id, 'New API test doc', 'testdoc1.txt', 'Default', $tempfilename,
'admin', 'admin', 'Testing API');
$this->assertEqual($doc['status_code'], 0);
$doc_id = $doc['results']['document_id'];
// Checkout the document
// test without authentication - should fail
$result1 = $this->ktapi->checkout_document($doc_id, 'Testing API', true);
$this->assertEqual($result1['status_code'], 1);
// test with authentication
$result2 = $this->ktapi->checkout_document($doc_id, 'Testing API', true, 'admin', 'admin');
$this->assertEqual($doc['status_code'], 0);
$this->assertTrue(!empty($result2['results']));
// Checkin the document
$dir = $default->uploadDirectory;
$tempfilename = $this->createRandomFile('some text', $dir);
// test without authentication - should fail
$result3 = $this->ktapi->checkin_document($doc_id, 'testdoc1.txt', 'Testing API', $tempfilename, false);
$this->assertEqual($result3['status_code'], 1);
// test with authentication
$result4 = $this->ktapi->checkin_document($doc_id, 'testdoc1.txt', 'Testing API', $tempfilename, false, 'admin', 'admin');
$this->assertEqual($result4['status_code'], 0);
$this->assertEqual($result4['results']['document_id'], $doc_id);
// Delete the document
// test without authentication - should fail
$result5 = $this->ktapi->delete_document($doc_id, 'Testing API');
$this->assertEqual($result5['status_code'], 1);
// test with authentication
$result6 = $this->ktapi->delete_document($doc_id, 'Testing API', 'admin', 'admin', true);
$this->assertEqual($result6['status_code'], 0);
// Clean up - delete the folder
// test without authentication - should fail
$result7 = $this->ktapi->delete_folder($folder_id, 'Testing API');
$this->assertEqual($result7['status_code'], 1);
$result8 = $this->ktapi->delete_folder($folder_id, 'Testing API', 'admin', 'admin');
$this->assertEqual($result8['status_code'], 0);
}
/**
* 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));
if ($this->esig_enabled)
{
$document = $folder->add_document($title, $filename, 'Default', $randomFile, 'admin', 'admin', 'Testing API');
}
else
{
$document = $folder->add_document($title, $filename, 'Default', $randomFile);
}
$this->assertNotError($document);
@unlink($randomFile);
if(PEAR::isError($document)) return false;
return $document;
}
/**
* Helper function to delete docs
*/
function deleteDocument($document)
{
$document->delete('Testing API');
$document->expunge();
}
function createRandomFile($content = 'this is some text', $uploadDir = null) {
if(is_null($uploadDir)){
$uploadDir = dirname(__FILE__);
}
$temp = tempnam($uploadDir, 'myfile');
$fp = fopen($temp, 'wt');
fwrite($fp, $content);
fclose($fp);
return $temp;
}
}
?>