Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 7ead511

Browse files
author
Michael Grauer
committed
BUG: refs #0349. Item create description is optional, fixed null revision ref.
There was an additional instance of the revision variable being set to null that was fixed, and now the item description being not included won't break the item create api call. Extensive testing of all fixes in this topic branch are included in the changes to the ApiCallMethodsTest, in fact, the extensive testing is what led to many of these small bugs being found.
1 parent 954c61f commit 7ead511

File tree

2 files changed

+222
-7
lines changed

2 files changed

+222
-7
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function uploadGeneratetoken($args)
232232
$revision->setUser_id($userDao->getKey());
233233
$revision->setDate(date('c'));
234234
$revision->setLicense(null);
235-
$revision = $itemModel->addRevision($item, $revision);
235+
$itemModel->addRevision($item, $revision);
236236
}
237237

238238
$siblings = $revision->getBitstreams();
@@ -859,11 +859,10 @@ function itemCreate($args)
859859
{
860860
throw new Exception('Cannot create item anonymously', MIDAS_INVALID_POLICY);
861861
}
862-
863862
$modelLoader = new MIDAS_ModelLoader();
864863
$itemModel = $modelLoader->loadModel('Item');
865864
$name = $args['name'];
866-
$description = $args['description'];
865+
$description = isset($args['description']) ? $args['description'] : '';
867866

868867
$uuid = isset($args['uuid']) ? $args['uuid'] : '';
869868
$record = false;

modules/api/tests/controllers/ApiCallMethodsTest.php

Lines changed: 220 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public function testUserFolders()
111111
$this->_assertStatusOk($resp);
112112
$this->assertEquals(count($resp->data), 2);
113113

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
114116
foreach($resp->data as $folder)
115117
{
116118
$this->assertEquals($folder->_model, 'Folder');
@@ -331,6 +333,7 @@ public function testBitstreamUpload()
331333
$usersFile = $this->loadData('User', 'default');
332334
$itemsFile = $this->loadData('Item', 'default');
333335

336+
// generate an upload token
334337
$this->params['token'] = $this->_loginAsNormalUser();
335338
$this->params['method'] = 'midas.upload.generatetoken';
336339
$this->params['filename'] = 'test.txt';
@@ -346,6 +349,8 @@ public function testBitstreamUpload()
346349
$this->resetAll();
347350
$usersFile = $this->loadData('User', 'default');
348351
$itemsFile = $this->loadData('Item', 'default');
352+
353+
// generate the test file
349354
$string = '';
350355
$length = 100;
351356
for($i = 0; $i < $length; $i++)
@@ -364,6 +369,7 @@ public function testBitstreamUpload()
364369
unlink($assetstoreFile);
365370
}
366371

372+
// generate another upload token
367373
$this->params['token'] = $this->_loginAsNormalUser();
368374
$this->params['method'] = 'midas.upload.generatetoken';
369375
$this->params['filename'] = 'test.txt';
@@ -372,14 +378,15 @@ public function testBitstreamUpload()
372378
$this->params['itemid'] = $itemsFile[1]->getKey();
373379
$resp = $this->_callJsonApi();
374380
$this->_assertStatusOk($resp);
375-
381+
// verify the upload token
376382
$token = $resp->data->token;
377383
$this->assertTrue(
378384
preg_match('/^'.$usersFile[0]->getKey().'\/'.$itemsFile[1]->getKey().'\/.+\..+$/', $token) > 0,
379385
'Upload token ('.$token.') is not of the form <userid>/<itemid>/*.*');
380386
$this->assertTrue(file_exists(BASE_PATH.'/tmp/misc/'.$token),
381387
'Token placeholder file '.$token.' was not created in the temp dir');
382388

389+
// attempt the upload
383390
$this->resetAll();
384391
$this->params['method'] = 'midas.upload.perform';
385392
$this->params['uploadtoken'] = $token;
@@ -391,8 +398,6 @@ public function testBitstreamUpload()
391398
$resp = $this->_callJsonApi();
392399
$this->_assertStatusOk($resp);
393400

394-
unlink(BASE_PATH.'/tmp/misc/test.txt');
395-
396401
$this->assertTrue(file_exists($assetstoreFile), 'File was not written to the assetstore');
397402
$this->assertEquals(filesize($assetstoreFile), $length, 'Assetstore file is the wrong length');
398403
$this->assertEquals(md5_file($assetstoreFile), $md5, 'Assetstore file had incorrect checksum');
@@ -407,7 +412,28 @@ public function testBitstreamUpload()
407412
$this->assertEquals($bitstreams[0]->sizebytes, $length);
408413
$this->assertEquals($bitstreams[0]->checksum, $md5);
409414

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+
410435
// Check that a redundant upload yields a blank upload token and a new reference
436+
// redundant upload meaning uploading a checksum that already exists
411437
$this->resetAll();
412438
$this->params['token'] = $this->_loginAsNormalUser();
413439
$this->params['method'] = 'midas.upload.generatetoken';
@@ -416,10 +442,11 @@ public function testBitstreamUpload()
416442
$this->params['itemid'] = $itemsFile[1]->getKey();
417443
$resp = $this->_callJsonApi();
418444
$this->_assertStatusOk($resp);
419-
420445
$token = $resp->data->token;
421446
$this->assertEquals($token, '', 'Redundant content upload did not return a blank token');
422447

448+
// check that the new bitstream has been created
449+
// in the generatetoken step
423450
$itemDao = $this->Item->load($itemsFile[1]->getKey());
424451
$revisions = $itemDao->getRevisions();
425452
$this->assertEquals(count($revisions), 1, 'Wrong number of revisions in the item');
@@ -431,6 +458,128 @@ public function testBitstreamUpload()
431458
$this->assertEquals($bitstreams[1]->name, 'test2.txt');
432459
$this->assertEquals($bitstreams[1]->sizebytes, $length);
433460
$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');
434583
}
435584

436585
/** test the bitstream count functionality on all resource types */
@@ -486,4 +635,71 @@ public function testBitstreamCount()
486635
$this->assertEquals($resp->data->count, 1);
487636
$this->assertEquals($resp->data->size, $expectedSize);
488637
}
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+
489705
}

0 commit comments

Comments
 (0)