db.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_DB extends PHPUnit_Framework_TestCase {
  9. protected $backupGlobals = FALSE;
  10. protected static $schema_file = 'static://test_db_scheme';
  11. protected $test_prefix;
  12. public function setUp() {
  13. $dbfile = OC::$SERVERROOT.'/tests/data/db_structure.xml';
  14. $r = '_'.OC_Util::generateRandomBytes('4').'_';
  15. $content = file_get_contents( $dbfile );
  16. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  17. file_put_contents( self::$schema_file, $content );
  18. OC_DB::createDbFromStructure(self::$schema_file);
  19. $this->test_prefix = $r;
  20. $this->table1 = $this->test_prefix.'cntcts_addrsbks';
  21. $this->table2 = $this->test_prefix.'cntcts_cards';
  22. $this->table3 = $this->test_prefix.'vcategory';
  23. }
  24. public function tearDown() {
  25. OC_DB::removeDBStructure(self::$schema_file);
  26. unlink(self::$schema_file);
  27. }
  28. public function testQuotes() {
  29. $query = OC_DB::prepare('SELECT `fullname` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  30. $result = $query->execute(array('uri_1'));
  31. $this->assertTrue((bool)$result);
  32. $row = $result->fetchRow();
  33. $this->assertFalse($row);
  34. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)');
  35. $result = $query->execute(array('fullname test', 'uri_1'));
  36. $this->assertEquals(1, $result);
  37. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  38. $result = $query->execute(array('uri_1'));
  39. $this->assertTrue((bool)$result);
  40. $row = $result->fetchRow();
  41. $this->assertArrayHasKey('fullname', $row);
  42. $this->assertEquals($row['fullname'], 'fullname test');
  43. $row = $result->fetchRow();
  44. $this->assertFalse((bool)$row); //PDO returns false, MDB2 returns null
  45. }
  46. /**
  47. * @medium
  48. */
  49. public function testNOW() {
  50. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (NOW(),?)');
  51. $result = $query->execute(array('uri_2'));
  52. $this->assertEquals(1, $result);
  53. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  54. $result = $query->execute(array('uri_2'));
  55. $this->assertTrue((bool)$result);
  56. }
  57. public function testUNIX_TIMESTAMP() {
  58. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (UNIX_TIMESTAMP(),?)');
  59. $result = $query->execute(array('uri_3'));
  60. $this->assertEquals(1, $result);
  61. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  62. $result = $query->execute(array('uri_3'));
  63. $this->assertTrue((bool)$result);
  64. }
  65. public function testLastInsertId() {
  66. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)');
  67. $result1 = OC_DB::executeAudited($query, array('insertid 1','uri_1'));
  68. $id1 = OC_DB::insertid('*PREFIX*'.$this->table2);
  69. // we don't know the id we should expect, so insert another row
  70. $result2 = OC_DB::executeAudited($query, array('insertid 2','uri_2'));
  71. $id2 = OC_DB::insertid('*PREFIX*'.$this->table2);
  72. // now we can check if the two ids are in correct order
  73. $this->assertGreaterThan($id1, $id2);
  74. }
  75. public function testinsertIfNotExist() {
  76. $categoryentries = array(
  77. array('user' => 'test', 'type' => 'contact', 'category' => 'Family', 'expectedResult' => 1),
  78. array('user' => 'test', 'type' => 'contact', 'category' => 'Friends', 'expectedResult' => 1),
  79. array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 1),
  80. array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 0),
  81. array('user' => 'test', 'type' => 'contact', 'category' => 'School', 'expectedResult' => 1),
  82. );
  83. foreach($categoryentries as $entry) {
  84. $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table3,
  85. array(
  86. 'uid' => $entry['user'],
  87. 'type' => $entry['type'],
  88. 'category' => $entry['category'],
  89. ));
  90. $this->assertEquals($entry['expectedResult'], $result);
  91. }
  92. $query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table3.'`');
  93. $result = $query->execute();
  94. $this->assertTrue((bool)$result);
  95. $this->assertEquals(4, count($result->fetchAll()));
  96. }
  97. public function testinsertIfNotExistDontOverwrite() {
  98. $fullname = 'fullname test';
  99. $uri = 'uri_1';
  100. $carddata = 'This is a vCard';
  101. // Normal test to have same known data inserted.
  102. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)');
  103. $result = $query->execute(array($fullname, $uri, $carddata));
  104. $this->assertEquals(1, $result);
  105. $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  106. $result = $query->execute(array($uri));
  107. $this->assertTrue((bool)$result);
  108. $row = $result->fetchRow();
  109. $this->assertArrayHasKey('carddata', $row);
  110. $this->assertEquals($carddata, $row['carddata']);
  111. $this->assertEquals(1, $result->numRows());
  112. // Try to insert a new row
  113. $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2,
  114. array(
  115. 'fullname' => $fullname,
  116. 'uri' => $uri,
  117. ));
  118. $this->assertEquals(0, $result);
  119. $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  120. $result = $query->execute(array($uri));
  121. $this->assertTrue((bool)$result);
  122. $row = $result->fetchRow();
  123. $this->assertArrayHasKey('carddata', $row);
  124. // Test that previously inserted data isn't overwritten
  125. $this->assertEquals($carddata, $row['carddata']);
  126. // And that a new row hasn't been inserted.
  127. $this->assertEquals(1, $result->numRows());
  128. }
  129. /**
  130. * Tests whether the database is configured so it accepts and returns dates
  131. * in the expected format.
  132. */
  133. public function testTimestampDateFormat() {
  134. $table = '*PREFIX*'.$this->test_prefix.'timestamp';
  135. $column = 'timestamptest';
  136. $expectedFormat = 'Y-m-d H:i:s';
  137. $expected = new \DateTime;
  138. $query = OC_DB::prepare("INSERT INTO `$table` (`$column`) VALUES (?)");
  139. $result = $query->execute(array($expected->format($expectedFormat)));
  140. $this->assertEquals(
  141. 1,
  142. $result,
  143. "Database failed to accept dates in the format '$expectedFormat'."
  144. );
  145. $id = OC_DB::insertid($table);
  146. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `id` = ?");
  147. $result = $query->execute(array($id));
  148. $row = $result->fetchRow();
  149. $actual = \DateTime::createFromFormat($expectedFormat, $row[$column]);
  150. $this->assertInstanceOf(
  151. '\DateTime',
  152. $actual,
  153. "Database failed to return dates in the format '$expectedFormat'."
  154. );
  155. $this->assertEquals(
  156. $expected,
  157. $actual,
  158. 'Failed asserting that the returned date is the same as the inserted.'
  159. );
  160. }
  161. }