2015-08-15 12:32:31 -04:00
|
|
|
<?php
|
2016-07-11 05:58:15 -04:00
|
|
|
class privatebin_dataTest extends PHPUnit_Framework_TestCase
|
2015-08-15 12:32:31 -04:00
|
|
|
{
|
|
|
|
private $_model;
|
|
|
|
|
|
|
|
private $_path;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
/* Setup Routine */
|
2016-07-11 05:58:15 -04:00
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
|
|
|
$this->_model = privatebin_data::getInstance(array('dir' => $this->_path));
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
helper::rmdir($this->_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFileBasedDataStoreWorks()
|
|
|
|
{
|
2015-09-26 06:29:27 -04:00
|
|
|
$this->_model->delete(helper::getPasteId());
|
|
|
|
|
2015-08-15 12:32:31 -04:00
|
|
|
// storing pastes
|
2015-09-21 16:32:52 -04:00
|
|
|
$paste = helper::getPaste(array('expire_date' => 1344803344));
|
|
|
|
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
|
|
|
|
$this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
|
|
|
|
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
|
|
|
|
$this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
|
|
|
|
$this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(helper::getPasteId()));
|
2015-08-15 12:32:31 -04:00
|
|
|
|
|
|
|
// storing comments
|
2015-09-21 16:32:52 -04:00
|
|
|
$this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment does not yet exist');
|
|
|
|
$this->assertTrue($this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()) !== false, 'store comment');
|
|
|
|
$this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it');
|
|
|
|
$comment = json_decode(json_encode(helper::getComment()));
|
2015-10-18 08:37:58 -04:00
|
|
|
$comment->id = helper::getCommentId();
|
|
|
|
$comment->parentid = helper::getPasteId();
|
2015-08-15 12:32:31 -04:00
|
|
|
$this->assertEquals(
|
|
|
|
array($comment->meta->postdate => $comment),
|
2015-09-21 16:32:52 -04:00
|
|
|
$this->_model->readComments(helper::getPasteId())
|
2015-08-15 12:32:31 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
// deleting pastes
|
2015-09-21 16:32:52 -04:00
|
|
|
$this->_model->delete(helper::getPasteId());
|
|
|
|
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
|
|
|
|
$this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment was deleted with paste');
|
|
|
|
$this->assertFalse($this->_model->read(helper::getPasteId()), 'paste can no longer be found');
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|
2015-09-26 06:29:27 -04:00
|
|
|
|
|
|
|
public function testFileBasedAttachmentStoreWorks()
|
|
|
|
{
|
|
|
|
$this->_model->delete(helper::getPasteId());
|
|
|
|
$original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
|
|
|
|
$paste['meta']['attachment'] = $paste['attachment'];
|
|
|
|
$paste['meta']['attachmentname'] = $paste['attachmentname'];
|
|
|
|
unset($paste['attachment'], $paste['attachmentname']);
|
|
|
|
$this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
|
|
|
|
$this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
|
|
|
|
$this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
|
|
|
|
$this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
|
|
|
|
$this->assertEquals(json_decode(json_encode($original)), $this->_model->read(helper::getPasteId()));
|
|
|
|
}
|
|
|
|
|
2016-07-15 11:02:59 -04:00
|
|
|
public function testPurge()
|
|
|
|
{
|
2016-07-18 08:47:32 -04:00
|
|
|
mkdir($this->_path . DIRECTORY_SEPARATOR . '00', 0777, true);
|
2016-07-15 11:02:59 -04:00
|
|
|
$expired = helper::getPaste(array('expire_date' => 1344803344));
|
|
|
|
$paste = helper::getPaste(array('expire_date' => time() + 3600));
|
|
|
|
$keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
|
|
|
|
$ids = array();
|
|
|
|
foreach ($keys as $key)
|
|
|
|
{
|
|
|
|
$ids[$key] = substr(md5($key), 0, 16);
|
|
|
|
$this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
|
|
|
|
if (in_array($key, array('x', 'y', 'z')))
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
|
|
|
|
}
|
|
|
|
$this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
|
|
|
|
}
|
|
|
|
$this->_model->purge(10);
|
|
|
|
foreach ($ids as $key => $id)
|
|
|
|
{
|
|
|
|
if (in_array($key, array('x', 'y', 'z')))
|
|
|
|
{
|
|
|
|
$this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after purge");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->_model->exists($ids[$key]), "paste $key was purged");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-15 12:32:31 -04:00
|
|
|
}
|