data.php 4.8 KB

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