improving unit tests, fixing regression in DB model

This commit is contained in:
El RIDO 2015-09-27 14:36:20 +02:00
parent 694138c5d4
commit ce3f10f143
3 changed files with 77 additions and 9 deletions

View file

@ -139,7 +139,7 @@ class zerobin_db extends zerobin_abstract
$tables = $statement->fetchAll(PDO::FETCH_COLUMN, 0); $tables = $statement->fetchAll(PDO::FETCH_COLUMN, 0);
// create paste table if needed // create paste table if needed
if (!array_key_exists(self::$_prefix . 'paste', $tables)) if (!in_array(self::$_prefix . 'paste', $tables))
{ {
self::$_db->exec( self::$_db->exec(
'CREATE TABLE ' . self::$_prefix . 'paste ( ' . 'CREATE TABLE ' . self::$_prefix . 'paste ( ' .

View file

@ -146,6 +146,7 @@ class modelTest extends PHPUnit_Framework_TestCase
$this->_model->getPaste(helper::getPasteId())->delete(); $this->_model->getPaste(helper::getPasteId())->delete();
$paste = $this->_model->getPaste(); $paste = $this->_model->getPaste();
$paste->setData($pasteData['data']); $paste->setData($pasteData['data']);
$paste->setBurnafterreading('0');
$paste->setOpendiscussion(); $paste->setOpendiscussion();
$paste->store(); $paste->store();
@ -166,4 +167,45 @@ class modelTest extends PHPUnit_Framework_TestCase
$this->assertFalse(model_paste::isValidId('foo'), 'invalid hex values'); $this->assertFalse(model_paste::isValidId('foo'), 'invalid hex values');
$this->assertFalse(model_paste::isValidId('../bar/baz'), 'path attack'); $this->assertFalse(model_paste::isValidId('../bar/baz'), 'path attack');
} }
/**
* @expectedException Exception
* @expectedExceptionCode 62
*/
public function testInvalidComment()
{
$paste = $this->_model->getPaste();
$comment = $paste->getComment(helper::getPasteId());
}
public function testExpiration()
{
$pasteData = helper::getPaste();
$this->_model->getPaste(helper::getPasteId())->delete();
$paste = $this->_model->getPaste(helper::getPasteId());
$this->assertFalse($paste->exists(), 'paste does not yet exist');
$paste = $this->_model->getPaste();
$paste->setData($pasteData['data']);
$paste->setExpiration('5min'); // = 300 seconds
$paste->store();
$paste = $paste->get();
$this->assertEquals(300, $paste->meta->remaining_time, 'remaining time is set correctly');
}
/**
* @expectedException Exception
* @expectedExceptionCode 64
*/
public function testCommentDeletion()
{
$pasteData = helper::getPaste();
$this->_model->getPaste(helper::getPasteId())->delete();
$paste = $this->_model->getPaste();
$paste->setData($pasteData['data']);
$paste->store();
$paste->getComment(helper::getPasteId())->delete();
}
} }

View file

@ -3,17 +3,17 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase
{ {
private $_model; private $_model;
public function setUp() private $_options = array(
{
/* Setup Routine */
$this->_model = zerobin_db::getInstance(
array(
'dsn' => 'sqlite::memory:', 'dsn' => 'sqlite::memory:',
'usr' => null, 'usr' => null,
'pwd' => null, 'pwd' => null,
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION), 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
)
); );
public function setUp()
{
/* Setup Routine */
$this->_model = zerobin_db::getInstance($this->_options);
} }
public function testDatabaseBasedDataStoreWorks() public function testDatabaseBasedDataStoreWorks()
@ -51,6 +51,7 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase
{ {
$this->_model->delete(helper::getPasteId()); $this->_model->delete(helper::getPasteId());
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344)); $original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
$paste['meta']['burnafterreading'] = $original['meta']['burnafterreading'] = true;
$paste['meta']['attachment'] = $paste['attachment']; $paste['meta']['attachment'] = $paste['attachment'];
$paste['meta']['attachmentname'] = $paste['attachmentname']; $paste['meta']['attachmentname'] = $paste['attachmentname'];
unset($paste['attachment'], $paste['attachmentname']); unset($paste['attachment'], $paste['attachmentname']);
@ -137,4 +138,29 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase
'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null 'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
)); ));
} }
public function testTableUpgrade()
{
$path = PATH . 'data/db-test.sq3';
@unlink($path);
$this->_options['dsn'] = 'sqlite:' . $path;
$this->_options['tbl'] = 'foo_';
$db = new PDO(
$this->_options['dsn'],
$this->_options['usr'],
$this->_options['pwd'],
$this->_options['opt']
);
$db->exec(
'CREATE TABLE foo_paste ( ' .
'dataid CHAR(16), ' .
'data TEXT, ' .
'postdate INT, ' .
'expiredate INT, ' .
'opendiscussion INT, ' .
'burnafterreading INT );'
);
zerobin_db::getInstance($this->_options);
@unlink($path);
}
} }