2015-09-26 23:57:46 +08:00
|
|
|
<?php
|
2016-07-21 23:09:48 +08:00
|
|
|
|
2022-10-30 16:05:29 +08:00
|
|
|
use Identicon\Identicon;
|
2020-10-10 18:08:58 +08:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-08-09 17:54:42 +08:00
|
|
|
use PrivateBin\Configuration;
|
|
|
|
use PrivateBin\Data\Database;
|
|
|
|
use PrivateBin\Model;
|
2019-05-12 04:18:35 +08:00
|
|
|
use PrivateBin\Model\Comment;
|
2016-08-09 17:54:42 +08:00
|
|
|
use PrivateBin\Model\Paste;
|
2016-08-09 18:21:32 +08:00
|
|
|
use PrivateBin\Persistence\ServerSalt;
|
2016-08-10 23:41:46 +08:00
|
|
|
use PrivateBin\Persistence\TrafficLimiter;
|
2016-08-09 17:54:42 +08:00
|
|
|
use PrivateBin\Vizhash16x16;
|
2016-07-21 23:09:48 +08:00
|
|
|
|
2020-10-10 18:08:58 +08:00
|
|
|
class ModelTest extends TestCase
|
2015-09-26 23:57:46 +08:00
|
|
|
{
|
|
|
|
private $_conf;
|
|
|
|
|
|
|
|
private $_model;
|
|
|
|
|
2016-08-09 18:21:32 +08:00
|
|
|
protected $_path;
|
|
|
|
|
2020-10-10 18:22:20 +08:00
|
|
|
public function setUp(): void
|
2015-09-26 23:57:46 +08:00
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-08-09 18:21:32 +08:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
2016-10-29 16:24:08 +08:00
|
|
|
if (!is_dir($this->_path)) {
|
|
|
|
mkdir($this->_path);
|
|
|
|
}
|
2017-10-08 17:03:17 +08:00
|
|
|
$options = parse_ini_file(CONF_SAMPLE, true);
|
2016-07-18 20:47:32 +08:00
|
|
|
$options['purge']['limit'] = 0;
|
2016-10-29 16:24:08 +08:00
|
|
|
$options['model'] = array(
|
2016-08-09 17:54:42 +08:00
|
|
|
'class' => 'Database',
|
2015-09-26 23:57:46 +08:00
|
|
|
);
|
|
|
|
$options['model_options'] = array(
|
|
|
|
'dsn' => 'sqlite::memory:',
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
2016-08-09 17:54:42 +08:00
|
|
|
Helper::confBackup();
|
|
|
|
Helper::createIniFile(CONF, $options);
|
2022-10-28 07:01:02 +08:00
|
|
|
ServerSalt::setStore(new Database($options['model_options']));
|
2016-10-29 16:24:08 +08:00
|
|
|
$this->_conf = new Configuration;
|
|
|
|
$this->_model = new Model($this->_conf);
|
2015-09-27 09:03:55 +08:00
|
|
|
$_SERVER['REMOTE_ADDR'] = '::1';
|
2015-09-26 23:57:46 +08:00
|
|
|
}
|
|
|
|
|
2020-10-10 18:22:20 +08:00
|
|
|
public function tearDown(): void
|
2015-09-26 23:57:46 +08:00
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
2017-10-08 17:03:17 +08:00
|
|
|
unlink(CONF);
|
2016-08-09 18:21:32 +08:00
|
|
|
Helper::confRestore();
|
|
|
|
Helper::rmDir($this->_path);
|
2015-09-26 23:57:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicWorkflow()
|
|
|
|
{
|
|
|
|
// storing pastes
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
|
|
|
unset($pasteData['meta']['created'], $pasteData['meta']['salt']);
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2015-09-26 23:57:46 +08:00
|
|
|
$this->assertFalse($paste->exists(), 'paste does not yet exist');
|
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2015-09-26 23:57:46 +08:00
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 17:54:42 +08:00
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2015-09-26 23:57:46 +08:00
|
|
|
$this->assertTrue($paste->exists(), 'paste exists after storing it');
|
|
|
|
$paste = $paste->get();
|
2019-05-07 04:15:21 +08:00
|
|
|
unset(
|
|
|
|
$pasteData['meta'],
|
|
|
|
$paste['meta'],
|
|
|
|
$paste['comments'],
|
|
|
|
$paste['comment_count'],
|
|
|
|
$paste['comment_offset'],
|
|
|
|
$paste['@context']
|
|
|
|
);
|
|
|
|
$this->assertEquals($pasteData, $paste);
|
2015-09-26 23:57:46 +08:00
|
|
|
|
|
|
|
// storing comments
|
2019-05-07 04:15:21 +08:00
|
|
|
$commentData = Helper::getCommentPost();
|
2016-10-29 16:24:08 +08:00
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
|
|
|
$comment = $paste->getComment(Helper::getPasteId(), Helper::getCommentId());
|
2015-09-26 23:57:46 +08:00
|
|
|
$this->assertFalse($comment->exists(), 'comment does not yet exist');
|
|
|
|
|
2016-08-09 17:54:42 +08:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2019-05-04 05:03:57 +08:00
|
|
|
$comment->setData($commentData);
|
2015-09-26 23:57:46 +08:00
|
|
|
$comment->store();
|
|
|
|
|
2019-05-07 04:15:21 +08:00
|
|
|
$comments = $this->_model->getPaste(Helper::getPasteId())->get()['comments'];
|
|
|
|
$this->assertTrue(count($comments) === 1, 'comment exists after storing it');
|
2019-05-11 03:45:34 +08:00
|
|
|
$commentData['id'] = Helper::getPasteId();
|
2019-05-07 04:15:21 +08:00
|
|
|
$commentData['meta']['created'] = current($comments)['meta']['created'];
|
2019-05-11 03:45:34 +08:00
|
|
|
$commentData['meta']['icon'] = current($comments)['meta']['icon'];
|
2019-05-07 04:15:21 +08:00
|
|
|
$this->assertEquals($commentData, current($comments));
|
2015-09-26 23:57:46 +08:00
|
|
|
|
|
|
|
// deleting pastes
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2015-09-26 23:57:46 +08:00
|
|
|
$this->assertFalse($paste->exists(), 'paste successfully deleted');
|
|
|
|
$this->assertEquals(array(), $paste->getComments(), 'comment was deleted with paste');
|
|
|
|
}
|
|
|
|
|
2021-06-13 16:44:26 +08:00
|
|
|
public function testPasteV1()
|
|
|
|
{
|
|
|
|
$pasteData = Helper::getPaste(1);
|
|
|
|
unset($pasteData['meta']['formatter']);
|
|
|
|
|
|
|
|
$path = $this->_path . DIRECTORY_SEPARATOR . 'v1-test.sq3';
|
|
|
|
if (is_file($path)) {
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
$options = parse_ini_file(CONF_SAMPLE, true);
|
|
|
|
$options['purge']['limit'] = 0;
|
|
|
|
$options['model'] = array(
|
|
|
|
'class' => 'Database',
|
|
|
|
);
|
|
|
|
$options['model_options'] = array(
|
|
|
|
'dsn' => 'sqlite:' . $path,
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$model = new Model(new Configuration);
|
|
|
|
$model->getPaste('0000000000000000')->exists(); // triggers database table creation
|
|
|
|
$model->getPaste(Helper::getPasteId())->delete(); // deletes the cache
|
|
|
|
|
|
|
|
$db = new PDO(
|
|
|
|
$options['model_options']['dsn'],
|
|
|
|
$options['model_options']['usr'],
|
|
|
|
$options['model_options']['pwd'],
|
|
|
|
$options['model_options']['opt']
|
|
|
|
);
|
|
|
|
$statement = $db->prepare('INSERT INTO paste VALUES(?,?,?,?,?,?,?,?,?)');
|
|
|
|
$statement->execute(
|
|
|
|
array(
|
|
|
|
Helper::getPasteId(),
|
|
|
|
$pasteData['data'],
|
|
|
|
$pasteData['meta']['postdate'],
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
json_encode($pasteData['meta']),
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$statement->closeCursor();
|
|
|
|
|
|
|
|
$paste = $model->getPaste(Helper::getPasteId());
|
2021-06-16 10:39:24 +08:00
|
|
|
$this->assertNotEmpty($paste->getDeleteToken(), 'excercise the condition to load the data from storage');
|
2021-06-13 16:44:26 +08:00
|
|
|
$this->assertEquals('plaintext', $paste->get()['meta']['formatter'], 'paste got created with default formatter');
|
|
|
|
}
|
|
|
|
|
2019-05-12 04:18:35 +08:00
|
|
|
public function testCommentDefaults()
|
|
|
|
{
|
2022-11-01 23:38:06 +08:00
|
|
|
$class = 'PrivateBin\\Data\\' . $this->_conf->getKey('class', 'model');
|
2019-05-12 04:18:35 +08:00
|
|
|
$comment = new Comment(
|
|
|
|
$this->_conf,
|
2022-10-28 07:01:02 +08:00
|
|
|
new $class(
|
2019-05-12 04:18:35 +08:00
|
|
|
$this->_conf->getSection('model_options')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$comment->setPaste($this->_model->getPaste(Helper::getPasteId()));
|
|
|
|
$this->assertEquals(Helper::getPasteId(), $comment->getParentId(), 'comment parent ID gets initialized to paste ID');
|
|
|
|
}
|
|
|
|
|
2015-09-26 23:57:46 +08:00
|
|
|
public function testPasteDuplicate()
|
|
|
|
{
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
2015-09-26 23:57:46 +08:00
|
|
|
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-26 23:57:46 +08:00
|
|
|
$paste = $this->_model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2015-09-26 23:57:46 +08:00
|
|
|
$paste->store();
|
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(75);
|
2015-09-26 23:57:46 +08:00
|
|
|
$paste->store();
|
|
|
|
}
|
|
|
|
|
2021-06-13 16:44:26 +08:00
|
|
|
public function testStoreFail()
|
|
|
|
{
|
|
|
|
$path = $this->_path . DIRECTORY_SEPARATOR . 'model-store-test.sq3';
|
|
|
|
if (is_file($path)) {
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
$options = parse_ini_file(CONF_SAMPLE, true);
|
|
|
|
$options['purge']['limit'] = 0;
|
|
|
|
$options['model'] = array(
|
|
|
|
'class' => 'Database',
|
|
|
|
);
|
|
|
|
$options['model_options'] = array(
|
|
|
|
'dsn' => 'sqlite:' . $path,
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$model = new Model(new Configuration);
|
|
|
|
|
|
|
|
$pasteData = Helper::getPastePost();
|
|
|
|
$model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$model->getPaste(Helper::getPasteId())->exists();
|
|
|
|
|
|
|
|
$db = new PDO(
|
|
|
|
$options['model_options']['dsn'],
|
|
|
|
$options['model_options']['usr'],
|
|
|
|
$options['model_options']['pwd'],
|
|
|
|
$options['model_options']['opt']
|
|
|
|
);
|
|
|
|
$statement = $db->prepare('DROP TABLE paste');
|
|
|
|
$statement->execute();
|
|
|
|
$statement->closeCursor();
|
|
|
|
|
|
|
|
$paste = $model->getPaste();
|
|
|
|
$paste->setData($pasteData);
|
2021-10-08 04:34:15 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(76);
|
2021-06-13 16:44:26 +08:00
|
|
|
$paste->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCommentStoreFail()
|
|
|
|
{
|
|
|
|
$path = $this->_path . DIRECTORY_SEPARATOR . 'model-test.sq3';
|
|
|
|
if (is_file($path)) {
|
|
|
|
unlink($path);
|
|
|
|
}
|
|
|
|
$options = parse_ini_file(CONF_SAMPLE, true);
|
|
|
|
$options['purge']['limit'] = 0;
|
|
|
|
$options['model'] = array(
|
|
|
|
'class' => 'Database',
|
|
|
|
);
|
|
|
|
$options['model_options'] = array(
|
|
|
|
'dsn' => 'sqlite:' . $path,
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$model = new Model(new Configuration);
|
|
|
|
|
|
|
|
$pasteData = Helper::getPastePost();
|
|
|
|
$commentData = Helper::getCommentPost();
|
|
|
|
$model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
|
|
|
|
$paste = $model->getPaste();
|
|
|
|
$paste->setData($pasteData);
|
|
|
|
$paste->store();
|
2022-11-06 15:13:07 +08:00
|
|
|
$this->assertTrue($paste->exists(), 'paste exists before creating comment');
|
2021-06-13 16:44:26 +08:00
|
|
|
|
2022-11-05 17:00:12 +08:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
|
|
|
$comment->setData($commentData);
|
2021-06-13 16:44:26 +08:00
|
|
|
|
|
|
|
$db = new PDO(
|
|
|
|
$options['model_options']['dsn'],
|
|
|
|
$options['model_options']['usr'],
|
|
|
|
$options['model_options']['pwd'],
|
|
|
|
$options['model_options']['opt']
|
|
|
|
);
|
|
|
|
$statement = $db->prepare('DROP TABLE comment');
|
|
|
|
$statement->execute();
|
|
|
|
$statement->closeCursor();
|
|
|
|
|
2021-10-08 04:34:15 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(70);
|
2021-06-13 16:44:26 +08:00
|
|
|
$comment->store();
|
|
|
|
}
|
|
|
|
|
2015-09-26 23:57:46 +08:00
|
|
|
public function testCommentDuplicate()
|
|
|
|
{
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
|
|
|
$commentData = Helper::getCommentPost();
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-26 23:57:46 +08:00
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2015-09-26 23:57:46 +08:00
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 17:54:42 +08:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2019-05-04 05:03:57 +08:00
|
|
|
$comment->setData($commentData);
|
2015-09-26 23:57:46 +08:00
|
|
|
$comment->store();
|
|
|
|
|
2016-08-09 17:54:42 +08:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2019-05-04 05:03:57 +08:00
|
|
|
$comment->setData($commentData);
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(69);
|
2015-09-26 23:57:46 +08:00
|
|
|
$comment->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testImplicitDefaults()
|
|
|
|
{
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
|
|
|
$commentData = Helper::getCommentPost();
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-26 23:57:46 +08:00
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2015-09-27 09:03:55 +08:00
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 17:54:42 +08:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2019-05-04 05:03:57 +08:00
|
|
|
$comment->setData($commentData);
|
2019-05-07 04:15:21 +08:00
|
|
|
$comment->get();
|
2015-09-26 23:57:46 +08:00
|
|
|
$comment->store();
|
|
|
|
|
2022-10-30 16:05:29 +08:00
|
|
|
$identicon = new Identicon();
|
|
|
|
$pngdata = $identicon->getImageDataUri(TrafficLimiter::getHash(), 16);
|
2019-05-07 04:15:21 +08:00
|
|
|
$comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
|
|
|
|
$this->assertEquals($pngdata, $comment['meta']['icon'], 'icon gets set');
|
2015-09-26 23:57:46 +08:00
|
|
|
}
|
2015-09-27 09:03:55 +08:00
|
|
|
|
|
|
|
public function testPasteIdValidation()
|
|
|
|
{
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->assertTrue(Paste::isValidId('a242ab7bdfb2581a'), 'valid paste id');
|
|
|
|
$this->assertFalse(Paste::isValidId('foo'), 'invalid hex values');
|
|
|
|
$this->assertFalse(Paste::isValidId('../bar/baz'), 'path attack');
|
2015-09-27 09:03:55 +08:00
|
|
|
}
|
2015-09-27 20:36:20 +08:00
|
|
|
|
2017-03-25 04:30:08 +08:00
|
|
|
public function testInvalidPaste()
|
|
|
|
{
|
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(64);
|
2017-03-25 04:30:08 +08:00
|
|
|
$paste->get();
|
|
|
|
}
|
|
|
|
|
2021-06-13 16:44:26 +08:00
|
|
|
public function testInvalidPasteFormat()
|
|
|
|
{
|
2021-06-13 17:16:29 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
2021-06-13 16:44:26 +08:00
|
|
|
$pasteData['adata'][1] = 'format does not exist';
|
2021-06-13 17:16:29 +08:00
|
|
|
$paste = $this->_model->getPaste();
|
2021-10-08 04:36:11 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(75);
|
2021-06-13 16:44:26 +08:00
|
|
|
$paste->setData($pasteData);
|
|
|
|
}
|
|
|
|
|
2019-05-07 04:15:21 +08:00
|
|
|
public function testInvalidPasteId()
|
|
|
|
{
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(60);
|
2019-05-07 04:15:21 +08:00
|
|
|
$this->_model->getPaste('');
|
|
|
|
}
|
|
|
|
|
2015-09-27 20:36:20 +08:00
|
|
|
public function testInvalidComment()
|
|
|
|
{
|
|
|
|
$paste = $this->_model->getPaste();
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(62);
|
2016-08-09 17:54:42 +08:00
|
|
|
$paste->getComment(Helper::getPasteId());
|
2015-09-27 20:36:20 +08:00
|
|
|
}
|
|
|
|
|
2017-03-25 04:30:08 +08:00
|
|
|
public function testInvalidCommentDeletedPaste()
|
|
|
|
{
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
2017-03-25 04:35:50 +08:00
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2017-03-25 04:30:08 +08:00
|
|
|
$paste->store();
|
|
|
|
|
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
|
|
|
$paste->delete();
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(67);
|
2017-03-25 04:30:08 +08:00
|
|
|
$comment->store();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidCommentData()
|
|
|
|
{
|
2019-05-11 03:45:34 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData['adata'][2] = 0;
|
2019-05-11 03:45:34 +08:00
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2017-03-25 04:30:08 +08:00
|
|
|
$paste->store();
|
|
|
|
|
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(68);
|
2017-03-25 04:30:08 +08:00
|
|
|
$comment->store();
|
|
|
|
}
|
|
|
|
|
2019-05-07 04:15:21 +08:00
|
|
|
public function testInvalidCommentParent()
|
|
|
|
{
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(65);
|
|
|
|
$paste->getComment('');
|
2019-05-07 04:15:21 +08:00
|
|
|
}
|
|
|
|
|
2015-09-27 20:36:20 +08:00
|
|
|
public function testExpiration()
|
|
|
|
{
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $this->_model->getPaste(Helper::getPasteId());
|
2015-09-27 20:36:20 +08:00
|
|
|
$this->assertFalse($paste->exists(), 'paste does not yet exist');
|
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2015-09-27 20:36:20 +08:00
|
|
|
$paste->store();
|
|
|
|
|
|
|
|
$paste = $paste->get();
|
2019-05-07 04:15:21 +08:00
|
|
|
$this->assertEquals((float) 300, (float) $paste['meta']['time_to_live'], 'remaining time is set correctly', 1.0);
|
2015-09-27 20:36:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCommentDeletion()
|
|
|
|
{
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
2015-09-27 20:36:20 +08:00
|
|
|
|
|
|
|
$paste = $this->_model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2015-09-27 20:36:20 +08:00
|
|
|
$paste->store();
|
2020-10-11 16:31:24 +08:00
|
|
|
$this->expectException(Exception::class);
|
|
|
|
$this->expectExceptionCode(64);
|
2016-08-09 17:54:42 +08:00
|
|
|
$paste->getComment(Helper::getPasteId())->delete();
|
2015-09-27 20:36:20 +08:00
|
|
|
}
|
2016-07-18 16:14:38 +08:00
|
|
|
|
2016-07-18 20:47:32 +08:00
|
|
|
public function testPurge()
|
|
|
|
{
|
2016-10-29 16:24:08 +08:00
|
|
|
$conf = new Configuration;
|
2022-10-28 07:01:02 +08:00
|
|
|
$store = new Database($conf->getSection('model_options'));
|
2016-08-09 17:54:42 +08:00
|
|
|
$store->delete(Helper::getPasteId());
|
2019-05-07 04:15:21 +08:00
|
|
|
$expired = Helper::getPaste(2, array('expire_date' => 1344803344));
|
|
|
|
$paste = Helper::getPaste(2, array('expire_date' => time() + 3600));
|
2016-10-29 16:24:08 +08:00
|
|
|
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
|
|
|
|
$ids = array();
|
2016-07-26 14:19:35 +08:00
|
|
|
foreach ($keys as $key) {
|
2019-05-11 04:46:39 +08:00
|
|
|
$ids[$key] = hash('fnv164', $key);
|
2016-07-18 20:47:32 +08:00
|
|
|
$store->delete($ids[$key]);
|
|
|
|
$this->assertFalse($store->exists($ids[$key]), "paste $key does not yet exist");
|
2016-07-26 14:19:35 +08:00
|
|
|
if (in_array($key, array('x', 'y', 'z'))) {
|
2016-07-18 20:47:32 +08:00
|
|
|
$this->assertTrue($store->create($ids[$key], $paste), "store $key paste");
|
2016-07-26 14:19:35 +08:00
|
|
|
} else {
|
2016-07-18 20:47:32 +08:00
|
|
|
$this->assertTrue($store->create($ids[$key], $expired), "store $key paste");
|
|
|
|
}
|
|
|
|
$this->assertTrue($store->exists($ids[$key]), "paste $key exists after storing it");
|
|
|
|
}
|
|
|
|
$this->_model->purge(10);
|
2016-07-26 14:19:35 +08:00
|
|
|
foreach ($ids as $key => $id) {
|
|
|
|
if (in_array($key, array('x', 'y', 'z'))) {
|
2016-07-19 14:40:33 +08:00
|
|
|
$this->assertTrue($this->_model->getPaste($id)->exists(), "paste $key exists after purge");
|
|
|
|
$this->_model->getPaste($id)->delete();
|
2016-07-26 14:19:35 +08:00
|
|
|
} else {
|
2016-07-19 14:40:33 +08:00
|
|
|
$this->assertFalse($this->_model->getPaste($id)->exists(), "paste $key was purged");
|
2016-07-18 20:47:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-18 16:14:38 +08:00
|
|
|
public function testCommentWithDisabledVizhash()
|
|
|
|
{
|
2016-10-29 16:24:08 +08:00
|
|
|
$options = parse_ini_file(CONF, true);
|
2016-08-10 23:41:46 +08:00
|
|
|
$options['main']['icon'] = 'none';
|
2016-10-29 16:24:08 +08:00
|
|
|
$options['model'] = array(
|
2016-08-10 23:41:46 +08:00
|
|
|
'class' => 'Database',
|
2016-07-18 16:14:38 +08:00
|
|
|
);
|
|
|
|
$options['model_options'] = array(
|
|
|
|
'dsn' => 'sqlite::memory:',
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
2016-08-09 17:54:42 +08:00
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$model = new Model(new Configuration);
|
2016-07-18 16:14:38 +08:00
|
|
|
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
2016-08-09 17:54:42 +08:00
|
|
|
$this->_model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
$paste = $model->getPaste(Helper::getPasteId());
|
2016-07-18 16:14:38 +08:00
|
|
|
$this->assertFalse($paste->exists(), 'paste does not yet exist');
|
|
|
|
|
|
|
|
$paste = $model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2016-07-18 16:14:38 +08:00
|
|
|
$paste->store();
|
|
|
|
|
2016-08-09 17:54:42 +08:00
|
|
|
$paste = $model->getPaste(Helper::getPasteId());
|
2016-07-18 16:14:38 +08:00
|
|
|
$this->assertTrue($paste->exists(), 'paste exists after storing it');
|
|
|
|
|
|
|
|
// storing comments
|
2019-05-07 04:15:21 +08:00
|
|
|
$commentData = Helper::getCommentPost();
|
|
|
|
unset($commentData['meta']['icon']);
|
2016-10-29 16:24:08 +08:00
|
|
|
$paste = $model->getPaste(Helper::getPasteId());
|
2019-05-07 04:15:21 +08:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
|
2016-07-18 16:14:38 +08:00
|
|
|
$this->assertFalse($comment->exists(), 'comment does not yet exist');
|
|
|
|
|
2016-08-09 17:54:42 +08:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2019-05-04 05:03:57 +08:00
|
|
|
$comment->setData($commentData);
|
2016-07-18 16:14:38 +08:00
|
|
|
$comment->store();
|
|
|
|
|
2019-05-07 04:15:21 +08:00
|
|
|
$comment = $paste->getComment(Helper::getPasteId(), Helper::getPasteId());
|
2016-07-18 16:14:38 +08:00
|
|
|
$this->assertTrue($comment->exists(), 'comment exists after storing it');
|
2016-08-10 23:41:46 +08:00
|
|
|
|
2019-05-07 04:15:21 +08:00
|
|
|
$comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
|
|
|
|
$this->assertFalse(array_key_exists('icon', $comment['meta']), 'icon was not generated');
|
2016-08-10 23:41:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCommentVizhash()
|
|
|
|
{
|
2016-10-29 16:24:08 +08:00
|
|
|
$options = parse_ini_file(CONF, true);
|
2016-08-10 23:41:46 +08:00
|
|
|
$options['main']['icon'] = 'vizhash';
|
2016-10-29 16:24:08 +08:00
|
|
|
$options['model'] = array(
|
2016-08-10 23:41:46 +08:00
|
|
|
'class' => 'Database',
|
|
|
|
);
|
|
|
|
$options['model_options'] = array(
|
|
|
|
'dsn' => 'sqlite::memory:',
|
|
|
|
'usr' => null,
|
|
|
|
'pwd' => null,
|
|
|
|
'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
|
|
|
|
);
|
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$model = new Model(new Configuration);
|
|
|
|
|
2019-05-07 04:15:21 +08:00
|
|
|
$pasteData = Helper::getPastePost();
|
|
|
|
$commentData = Helper::getCommentPost();
|
2016-08-10 23:41:46 +08:00
|
|
|
$model->getPaste(Helper::getPasteId())->delete();
|
|
|
|
|
|
|
|
$paste = $model->getPaste();
|
2019-05-04 05:03:57 +08:00
|
|
|
$paste->setData($pasteData);
|
2016-08-10 23:41:46 +08:00
|
|
|
$paste->store();
|
|
|
|
|
|
|
|
$comment = $paste->getComment(Helper::getPasteId());
|
2019-05-04 05:03:57 +08:00
|
|
|
$comment->setData($commentData);
|
2016-08-10 23:41:46 +08:00
|
|
|
$comment->store();
|
|
|
|
|
2019-05-11 03:45:34 +08:00
|
|
|
$vz = new Vizhash16x16();
|
|
|
|
$pngdata = 'data:image/png;base64,' . base64_encode($vz->generate(TrafficLimiter::getHash()));
|
2019-05-07 04:15:21 +08:00
|
|
|
$comment = current($this->_model->getPaste(Helper::getPasteId())->get()['comments']);
|
|
|
|
$this->assertEquals($pngdata, $comment['meta']['icon'], 'nickname triggers vizhash to be set');
|
2016-08-10 23:41:46 +08:00
|
|
|
}
|
2016-07-11 20:15:20 +08:00
|
|
|
}
|