From add980d36fac93a01b00041f2c4ebe4b4d2f51aa Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sat, 3 Oct 2015 15:52:37 +0200 Subject: [PATCH] adding UI tests for database configuration, fixed an issue with comment table creation --- lib/zerobin/abstract.php | 2 +- lib/zerobin/db.php | 4 ++-- tst/bootstrap.php | 15 +++++++++++++- tst/zerobin.php | 28 +++++++++++++++++--------- tst/zerobinWithDb.php | 43 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 tst/zerobinWithDb.php diff --git a/lib/zerobin/abstract.php b/lib/zerobin/abstract.php index 89e1150..ad9cfe5 100644 --- a/lib/zerobin/abstract.php +++ b/lib/zerobin/abstract.php @@ -22,7 +22,7 @@ abstract class zerobin_abstract * * @access protected * @static - * @var zerobin + * @var zerobin_abstract */ protected static $_instance = null; diff --git a/lib/zerobin/db.php b/lib/zerobin/db.php index f920310..89bedfd 100644 --- a/lib/zerobin/db.php +++ b/lib/zerobin/db.php @@ -166,7 +166,7 @@ class zerobin_db extends zerobin_abstract } // create comment table if needed - if (!array_key_exists(self::$_prefix . 'comment', $tables)) + if (!in_array(self::$_prefix . 'comment', $tables)) { self::$_db->exec( 'CREATE TABLE ' . self::$_prefix . 'comment ( ' . @@ -182,7 +182,7 @@ class zerobin_db extends zerobin_abstract } } - return parent::$_instance; + return self::$_instance; } /** diff --git a/tst/bootstrap.php b/tst/bootstrap.php index 947bc37..ba73cee 100644 --- a/tst/bootstrap.php +++ b/tst/bootstrap.php @@ -27,9 +27,9 @@ class helper 'attachment' => '{"iv":"Pd4pOKWkmDTT9uPwVwd5Ag","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"6nOCU3peNDclDDpFtJEBKA"}', 'attachmentname' => '{"iv":"76MkAtOGC4oFogX/aSMxRA","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"ZIUhFTliVz4","ct":"b6Ae/U1xJdsX/+lATud4sQ"}', 'meta' => array( + 'formatter' => 'plaintext', 'postdate' => 1344803344, 'opendiscussion' => true, - 'formatter' => 'plaintext', ), ); @@ -110,6 +110,19 @@ class helper return $example; } + /** + * get example comment + * + * @return array + */ + public static function getCommentPost($meta = array()) + { + $example = self::getComment($meta); + $example['nickname'] = $example['meta']['nickname']; + unset($example['meta']['nickname']); + return $example; + } + /** * delete directory and all its contents recursively * diff --git a/tst/zerobin.php b/tst/zerobin.php index 95f420e..ac1fb1b 100644 --- a/tst/zerobin.php +++ b/tst/zerobin.php @@ -1,7 +1,7 @@ reset(); + $options = parse_ini_file(CONF, true); + $options['traffic']['limit'] = 0; + helper::confBackup(); + helper::createIniFile(CONF, $options); $_POST = helper::getPaste(); $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; $_SERVER['REQUEST_METHOD'] = 'POST'; @@ -139,6 +144,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; $_SERVER['REQUEST_METHOD'] = 'POST'; $_SERVER['REMOTE_ADDR'] = '::1'; + trafficlimiter::canPass(); ob_start(); new zerobin; $content = ob_get_contents(); @@ -390,7 +396,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $options['traffic']['limit'] = 0; helper::confBackup(); helper::createIniFile(CONF, $options); - $_POST = helper::getComment(); + $_POST = helper::getCommentPost(); $_POST['pasteid'] = helper::getPasteId(); $_POST['parentid'] = helper::getPasteId(); $_POST['nickname'] = 'foo'; @@ -416,7 +422,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $options['traffic']['limit'] = 0; helper::confBackup(); helper::createIniFile(CONF, $options); - $_POST = helper::getComment(); + $_POST = helper::getCommentPost(); $_POST['pasteid'] = helper::getPasteId(); $_POST['parentid'] = helper::getPasteId(); $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; @@ -441,7 +447,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $options['traffic']['limit'] = 0; helper::confBackup(); helper::createIniFile(CONF, $options); - $_POST = helper::getComment(); + $_POST = helper::getCommentPost(); $_POST['pasteid'] = helper::getPasteId(); $_POST['parentid'] = 'foo'; $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; @@ -466,7 +472,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $options['traffic']['limit'] = 0; helper::confBackup(); helper::createIniFile(CONF, $options); - $_POST = helper::getComment(); + $_POST = helper::getCommentPost(); $_POST['pasteid'] = helper::getPasteId(); $_POST['parentid'] = helper::getPasteId(); $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; @@ -492,7 +498,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $options['traffic']['limit'] = 0; helper::confBackup(); helper::createIniFile(CONF, $options); - $_POST = helper::getComment(); + $_POST = helper::getCommentPost(); $_POST['pasteid'] = helper::getPasteId(); $_POST['parentid'] = helper::getPasteId(); $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; @@ -519,7 +525,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase $this->_model->create(helper::getPasteId(), helper::getPaste()); $this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()); $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists before posting data'); - $_POST = helper::getComment(); + $_POST = helper::getCommentPost(); $_POST['pasteid'] = helper::getPasteId(); $_POST['parentid'] = helper::getPasteId(); $_SERVER['HTTP_X_REQUESTED_WITH'] = 'JSONHttpRequest'; @@ -676,8 +682,12 @@ class zerobinTest extends PHPUnit_Framework_TestCase public function testReadOldSyntax() { $this->reset(); - $oldPaste = helper::getPaste(array('syntaxcoloring' => true)); - unset($oldPaste['meta']['formatter']); + $oldPaste = helper::getPaste(); + $oldPaste['meta'] = array( + 'syntaxcoloring' => true, + 'postdate' => $oldPaste['meta']['postdate'], + 'opendiscussion' => $oldPaste['meta']['opendiscussion'], + ); $this->_model->create(helper::getPasteId(), $oldPaste); $_SERVER['QUERY_STRING'] = helper::getPasteId(); ob_start(); diff --git a/tst/zerobinWithDb.php b/tst/zerobinWithDb.php new file mode 100644 index 0000000..18c0b88 --- /dev/null +++ b/tst/zerobinWithDb.php @@ -0,0 +1,43 @@ + 'sqlite:../data/tst.sq3', + 'usr' => null, + 'pwd' => null, + 'opt' => array( + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_PERSISTENT => true + ), + ); + + public function setUp() + { + /* Setup Routine */ + $this->_model = zerobin_db::getInstance($this->_options); + serversalt::setPath(PATH . 'data'); + $this->reset(); + } + + public function tearDown() + { + /* Tear Down Routine */ + parent::tearDown(); + @unlink('../data/tst.sq3'); + } + + public function reset() + { + parent::reset(); + // but then inject a db config + $options = parse_ini_file(CONF, true); + $options['model'] = array( + 'class' => 'zerobin_db', + ); + $options['model_options'] = $this->_options; + helper::confBackup(); + helper::createIniFile(CONF, $options); + } +} \ No newline at end of file