zerobin.php 27 KB

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