adding tests and unifying paste creation output

This commit is contained in:
El RIDO 2015-09-03 22:55:36 +02:00
parent 2d79ba8243
commit 411419d597
3 changed files with 211 additions and 29 deletions

View File

@ -60,7 +60,7 @@ abstract class zerobin_abstract
* @access public * @access public
* @param string $pasteid * @param string $pasteid
* @param array $paste * @param array $paste
* @return int|false * @return bool
*/ */
abstract public function create($pasteid, $paste); abstract public function create($pasteid, $paste);
@ -99,7 +99,7 @@ abstract class zerobin_abstract
* @param string $parentid * @param string $parentid
* @param string $commentid * @param string $commentid
* @param array $comment * @param array $comment
* @return int|false * @return bool
*/ */
abstract public function createComment($pasteid, $parentid, $commentid, $comment); abstract public function createComment($pasteid, $parentid, $commentid, $comment);

View File

@ -55,14 +55,14 @@ class zerobin_data extends zerobin_abstract
* @access public * @access public
* @param string $pasteid * @param string $pasteid
* @param array $paste * @param array $paste
* @return int|false * @return bool
*/ */
public function create($pasteid, $paste) public function create($pasteid, $paste)
{ {
$storagedir = self::_dataid2path($pasteid); $storagedir = self::_dataid2path($pasteid);
if (is_file($storagedir . $pasteid)) return false; if (is_file($storagedir . $pasteid)) return false;
if (!is_dir($storagedir)) mkdir($storagedir, 0705, true); if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
return (bool) file_put_contents($storagedir . $pasteid, json_encode($paste)); return (bool) @file_put_contents($storagedir . $pasteid, json_encode($paste));
} }
/** /**
@ -105,7 +105,7 @@ class zerobin_data extends zerobin_abstract
$dir->close(); $dir->close();
// Delete the discussion directory. // Delete the discussion directory.
rmdir($discdir); @rmdir($discdir);
} }
} }
@ -129,7 +129,7 @@ class zerobin_data extends zerobin_abstract
* @param string $parentid * @param string $parentid
* @param string $commentid * @param string $commentid
* @param array $comment * @param array $comment
* @return int|false * @return bool
*/ */
public function createComment($pasteid, $parentid, $commentid, $comment) public function createComment($pasteid, $parentid, $commentid, $comment)
{ {
@ -137,7 +137,7 @@ class zerobin_data extends zerobin_abstract
$filename = $pasteid . '.' . $commentid . '.' . $parentid; $filename = $pasteid . '.' . $commentid . '.' . $parentid;
if (is_file($storagedir . $filename)) return false; if (is_file($storagedir . $filename)) return false;
if (!is_dir($storagedir)) mkdir($storagedir, 0705, true); if (!is_dir($storagedir)) mkdir($storagedir, 0705, true);
return file_put_contents($storagedir . $filename, json_encode($comment)); return (bool) @file_put_contents($storagedir . $filename, json_encode($comment));
} }
/** /**

View File

@ -22,6 +22,8 @@ class zerobinTest extends PHPUnit_Framework_TestCase
), ),
); );
private $_conf;
private $_model; private $_model;
public function setUp() public function setUp()
@ -29,6 +31,7 @@ class zerobinTest extends PHPUnit_Framework_TestCase
/* Setup Routine */ /* Setup Routine */
$this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data')); $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
serversalt::setPath(PATH . 'data'); serversalt::setPath(PATH . 'data');
$this->_conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
$this->reset(); $this->reset();
} }
@ -44,9 +47,8 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$_SERVER = array(); $_SERVER = array();
if ($this->_model->exists(self::$pasteid)) if ($this->_model->exists(self::$pasteid))
$this->_model->delete(self::$pasteid); $this->_model->delete(self::$pasteid);
$conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini'; if (is_file($this->_conf . '.bak'))
if (is_file($conf . '.bak')) rename($this->_conf . '.bak', $this->_conf);
rename($conf . '.bak', $conf);
} }
/** /**
@ -98,10 +100,9 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testConf() public function testConf()
{ {
$this->reset(); $this->reset();
$conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini'; if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
if (!is_file($conf . '.bak') && is_file($conf)) rename($this->_conf, $this->_conf . '.bak');
rename($conf, $conf . '.bak'); file_put_contents($this->_conf, '');
file_put_contents($conf, '');
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -113,12 +114,11 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testConfMissingExpireLabel() public function testConfMissingExpireLabel()
{ {
$this->reset(); $this->reset();
$conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini'; $options = parse_ini_file($this->_conf, true);
$options = parse_ini_file($conf, true);
$options['expire_options']['foobar123'] = 10; $options['expire_options']['foobar123'] = 10;
if (!is_file($conf . '.bak') && is_file($conf)) if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($conf, $conf . '.bak'); rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($conf, $options); helper::createIniFile($this->_conf, $options);
ini_set('magic_quotes_gpc', 1); ini_set('magic_quotes_gpc', 1);
ob_start(); ob_start();
new zerobin; new zerobin;
@ -149,13 +149,77 @@ class zerobinTest extends PHPUnit_Framework_TestCase
/** /**
* @runInSeparateProcess * @runInSeparateProcess
*/ */
public function testCreateValidExpire() public function testCreateInvalidTimelimit()
{ {
$this->reset(); $this->reset();
$_POST = self::$paste; $_POST = self::$paste;
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateInvalidSize()
{
$this->reset();
$options = parse_ini_file($this->_conf, true);
$options['main']['sizelimit'] = 10;
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$paste;
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->exists(self::$pasteid), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateDuplicateId()
{
$this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$this->_model->create(self::$pasteid, self::$paste);
$_POST = self::$paste;
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertTrue($this->_model->exists(self::$pasteid), 'paste exists after posting data');
}
/**
* @runInSeparateProcess
*/
public function testCreateValidExpire()
{
$this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$paste;
$_POST['expire'] = '5min'; $_POST['expire'] = '5min';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -175,10 +239,14 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidExpire() public function testCreateInvalidExpire()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$paste; $_POST = self::$paste;
$_POST['expire'] = 'foo'; $_POST['expire'] = 'foo';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -198,10 +266,14 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidBurn() public function testCreateInvalidBurn()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$paste; $_POST = self::$paste;
$_POST['burnafterreading'] = 'neither 1 nor 0'; $_POST['burnafterreading'] = 'neither 1 nor 0';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -216,10 +288,14 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidOpenDiscussion() public function testCreateInvalidOpenDiscussion()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$paste; $_POST = self::$paste;
$_POST['opendiscussion'] = 'neither 1 nor 0'; $_POST['opendiscussion'] = 'neither 1 nor 0';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -234,10 +310,14 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateValidNick() public function testCreateValidNick()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$paste; $_POST = self::$paste;
$_POST['nickname'] = self::$comment['meta']['nickname']; $_POST['nickname'] = self::$comment['meta']['nickname'];
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -257,10 +337,14 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateInvalidNick() public function testCreateInvalidNick()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$paste; $_POST = self::$paste;
$_POST['nickname'] = 'foo'; $_POST['nickname'] = 'foo';
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -275,12 +359,16 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateComment() public function testCreateComment()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$comment; $_POST = self::$comment;
$_POST['pasteid'] = self::$pasteid; $_POST['pasteid'] = self::$pasteid;
$_POST['parentid'] = self::$pasteid; $_POST['parentid'] = self::$pasteid;
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
$this->_model->create(self::$pasteid, self::$paste); $this->_model->create(self::$pasteid, self::$paste);
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -289,12 +377,41 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, $response['id']), 'paste exists after posting data'); $this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, $response['id']), 'paste exists after posting data');
} }
/**
* @runInSeparateProcess
*/
public function testCreateInvalidComment()
{
$this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$comment;
$_POST['pasteid'] = self::$pasteid;
$_POST['parentid'] = 'foo';
$_SERVER['REMOTE_ADDR'] = '::1';
$this->_model->create(self::$pasteid, self::$paste);
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
}
/** /**
* @runInSeparateProcess * @runInSeparateProcess
*/ */
public function testCreateCommentDiscussionDisabled() public function testCreateCommentDiscussionDisabled()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$comment; $_POST = self::$comment;
$_POST['pasteid'] = self::$pasteid; $_POST['pasteid'] = self::$pasteid;
$_POST['parentid'] = self::$pasteid; $_POST['parentid'] = self::$pasteid;
@ -302,7 +419,6 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$paste = self::$paste; $paste = self::$paste;
$paste['meta']['opendiscussion'] = false; $paste['meta']['opendiscussion'] = false;
$this->_model->create(self::$pasteid, $paste); $this->_model->create(self::$pasteid, $paste);
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -317,11 +433,15 @@ class zerobinTest extends PHPUnit_Framework_TestCase
public function testCreateCommentInvalidPaste() public function testCreateCommentInvalidPaste()
{ {
$this->reset(); $this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$_POST = self::$comment; $_POST = self::$comment;
$_POST['pasteid'] = self::$pasteid; $_POST['pasteid'] = self::$pasteid;
$_POST['parentid'] = self::$pasteid; $_POST['parentid'] = self::$pasteid;
$_SERVER['REMOTE_ADDR'] = '::1'; $_SERVER['REMOTE_ADDR'] = '::1';
sleep(11);
ob_start(); ob_start();
new zerobin; new zerobin;
$content = ob_get_contents(); $content = ob_get_contents();
@ -330,6 +450,30 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data'); $this->assertFalse($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
} }
/**
* @runInSeparateProcess
*/
public function testCreateDuplicateComment()
{
$this->reset();
$options = parse_ini_file($this->_conf, true);
$options['traffic']['limit'] = 0;
if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
rename($this->_conf, $this->_conf . '.bak');
helper::createIniFile($this->_conf, $options);
$this->_model->createComment(self::$pasteid, self::$pasteid, self::$commentid, self::$comment);
$_POST = self::$comment;
$_POST['pasteid'] = self::$pasteid;
$_POST['parentid'] = self::$pasteid;
$_SERVER['REMOTE_ADDR'] = '::1';
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
$this->assertTrue($this->_model->existsComment(self::$pasteid, self::$pasteid, self::$commentid), 'paste exists after posting data');
}
/** /**
* @runInSeparateProcess * @runInSeparateProcess
*/ */
@ -453,6 +597,20 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$this->assertEquals(array(self::$paste), $response['messages'], 'outputs data correctly'); $this->assertEquals(array(self::$paste), $response['messages'], 'outputs data correctly');
} }
/**
* @runInSeparateProcess
*/
public function testReadInvalidJson()
{
$this->reset();
$_SERVER['QUERY_STRING'] = self::$pasteid . '&json';
ob_start();
new zerobin;
$content = ob_get_contents();
$response = json_decode($content, true);
$this->assertEquals(1, $response['status'], 'outputs error status');
}
/** /**
* @runInSeparateProcess * @runInSeparateProcess
*/ */
@ -581,4 +739,28 @@ class zerobinTest extends PHPUnit_Framework_TestCase
$this->assertEquals(1, $response['status'], 'outputs status'); $this->assertEquals(1, $response['status'], 'outputs status');
$this->assertTrue($this->_model->exists(self::$pasteid), 'paste successfully deleted'); $this->assertTrue($this->_model->exists(self::$pasteid), 'paste successfully deleted');
} }
/**
* @runInSeparateProcess
*/
public function testDeleteExpired()
{
$this->reset();
$expiredPaste = self::$paste;
$expiredPaste['meta']['expire_date'] = $expiredPaste['meta']['postdate'];
$this->_model->create(self::$pasteid, $expiredPaste);
$_SERVER['QUERY_STRING'] = self::$pasteid;
ob_start();
new zerobin;
$content = ob_get_contents();
$this->assertTag(
array(
'id' => 'errormessage',
'content' => 'Paste does not exist'
),
$content,
'outputs error correctly'
);
$this->assertFalse($this->_model->exists(self::$pasteid), 'paste successfully deleted');
}
} }