FilesystemTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. use PrivateBin\Data\Filesystem;
  3. class FilesystemTest 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 = Filesystem::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. $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. $this->assertTrue($this->_model->create($ids[$key], $paste), "store $key paste");
  70. } else {
  71. $this->assertTrue($this->_model->create($ids[$key], $expired), "store $key paste");
  72. }
  73. $this->assertTrue($this->_model->exists($ids[$key]), "paste $key exists after storing it");
  74. }
  75. $this->_model->purge(10);
  76. foreach ($ids as $key => $id) {
  77. if (in_array($key, array('x', 'y', 'z'))) {
  78. $this->assertTrue($this->_model->exists($id), "paste $key exists after purge");
  79. $this->_model->delete($id);
  80. } else {
  81. $this->assertFalse($this->_model->exists($id), "paste $key was purged");
  82. }
  83. }
  84. }
  85. }