db.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. class privatebin_dbTest extends PHPUnit_Framework_TestCase
  3. {
  4. private $_model;
  5. private $_options = array(
  6. 'dsn' => 'sqlite::memory:',
  7. 'usr' => null,
  8. 'pwd' => null,
  9. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION),
  10. );
  11. public function setUp()
  12. {
  13. /* Setup Routine */
  14. $this->_model = privatebin_db::getInstance($this->_options);
  15. }
  16. public function testDatabaseBasedDataStoreWorks()
  17. {
  18. $this->_model->delete(helper::getPasteId());
  19. // storing pastes
  20. $paste = helper::getPaste(array('expire_date' => 1344803344));
  21. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste does not yet exist');
  22. $this->assertTrue($this->_model->create(helper::getPasteId(), $paste), 'store new paste');
  23. $this->assertTrue($this->_model->exists(helper::getPasteId()), 'paste exists after storing it');
  24. $this->assertFalse($this->_model->create(helper::getPasteId(), $paste), 'unable to store the same paste twice');
  25. $this->assertEquals(json_decode(json_encode($paste)), $this->_model->read(helper::getPasteId()));
  26. // storing comments
  27. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment does not yet exist');
  28. $this->assertTrue($this->_model->createComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId(), helper::getComment()) !== false, 'store comment');
  29. $this->assertTrue($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment exists after storing it');
  30. $comment = json_decode(json_encode(helper::getComment()));
  31. $comment->id = helper::getCommentId();
  32. $comment->parentid = helper::getPasteId();
  33. $this->assertEquals(
  34. array($comment->meta->postdate => $comment),
  35. $this->_model->readComments(helper::getPasteId())
  36. );
  37. // deleting pastes
  38. $this->_model->delete(helper::getPasteId());
  39. $this->assertFalse($this->_model->exists(helper::getPasteId()), 'paste successfully deleted');
  40. $this->assertFalse($this->_model->existsComment(helper::getPasteId(), helper::getPasteId(), helper::getCommentId()), 'comment was deleted with paste');
  41. $this->assertFalse($this->_model->read(helper::getPasteId()), 'paste can no longer be found');
  42. }
  43. public function testDatabaseBasedAttachmentStoreWorks()
  44. {
  45. $this->_model->delete(helper::getPasteId());
  46. $original = $paste = helper::getPasteWithAttachment(array('expire_date' => 1344803344));
  47. $paste['meta']['burnafterreading'] = $original['meta']['burnafterreading'] = true;
  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. /**
  58. * @expectedException PDOException
  59. */
  60. public function testGetIbmInstance()
  61. {
  62. privatebin_db::getInstance(array(
  63. 'dsn' => 'ibm:', 'usr' => null, 'pwd' => null,
  64. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  65. ));
  66. }
  67. /**
  68. * @expectedException PDOException
  69. */
  70. public function testGetInformixInstance()
  71. {
  72. privatebin_db::getInstance(array(
  73. 'dsn' => 'informix:', 'usr' => null, 'pwd' => null,
  74. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  75. ));
  76. }
  77. /**
  78. * @expectedException PDOException
  79. */
  80. public function testGetMssqlInstance()
  81. {
  82. privatebin_db::getInstance(array(
  83. 'dsn' => 'mssql:', 'usr' => null, 'pwd' => null,
  84. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  85. ));
  86. }
  87. /**
  88. * @expectedException PDOException
  89. */
  90. public function testGetMysqlInstance()
  91. {
  92. privatebin_db::getInstance(array(
  93. 'dsn' => 'mysql:', 'usr' => null, 'pwd' => null,
  94. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  95. ));
  96. }
  97. /**
  98. * @expectedException PDOException
  99. */
  100. public function testGetOciInstance()
  101. {
  102. privatebin_db::getInstance(array(
  103. 'dsn' => 'oci:', 'usr' => null, 'pwd' => null,
  104. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  105. ));
  106. }
  107. /**
  108. * @expectedException PDOException
  109. */
  110. public function testGetPgsqlInstance()
  111. {
  112. privatebin_db::getInstance(array(
  113. 'dsn' => 'pgsql:', 'usr' => null, 'pwd' => null,
  114. 'opt' => array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
  115. ));
  116. }
  117. /**
  118. * @expectedException Exception
  119. * @expectedExceptionCode 5
  120. */
  121. public function testGetFooInstance()
  122. {
  123. privatebin_db::getInstance(array(
  124. 'dsn' => 'foo:', 'usr' => null, 'pwd' => null, 'opt' => null
  125. ));
  126. }
  127. public function testTableUpgrade()
  128. {
  129. $path = PATH . 'data/db-test.sq3';
  130. @unlink($path);
  131. $this->_options['dsn'] = 'sqlite:' . $path;
  132. $this->_options['tbl'] = 'foo_';
  133. $db = new PDO(
  134. $this->_options['dsn'],
  135. $this->_options['usr'],
  136. $this->_options['pwd'],
  137. $this->_options['opt']
  138. );
  139. $db->exec(
  140. 'CREATE TABLE foo_paste ( ' .
  141. 'dataid CHAR(16), ' .
  142. 'data TEXT, ' .
  143. 'postdate INT, ' .
  144. 'expiredate INT, ' .
  145. 'opendiscussion INT, ' .
  146. 'burnafterreading INT );'
  147. );
  148. privatebin_db::getInstance($this->_options);
  149. @unlink($path);
  150. }
  151. }