db.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  128. * @expectedException Exception
  129. * @expectedExceptionCode 6
  130. */
  131. public function testMissingDsn()
  132. {
  133. $options = $this->_options;
  134. unset($options['dsn']);
  135. privatebin_db::getInstance($options);
  136. }
  137. /**
  138. * @expectedException Exception
  139. * @expectedExceptionCode 6
  140. */
  141. public function testMissingUsr()
  142. {
  143. $options = $this->_options;
  144. unset($options['usr']);
  145. privatebin_db::getInstance($options);
  146. }
  147. /**
  148. * @expectedException Exception
  149. * @expectedExceptionCode 6
  150. */
  151. public function testMissingPwd()
  152. {
  153. $options = $this->_options;
  154. unset($options['pwd']);
  155. privatebin_db::getInstance($options);
  156. }
  157. /**
  158. * @expectedException Exception
  159. * @expectedExceptionCode 6
  160. */
  161. public function testMissingOpt()
  162. {
  163. $options = $this->_options;
  164. unset($options['opt']);
  165. privatebin_db::getInstance($options);
  166. }
  167. public function testTableUpgrade()
  168. {
  169. $path = PATH . 'data/db-test.sq3';
  170. @unlink($path);
  171. $this->_options['dsn'] = 'sqlite:' . $path;
  172. $this->_options['tbl'] = 'foo_';
  173. $db = new PDO(
  174. $this->_options['dsn'],
  175. $this->_options['usr'],
  176. $this->_options['pwd'],
  177. $this->_options['opt']
  178. );
  179. $db->exec(
  180. 'CREATE TABLE foo_paste ( ' .
  181. 'dataid CHAR(16), ' .
  182. 'data TEXT, ' .
  183. 'postdate INT, ' .
  184. 'expiredate INT, ' .
  185. 'opendiscussion INT, ' .
  186. 'burnafterreading INT );'
  187. );
  188. privatebin_db::getInstance($this->_options);
  189. @unlink($path);
  190. }
  191. }