zerobin.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. <?php
  2. class zerobinTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_model;
  5. public function setUp()
  6. {
  7. /* Setup Routine */
  8. $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
  9. serversalt::setPath(PATH . 'data');
  10. $this->reset();
  11. }
  12. public function tearDown()
  13. {
  14. /* Tear Down Routine */
  15. }
  16. public function reset()
  17. {
  18. $_POST = array();
  19. $_GET = array();
  20. $_SERVER = array();
  21. if ($this->_model->exists(helper::getPasteId()))
  22. $this->_model->delete(helper::getPasteId());
  23. helper::confRestore();
  24. }
  25. /**
  26. * @runInSeparateProcess
  27. */
  28. public function testView()
  29. {
  30. $this->reset();
  31. ob_start();
  32. new zerobin;
  33. $content = ob_get_contents();
  34. $this->assertTag(
  35. array(
  36. 'tag' => 'title',
  37. 'content' => 'ZeroBin'
  38. ),
  39. $content,
  40. 'outputs title correctly'
  41. );
  42. }
  43. /**
  44. * @runInSeparateProcess
  45. */
  46. public function testViewLanguageSelection()
  47. {
  48. $this->reset();
  49. $options = parse_ini_file(CONF, true);
  50. $options['main']['languageselection'] = true;
  51. helper::confBackup();
  52. helper::createIniFile(CONF, $options);
  53. $_COOKIE['lang'] = 'de';
  54. ob_start();
  55. new zerobin;
  56. $content = ob_get_contents();
  57. $this->assertTag(
  58. array(
  59. 'tag' => 'title',
  60. 'content' => 'ZeroBin'
  61. ),
  62. $content,
  63. 'outputs title correctly'
  64. );
  65. }
  66. /**
  67. * @runInSeparateProcess
  68. */
  69. public function testHtaccess()
  70. {
  71. $this->reset();
  72. $dirs = array('cfg', 'lib');
  73. foreach ($dirs as $dir) {
  74. $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
  75. @unlink($file);
  76. }
  77. ob_start();
  78. new zerobin;
  79. $content = ob_get_contents();
  80. foreach ($dirs as $dir) {
  81. $file = PATH . $dir . DIRECTORY_SEPARATOR . '.htaccess';
  82. $this->assertFileExists(
  83. $file,
  84. "$dir htaccess recreated"
  85. );
  86. }
  87. }
  88. /**
  89. * @expectedException Exception
  90. * @expectedExceptionCode 2
  91. */
  92. public function testConf()
  93. {
  94. $this->reset();
  95. helper::confBackup();
  96. file_put_contents(CONF, '');
  97. ob_start();
  98. new zerobin;
  99. $content = ob_get_contents();
  100. }
  101. /**
  102. * @runInSeparateProcess
  103. */
  104. public function testCreate()
  105. {
  106. $this->reset();
  107. $_POST = helper::getPaste();
  108. $_SERVER['REMOTE_ADDR'] = '::1';
  109. ob_start();
  110. new zerobin;
  111. $content = ob_get_contents();
  112. $response = json_decode($content, true);
  113. $this->assertEquals(0, $response['status'], 'outputs status');
  114. $this->assertEquals(
  115. hash_hmac('sha1', $response['id'], serversalt::get()),
  116. $response['deletetoken'],
  117. 'outputs valid delete token'
  118. );
  119. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  120. }
  121. /**
  122. * @runInSeparateProcess
  123. */
  124. public function testCreateInvalidTimelimit()
  125. {
  126. $this->reset();
  127. $_POST = helper::getPaste();
  128. $_SERVER['REMOTE_ADDR'] = '::1';
  129. ob_start();
  130. new zerobin;
  131. $content = ob_get_contents();
  132. $response = json_decode($content, true);
  133. $this->assertEquals(1, $response['status'], 'outputs error status');
  134. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  135. }
  136. /**
  137. * @runInSeparateProcess
  138. */
  139. public function testCreateInvalidSize()
  140. {
  141. $this->reset();
  142. $options = parse_ini_file(CONF, true);
  143. $options['main']['sizelimit'] = 10;
  144. $options['traffic']['limit'] = 0;
  145. helper::confBackup();
  146. helper::createIniFile(CONF, $options);
  147. $_POST = helper::getPaste();
  148. $_SERVER['REMOTE_ADDR'] = '::1';
  149. ob_start();
  150. new zerobin;
  151. $content = ob_get_contents();
  152. $response = json_decode($content, true);
  153. $this->assertEquals(1, $response['status'], 'outputs error status');
  154. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  155. }
  156. /**
  157. * @runInSeparateProcess
  158. */
  159. public function testCreateProxyHeader()
  160. {
  161. $this->reset();
  162. $options = parse_ini_file(CONF, true);
  163. $options['traffic']['header'] = 'X_FORWARDED_FOR';
  164. helper::confBackup();
  165. helper::createIniFile(CONF, $options);
  166. $_POST = helper::getPaste();
  167. $_SERVER['HTTP_X_FORWARDED_FOR'] = '::1';
  168. ob_start();
  169. new zerobin;
  170. $content = ob_get_contents();
  171. $response = json_decode($content, true);
  172. $this->assertEquals(1, $response['status'], 'outputs error status');
  173. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  174. }
  175. /**
  176. * @runInSeparateProcess
  177. */
  178. public function testCreateDuplicateId()
  179. {
  180. $this->reset();
  181. $options = parse_ini_file(CONF, true);
  182. $options['traffic']['limit'] = 0;
  183. helper::confBackup();
  184. helper::createIniFile(CONF, $options);
  185. $this->_model->create(helper::getPasteId(), helper::getPaste());
  186. $_POST = helper::getPaste();
  187. $_SERVER['REMOTE_ADDR'] = '::1';
  188. ob_start();
  189. new zerobin;
  190. $content = ob_get_contents();
  191. $response = json_decode($content, true);
  192. $this->assertEquals(1, $response['status'], 'outputs error status');
  193. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  194. }
  195. /**
  196. * @runInSeparateProcess
  197. */
  198. public function testCreateValidExpire()
  199. {
  200. $this->reset();
  201. $options = parse_ini_file(CONF, true);
  202. $options['traffic']['limit'] = 0;
  203. helper::confBackup();
  204. helper::createIniFile(CONF, $options);
  205. $_POST = helper::getPaste();
  206. $_POST['expire'] = '5min';
  207. $_POST['formatter'] = 'foo';
  208. $_SERVER['REMOTE_ADDR'] = '::1';
  209. ob_start();
  210. new zerobin;
  211. $content = ob_get_contents();
  212. $response = json_decode($content, true);
  213. $this->assertEquals(0, $response['status'], 'outputs status');
  214. $this->assertEquals(
  215. hash_hmac('sha1', $response['id'], serversalt::get()),
  216. $response['deletetoken'],
  217. 'outputs valid delete token'
  218. );
  219. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  220. }
  221. /**
  222. * @runInSeparateProcess
  223. */
  224. public function testCreateInvalidExpire()
  225. {
  226. $this->reset();
  227. $options = parse_ini_file(CONF, true);
  228. $options['traffic']['limit'] = 0;
  229. helper::confBackup();
  230. helper::createIniFile(CONF, $options);
  231. $_POST = helper::getPaste();
  232. $_POST['expire'] = 'foo';
  233. $_SERVER['REMOTE_ADDR'] = '::1';
  234. ob_start();
  235. new zerobin;
  236. $content = ob_get_contents();
  237. $response = json_decode($content, true);
  238. $this->assertEquals(0, $response['status'], 'outputs status');
  239. $this->assertEquals(
  240. hash_hmac('sha1', $response['id'], serversalt::get()),
  241. $response['deletetoken'],
  242. 'outputs valid delete token'
  243. );
  244. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  245. }
  246. /**
  247. * @runInSeparateProcess
  248. */
  249. public function testCreateInvalidBurn()
  250. {
  251. $this->reset();
  252. $options = parse_ini_file(CONF, true);
  253. $options['traffic']['limit'] = 0;
  254. helper::confBackup();
  255. helper::createIniFile(CONF, $options);
  256. $_POST = helper::getPaste();
  257. $_POST['burnafterreading'] = 'neither 1 nor 0';
  258. $_SERVER['REMOTE_ADDR'] = '::1';
  259. ob_start();
  260. new zerobin;
  261. $content = ob_get_contents();
  262. $response = json_decode($content, true);
  263. $this->assertEquals(1, $response['status'], 'outputs error status');
  264. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  265. }
  266. /**
  267. * @runInSeparateProcess
  268. */
  269. public function testCreateInvalidOpenDiscussion()
  270. {
  271. $this->reset();
  272. $options = parse_ini_file(CONF, true);
  273. $options['traffic']['limit'] = 0;
  274. helper::confBackup();
  275. helper::createIniFile(CONF, $options);
  276. $_POST = helper::getPaste();
  277. $_POST['opendiscussion'] = 'neither 1 nor 0';
  278. $_SERVER['REMOTE_ADDR'] = '::1';
  279. ob_start();
  280. new zerobin;
  281. $content = ob_get_contents();
  282. $response = json_decode($content, true);
  283. $this->assertEquals(1, $response['status'], 'outputs error status');
  284. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  285. }
  286. /**
  287. * @runInSeparateProcess
  288. */
  289. public function testCreateAttachment()
  290. {
  291. $this->reset();
  292. $options = parse_ini_file(CONF, true);
  293. $options['traffic']['limit'] = 0;
  294. $options['main']['fileupload'] = true;
  295. helper::confBackup();
  296. helper::createIniFile(CONF, $options);
  297. $_POST = helper::getPaste();
  298. $_POST['attachment'] = $_POST['meta']['attachment'];
  299. $_POST['attachmentname'] = $_POST['meta']['attachmentname'];
  300. $_SERVER['REMOTE_ADDR'] = '::1';
  301. ob_start();
  302. new zerobin;
  303. $content = ob_get_contents();
  304. $response = json_decode($content, true);
  305. $this->assertEquals(0, $response['status'], 'outputs status');
  306. $this->assertEquals(
  307. hash_hmac('sha1', $response['id'], serversalt::get()),
  308. $response['deletetoken'],
  309. 'outputs valid delete token'
  310. );
  311. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  312. }
  313. /**
  314. * @runInSeparateProcess
  315. */
  316. public function testCreateValidNick()
  317. {
  318. $this->reset();
  319. $options = parse_ini_file(CONF, true);
  320. $options['traffic']['limit'] = 0;
  321. helper::confBackup();
  322. helper::createIniFile(CONF, $options);
  323. $_POST = helper::getPaste();
  324. $_POST['nickname'] = helper::getComment()['meta']['nickname'];
  325. $_SERVER['REMOTE_ADDR'] = '::1';
  326. ob_start();
  327. new zerobin;
  328. $content = ob_get_contents();
  329. $response = json_decode($content, true);
  330. $this->assertEquals(0, $response['status'], 'outputs status');
  331. $this->assertEquals(
  332. hash_hmac('sha1', $response['id'], serversalt::get()),
  333. $response['deletetoken'],
  334. 'outputs valid delete token'
  335. );
  336. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  337. }
  338. /**
  339. * @runInSeparateProcess
  340. */
  341. public function testCreateInvalidNick()
  342. {
  343. $this->reset();
  344. $options = parse_ini_file(CONF, true);
  345. $options['traffic']['limit'] = 0;
  346. helper::confBackup();
  347. helper::createIniFile(CONF, $options);
  348. $_POST = helper::getPaste();
  349. $_POST['nickname'] = 'foo';
  350. $_SERVER['REMOTE_ADDR'] = '::1';
  351. ob_start();
  352. new zerobin;
  353. $content = ob_get_contents();
  354. $response = json_decode($content, true);
  355. $this->assertEquals(1, $response['status'], 'outputs error status');
  356. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  357. }
  358. /**
  359. * @runInSeparateProcess
  360. */
  361. public function testCreateComment()
  362. {
  363. $this->reset();
  364. $options = parse_ini_file(CONF, true);
  365. $options['traffic']['limit'] = 0;
  366. helper::confBackup();
  367. helper::createIniFile(CONF, $options);
  368. $_POST = helper::getComment();
  369. $_POST['pasteid'] = helper::getPasteId();
  370. $_POST['parentid'] = helper::getPasteId();
  371. $_SERVER['REMOTE_ADDR'] = '::1';
  372. $this->_model->create(helper::getPasteId(), helper::getPaste());
  373. ob_start();
  374. new zerobin;
  375. $content = ob_get_contents();
  376. $response = json_decode($content, true);
  377. $this->assertEquals(0, $response['status'], 'outputs status');
  378. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), $response['id']), 'paste exists after posting data');
  379. }
  380. /**
  381. * @runInSeparateProcess
  382. */
  383. public function testCreateInvalidComment()
  384. {
  385. $this->reset();
  386. $options = parse_ini_file(CONF, true);
  387. $options['traffic']['limit'] = 0;
  388. helper::confBackup();
  389. helper::createIniFile(CONF, $options);
  390. $_POST = helper::getComment();
  391. $_POST['pasteid'] = helper::getPasteId();
  392. $_POST['parentid'] = 'foo';
  393. $_SERVER['REMOTE_ADDR'] = '::1';
  394. $this->_model->create(helper::getPasteId(), helper::getPaste());
  395. ob_start();
  396. new zerobin;
  397. $content = ob_get_contents();
  398. $response = json_decode($content, true);
  399. $this->assertEquals(1, $response['status'], 'outputs error status');
  400. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  401. }
  402. /**
  403. * @runInSeparateProcess
  404. */
  405. public function testCreateCommentDiscussionDisabled()
  406. {
  407. $this->reset();
  408. $options = parse_ini_file(CONF, true);
  409. $options['traffic']['limit'] = 0;
  410. helper::confBackup();
  411. helper::createIniFile(CONF, $options);
  412. $_POST = helper::getComment();
  413. $_POST['pasteid'] = helper::getPasteId();
  414. $_POST['parentid'] = helper::getPasteId();
  415. $_SERVER['REMOTE_ADDR'] = '::1';
  416. $paste = helper::getPaste(array('opendiscussion' => false));
  417. $this->_model->create(helper::getPasteId(), $paste);
  418. ob_start();
  419. new zerobin;
  420. $content = ob_get_contents();
  421. $response = json_decode($content, true);
  422. $this->assertEquals(1, $response['status'], 'outputs error status');
  423. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  424. }
  425. /**
  426. * @runInSeparateProcess
  427. */
  428. public function testCreateCommentInvalidPaste()
  429. {
  430. $this->reset();
  431. $options = parse_ini_file(CONF, true);
  432. $options['traffic']['limit'] = 0;
  433. helper::confBackup();
  434. helper::createIniFile(CONF, $options);
  435. $_POST = helper::getComment();
  436. $_POST['pasteid'] = helper::getPasteId();
  437. $_POST['parentid'] = helper::getPasteId();
  438. $_SERVER['REMOTE_ADDR'] = '::1';
  439. ob_start();
  440. new zerobin;
  441. $content = ob_get_contents();
  442. $response = json_decode($content, true);
  443. $this->assertEquals(1, $response['status'], 'outputs error status');
  444. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  445. }
  446. /**
  447. * @runInSeparateProcess
  448. */
  449. public function testCreateDuplicateComment()
  450. {
  451. $this->reset();
  452. $options = parse_ini_file(CONF, true);
  453. $options['traffic']['limit'] = 0;
  454. helper::confBackup();
  455. helper::createIniFile(CONF, $options);
  456. $this->_model->create(helper::getPasteId(), helper::getPaste());
  457. $this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment());
  458. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists before posting data');
  459. $_POST = helper::getComment();
  460. $_POST['pasteid'] = helper::getPasteId();
  461. $_POST['parentid'] = helper::getPasteId();
  462. $_SERVER['REMOTE_ADDR'] = '::1';
  463. ob_start();
  464. new zerobin;
  465. $content = ob_get_contents();
  466. $response = json_decode($content, true);
  467. $this->assertEquals(1, $response['status'], 'outputs error status');
  468. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  469. }
  470. /**
  471. * @runInSeparateProcess
  472. */
  473. public function testRead()
  474. {
  475. $this->reset();
  476. $this->_model->create(helper::getPasteId(), helper::getPaste());
  477. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  478. ob_start();
  479. new zerobin;
  480. $content = ob_get_contents();
  481. $this->assertTag(
  482. array(
  483. 'id' => 'cipherdata',
  484. 'content' => htmlspecialchars(json_encode(helper::getPaste()), ENT_NOQUOTES)
  485. ),
  486. $content,
  487. 'outputs data correctly'
  488. );
  489. }
  490. /**
  491. * @runInSeparateProcess
  492. */
  493. public function testReadInvalidId()
  494. {
  495. $this->reset();
  496. $_SERVER['QUERY_STRING'] = 'foo';
  497. ob_start();
  498. new zerobin;
  499. $content = ob_get_contents();
  500. $this->assertTag(
  501. array(
  502. 'id' => 'errormessage',
  503. 'content' => 'Invalid paste ID'
  504. ),
  505. $content,
  506. 'outputs error correctly'
  507. );
  508. }
  509. /**
  510. * @runInSeparateProcess
  511. */
  512. public function testReadNonexisting()
  513. {
  514. $this->reset();
  515. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  516. ob_start();
  517. new zerobin;
  518. $content = ob_get_contents();
  519. $this->assertTag(
  520. array(
  521. 'id' => 'errormessage',
  522. 'content' => 'Paste does not exist'
  523. ),
  524. $content,
  525. 'outputs error correctly'
  526. );
  527. }
  528. /**
  529. * @runInSeparateProcess
  530. */
  531. public function testReadExpired()
  532. {
  533. $this->reset();
  534. $expiredPaste = helper::getPaste(array('expire_date' => 1344803344));
  535. $this->_model->create(helper::getPasteId(), $expiredPaste);
  536. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  537. ob_start();
  538. new zerobin;
  539. $content = ob_get_contents();
  540. $this->assertTag(
  541. array(
  542. 'id' => 'errormessage',
  543. 'content' => 'Paste does not exist'
  544. ),
  545. $content,
  546. 'outputs error correctly'
  547. );
  548. }
  549. /**
  550. * @runInSeparateProcess
  551. */
  552. public function testReadBurn()
  553. {
  554. $this->reset();
  555. $burnPaste = helper::getPaste(array('burnafterreading' => true));
  556. $this->_model->create(helper::getPasteId(), $burnPaste);
  557. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  558. ob_start();
  559. new zerobin;
  560. $content = ob_get_contents();
  561. $this->assertTag(
  562. array(
  563. 'id' => 'cipherdata',
  564. 'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
  565. ),
  566. $content,
  567. 'outputs data correctly'
  568. );
  569. }
  570. /**
  571. * @runInSeparateProcess
  572. */
  573. public function testReadJson()
  574. {
  575. $this->reset();
  576. $this->_model->create(helper::getPasteId(), helper::getPaste());
  577. $_SERVER['QUERY_STRING'] = helper::getPasteId() . '&json';
  578. ob_start();
  579. new zerobin;
  580. $content = ob_get_contents();
  581. $response = json_decode($content, true);
  582. $this->assertEquals(0, $response['status'], 'outputs success status');
  583. $this->assertEquals(array(helper::getPaste()), $response['messages'], 'outputs data correctly');
  584. }
  585. /**
  586. * @runInSeparateProcess
  587. */
  588. public function testReadInvalidJson()
  589. {
  590. $this->reset();
  591. $_SERVER['QUERY_STRING'] = helper::getPasteId() . '&json';
  592. ob_start();
  593. new zerobin;
  594. $content = ob_get_contents();
  595. $response = json_decode($content, true);
  596. $this->assertEquals(1, $response['status'], 'outputs error status');
  597. }
  598. /**
  599. * @runInSeparateProcess
  600. */
  601. public function testReadOldSyntax()
  602. {
  603. $this->reset();
  604. $oldPaste = helper::getPaste(array('syntaxcoloring' => true));
  605. unset($oldPaste['meta']['formatter']);
  606. $this->_model->create(helper::getPasteId(), $oldPaste);
  607. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  608. ob_start();
  609. new zerobin;
  610. $content = ob_get_contents();
  611. $oldPaste['meta']['formatter'] = 'syntaxhighlighting';
  612. $this->assertTag(
  613. array(
  614. 'id' => 'cipherdata',
  615. 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
  616. ),
  617. $content,
  618. 'outputs data correctly'
  619. );
  620. }
  621. /**
  622. * @runInSeparateProcess
  623. */
  624. public function testReadOldFormat()
  625. {
  626. $this->reset();
  627. $oldPaste = helper::getPaste();
  628. unset($oldPaste['meta']['formatter']);
  629. $this->_model->create(helper::getPasteId(), $oldPaste);
  630. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  631. ob_start();
  632. new zerobin;
  633. $content = ob_get_contents();
  634. $oldPaste['meta']['formatter'] = 'plaintext';
  635. $this->assertTag(
  636. array(
  637. 'id' => 'cipherdata',
  638. 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
  639. ),
  640. $content,
  641. 'outputs data correctly'
  642. );
  643. }
  644. /**
  645. * @runInSeparateProcess
  646. */
  647. public function testDelete()
  648. {
  649. $this->reset();
  650. $this->_model->create(helper::getPasteId(), helper::getPaste());
  651. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  652. $_GET['pasteid'] = helper::getPasteId();
  653. $_GET['deletetoken'] = hash_hmac('sha1', helper::getPasteId(), serversalt::get());
  654. ob_start();
  655. new zerobin;
  656. $content = ob_get_contents();
  657. $this->assertTag(
  658. array(
  659. 'id' => 'status',
  660. 'content' => 'Paste was properly deleted'
  661. ),
  662. $content,
  663. 'outputs deleted status correctly'
  664. );
  665. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  666. }
  667. /**
  668. * @runInSeparateProcess
  669. */
  670. public function testDeleteInvalidId()
  671. {
  672. $this->reset();
  673. $this->_model->create(helper::getPasteId(), helper::getPaste());
  674. $_GET['pasteid'] = 'foo';
  675. $_GET['deletetoken'] = 'bar';
  676. ob_start();
  677. new zerobin;
  678. $content = ob_get_contents();
  679. $this->assertTag(
  680. array(
  681. 'id' => 'errormessage',
  682. 'content' => 'Invalid paste ID'
  683. ),
  684. $content,
  685. 'outputs delete error correctly'
  686. );
  687. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
  688. }
  689. /**
  690. * @runInSeparateProcess
  691. */
  692. public function testDeleteInexistantId()
  693. {
  694. $this->reset();
  695. $_GET['pasteid'] = helper::getPasteId();
  696. $_GET['deletetoken'] = 'bar';
  697. ob_start();
  698. new zerobin;
  699. $content = ob_get_contents();
  700. $this->assertTag(
  701. array(
  702. 'id' => 'errormessage',
  703. 'content' => 'Paste does not exist'
  704. ),
  705. $content,
  706. 'outputs delete error correctly'
  707. );
  708. }
  709. /**
  710. * @runInSeparateProcess
  711. */
  712. public function testDeleteInvalidToken()
  713. {
  714. $this->reset();
  715. $this->_model->create(helper::getPasteId(), helper::getPaste());
  716. $_GET['pasteid'] = helper::getPasteId();
  717. $_GET['deletetoken'] = 'bar';
  718. ob_start();
  719. new zerobin;
  720. $content = ob_get_contents();
  721. $this->assertTag(
  722. array(
  723. 'id' => 'errormessage',
  724. 'content' => 'Wrong deletion token'
  725. ),
  726. $content,
  727. 'outputs delete error correctly'
  728. );
  729. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
  730. }
  731. /**
  732. * @runInSeparateProcess
  733. */
  734. public function testDeleteBurnAfterReading()
  735. {
  736. $this->reset();
  737. $burnPaste = helper::getPaste(array('burnafterreading' => true));
  738. $this->_model->create(helper::getPasteId(), $burnPaste);
  739. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  740. $_GET['pasteid'] = helper::getPasteId();
  741. $_GET['deletetoken'] = 'burnafterreading';
  742. ob_start();
  743. new zerobin;
  744. $content = ob_get_contents();
  745. $response = json_decode($content, true);
  746. $this->assertEquals(0, $response['status'], 'outputs status');
  747. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  748. }
  749. /**
  750. * @runInSeparateProcess
  751. */
  752. public function testDeleteInvalidBurnAfterReading()
  753. {
  754. $this->reset();
  755. $this->_model->create(helper::getPasteId(), helper::getPaste());
  756. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  757. $_GET['pasteid'] = helper::getPasteId();
  758. $_GET['deletetoken'] = 'burnafterreading';
  759. ob_start();
  760. new zerobin;
  761. $content = ob_get_contents();
  762. $response = json_decode($content, true);
  763. $this->assertEquals(1, $response['status'], 'outputs status');
  764. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  765. }
  766. /**
  767. * @runInSeparateProcess
  768. */
  769. public function testDeleteExpired()
  770. {
  771. $this->reset();
  772. $expiredPaste = helper::getPaste(array('expire_date' => 1000));
  773. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exist before being created');
  774. $this->_model->create(helper::getPasteId(), $expiredPaste);
  775. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  776. $_GET['pasteid'] = helper::getPasteId();
  777. $_GET['deletetoken'] = 'does not matter in this context, but has to be set';
  778. ob_start();
  779. new zerobin;
  780. $content = ob_get_contents();
  781. $this->assertTag(
  782. array(
  783. 'id' => 'errormessage',
  784. 'content' => 'Paste does not exist'
  785. ),
  786. $content,
  787. 'outputs error correctly'
  788. );
  789. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  790. }
  791. }