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

Commit

Permalink
BUG: refs #0281: Enforced unique Assetstore path and name in model, p…
Browse files Browse the repository at this point in the history
…lus tests.
  • Loading branch information
Michael Grauer committed Oct 11, 2011
1 parent ccf426a commit 88dd75e
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 0 deletions.
74 changes: 74 additions & 0 deletions core/models/base/AssetstoreModelBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,83 @@ public function __construct()
/** Abstract functions */
abstract function getAll();

/**
* helper function to detect a collision between a row with the
* passed in keyVal (which should be null for a new row), and any
* existing rows with the column $var having the value $value.
* @param type $var column name
* @param type $value column value
* @param type $keyVal key value for passed in row, or null if a new row
* @return type
*/
protected function detectCollision($var, $value, $keyVal)
{
// detect whether the current Assetstore will collide for the
// variable value with an existing row
$found = $this->findBy($var, $value);
if(isset($found) && sizeof($found) > 0)
{
foreach($found as $existingAssetstoreDao)
{
// if the current dao does not already exist
// then it collides with an existing row
if(empty($keyVal))
{
return true;
}
else
{
// the current dao does already exist
if($existingAssetstoreDao->getKey() !== $keyVal)
{
// if the current dao does exist and it is different than
// the existingAssestoreDao then
// it is a collision with an existing other row
return true;
}
else
{
// if the current dao does exist and it shares the same key
// it is updating itself, so there is no collision
return false;
}
}
}
}
else
{
// no matching rows found, no collision
return false;
}
}



/** save an assetsore*/
public function save($dao)
{
$key = $this->_key;
// get the keyValue of the dao that is to be saved
if(isset($dao->$key))
{
// the dao has a keyValue
$keyVal = $dao->$key;
}
else
{
// the dao is for a new row, and doesn't yet have a keyValue
$keyVal = null;
}
$path = $dao->getPath();
if(empty($path) || $this->detectCollision('path', $path, $keyVal))
{
throw new Zend_Exception('Assetstore paths must be unique');
}
$name = $dao->getName();
if(empty($name) || $this->detectCollision('name', $name, $keyVal))
{
throw new Zend_Exception('Assetstore names must be unique');
}
parent::save($dao);
}

