Added tests for JSON errors, should help us figure out the cause of the problem in #11
This commit is contained in:
parent
6144e73405
commit
6cb7454d07
3 changed files with 84 additions and 3 deletions
|
@ -13,6 +13,7 @@
|
|||
namespace PrivateBin\Data;
|
||||
|
||||
use PrivateBin\Model\Paste;
|
||||
use PrivateBin\Json;
|
||||
|
||||
/**
|
||||
* Filesystem
|
||||
|
@ -61,6 +62,7 @@ class Filesystem extends AbstractData
|
|||
* @access public
|
||||
* @param string $pasteid
|
||||
* @param array $paste
|
||||
* @throws Exception
|
||||
* @return bool
|
||||
*/
|
||||
public function create($pasteid, $paste)
|
||||
|
@ -72,7 +74,7 @@ class Filesystem extends AbstractData
|
|||
if (!is_dir($storagedir)) {
|
||||
mkdir($storagedir, 0700, true);
|
||||
}
|
||||
return (bool) file_put_contents($storagedir . $pasteid, json_encode($paste));
|
||||
return (bool) file_put_contents($storagedir . $pasteid, Json::encode($paste));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -153,6 +155,7 @@ class Filesystem extends AbstractData
|
|||
* @param string $parentid
|
||||
* @param string $commentid
|
||||
* @param array $comment
|
||||
* @throws Exception
|
||||
* @return bool
|
||||
*/
|
||||
public function createComment($pasteid, $parentid, $commentid, $comment)
|
||||
|
@ -165,7 +168,7 @@ class Filesystem extends AbstractData
|
|||
if (!is_dir($storagedir)) {
|
||||
mkdir($storagedir, 0700, true);
|
||||
}
|
||||
return (bool) file_put_contents($storagedir . $filename, json_encode($comment));
|
||||
return (bool) file_put_contents($storagedir . $filename, Json::encode($comment));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
48
lib/Json.php
Normal file
48
lib/Json.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php
|
||||
/**
|
||||
* PrivateBin
|
||||
*
|
||||
* a zero-knowledge paste bin
|
||||
*
|
||||
* @link https://github.com/PrivateBin/PrivateBin
|
||||
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
||||
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
||||
* @version 0.22
|
||||
*/
|
||||
|
||||
namespace PrivateBin;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Json
|
||||
*
|
||||
* Provides JSON functions in an object oriented way.
|
||||
*/
|
||||
class Json
|
||||
{
|
||||
/**
|
||||
* Returns a string containing the JSON representation of the given input
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param mixed $input
|
||||
* @throws Exception
|
||||
* @return string
|
||||
*/
|
||||
public static function encode($input)
|
||||
{
|
||||
$jsonString = json_encode($input);
|
||||
$errorCode = json_last_error();
|
||||
if ($errorCode === JSON_ERROR_NONE) {
|
||||
return $jsonString;
|
||||
}
|
||||
|
||||
$message = 'A JSON error occurred';
|
||||
if (function_exists('json_last_error_msg')) {
|
||||
$message .= ': ' . json_last_error_msg();
|
||||
}
|
||||
$message .= ' (' . $errorCode . ')';
|
||||
throw new Exception($message, 90);
|
||||
}
|
||||
}
|
|
@ -35,7 +35,7 @@ class FilesystemTest extends PHPUnit_Framework_TestCase
|
|||
|
||||
// storing comments
|
||||
$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->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), Helper::getComment()), '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()));
|
||||
$comment->id = Helper::getCommentId();
|
||||
|
@ -93,4 +93,34 @@ class FilesystemTest extends PHPUnit_Framework_TestCase
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionCode 90
|
||||
*/
|
||||
public function testErrorDetection()
|
||||
{
|
||||
$this->_model->delete(Helper::getPasteId());
|
||||
$paste = Helper::getPaste(array('formatter' => "Invalid UTF-8 sequence: \xB1\x31"));
|
||||
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
|
||||
$this->assertFalse($this->_model->create(Helper::getPasteId(), $paste), 'unable to store broken paste');
|
||||
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does still not exist');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Exception
|
||||
* @expectedExceptionCode 90
|
||||
*/
|
||||
public function testCommentErrorDetection()
|
||||
{
|
||||
$this->_model->delete(Helper::getPasteId());
|
||||
$paste = Helper::getPaste();
|
||||
$comment = Helper::getComment(array('formatter' => "Invalid UTF-8 sequence: \xB1\x31"));
|
||||
$this->assertFalse($this->_model->exists(Helper::getPasteId()), 'paste does not yet exist');
|
||||
$this->assertTrue($this->_model->create(Helper::getPasteId(), Helper::getPaste()) === true, 'store new paste');
|
||||
$this->assertTrue($this->_model->exists(Helper::getPasteId()), 'paste exists after storing it');
|
||||
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does not yet exist');
|
||||
$this->assertFalse($this->_model->createComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId(), $comment), 'unable to store broken comment');
|
||||
$this->assertFalse($this->_model->existsComment(Helper::getPasteId(), Helper::getPasteId(), Helper::getCommentId()), 'comment does still not exist');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue