data.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. class privatebin_dataTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_model;
  5. private $_path;
  6. public function setUp()
  7. {
  8. /* Setup Routine */
  9. $this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
  10. $this->_model = privatebin_data::getInstance(array('dir' => $this->_path));
  11. }
  12. public function tearDown()
  13. {
  14. /* Tear Down Routine */
  15. helper::rmdir($this->_path);
  16. }
  17. public function testFileBasedDataStoreWorks()
  18. {
  19. $this->_model->delete(helper::getPasteId());
  20. // storing pastes
  21. $paste = helper::getPaste(array('expire_date' => 1344803344));
  22. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
  23. $this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
  24. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
  25. $this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
  26. $this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(helper::getPasteId()));
  27. // storing comments
  28. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment does not yet exist');
  29. $this->assertTrue($this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()) !== false, 'store comment');
  30. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it');
  31. $comment = json_decode(json_encode(helper::getComment()));
  32. $comment->id = helper::getCommentId();
  33. $comment->parentid = helper::getPasteId();
  34. $this->assertEquals(
  35. array($comment->meta->postdate => $comment),
  36. $this->_model->readComments(helper::getPasteId())
  37. );
  38. // deleting pastes
  39. $this->_model->delete(helper::getPasteId());
  40. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  41. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment was deleted with paste');
  42. $this->assertFalse($this->_model->read(helper::getPasteId()), 'paste can no longer be found');
  43. }
  44. public function testFileBasedAttachmentStoreWorks()
  45. {
  46. $this->_model->delete(helper::getPasteId());
  47. $original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
  48. $paste['meta']['attachment'] = $paste['attachment'];
  49. $paste['meta']['attachmentname'] = $paste['attachmentname'];
  50. unset($paste['attachment'], $paste['attachmentname']);
  51. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
  52. $this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
  53. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
  54. $this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
  55. $this->assertEquals(json_decode(json_encode($original)), $this->_model->read(helper::getPasteId()));
  56. }
  57. public function testPurge()
  58. {
  59. mkdir($this->_path . DIRECTORY_SEPARATOR . '00', 0777, true);
  60. $expired = helper::getPaste(array('expire_date' => 1344803344));
  61. $paste = helper::getPaste(array('expire_date' => time() + 3600));
  62. $keys = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'x', 'y', 'z');
  63. $ids = array();
  64. foreach ($keys as $key)
  65. {
  66. $ids[$key] = substr(md5($key), 0, 16);
  67. $this->assertFalse($this->_model->exists($ids[$key]), "paste $key does not yet exist");
  68. if (in_array($key, array('x', 'y', 'z')))
  69. {
  70. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  71. }
  72. else
  73. {
  74. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  75. }
  76. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  77. }
  78. $this->_model->purge(10);
  79. foreach ($ids as $key => $id)
  80. {
  81. if (in_array($key, array('x', 'y', 'z')))
  82. {
  83. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  84. $this->_model->delete($id);
  85. }
  86. else
  87. {
  88. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  89. }
  90. }
  91. }
  92. }