2015-08-27 21:30:35 +00:00
< ? php
class zerobinTest extends PHPUnit_Framework_TestCase
{
2015-09-03 20:55:36 +00:00
private $_conf ;
2015-08-27 21:30:35 +00:00
private $_model ;
public function setUp ()
{
/* Setup Routine */
$this -> _model = zerobin_data :: getInstance ( array ( 'dir' => PATH . 'data' ));
serversalt :: setPath ( PATH . 'data' );
2015-09-03 20:55:36 +00:00
$this -> _conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini' ;
2015-08-27 21:30:35 +00:00
$this -> reset ();
}
public function tearDown ()
{
/* Tear Down Routine */
}
public function reset ()
{
$_POST = array ();
$_GET = array ();
$_SERVER = array ();
2015-09-21 20:32:52 +00:00
if ( $this -> _model -> exists ( helper :: getPasteId ()))
$this -> _model -> delete ( helper :: getPasteId ());
2015-09-03 20:55:36 +00:00
if ( is_file ( $this -> _conf . '.bak' ))
rename ( $this -> _conf . '.bak' , $this -> _conf );
2015-08-27 21:30:35 +00:00
}
/**
* @ runInSeparateProcess
*/
public function testView ()
{
$this -> reset ();
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'tag' => 'title' ,
'content' => 'ZeroBin'
),
$content ,
'outputs title correctly'
);
}
2015-09-19 15:23:10 +00:00
/**
* @ runInSeparateProcess
*/
public function testViewLanguageSelection ()
{
$this -> reset ();
$options = parse_ini_file ( $this -> _conf , true );
$options [ 'main' ][ 'languageselection' ] = true ;
if ( ! is_file ( $this -> _conf . '.bak' ) && is_file ( $this -> _conf ))
rename ( $this -> _conf , $this -> _conf . '.bak' );
helper :: createIniFile ( $this -> _conf , $options );
$_COOKIE [ 'lang' ] = 'de' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'tag' => 'title' ,
'content' => 'ZeroBin'
),
$content ,
'outputs title correctly'
);
}
2015-08-29 18:29:14 +00:00
/**
* @ runInSeparateProcess
*/
public function testHtaccess ()
{
$this -> reset ();
$dirs = array ( 'cfg' , 'lib' );
foreach ( $dirs as $dir ) {
$file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess' ;
@ unlink ( $file );
}
ob_start ();
new zerobin ;
$content = ob_get_contents ();
foreach ( $dirs as $dir ) {
$file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess' ;
$this -> assertFileExists (
$file ,
" $dir htaccess recreated "
);
}
}
/**
* @ expectedException Exception
* @ expectedExceptionCode 2
*/
public function testConf ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
if ( ! is_file ( $this -> _conf . '.bak' ) && is_file ( $this -> _conf ))
rename ( $this -> _conf , $this -> _conf . '.bak' );
file_put_contents ( $this -> _conf , '' );
2015-08-29 18:29:14 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
}
2015-08-27 21:30:35 +00:00
/**
* @ runInSeparateProcess
*/
public function testCreate ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-08-27 21:30:35 +00:00
$_SERVER [ 'REMOTE_ADDR' ] = '::1' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
2015-08-29 18:29:14 +00:00
$this -> assertEquals ( 0 , $response [ 'status' ], 'outputs status' );
2015-08-27 21:30:35 +00:00
$this -> assertEquals (
2015-08-29 18:29:14 +00:00
hash_hmac ( 'sha1' , $response [ 'id' ], serversalt :: get ()),
2015-08-27 21:30:35 +00:00
$response [ 'deletetoken' ],
2015-08-29 18:29:14 +00:00
'outputs valid delete token'
);
$this -> assertTrue ( $this -> _model -> exists ( $response [ 'id' ]), 'paste exists after posting data' );
}
2015-09-03 20:55:36 +00:00
/**
* @ runInSeparateProcess
*/
public function testCreateInvalidTimelimit ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-09-03 20:55:36 +00:00
$_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' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after posting data' );
2015-09-03 20:55:36 +00:00
}
/**
* @ 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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-09-03 20:55:36 +00:00
$_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' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after posting data' );
2015-09-03 20:55:36 +00:00
}
2015-09-19 15:23:10 +00:00
/**
* @ runInSeparateProcess
*/
public function testCreateProxyHeader ()
{
$this -> reset ();
$options = parse_ini_file ( $this -> _conf , true );
$options [ 'traffic' ][ 'header' ] = 'X_FORWARDED_FOR' ;
if ( ! is_file ( $this -> _conf . '.bak' ) && is_file ( $this -> _conf ))
rename ( $this -> _conf , $this -> _conf . '.bak' );
helper :: createIniFile ( $this -> _conf , $options );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-09-19 15:23:10 +00:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '::1' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 1 , $response [ 'status' ], 'outputs error status' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after posting data' );
2015-09-19 15:23:10 +00:00
}
2015-09-03 20:55:36 +00:00
/**
* @ 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 );
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
$_POST = helper :: getPaste ();
2015-09-03 20:55:36 +00:00
$_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' );
2015-09-21 20:32:52 +00:00
$this -> assertTrue ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after posting data' );
2015-09-03 20:55:36 +00:00
}
2015-08-29 18:29:14 +00:00
/**
* @ runInSeparateProcess
*/
public function testCreateValidExpire ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-08-29 18:29:14 +00:00
$_POST [ 'expire' ] = '5min' ;
2015-09-12 15:33:16 +00:00
$_POST [ 'formatter' ] = 'foo' ;
2015-08-29 18:29:14 +00:00
$_SERVER [ 'REMOTE_ADDR' ] = '::1' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 0 , $response [ 'status' ], 'outputs status' );
$this -> assertEquals (
2015-08-27 21:30:35 +00:00
hash_hmac ( 'sha1' , $response [ 'id' ], serversalt :: get ()),
2015-08-29 18:29:14 +00:00
$response [ 'deletetoken' ],
2015-08-27 21:30:35 +00:00
'outputs valid delete token'
);
$this -> assertTrue ( $this -> _model -> exists ( $response [ 'id' ]), 'paste exists after posting data' );
}
2015-08-29 18:29:14 +00:00
/**
* @ runInSeparateProcess
*/
public function testCreateInvalidExpire ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-08-29 18:29:14 +00:00
$_POST [ 'expire' ] = 'foo' ;
$_SERVER [ 'REMOTE_ADDR' ] = '::1' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 0 , $response [ 'status' ], 'outputs status' );
$this -> assertEquals (
hash_hmac ( 'sha1' , $response [ 'id' ], serversalt :: get ()),
$response [ 'deletetoken' ],
'outputs valid delete token'
);
$this -> assertTrue ( $this -> _model -> exists ( $response [ 'id' ]), 'paste exists after posting data' );
}
/**
* @ runInSeparateProcess
*/
public function testCreateInvalidBurn ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-08-29 18:29:14 +00:00
$_POST [ 'burnafterreading' ] = 'neither 1 nor 0' ;
$_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' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after posting data' );
2015-08-29 18:29:14 +00:00
}
/**
* @ runInSeparateProcess
*/
public function testCreateInvalidOpenDiscussion ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-08-29 18:29:14 +00:00
$_POST [ 'opendiscussion' ] = 'neither 1 nor 0' ;
$_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' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after posting data' );
2015-08-29 18:29:14 +00:00
}
2015-09-19 15:23:10 +00:00
/**
* @ runInSeparateProcess
*/
public function testCreateAttachment ()
{
$this -> reset ();
$options = parse_ini_file ( $this -> _conf , true );
$options [ 'traffic' ][ 'limit' ] = 0 ;
$options [ 'main' ][ 'fileupload' ] = true ;
if ( ! is_file ( $this -> _conf . '.bak' ) && is_file ( $this -> _conf ))
rename ( $this -> _conf , $this -> _conf . '.bak' );
helper :: createIniFile ( $this -> _conf , $options );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-09-19 15:23:10 +00:00
$_SERVER [ 'REMOTE_ADDR' ] = '::1' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 0 , $response [ 'status' ], 'outputs status' );
$this -> assertEquals (
hash_hmac ( 'sha1' , $response [ 'id' ], serversalt :: get ()),
$response [ 'deletetoken' ],
'outputs valid delete token'
);
$this -> assertTrue ( $this -> _model -> exists ( $response [ 'id' ]), 'paste exists after posting data' );
}
2015-08-29 18:29:14 +00:00
/**
* @ runInSeparateProcess
*/
public function testCreateValidNick ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
$_POST [ 'nickname' ] = helper :: getComment ()[ 'meta' ][ 'nickname' ];
2015-08-29 18:29:14 +00:00
$_SERVER [ 'REMOTE_ADDR' ] = '::1' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 0 , $response [ 'status' ], 'outputs status' );
$this -> assertEquals (
hash_hmac ( 'sha1' , $response [ 'id' ], serversalt :: get ()),
$response [ 'deletetoken' ],
'outputs valid delete token'
);
$this -> assertTrue ( $this -> _model -> exists ( $response [ 'id' ]), 'paste exists after posting data' );
}
/**
* @ runInSeparateProcess
*/
public function testCreateInvalidNick ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getPaste ();
2015-08-29 18:29:14 +00:00
$_POST [ 'nickname' ] = 'foo' ;
$_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' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after posting data' );
2015-08-29 18:29:14 +00:00
}
/**
* @ runInSeparateProcess
*/
public function testCreateComment ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getComment ();
$_POST [ 'pasteid' ] = helper :: getPasteId ();
$_POST [ 'parentid' ] = helper :: getPasteId ();
2015-08-29 18:29:14 +00:00
$_SERVER [ 'REMOTE_ADDR' ] = '::1' ;
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
2015-08-29 18:29:14 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 0 , $response [ 'status' ], 'outputs status' );
2015-09-21 20:32:52 +00:00
$this -> assertTrue ( $this -> _model -> existsComment ( helper :: getPasteId (), helper :: getPasteId (), $response [ 'id' ]), 'paste exists after posting data' );
2015-08-29 18:29:14 +00:00
}
2015-09-03 20:55:36 +00:00
/**
* @ 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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getComment ();
$_POST [ 'pasteid' ] = helper :: getPasteId ();
2015-09-03 20:55:36 +00:00
$_POST [ 'parentid' ] = 'foo' ;
$_SERVER [ 'REMOTE_ADDR' ] = '::1' ;
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
2015-09-03 20:55:36 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 1 , $response [ 'status' ], 'outputs error status' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> existsComment ( helper :: getPasteId (), helper :: getPasteId (), helper :: getCommentId ()), 'paste exists after posting data' );
2015-09-03 20:55:36 +00:00
}
2015-08-29 18:29:14 +00:00
/**
* @ runInSeparateProcess
*/
public function testCreateCommentDiscussionDisabled ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getComment ();
$_POST [ 'pasteid' ] = helper :: getPasteId ();
$_POST [ 'parentid' ] = helper :: getPasteId ();
2015-08-29 18:29:14 +00:00
$_SERVER [ 'REMOTE_ADDR' ] = '::1' ;
2015-09-21 20:32:52 +00:00
$paste = helper :: getPaste ( array ( 'opendiscussion' => false ));
$this -> _model -> create ( helper :: getPasteId (), $paste );
2015-08-29 18:29:14 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 1 , $response [ 'status' ], 'outputs error status' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> existsComment ( helper :: getPasteId (), helper :: getPasteId (), helper :: getCommentId ()), 'paste exists after posting data' );
2015-08-29 18:29:14 +00:00
}
/**
* @ runInSeparateProcess
*/
public function testCreateCommentInvalidPaste ()
{
$this -> reset ();
2015-09-03 20:55:36 +00:00
$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 );
2015-09-21 20:32:52 +00:00
$_POST = helper :: getComment ();
$_POST [ 'pasteid' ] = helper :: getPasteId ();
$_POST [ 'parentid' ] = helper :: getPasteId ();
2015-08-29 18:29:14 +00:00
$_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' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> existsComment ( helper :: getPasteId (), helper :: getPasteId (), helper :: getCommentId ()), 'paste exists after posting data' );
2015-08-29 18:29:14 +00:00
}
2015-09-03 20:55:36 +00:00
/**
* @ 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 );
2015-09-21 20:32:52 +00:00
$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 [ 'pasteid' ] = helper :: getPasteId ();
$_POST [ 'parentid' ] = helper :: getPasteId ();
2015-09-03 20:55:36 +00:00
$_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' );
2015-09-21 20:32:52 +00:00
$this -> assertTrue ( $this -> _model -> existsComment ( helper :: getPasteId (), helper :: getPasteId (), helper :: getCommentId ()), 'paste exists after posting data' );
2015-09-03 20:55:36 +00:00
}
2015-08-27 21:30:35 +00:00
/**
* @ runInSeparateProcess
*/
public function testRead ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
$_SERVER [ 'QUERY_STRING' ] = helper :: getPasteId ();
2015-08-27 21:30:35 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'cipherdata' ,
2015-09-21 20:32:52 +00:00
'content' => htmlspecialchars ( json_encode ( helper :: getPaste ()), ENT_NOQUOTES )
2015-08-27 21:30:35 +00:00
),
$content ,
'outputs data correctly'
);
}
2015-08-29 18:29:14 +00:00
/**
* @ runInSeparateProcess
*/
public function testReadInvalidId ()
{
$this -> reset ();
$_SERVER [ 'QUERY_STRING' ] = 'foo' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'errormessage' ,
'content' => 'Invalid paste ID'
),
$content ,
'outputs error correctly'
);
}
/**
* @ runInSeparateProcess
*/
public function testReadNonexisting ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$_SERVER [ 'QUERY_STRING' ] = helper :: getPasteId ();
2015-08-29 18:29:14 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'errormessage' ,
'content' => 'Paste does not exist'
),
$content ,
'outputs error correctly'
);
}
/**
* @ runInSeparateProcess
*/
public function testReadExpired ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$expiredPaste = helper :: getPaste ( array ( 'expire_date' => 1344803344 ));
$this -> _model -> create ( helper :: getPasteId (), $expiredPaste );
$_SERVER [ 'QUERY_STRING' ] = helper :: getPasteId ();
2015-08-29 18:29:14 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'errormessage' ,
'content' => 'Paste does not exist'
),
$content ,
'outputs error correctly'
);
}
/**
* @ runInSeparateProcess
*/
public function testReadBurn ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$burnPaste = helper :: getPaste ( array ( 'burnafterreading' => true ));
$this -> _model -> create ( helper :: getPasteId (), $burnPaste );
$_SERVER [ 'QUERY_STRING' ] = helper :: getPasteId ();
2015-08-29 18:29:14 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'cipherdata' ,
'content' => htmlspecialchars ( json_encode ( $burnPaste ), ENT_NOQUOTES )
),
$content ,
'outputs data correctly'
);
}
2015-09-01 20:33:07 +00:00
/**
* @ runInSeparateProcess
*/
public function testReadJson ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
$_SERVER [ 'QUERY_STRING' ] = helper :: getPasteId () . '&json' ;
2015-09-01 20:33:07 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 0 , $response [ 'status' ], 'outputs success status' );
2015-09-21 20:32:52 +00:00
$this -> assertEquals ( array ( helper :: getPaste ()), $response [ 'messages' ], 'outputs data correctly' );
2015-09-01 20:33:07 +00:00
}
2015-09-03 20:55:36 +00:00
/**
* @ runInSeparateProcess
*/
public function testReadInvalidJson ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$_SERVER [ 'QUERY_STRING' ] = helper :: getPasteId () . '&json' ;
2015-09-03 20:55:36 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 1 , $response [ 'status' ], 'outputs error status' );
}
2015-09-19 15:23:10 +00:00
/**
* @ runInSeparateProcess
*/
public function testReadOldSyntax ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$oldPaste = helper :: getPaste ( array ( 'syntaxcoloring' => true ));
2015-09-19 15:23:10 +00:00
unset ( $oldPaste [ 'meta' ][ 'formatter' ]);
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), $oldPaste );
$_SERVER [ 'QUERY_STRING' ] = helper :: getPasteId ();
2015-09-19 15:23:10 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$oldPaste [ 'meta' ][ 'formatter' ] = 'syntaxhighlighting' ;
$this -> assertTag (
array (
'id' => 'cipherdata' ,
'content' => htmlspecialchars ( json_encode ( $oldPaste ), ENT_NOQUOTES )
),
$content ,
'outputs data correctly'
);
}
/**
* @ runInSeparateProcess
*/
public function testReadOldFormat ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$oldPaste = helper :: getPaste ();
2015-09-19 15:23:10 +00:00
unset ( $oldPaste [ 'meta' ][ 'formatter' ]);
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), $oldPaste );
$_SERVER [ 'QUERY_STRING' ] = helper :: getPasteId ();
2015-09-19 15:23:10 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$oldPaste [ 'meta' ][ 'formatter' ] = 'plaintext' ;
$this -> assertTag (
array (
'id' => 'cipherdata' ,
'content' => htmlspecialchars ( json_encode ( $oldPaste ), ENT_NOQUOTES )
),
$content ,
'outputs data correctly'
);
}
2015-08-27 21:30:35 +00:00
/**
* @ runInSeparateProcess
*/
public function testDelete ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
$this -> assertTrue ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists before deleting data' );
$_GET [ 'pasteid' ] = helper :: getPasteId ();
$_GET [ 'deletetoken' ] = hash_hmac ( 'sha1' , helper :: getPasteId (), serversalt :: get ());
2015-08-27 21:30:35 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'status' ,
'content' => 'Paste was properly deleted'
),
$content ,
'outputs deleted status correctly'
);
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste successfully deleted' );
2015-08-27 21:30:35 +00:00
}
2015-08-29 18:29:14 +00:00
/**
* @ runInSeparateProcess
*/
public function testDeleteInvalidId ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
2015-08-29 18:29:14 +00:00
$_GET [ 'pasteid' ] = 'foo' ;
$_GET [ 'deletetoken' ] = 'bar' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'errormessage' ,
'content' => 'Invalid paste ID'
),
$content ,
'outputs delete error correctly'
);
2015-09-21 20:32:52 +00:00
$this -> assertTrue ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after failing to delete data' );
2015-08-29 18:29:14 +00:00
}
/**
* @ runInSeparateProcess
*/
public function testDeleteInexistantId ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$_GET [ 'pasteid' ] = helper :: getPasteId ();
2015-08-29 18:29:14 +00:00
$_GET [ 'deletetoken' ] = 'bar' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'errormessage' ,
'content' => 'Paste does not exist'
),
$content ,
'outputs delete error correctly'
);
}
/**
* @ runInSeparateProcess
*/
public function testDeleteInvalidToken ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
$_GET [ 'pasteid' ] = helper :: getPasteId ();
2015-08-29 18:29:14 +00:00
$_GET [ 'deletetoken' ] = 'bar' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'errormessage' ,
'content' => 'Wrong deletion token'
),
$content ,
'outputs delete error correctly'
);
2015-09-21 20:32:52 +00:00
$this -> assertTrue ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists after failing to delete data' );
2015-08-29 18:29:14 +00:00
}
2015-08-31 20:10:41 +00:00
/**
* @ runInSeparateProcess
*/
public function testDeleteBurnAfterReading ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$burnPaste = helper :: getPaste ( array ( 'burnafterreading' => true ));
$this -> _model -> create ( helper :: getPasteId (), $burnPaste );
$this -> assertTrue ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists before deleting data' );
$_GET [ 'pasteid' ] = helper :: getPasteId ();
2015-08-31 20:10:41 +00:00
$_GET [ 'deletetoken' ] = 'burnafterreading' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 0 , $response [ 'status' ], 'outputs status' );
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste successfully deleted' );
2015-08-31 20:10:41 +00:00
}
/**
* @ runInSeparateProcess
*/
public function testDeleteInvalidBurnAfterReading ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$this -> _model -> create ( helper :: getPasteId (), helper :: getPaste ());
$this -> assertTrue ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists before deleting data' );
$_GET [ 'pasteid' ] = helper :: getPasteId ();
2015-08-31 20:10:41 +00:00
$_GET [ 'deletetoken' ] = 'burnafterreading' ;
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$response = json_decode ( $content , true );
$this -> assertEquals ( 1 , $response [ 'status' ], 'outputs status' );
2015-09-21 20:32:52 +00:00
$this -> assertTrue ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste successfully deleted' );
2015-08-31 20:10:41 +00:00
}
2015-09-03 20:55:36 +00:00
/**
* @ runInSeparateProcess
*/
public function testDeleteExpired ()
{
$this -> reset ();
2015-09-21 20:32:52 +00:00
$expiredPaste = helper :: getPaste ( array ( 'expire_date' => 1000 ));
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste does not exist before being created' );
$this -> _model -> create ( helper :: getPasteId (), $expiredPaste );
$this -> assertTrue ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste exists before deleting data' );
$_GET [ 'pasteid' ] = helper :: getPasteId ();
2015-09-06 17:21:17 +00:00
$_GET [ 'deletetoken' ] = 'does not matter in this context, but has to be set' ;
2015-09-03 20:55:36 +00:00
ob_start ();
new zerobin ;
$content = ob_get_contents ();
$this -> assertTag (
array (
'id' => 'errormessage' ,
'content' => 'Paste does not exist'
),
$content ,
'outputs error correctly'
);
2015-09-21 20:32:52 +00:00
$this -> assertFalse ( $this -> _model -> exists ( helper :: getPasteId ()), 'paste successfully deleted' );
2015-09-03 20:55:36 +00:00
}
2015-08-27 21:30:35 +00:00
}