zerobin.php 32 KB

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