From ce3f10f143be07220105413366f97373ade1fa7b Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 27 Sep 2015 14:36:20 +0200 Subject: [PATCH] improving unit tests, fixing regression in DB model --- lib/zerobin/db.php | 2 +- tst/model.php | 42 ++++++++++++++++++++++++++++++++++++++++++ tst/zerobin/db.php | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 77 insertions(+), 9 deletions(-) diff --git a/lib/zerobin/db.php b/lib/zerobin/db.php index 12486757..f9203100 100644 --- a/lib/zerobin/db.php +++ b/lib/zerobin/db.php @@ -139,7 +139,7 @@ class zerobin_db extends zerobin_abstract $tables = $statement->fetchAll(PDO::FETCH_COLUMN, 0); // create paste table if needed - if (!array_key_exists(self::$_prefix . 'paste', $tables)) + if (!in_array(self::$_prefix . 'paste', $tables)) { self::$_db->exec( 'CREATE TABLE ' . self::$_prefix . 'paste ( ' . diff --git a/tst/model.php b/tst/model.php index aefa8f8c..c5c21c18 100644 --- a/tst/model.php +++ b/tst/model.php @@ -146,6 +146,7 @@ class modelTest extends PHPUnit_Framework_TestCase $this->_model->getPaste(helper::getPasteId())->delete(); $paste = $this->_model->getPaste(); $paste->setData($pasteData['data']); + $paste->setBurnafterreading('0'); $paste->setOpendiscussion(); $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('../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(); + } } \ No newline at end of file diff --git a/tst/zerobin/db.php b/tst/zerobin/db.php index 0c6b0165..3697b503 100644 --- a/tst/zerobin/db.php +++ b/tst/zerobin/db.php @@ -3,17 +3,17 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase { private $_model; + private $_options = array( + 'dsn' => 'sqlite::memory:', + 'usr' => null, + 'pwd' => null, + 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION), + ); + public function setUp() { /* Setup Routine */ - $this->_model = zerobin_db::getInstance( - array( - 'dsn' => 'sqlite::memory:', - 'usr' => null, - 'pwd' => null, - 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION), - ) - ); + $this->_model = zerobin_db::getInstance($this->_options); } public function testDatabaseBasedDataStoreWorks() @@ -51,6 +51,7 @@ class zerobin_dbTest extends PHPUnit_Framework_TestCase { $this->_model->delete(helper::getPasteId()); $original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344)); + $paste['meta']['burnafterreading'] = $original['meta']['burnafterreading'] = true; $paste['meta']['attachment'] = $paste['attachment']; $paste['meta']['attachmentname'] = $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 )); } + + 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); + } }