Expand Down
178 changes: 178 additions & 0 deletions core/tests/models/base/AssetstoreModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
69328 Lyon, FRANCE.
See Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
/** AssetstoreModelTest*/
class AssetstoreModelTest extends DatabaseTestCase
{
/** init test*/
public function setUp()
{
$this->setupDatabase(array('default'));
$this->_models = array('Assetstore');
$this->_daos = array();
parent::setUp();
}



/**
* helper function, will save an Assetstore, using either the passed in
* assetstoreDao or creating a new one, will set the three parameter
* values before saving.
* @param type $name
* @param type $path
* @param type $type
* @param AssetstoreDao $assetstoreDao
* @return AssetstoreDao
*/
protected function validSaveTestcase($name, $path, $type, $assetstoreDao = null)
{
if(empty($assetstoreDao))
{
$assetstoreDao = new AssetstoreDao();
}
$assetstoreDao->setName($name);
$assetstoreDao->setPath($path);
$assetstoreDao->setType($type);
$this->Assetstore->save($assetstoreDao);
return $assetstoreDao;
}

/**
* helper function, attempts to save an Assetstore, using either the passed in
* assetstoreDao or creating a new one, will set the three parameter
* values before saving, expects that the save will fail, and asserts
* that an exception has been thrown by the Assetstore Model.
* @param type $name
* @param type $path
* @param type $type
* @param AssetstoreDao $assetstoreDao
*/
protected function invalidSaveTestcase($name, $path, $type, $assetstoreDao = null)
{
if(empty($assetstoreDao))
{
$assetstoreDao = new AssetstoreDao();
}
$assetstoreDao->setName($name);
$assetstoreDao->setPath($path);
$assetstoreDao->setType($type);
try
{
$this->Assetstore->save($assetstoreDao);
$this->fail('Expected an exception saving assetstoreDao, but did not get one.');
}
catch(Zend_Exception $ze)
{
// if we got here, this is the correct behavior
$this->assertTrue(true);
}
}

/** test the save method*/
public function testSave()
{

// expect that there will be one assetstore to start out, Default
// this will probably need to be expanded once we start adding implementations
// for the other two types of assetstore
// for now, a local Default assetstore is expected
$all = $this->Assetstore->getAll();
$this->assertEquals(sizeof($all), 1);
$default = $all[0];
$this->assertEquals('Default', $default->getName());
// Correct Create Test


// create new ones with a different path from existing ones

$assetstoreDao1 = $this->validSaveTestcase('test_assetstore_1', '/testassetstore1/path', '0');
$assetstoreDao2 = $this->validSaveTestcase('test_assetstore_2', '/testassetstore2/path', '1');
$assetstoreDao3 = $this->validSaveTestcase('test_assetstore_3', '/testassetstore3/path', '2');

// make sure one got saved
$found = $this->Assetstore->findBy('name', 'test_assetstore_3');
$this->assertNotEmpty($found);
$savedDao = $found[0];
$this->assertTrue($this->Assetstore->compareDao($assetstoreDao3, $savedDao));

// Incorrect Create Tests

// create a new one with empty path, should fail
$this->invalidSaveTestcase('test_assetstore_4', '', '0');

// create a new one with same path as existing ones, should fail
$this->invalidSaveTestcase('test_assetstore_4', '/testassetstore1/path', '0');

// create a new one with empty name, should fail
$this->invalidSaveTestcase('', '/testassetstore4/path', '0');

// create a new one with same path as existing ones, should fail
$this->invalidSaveTestcase('test_assetstore_3', '/testassetstore4/path', '0');

// Incorrect Edit&Save Tests

// take existing, try to save with empty name
$savedName = $assetstoreDao1->getName();
$this->invalidSaveTestcase('', $assetstoreDao1->getPath(), $assetstoreDao1->getType(), $assetstoreDao1);
$assetstoreDao1->setName($savedName);

// take existing, try to save with empty path
$savedPath = $assetstoreDao1->getPath();
$this->invalidSaveTestcase($assetstoreDao1->getName(), '', $assetstoreDao1->getType(), $assetstoreDao1);
$assetstoreDao1->setPath($savedPath);

// take existing, try to save with a colliding name
$savedName = $assetstoreDao1->getName();
$this->invalidSaveTestcase($assetstoreDao2->getName(), $assetstoreDao1->getPath(), $assetstoreDao1->getType(), $assetstoreDao1);
$assetstoreDao1->setName($savedName);

// take existing, try to save with a colliding path
$savedPath = $assetstoreDao1->getPath();
$this->invalidSaveTestcase($assetstoreDao1->getName(), $assetstoreDao2->getPath(), $assetstoreDao1->getType(), $assetstoreDao1);
$assetstoreDao1->setPath($savedPath);

// Correct Edit&Save Tests

// take existing, try to save with a non-colliding name
$savedName = $assetstoreDao1->getName();
$newName = 'noncollidingname1';
$this->validSaveTestcase($newName, $assetstoreDao1->getPath(), $assetstoreDao1->getType(), $assetstoreDao1);

// check that the new value is saved
$found = $this->Assetstore->findBy('name', $newName);
$this->assertNotEmpty($found);
$foundDao = $found[0];
$this->assertTrue($this->Assetstore->compareDao($foundDao, $assetstoreDao1));
$this->assertEquals($foundDao->getName(), $newName);
$assetstoreDao1->setName($savedName);


// take existing, try to save with a non-colliding path
$savedPath = $assetstoreDao1->getPath();
$newPath = 'noncolliding/path';
$this->validSaveTestcase($assetstoreDao1->getName(), $newPath, $assetstoreDao1->getType(), $assetstoreDao1);

// check that the new value is saved
$found = $this->Assetstore->findBy('path', $newPath);
$this->assertNotEmpty($found);
$foundDao = $found[0];
$this->assertTrue($this->Assetstore->compareDao($foundDao, $assetstoreDao1));
$this->assertEquals($foundDao->getPath(), $newPath);
$assetstoreDao1->setPath($savedPath);

// delete the newly added ones to clean up
$this->Assetstore->delete($assetstoreDao1);
$this->Assetstore->delete($assetstoreDao2);
$this->Assetstore->delete($assetstoreDao3);
}

}
1 change: 1 addition & 0 deletions core/tests/models/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_midas_test( AssetstoreModel AssetstoreModelTest.php )
add_midas_test( FeedModel FeedModelTest.php )
add_midas_test( FeedpolicygroupModel FeedpolicygroupModelTest.php )
add_midas_test( FeedpolicyuserModel FeedpolicyuserModelTest.php )
Expand Down

0 comments on commit 88dd75e

Please sign in to comment.