zerobin.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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::getPasteWithAttachment();
  298. $_SERVER['REMOTE_ADDR'] = '::1';
  299. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exists before posting data');
  300. ob_start();
  301. new zerobin;
  302. $content = ob_get_contents();
  303. $response = json_decode($content, true);
  304. $this->assertEquals(0, $response['status'], 'outputs status');
  305. $this->assertEquals(
  306. hash_hmac('sha1', $response['id'], serversalt::get()),
  307. $response['deletetoken'],
  308. 'outputs valid delete token'
  309. );
  310. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  311. $original = json_decode(json_encode($_POST));
  312. $stored = $this->_model->read($response['id']);
  313. foreach (array('data', 'attachment', 'attachmentname') as $key) {
  314. $this->assertEquals($original->$key, $stored->$key);
  315. }
  316. }
  317. /**
  318. * @runInSeparateProcess
  319. */
  320. public function testCreateValidNick()
  321. {
  322. $this->reset();
  323. $options = parse_ini_file(CONF, true);
  324. $options['traffic']['limit'] = 0;
  325. helper::confBackup();
  326. helper::createIniFile(CONF, $options);
  327. $_POST = helper::getPaste();
  328. $_POST['nickname'] = helper::getComment()['meta']['nickname'];
  329. $_SERVER['REMOTE_ADDR'] = '::1';
  330. ob_start();
  331. new zerobin;
  332. $content = ob_get_contents();
  333. $response = json_decode($content, true);
  334. $this->assertEquals(0, $response['status'], 'outputs status');
  335. $this->assertEquals(
  336. hash_hmac('sha1', $response['id'], serversalt::get()),
  337. $response['deletetoken'],
  338. 'outputs valid delete token'
  339. );
  340. $this->assertTrue($this->_model->exists($response['id']), 'paste exists after posting data');
  341. }
  342. /**
  343. * @runInSeparateProcess
  344. */
  345. public function testCreateInvalidNick()
  346. {
  347. $this->reset();
  348. $options = parse_ini_file(CONF, true);
  349. $options['traffic']['limit'] = 0;
  350. helper::confBackup();
  351. helper::createIniFile(CONF, $options);
  352. $_POST = helper::getPaste();
  353. $_POST['nickname'] = 'foo';
  354. $_SERVER['REMOTE_ADDR'] = '::1';
  355. ob_start();
  356. new zerobin;
  357. $content = ob_get_contents();
  358. $response = json_decode($content, true);
  359. $this->assertEquals(1, $response['status'], 'outputs error status');
  360. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste exists after posting data');
  361. }
  362. /**
  363. * @runInSeparateProcess
  364. */
  365. public function testCreateComment()
  366. {
  367. $this->reset();
  368. $options = parse_ini_file(CONF, true);
  369. $options['traffic']['limit'] = 0;
  370. helper::confBackup();
  371. helper::createIniFile(CONF, $options);
  372. $_POST = helper::getComment();
  373. $_POST['pasteid'] = helper::getPasteId();
  374. $_POST['parentid'] = helper::getPasteId();
  375. $_SERVER['REMOTE_ADDR'] = '::1';
  376. $this->_model->create(helper::getPasteId(), helper::getPaste());
  377. ob_start();
  378. new zerobin;
  379. $content = ob_get_contents();
  380. $response = json_decode($content, true);
  381. $this->assertEquals(0, $response['status'], 'outputs status');
  382. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), $response['id']), 'paste exists after posting data');
  383. }
  384. /**
  385. * @runInSeparateProcess
  386. */
  387. public function testCreateInvalidComment()
  388. {
  389. $this->reset();
  390. $options = parse_ini_file(CONF, true);
  391. $options['traffic']['limit'] = 0;
  392. helper::confBackup();
  393. helper::createIniFile(CONF, $options);
  394. $_POST = helper::getComment();
  395. $_POST['pasteid'] = helper::getPasteId();
  396. $_POST['parentid'] = 'foo';
  397. $_SERVER['REMOTE_ADDR'] = '::1';
  398. $this->_model->create(helper::getPasteId(), helper::getPaste());
  399. ob_start();
  400. new zerobin;
  401. $content = ob_get_contents();
  402. $response = json_decode($content, true);
  403. $this->assertEquals(1, $response['status'], 'outputs error status');
  404. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  405. }
  406. /**
  407. * @runInSeparateProcess
  408. */
  409. public function testCreateCommentDiscussionDisabled()
  410. {
  411. $this->reset();
  412. $options = parse_ini_file(CONF, true);
  413. $options['traffic']['limit'] = 0;
  414. helper::confBackup();
  415. helper::createIniFile(CONF, $options);
  416. $_POST = helper::getComment();
  417. $_POST['pasteid'] = helper::getPasteId();
  418. $_POST['parentid'] = helper::getPasteId();
  419. $_SERVER['REMOTE_ADDR'] = '::1';
  420. $paste = helper::getPaste(array('opendiscussion' => false));
  421. $this->_model->create(helper::getPasteId(), $paste);
  422. ob_start();
  423. new zerobin;
  424. $content = ob_get_contents();
  425. $response = json_decode($content, true);
  426. $this->assertEquals(1, $response['status'], 'outputs error status');
  427. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  428. }
  429. /**
  430. * @runInSeparateProcess
  431. */
  432. public function testCreateCommentInvalidPaste()
  433. {
  434. $this->reset();
  435. $options = parse_ini_file(CONF, true);
  436. $options['traffic']['limit'] = 0;
  437. helper::confBackup();
  438. helper::createIniFile(CONF, $options);
  439. $_POST = helper::getComment();
  440. $_POST['pasteid'] = helper::getPasteId();
  441. $_POST['parentid'] = helper::getPasteId();
  442. $_SERVER['REMOTE_ADDR'] = '::1';
  443. ob_start();
  444. new zerobin;
  445. $content = ob_get_contents();
  446. $response = json_decode($content, true);
  447. $this->assertEquals(1, $response['status'], 'outputs error status');
  448. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  449. }
  450. /**
  451. * @runInSeparateProcess
  452. */
  453. public function testCreateDuplicateComment()
  454. {
  455. $this->reset();
  456. $options = parse_ini_file(CONF, true);
  457. $options['traffic']['limit'] = 0;
  458. helper::confBackup();
  459. helper::createIniFile(CONF, $options);
  460. $this->_model->create(helper::getPasteId(), helper::getPaste());
  461. $this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment());
  462. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists before posting data');
  463. $_POST = helper::getComment();
  464. $_POST['pasteid'] = helper::getPasteId();
  465. $_POST['parentid'] = helper::getPasteId();
  466. $_SERVER['REMOTE_ADDR'] = '::1';
  467. ob_start();
  468. new zerobin;
  469. $content = ob_get_contents();
  470. $response = json_decode($content, true);
  471. $this->assertEquals(1, $response['status'], 'outputs error status');
  472. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'paste exists after posting data');
  473. }
  474. /**
  475. * @runInSeparateProcess
  476. */
  477. public function testRead()
  478. {
  479. $this->reset();
  480. $this->_model->create(helper::getPasteId(), helper::getPaste());
  481. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  482. ob_start();
  483. new zerobin;
  484. $content = ob_get_contents();
  485. $this->assertTag(
  486. array(
  487. 'id' => 'cipherdata',
  488. 'content' => htmlspecialchars(json_encode(helper::getPaste()), ENT_NOQUOTES)
  489. ),
  490. $content,
  491. 'outputs data correctly'
  492. );
  493. }
  494. /**
  495. * @runInSeparateProcess
  496. */
  497. public function testReadInvalidId()
  498. {
  499. $this->reset();
  500. $_SERVER['QUERY_STRING'] = 'foo';
  501. ob_start();
  502. new zerobin;
  503. $content = ob_get_contents();
  504. $this->assertTag(
  505. array(
  506. 'id' => 'errormessage',
  507. 'content' => 'Invalid paste ID'
  508. ),
  509. $content,
  510. 'outputs error correctly'
  511. );
  512. }
  513. /**
  514. * @runInSeparateProcess
  515. */
  516. public function testReadNonexisting()
  517. {
  518. $this->reset();
  519. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  520. ob_start();
  521. new zerobin;
  522. $content = ob_get_contents();
  523. $this->assertTag(
  524. array(
  525. 'id' => 'errormessage',
  526. 'content' => 'Paste does not exist'
  527. ),
  528. $content,
  529. 'outputs error correctly'
  530. );
  531. }
  532. /**
  533. * @runInSeparateProcess
  534. */
  535. public function testReadExpired()
  536. {
  537. $this->reset();
  538. $expiredPaste = helper::getPaste(array('expire_date' => 1344803344));
  539. $this->_model->create(helper::getPasteId(), $expiredPaste);
  540. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  541. ob_start();
  542. new zerobin;
  543. $content = ob_get_contents();
  544. $this->assertTag(
  545. array(
  546. 'id' => 'errormessage',
  547. 'content' => 'Paste does not exist'
  548. ),
  549. $content,
  550. 'outputs error correctly'
  551. );
  552. }
  553. /**
  554. * @runInSeparateProcess
  555. */
  556. public function testReadBurn()
  557. {
  558. $this->reset();
  559. $burnPaste = helper::getPaste(array('burnafterreading' => true));
  560. $this->_model->create(helper::getPasteId(), $burnPaste);
  561. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  562. ob_start();
  563. new zerobin;
  564. $content = ob_get_contents();
  565. $this->assertTag(
  566. array(
  567. 'id' => 'cipherdata',
  568. 'content' => htmlspecialchars(json_encode($burnPaste), ENT_NOQUOTES)
  569. ),
  570. $content,
  571. 'outputs data correctly'
  572. );
  573. }
  574. /**
  575. * @runInSeparateProcess
  576. */
  577. public function testReadJson()
  578. {
  579. $this->reset();
  580. $this->_model->create(helper::getPasteId(), helper::getPaste());
  581. $_SERVER['QUERY_STRING'] = helper::getPasteId() . '&json';
  582. ob_start();
  583. new zerobin;
  584. $content = ob_get_contents();
  585. $response = json_decode($content, true);
  586. $this->assertEquals(0, $response['status'], 'outputs success status');
  587. $this->assertEquals(array(helper::getPaste()), $response['messages'], 'outputs data correctly');
  588. }
  589. /**
  590. * @runInSeparateProcess
  591. */
  592. public function testReadInvalidJson()
  593. {
  594. $this->reset();
  595. $_SERVER['QUERY_STRING'] = helper::getPasteId() . '&json';
  596. ob_start();
  597. new zerobin;
  598. $content = ob_get_contents();
  599. $response = json_decode($content, true);
  600. $this->assertEquals(1, $response['status'], 'outputs error status');
  601. }
  602. /**
  603. * @runInSeparateProcess
  604. */
  605. public function testReadOldSyntax()
  606. {
  607. $this->reset();
  608. $oldPaste = helper::getPaste(array('syntaxcoloring' => true));
  609. unset($oldPaste['meta']['formatter']);
  610. $this->_model->create(helper::getPasteId(), $oldPaste);
  611. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  612. ob_start();
  613. new zerobin;
  614. $content = ob_get_contents();
  615. $oldPaste['meta']['formatter'] = 'syntaxhighlighting';
  616. $this->assertTag(
  617. array(
  618. 'id' => 'cipherdata',
  619. 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
  620. ),
  621. $content,
  622. 'outputs data correctly'
  623. );
  624. }
  625. /**
  626. * @runInSeparateProcess
  627. */
  628. public function testReadOldFormat()
  629. {
  630. $this->reset();
  631. $oldPaste = helper::getPaste();
  632. unset($oldPaste['meta']['formatter']);
  633. $this->_model->create(helper::getPasteId(), $oldPaste);
  634. $_SERVER['QUERY_STRING'] = helper::getPasteId();
  635. ob_start();
  636. new zerobin;
  637. $content = ob_get_contents();
  638. $oldPaste['meta']['formatter'] = 'plaintext';
  639. $this->assertTag(
  640. array(
  641. 'id' => 'cipherdata',
  642. 'content' => htmlspecialchars(json_encode($oldPaste), ENT_NOQUOTES)
  643. ),
  644. $content,
  645. 'outputs data correctly'
  646. );
  647. }
  648. /**
  649. * @runInSeparateProcess
  650. */
  651. public function testDelete()
  652. {
  653. $this->reset();
  654. $this->_model->create(helper::getPasteId(), helper::getPaste());
  655. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  656. $_GET['pasteid'] = helper::getPasteId();
  657. $_GET['deletetoken'] = hash_hmac('sha1', helper::getPasteId(), serversalt::get());
  658. ob_start();
  659. new zerobin;
  660. $content = ob_get_contents();
  661. $this->assertTag(
  662. array(
  663. 'id' => 'status',
  664. 'content' => 'Paste was properly deleted'
  665. ),
  666. $content,
  667. 'outputs deleted status correctly'
  668. );
  669. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  670. }
  671. /**
  672. * @runInSeparateProcess
  673. */
  674. public function testDeleteInvalidId()
  675. {
  676. $this->reset();
  677. $this->_model->create(helper::getPasteId(), helper::getPaste());
  678. $_GET['pasteid'] = 'foo';
  679. $_GET['deletetoken'] = 'bar';
  680. ob_start();
  681. new zerobin;
  682. $content = ob_get_contents();
  683. $this->assertTag(
  684. array(
  685. 'id' => 'errormessage',
  686. 'content' => 'Invalid paste ID'
  687. ),
  688. $content,
  689. 'outputs delete error correctly'
  690. );
  691. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
  692. }
  693. /**
  694. * @runInSeparateProcess
  695. */
  696. public function testDeleteInexistantId()
  697. {
  698. $this->reset();
  699. $_GET['pasteid'] = helper::getPasteId();
  700. $_GET['deletetoken'] = 'bar';
  701. ob_start();
  702. new zerobin;
  703. $content = ob_get_contents();
  704. $this->assertTag(
  705. array(
  706. 'id' => 'errormessage',
  707. 'content' => 'Paste does not exist'
  708. ),
  709. $content,
  710. 'outputs delete error correctly'
  711. );
  712. }
  713. /**
  714. * @runInSeparateProcess
  715. */
  716. public function testDeleteInvalidToken()
  717. {
  718. $this->reset();
  719. $this->_model->create(helper::getPasteId(), helper::getPaste());
  720. $_GET['pasteid'] = helper::getPasteId();
  721. $_GET['deletetoken'] = 'bar';
  722. ob_start();
  723. new zerobin;
  724. $content = ob_get_contents();
  725. $this->assertTag(
  726. array(
  727. 'id' => 'errormessage',
  728. 'content' => 'Wrong deletion token'
  729. ),
  730. $content,
  731. 'outputs delete error correctly'
  732. );
  733. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after failing to delete data');
  734. }
  735. /**
  736. * @runInSeparateProcess
  737. */
  738. public function testDeleteBurnAfterReading()
  739. {
  740. $this->reset();
  741. $burnPaste = helper::getPaste(array('burnafterreading' => true));
  742. $this->_model->create(helper::getPasteId(), $burnPaste);
  743. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  744. $_GET['pasteid'] = helper::getPasteId();
  745. $_GET['deletetoken'] = 'burnafterreading';
  746. ob_start();
  747. new zerobin;
  748. $content = ob_get_contents();
  749. $response = json_decode($content, true);
  750. $this->assertEquals(0, $response['status'], 'outputs status');
  751. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  752. }
  753. /**
  754. * @runInSeparateProcess
  755. */
  756. public function testDeleteInvalidBurnAfterReading()
  757. {
  758. $this->reset();
  759. $this->_model->create(helper::getPasteId(), helper::getPaste());
  760. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  761. $_GET['pasteid'] = helper::getPasteId();
  762. $_GET['deletetoken'] = 'burnafterreading';
  763. ob_start();
  764. new zerobin;
  765. $content = ob_get_contents();
  766. $response = json_decode($content, true);
  767. $this->assertEquals(1, $response['status'], 'outputs status');
  768. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  769. }
  770. /**
  771. * @runInSeparateProcess
  772. */
  773. public function testDeleteExpired()
  774. {
  775. $this->reset();
  776. $expiredPaste = helper::getPaste(array('expire_date' => 1000));
  777. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not exist before being created');
  778. $this->_model->create(helper::getPasteId(), $expiredPaste);
  779. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists before deleting data');
  780. $_GET['pasteid'] = helper::getPasteId();
  781. $_GET['deletetoken'] = 'does not matter in this context, but has to be set';
  782. ob_start();
  783. new zerobin;
  784. $content = ob_get_contents();
  785. $this->assertTag(
  786. array(
  787. 'id' => 'errormessage',
  788. 'content' => 'Paste does not exist'
  789. ),
  790. $content,
  791. 'outputs error correctly'
  792. );
  793. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  794. }
  795. }