@@ -19,7 +19,7 @@ public function setUp()
19
19
$ this ->setupDatabase (array ('default ' )); //core dataset
20
20
$ this ->setupDatabase (array ('default ' ), 'api ' ); // module dataset
21
21
$ this ->enabledModules = array ('api ' );
22
- $ this ->_models = array ('User ' , 'Folder ' , 'Item ' );
22
+ $ this ->_models = array ('User ' , 'Folder ' , 'Item ' , ' ItemRevision ' );
23
23
$ this ->_daos = array ('User ' , 'Folder ' , 'Item ' );
24
24
25
25
parent ::setUp ();
@@ -251,4 +251,88 @@ public function testSessionAuthentication()
251
251
$ this ->assertEquals ($ resp ->data [0 ]->name , 'User 1 name Folder 2 ' );
252
252
$ this ->assertEquals ($ resp ->data [1 ]->name , 'User 1 name Folder 3 ' );
253
253
}
254
+
255
+ /** Test file upload */
256
+ public function testUpload ()
257
+ {
258
+ $ this ->resetAll ();
259
+ $ usersFile = $ this ->loadData ('User ' , 'default ' );
260
+ $ itemsFile = $ this ->loadData ('Item ' , 'default ' );
261
+
262
+ $ this ->params ['token ' ] = $ this ->_loginUsingApiKey ();
263
+ $ this ->params ['method ' ] = 'midas.upload.generatetoken ' ;
264
+ $ this ->params ['filename ' ] = 'test.txt ' ;
265
+ // call should fail for the first item since we don't have write permission
266
+ $ this ->params ['itemid ' ] = $ itemsFile [0 ]->getKey ();
267
+ $ this ->request ->setMethod ('POST ' );
268
+ $ resp = $ this ->_callJsonApi ();
269
+ $ this ->assertEquals ($ resp ->stat , 'fail ' );
270
+ $ this ->assertEquals ($ resp ->message , 'Invalid policy or itemid ' );
271
+ $ this ->assertTrue ($ resp ->code != 0 );
272
+
273
+ $ this ->resetAll ();
274
+ $ usersFile = $ this ->loadData ('User ' , 'default ' );
275
+ $ itemsFile = $ this ->loadData ('Item ' , 'default ' );
276
+
277
+ $ this ->params ['token ' ] = $ this ->_loginUsingApiKey ();
278
+ $ this ->params ['method ' ] = 'midas.upload.generatetoken ' ;
279
+ $ this ->params ['filename ' ] = 'test.txt ' ;
280
+ // use the second item since it has write permission set for our user
281
+ $ this ->params ['itemid ' ] = $ itemsFile [1 ]->getKey ();
282
+ $ this ->request ->setMethod ('POST ' );
283
+ $ resp = $ this ->_callJsonApi ();
284
+
285
+ $ this ->_assertStatusOk ($ resp );
286
+ $ token = $ resp ->data ->token ;
287
+ $ this ->assertTrue (
288
+ preg_match ('/^ ' .$ usersFile [0 ]->getKey ().'\/ ' .$ itemsFile [1 ]->getKey ().'\/.+\.tmp$/ ' , $ token ) > 0 ,
289
+ 'Upload token is not of the form <userid>/<itemid>/*.tmp ' );
290
+ $ this ->assertTrue (file_exists (BASE_PATH .'/tmp/misc/ ' .$ token ),
291
+ "Token placeholder file $ token was not created in the temp dir " );
292
+
293
+ //now upload using our token
294
+ $ this ->resetAll ();
295
+
296
+ $ string = '' ;
297
+ $ length = 100 ;
298
+ for ($ i = 0 ; $ i < $ length ; $ i ++)
299
+ {
300
+ $ string .= 'a ' ;
301
+ }
302
+ $ fh = fopen (BASE_PATH .'/tmp/misc/test.txt ' , 'w ' );
303
+ fwrite ($ fh , $ string );
304
+ fclose ($ fh );
305
+ $ md5 = md5 ($ string );
306
+ $ assetstoreFile = BASE_PATH .'/data/assetstore/ ' .substr ($ md5 , 0 , 2 ).'/ ' .substr ($ md5 , 2 , 2 ).'/ ' .$ md5 ;
307
+ unlink ($ assetstoreFile );
308
+
309
+ $ this ->params ['method ' ] = 'midas.upload.perform ' ;
310
+ $ this ->params ['uploadtoken ' ] = $ token ;
311
+ $ this ->params ['filename ' ] = 'test.txt ' ;
312
+ $ this ->params ['length ' ] = $ length ;
313
+ $ this ->params ['itemid ' ] = $ itemsFile [1 ]->getKey ();
314
+ $ this ->params ['revision ' ] = 'head ' ; //upload into head revision
315
+ $ this ->params ['testingmode ' ] = 'true ' ;
316
+
317
+ $ this ->request ->setMethod ('POST ' );
318
+ $ resp = $ this ->_callJsonApi ();
319
+
320
+ unlink (BASE_PATH .'/tmp/misc/test.txt ' );
321
+
322
+ $ this ->_assertStatusOk ($ resp );
323
+
324
+ $ this ->assertTrue (file_exists ($ assetstoreFile ), 'File was not written to the assetstore ' );
325
+ $ this ->assertEquals (filesize ($ assetstoreFile ), $ length , 'Assetstore file is the wrong length: ' .filesize ($ assetstoreFile ));
326
+ $ this ->assertEquals (md5_file ($ assetstoreFile ), $ md5 , 'Assetstore file had incorrect checksum ' );
327
+
328
+ // make sure it was uploaded to the head revision of the item
329
+ $ itemDao = $ this ->Item ->load ($ itemsFile [1 ]->getKey ());
330
+ $ revisions = $ itemDao ->getRevisions ();
331
+ $ this ->assertEquals (count ($ revisions ), 1 , 'Too many revisions in the item ' );
332
+ $ bitstreams = $ revisions [0 ]->getBitstreams ();
333
+ $ this ->assertEquals (count ($ bitstreams ), 1 , 'Too many bitstreams in the revision ' );
334
+ $ this ->assertEquals ($ bitstreams [0 ]->name , 'test.txt ' );
335
+ $ this ->assertEquals ($ bitstreams [0 ]->sizebytes , $ length );
336
+ $ this ->assertEquals ($ bitstreams [0 ]->checksum , $ md5 );
337
+ }
254
338
}
0 commit comments