@@ -111,6 +111,8 @@ public function testUserFolders()
111
111
$ this ->_assertStatusOk ($ resp );
112
112
$ this ->assertEquals (count ($ resp ->data ), 2 );
113
113
114
+ // We do not expect folder 1000 to be returned, as this is an internal-only
115
+ // value not intended to be exposed by the web api
114
116
foreach ($ resp ->data as $ folder )
115
117
{
116
118
$ this ->assertEquals ($ folder ->_model , 'Folder ' );
@@ -331,6 +333,7 @@ public function testBitstreamUpload()
331
333
$ usersFile = $ this ->loadData ('User ' , 'default ' );
332
334
$ itemsFile = $ this ->loadData ('Item ' , 'default ' );
333
335
336
+ // generate an upload token
334
337
$ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
335
338
$ this ->params ['method ' ] = 'midas.upload.generatetoken ' ;
336
339
$ this ->params ['filename ' ] = 'test.txt ' ;
@@ -346,6 +349,8 @@ public function testBitstreamUpload()
346
349
$ this ->resetAll ();
347
350
$ usersFile = $ this ->loadData ('User ' , 'default ' );
348
351
$ itemsFile = $ this ->loadData ('Item ' , 'default ' );
352
+
353
+ // generate the test file
349
354
$ string = '' ;
350
355
$ length = 100 ;
351
356
for ($ i = 0 ; $ i < $ length ; $ i ++)
@@ -364,6 +369,7 @@ public function testBitstreamUpload()
364
369
unlink ($ assetstoreFile );
365
370
}
366
371
372
+ // generate another upload token
367
373
$ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
368
374
$ this ->params ['method ' ] = 'midas.upload.generatetoken ' ;
369
375
$ this ->params ['filename ' ] = 'test.txt ' ;
@@ -372,14 +378,15 @@ public function testBitstreamUpload()
372
378
$ this ->params ['itemid ' ] = $ itemsFile [1 ]->getKey ();
373
379
$ resp = $ this ->_callJsonApi ();
374
380
$ this ->_assertStatusOk ($ resp );
375
-
381
+ // verify the upload token
376
382
$ token = $ resp ->data ->token ;
377
383
$ this ->assertTrue (
378
384
preg_match ('/^ ' .$ usersFile [0 ]->getKey ().'\/ ' .$ itemsFile [1 ]->getKey ().'\/.+\..+$/ ' , $ token ) > 0 ,
379
385
'Upload token ( ' .$ token .') is not of the form <userid>/<itemid>/*.* ' );
380
386
$ this ->assertTrue (file_exists (BASE_PATH .'/tmp/misc/ ' .$ token ),
381
387
'Token placeholder file ' .$ token .' was not created in the temp dir ' );
382
388
389
+ // attempt the upload
383
390
$ this ->resetAll ();
384
391
$ this ->params ['method ' ] = 'midas.upload.perform ' ;
385
392
$ this ->params ['uploadtoken ' ] = $ token ;
@@ -391,8 +398,6 @@ public function testBitstreamUpload()
391
398
$ resp = $ this ->_callJsonApi ();
392
399
$ this ->_assertStatusOk ($ resp );
393
400
394
- unlink (BASE_PATH .'/tmp/misc/test.txt ' );
395
-
396
401
$ this ->assertTrue (file_exists ($ assetstoreFile ), 'File was not written to the assetstore ' );
397
402
$ this ->assertEquals (filesize ($ assetstoreFile ), $ length , 'Assetstore file is the wrong length ' );
398
403
$ this ->assertEquals (md5_file ($ assetstoreFile ), $ md5 , 'Assetstore file had incorrect checksum ' );
@@ -407,7 +412,28 @@ public function testBitstreamUpload()
407
412
$ this ->assertEquals ($ bitstreams [0 ]->sizebytes , $ length );
408
413
$ this ->assertEquals ($ bitstreams [0 ]->checksum , $ md5 );
409
414
415
+
416
+ // when calling midas.upload.perform 2x in a row with the same params
417
+ // (same upload token, same file that had just been uploaded),
418
+ // the response should be an invalid token, -141.
419
+ //
420
+ // This is because the token is good for a single upload, and it no longer
421
+ // exists once the original upload is finished.
422
+ $ this ->resetAll ();
423
+ $ this ->params ['method ' ] = 'midas.upload.perform ' ;
424
+ $ this ->params ['uploadtoken ' ] = $ token ;
425
+ $ this ->params ['filename ' ] = 'test.txt ' ;
426
+ $ this ->params ['length ' ] = $ length ;
427
+ $ this ->params ['itemid ' ] = $ itemsFile [1 ]->getKey ();
428
+ $ this ->params ['revision ' ] = 'head ' ; //upload into head revision
429
+ $ this ->params ['testingmode ' ] = 'true ' ;
430
+ $ resp = $ this ->_callJsonApi ();
431
+ $ this ->assertNotEquals ($ resp , false );
432
+ $ this ->assertEquals ($ resp ->stat , 'fail ' );
433
+ $ this ->assertEquals ($ resp ->code , -141 );
434
+
410
435
// Check that a redundant upload yields a blank upload token and a new reference
436
+ // redundant upload meaning uploading a checksum that already exists
411
437
$ this ->resetAll ();
412
438
$ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
413
439
$ this ->params ['method ' ] = 'midas.upload.generatetoken ' ;
@@ -416,10 +442,11 @@ public function testBitstreamUpload()
416
442
$ this ->params ['itemid ' ] = $ itemsFile [1 ]->getKey ();
417
443
$ resp = $ this ->_callJsonApi ();
418
444
$ this ->_assertStatusOk ($ resp );
419
-
420
445
$ token = $ resp ->data ->token ;
421
446
$ this ->assertEquals ($ token , '' , 'Redundant content upload did not return a blank token ' );
422
447
448
+ // check that the new bitstream has been created
449
+ // in the generatetoken step
423
450
$ itemDao = $ this ->Item ->load ($ itemsFile [1 ]->getKey ());
424
451
$ revisions = $ itemDao ->getRevisions ();
425
452
$ this ->assertEquals (count ($ revisions ), 1 , 'Wrong number of revisions in the item ' );
@@ -431,6 +458,128 @@ public function testBitstreamUpload()
431
458
$ this ->assertEquals ($ bitstreams [1 ]->name , 'test2.txt ' );
432
459
$ this ->assertEquals ($ bitstreams [1 ]->sizebytes , $ length );
433
460
$ this ->assertEquals ($ bitstreams [1 ]->checksum , $ md5 );
461
+
462
+ //separate testing for item create and delete
463
+ // create a new item in the user root folder
464
+ // use folderid 1000
465
+ $ this ->resetAll ();
466
+ $ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
467
+ $ this ->params ['method ' ] = 'midas.item.create ' ;
468
+ $ this ->params ['name ' ] = 'created_item ' ;
469
+ $ this ->params ['parentid ' ] = '1000 ' ;
470
+ $ resp = $ this ->_callJsonApi ();
471
+ $ this ->_assertStatusOk ($ resp );
472
+ $ generatedItemId = $ resp ->data ->item_id ;
473
+ $ itemDao = $ this ->Item ->load ($ generatedItemId );
474
+ $ revisions = $ itemDao ->getRevisions ();
475
+ $ this ->assertEquals (count ($ revisions ), 0 , 'Wrong number of revisions in the new item ' );
476
+
477
+ // generate upload token
478
+ // when we generate an upload token for a newly created item with a
479
+ // previously uploaded bitstream, and we are passing the checksum,
480
+ // we expect that the item will create a new revision for the bitstream,
481
+ // but pass back an empty upload token, since we have the bitstream content
482
+ // already and do not need to actually upload it
483
+ $ this ->resetAll ();
484
+ $ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
485
+ $ this ->params ['method ' ] = 'midas.upload.generatetoken ' ;
486
+ $ this ->params ['filename ' ] = 'test3.txt ' ;
487
+ $ this ->params ['checksum ' ] = $ md5 ;
488
+ $ this ->params ['itemid ' ] = $ generatedItemId ;
489
+ $ resp = $ this ->_callJsonApi ();
490
+ $ this ->_assertStatusOk ($ resp );
491
+ $ token = $ resp ->data ->token ;
492
+ $ this ->assertEquals ($ token , '' , 'Redundant content upload did not return a blank token ' );
493
+
494
+ $ itemDao = $ this ->Item ->load ($ generatedItemId );
495
+ $ revisions = $ itemDao ->getRevisions ();
496
+ $ this ->assertEquals (count ($ revisions ), 1 , 'Wrong number of revisions in the item ' );
497
+ $ bitstreams = $ revisions [0 ]->getBitstreams ();
498
+ $ this ->assertEquals (count ($ bitstreams ), 1 , 'Wrong number of bitstreams in the revision ' );
499
+ $ this ->assertEquals ($ bitstreams [0 ]->name , 'test3.txt ' );
500
+ $ this ->assertEquals ($ bitstreams [0 ]->sizebytes , $ length );
501
+ $ this ->assertEquals ($ bitstreams [0 ]->checksum , $ md5 );
502
+
503
+ // delete the newly created item
504
+ $ this ->Item ->delete ($ itemDao );
505
+
506
+ // create a new item in the user root folder
507
+ // use folderid 1000
508
+ // need a new item because we are testing functionality involved with
509
+ // a new item
510
+ $ this ->resetAll ();
511
+ $ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
512
+ $ this ->params ['method ' ] = 'midas.item.create ' ;
513
+ $ this ->params ['name ' ] = 'created_item2 ' ;
514
+ $ this ->params ['parentid ' ] = '1000 ' ;
515
+ $ resp = $ this ->_callJsonApi ();
516
+ $ this ->_assertStatusOk ($ resp );
517
+ $ generatedItemId = $ resp ->data ->item_id ;
518
+ $ itemDao = $ this ->Item ->load ($ generatedItemId );
519
+ $ revisions = $ itemDao ->getRevisions ();
520
+ $ this ->assertEquals (count ($ revisions ), 0 , 'Wrong number of revisions in the new item ' );
521
+
522
+ // generate upload token
523
+ // when we generate an upload token for a newly created item without any
524
+ // previously uploaded bitstream (and we don't pass a checksum),
525
+ // we expect that the item will not create any new revision for the bitstream,
526
+ // and that a non-blank upload token will be returned.
527
+ $ this ->resetAll ();
528
+ $ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
529
+ $ this ->params ['method ' ] = 'midas.upload.generatetoken ' ;
530
+ $ this ->params ['filename ' ] = 'test.txt ' ;
531
+ $ this ->params ['itemid ' ] = $ generatedItemId ;
532
+ $ resp = $ this ->_callJsonApi ();
533
+ $ token = $ resp ->data ->token ;
534
+
535
+ // verify the token
536
+ $ this ->assertTrue (
537
+ preg_match ('/^ ' .$ usersFile [0 ]->getKey ().'\/ ' .$ generatedItemId .'\/.+\..+$/ ' , $ token ) > 0 ,
538
+ 'Upload token ( ' .$ token .') is not of the form <userid>/<itemid>/*.* ' );
539
+ $ this ->assertTrue (file_exists (BASE_PATH .'/tmp/misc/ ' .$ token ),
540
+ 'Token placeholder file ' .$ token .' was not created in the temp dir ' );
541
+
542
+ $ itemDao = $ this ->Item ->load ($ generatedItemId );
543
+ $ revisions = $ itemDao ->getRevisions ();
544
+ $ this ->assertEquals (count ($ revisions ), 0 , 'Wrong number of revisions in the new item ' );
545
+
546
+ // upload to revision 1, this should be an error since there is no such rev
547
+ $ this ->resetAll ();
548
+ $ this ->params ['method ' ] = 'midas.upload.perform ' ;
549
+ $ this ->params ['uploadtoken ' ] = $ token ;
550
+ $ this ->params ['filename ' ] = 'test.txt ' ;
551
+ $ this ->params ['length ' ] = $ length ;
552
+ $ this ->params ['itemid ' ] = $ generatedItemId ;
553
+ $ this ->params ['revision ' ] = '1 ' ; //upload into head revision
554
+ $ this ->params ['testingmode ' ] = 'true ' ;
555
+ $ resp = $ this ->_callJsonApi ();
556
+ $ this ->assertNotEquals ($ resp , false );
557
+ $ this ->assertEquals ($ resp ->stat , 'fail ' );
558
+ $ this ->assertEquals ($ resp ->code , -150 );
559
+
560
+ // upload to head revision, this should create a revision 1 and
561
+ // put the bitstream there
562
+ $ this ->resetAll ();
563
+ $ this ->params ['method ' ] = 'midas.upload.perform ' ;
564
+ $ this ->params ['uploadtoken ' ] = $ token ;
565
+ $ this ->params ['filename ' ] = 'test.txt ' ;
566
+ $ this ->params ['length ' ] = $ length ;
567
+ $ this ->params ['itemid ' ] = $ generatedItemId ;
568
+ $ this ->params ['revision ' ] = 'head ' ; //upload into head revision
569
+ $ this ->params ['testingmode ' ] = 'true ' ;
570
+ $ this ->params ['DBG ' ] = 'true ' ;
571
+ $ resp = $ this ->_callJsonApi ();
572
+
573
+ // ensure that there is 1 revision with 1 bitstream
574
+ $ revisions = $ itemDao ->getRevisions ();
575
+ $ this ->assertEquals (count ($ revisions ), 1 , 'Wrong number of revisions in the new item ' );
576
+ $ bitstreams = $ revisions [0 ]->getBitstreams ();
577
+ $ this ->assertEquals (count ($ bitstreams ), 1 , 'Wrong number of bitstreams in the revision ' );
578
+ $ this ->assertEquals ($ bitstreams [0 ]->name , 'test.txt ' );
579
+ $ this ->assertEquals ($ bitstreams [0 ]->sizebytes , $ length );
580
+ $ this ->assertEquals ($ bitstreams [0 ]->checksum , $ md5 );
581
+
582
+ unlink (BASE_PATH .'/tmp/misc/test.txt ' );
434
583
}
435
584
436
585
/** test the bitstream count functionality on all resource types */
@@ -486,4 +635,71 @@ public function testBitstreamCount()
486
635
$ this ->assertEquals ($ resp ->data ->count , 1 );
487
636
$ this ->assertEquals ($ resp ->data ->size , $ expectedSize );
488
637
}
638
+
639
+
640
+
641
+
642
+ /** test item creation and deletion */
643
+ public function testCreateitemDeleteitem ()
644
+ {
645
+ // create an item with only required options
646
+ $ this ->resetAll ();
647
+ $ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
648
+ $ this ->params ['method ' ] = 'midas.item.create ' ;
649
+ $ this ->params ['name ' ] = 'created_item_1 ' ;
650
+ $ this ->params ['parentid ' ] = '1000 ' ;
651
+ $ resp = $ this ->_callJsonApi ();
652
+ $ this ->_assertStatusOk ($ resp );
653
+ $ generatedItemId = $ resp ->data ->item_id ;
654
+ $ itemDao = $ this ->Item ->load ($ generatedItemId );
655
+ $ this ->assertEquals ($ itemDao ->getName (), $ this ->params ['name ' ], 'Item name is not set correctly ' );
656
+ $ this ->assertEquals ($ itemDao ->getDescription (), '' , 'Item name is not set correctly ' );
657
+
658
+ // delete it
659
+ $ this ->resetAll ();
660
+ $ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
661
+ $ this ->params ['method ' ] = 'midas.item.delete ' ;
662
+ $ this ->params ['id ' ] = $ generatedItemId ;
663
+ $ resp = $ this ->_callJsonApi ();
664
+ $ this ->assertNotEquals ($ resp , false );
665
+ $ this ->assertEquals ($ resp ->message , '' );
666
+ $ this ->assertEquals ($ resp ->stat , 'ok ' );
667
+ $ this ->assertEquals ($ resp ->code , 0 );
668
+ $ itemDao = $ this ->Item ->load ($ generatedItemId );
669
+ $ this ->assertFalse ($ itemDao , 'Item should have been deleted, but was not. ' );
670
+
671
+ // create an item with required options, plus description and uuid
672
+ $ this ->resetAll ();
673
+ $ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
674
+ $ this ->params ['method ' ] = 'midas.item.create ' ;
675
+ $ this ->params ['name ' ] = 'created_item_2 ' ;
676
+ $ this ->params ['description ' ] = 'my item description ' ;
677
+ $ this ->params ['uuid ' ] = uniqid () . md5 (mt_rand ());
678
+ $ this ->params ['parentid ' ] = '1000 ' ;
679
+ $ resp = $ this ->_callJsonApi ();
680
+ $ this ->_assertStatusOk ($ resp );
681
+ $ generatedItemId = $ resp ->data ->item_id ;
682
+ $ itemDao = $ this ->Item ->load ($ generatedItemId );
683
+ $ this ->assertEquals ($ itemDao ->getName (), $ this ->params ['name ' ], 'Item name is not set correctly ' );
684
+ $ this ->assertEquals ($ itemDao ->getUuid (), $ this ->params ['uuid ' ], 'Item uuid is not set correctly ' );
685
+ $ this ->assertEquals ($ itemDao ->getDescription (), $ this ->params ['description ' ], 'Item description is not set correctly ' );
686
+
687
+ // delete the second one
688
+ $ this ->resetAll ();
689
+ $ this ->params ['token ' ] = $ this ->_loginAsNormalUser ();
690
+ $ this ->params ['method ' ] = 'midas.item.delete ' ;
691
+ $ this ->params ['id ' ] = $ generatedItemId ;
692
+ $ resp = $ this ->_callJsonApi ();
693
+ $ this ->assertNotEquals ($ resp , false );
694
+ $ this ->assertEquals ($ resp ->message , '' );
695
+ $ this ->assertEquals ($ resp ->stat , 'ok ' );
696
+ $ this ->assertEquals ($ resp ->code , 0 );
697
+ $ itemDao = $ this ->Item ->load ($ generatedItemId );
698
+ $ this ->assertFalse ($ itemDao , 'Item should have been deleted, but was not. ' );
699
+ }
700
+
701
+
702
+
703
+
704
+
489
705
}
0 commit comments