db.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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 \Test\TestCase {
  9. protected $backupGlobals = FALSE;
  10. protected static $schema_file = 'static://test_db_scheme';
  11. protected $test_prefix;
  12. /**
  13. * @var string
  14. */
  15. private $table1;
  16. /**
  17. * @var string
  18. */
  19. private $table2;
  20. /**
  21. * @var string
  22. */
  23. private $table3;
  24. /**
  25. * @var string
  26. */
  27. private $table4;
  28. /**
  29. * @var string
  30. */
  31. private $table5;
  32. protected function setUp() {
  33. parent::setUp();
  34. $dbFile = OC::$SERVERROOT.'/tests/data/db_structure.xml';
  35. $r = $this->getUniqueID('_', 4).'_';
  36. $content = file_get_contents( $dbFile );
  37. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  38. file_put_contents( self::$schema_file, $content );
  39. OC_DB::createDbFromStructure(self::$schema_file);
  40. $this->test_prefix = $r;
  41. $this->table1 = $this->test_prefix.'cntcts_addrsbks';
  42. $this->table2 = $this->test_prefix.'cntcts_cards';
  43. $this->table3 = $this->test_prefix.'vcategory';
  44. $this->table4 = $this->test_prefix.'decimal';
  45. $this->table5 = $this->test_prefix.'uniconst';
  46. }
  47. protected function tearDown() {
  48. OC_DB::removeDBStructure(self::$schema_file);
  49. unlink(self::$schema_file);
  50. parent::tearDown();
  51. }
  52. public function testQuotes() {
  53. $query = OC_DB::prepare('SELECT `fullname` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  54. $result = $query->execute(array('uri_1'));
  55. $this->assertTrue((bool)$result);
  56. $row = $result->fetchRow();
  57. $this->assertFalse($row);
  58. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)');
  59. $result = $query->execute(array('fullname test', 'uri_1'));
  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_1'));
  63. $this->assertTrue((bool)$result);
  64. $row = $result->fetchRow();
  65. $this->assertArrayHasKey('fullname', $row);
  66. $this->assertEquals($row['fullname'], 'fullname test');
  67. $row = $result->fetchRow();
  68. $this->assertFalse((bool)$row); //PDO returns false, MDB2 returns null
  69. }
  70. /**
  71. * @medium
  72. */
  73. public function testNOW() {
  74. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (NOW(),?)');
  75. $result = $query->execute(array('uri_2'));
  76. $this->assertEquals(1, $result);
  77. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  78. $result = $query->execute(array('uri_2'));
  79. $this->assertTrue((bool)$result);
  80. }
  81. public function testUNIX_TIMESTAMP() {
  82. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (UNIX_TIMESTAMP(),?)');
  83. $result = $query->execute(array('uri_3'));
  84. $this->assertEquals(1, $result);
  85. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  86. $result = $query->execute(array('uri_3'));
  87. $this->assertTrue((bool)$result);
  88. }
  89. public function testLastInsertId() {
  90. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)');
  91. $result1 = OC_DB::executeAudited($query, array('insertid 1','uri_1'));
  92. $id1 = OC_DB::insertid('*PREFIX*'.$this->table2);
  93. // we don't know the id we should expect, so insert another row
  94. $result2 = OC_DB::executeAudited($query, array('insertid 2','uri_2'));
  95. $id2 = OC_DB::insertid('*PREFIX*'.$this->table2);
  96. // now we can check if the two ids are in correct order
  97. $this->assertGreaterThan($id1, $id2);
  98. }
  99. public function testinsertIfNotExist() {
  100. $categoryEntries = array(
  101. array('user' => 'test', 'type' => 'contact', 'category' => 'Family', 'expectedResult' => 1),
  102. array('user' => 'test', 'type' => 'contact', 'category' => 'Friends', 'expectedResult' => 1),
  103. array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 1),
  104. array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 0),
  105. array('user' => 'test', 'type' => 'contact', 'category' => 'School', 'expectedResult' => 1),
  106. );
  107. foreach($categoryEntries as $entry) {
  108. $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table3,
  109. array(
  110. 'uid' => $entry['user'],
  111. 'type' => $entry['type'],
  112. 'category' => $entry['category'],
  113. ));
  114. $this->assertEquals($entry['expectedResult'], $result);
  115. }
  116. $query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table3.'`');
  117. $result = $query->execute();
  118. $this->assertTrue((bool)$result);
  119. $this->assertEquals(4, count($result->fetchAll()));
  120. }
  121. public function testInsertIfNotExistNull() {
  122. $categoryEntries = array(
  123. array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 1),
  124. array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 0),
  125. array('addressbookid' => 123, 'fullname' => 'test', 'expectedResult' => 1),
  126. );
  127. foreach($categoryEntries as $entry) {
  128. $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2,
  129. array(
  130. 'addressbookid' => $entry['addressbookid'],
  131. 'fullname' => $entry['fullname'],
  132. ));
  133. $this->assertEquals($entry['expectedResult'], $result);
  134. }
  135. $query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table2.'`');
  136. $result = $query->execute();
  137. $this->assertTrue((bool)$result);
  138. $this->assertEquals(2, count($result->fetchAll()));
  139. }
  140. public function testInsertIfNotExistDonTOverwrite() {
  141. $fullName = 'fullname test';
  142. $uri = 'uri_1';
  143. $carddata = 'This is a vCard';
  144. // Normal test to have same known data inserted.
  145. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)');
  146. $result = $query->execute(array($fullName, $uri, $carddata));
  147. $this->assertEquals(1, $result);
  148. $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  149. $result = $query->execute(array($uri));
  150. $this->assertTrue((bool)$result);
  151. $rowset = $result->fetchAll();
  152. $this->assertEquals(1, count($rowset));
  153. $this->assertArrayHasKey('carddata', $rowset[0]);
  154. $this->assertEquals($carddata, $rowset[0]['carddata']);
  155. // Try to insert a new row
  156. $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table2,
  157. array(
  158. 'fullname' => $fullName,
  159. 'uri' => $uri,
  160. ));
  161. $this->assertEquals(0, $result);
  162. $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  163. $result = $query->execute(array($uri));
  164. $this->assertTrue((bool)$result);
  165. // Test that previously inserted data isn't overwritten
  166. // And that a new row hasn't been inserted.
  167. $rowset = $result->fetchAll();
  168. $this->assertEquals(1, count($rowset));
  169. $this->assertArrayHasKey('carddata', $rowset[0]);
  170. $this->assertEquals($carddata, $rowset[0]['carddata']);
  171. }
  172. public function testInsertIfNotExistsViolating() {
  173. $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
  174. array(
  175. 'storage' => 1,
  176. 'path_hash' => md5('welcome.txt'),
  177. 'etag' => $this->getUniqueID()
  178. ));
  179. $this->assertEquals(1, $result);
  180. $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
  181. array(
  182. 'storage' => 1,
  183. 'path_hash' => md5('welcome.txt'),
  184. 'etag' => $this->getUniqueID()
  185. ),['storage', 'path_hash']);
  186. $this->assertEquals(0, $result);
  187. }
  188. public function insertIfNotExistsViolatingThrows() {
  189. return [
  190. [null],
  191. [['etag']],
  192. ];
  193. }
  194. /**
  195. * @dataProvider insertIfNotExistsViolatingThrows
  196. * @expectedException \Doctrine\DBAL\Exception\UniqueConstraintViolationException
  197. *
  198. * @param array $compareKeys
  199. */
  200. public function testInsertIfNotExistsViolatingThrows($compareKeys) {
  201. $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
  202. array(
  203. 'storage' => 1,
  204. 'path_hash' => md5('welcome.txt'),
  205. 'etag' => $this->getUniqueID()
  206. ));
  207. $this->assertEquals(1, $result);
  208. $result = \OCP\DB::insertIfNotExist('*PREFIX*'.$this->table5,
  209. array(
  210. 'storage' => 1,
  211. 'path_hash' => md5('welcome.txt'),
  212. 'etag' => $this->getUniqueID()
  213. ), $compareKeys);
  214. $this->assertEquals(0, $result);
  215. }
  216. public function testUtf8Data() {
  217. $table = "*PREFIX*{$this->table2}";
  218. $expected = "Ћö雙喜\xE2\x80\xA2";
  219. $query = OC_DB::prepare("INSERT INTO `$table` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
  220. $result = $query->execute(array($expected, 'uri_1', 'This is a vCard'));
  221. $this->assertEquals(1, $result);
  222. $actual = OC_DB::prepare("SELECT `fullname` FROM `$table`")->execute()->fetchOne();
  223. $this->assertSame($expected, $actual);
  224. }
  225. public function testDecimal() {
  226. $table = "*PREFIX*" . $this->table4;
  227. $rowname = 'decimaltest';
  228. // Insert, select and delete decimal(12,2) values
  229. $inserts = array('1337133713.37', '1234567890');
  230. $expects = array('1337133713.37', '1234567890.00');
  231. for ($i = 0; $i < count($inserts); $i++) {
  232. $insert = $inserts[$i];
  233. $expect = $expects[$i];
  234. $query = OC_DB::prepare('INSERT INTO `' . $table . '` (`' . $rowname . '`) VALUES (?)');
  235. $result = $query->execute(array($insert));
  236. $this->assertEquals(1, $result);
  237. $query = OC_DB::prepare('SELECT `' . $rowname . '` FROM `' . $table . '`');
  238. $result = $query->execute();
  239. $this->assertTrue((bool)$result);
  240. $row = $result->fetchRow();
  241. $this->assertArrayHasKey($rowname, $row);
  242. $this->assertEquals($expect, $row[$rowname]);
  243. $query = OC_DB::prepare('DELETE FROM `' . $table . '`');
  244. $result = $query->execute();
  245. $this->assertTrue((bool)$result);
  246. }
  247. }
  248. public function testUpdateAffectedRowsNoMatch() {
  249. $this->insertCardData('fullname1', 'uri1');
  250. // The WHERE clause does not match any rows
  251. $this->assertSame(0, $this->updateCardData('fullname3', 'uri2'));
  252. }
  253. public function testUpdateAffectedRowsDifferent() {
  254. $this->insertCardData('fullname1', 'uri1');
  255. // The WHERE clause matches a single row and the value we are updating
  256. // is different from the one already present.
  257. $this->assertSame(1, $this->updateCardData('fullname1', 'uri2'));
  258. }
  259. public function testUpdateAffectedRowsSame() {
  260. $this->insertCardData('fullname1', 'uri1');
  261. // The WHERE clause matches a single row and the value we are updating
  262. // to is the same as the one already present. MySQL reports 0 here when
  263. // the PDO::MYSQL_ATTR_FOUND_ROWS flag is not specified.
  264. $this->assertSame(1, $this->updateCardData('fullname1', 'uri1'));
  265. }
  266. public function testUpdateAffectedRowsMultiple() {
  267. $this->insertCardData('fullname1', 'uri1');
  268. $this->insertCardData('fullname2', 'uri2');
  269. // The WHERE clause matches two rows. One row contains a value that
  270. // needs to be updated, the other one already contains the value we are
  271. // updating to. MySQL reports 1 here when the PDO::MYSQL_ATTR_FOUND_ROWS
  272. // flag is not specified.
  273. $query = OC_DB::prepare("UPDATE `*PREFIX*{$this->table2}` SET `uri` = ?");
  274. $this->assertSame(2, $query->execute(array('uri1')));
  275. }
  276. protected function insertCardData($fullname, $uri) {
  277. $query = OC_DB::prepare("INSERT INTO `*PREFIX*{$this->table2}` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
  278. $this->assertSame(1, $query->execute(array($fullname, $uri, $this->getUniqueID())));
  279. }
  280. protected function updateCardData($fullname, $uri) {
  281. $query = OC_DB::prepare("UPDATE `*PREFIX*{$this->table2}` SET `uri` = ? WHERE `fullname` = ?");
  282. return $query->execute(array($uri, $fullname));
  283. }
  284. public function testILIKE() {
  285. $table = "*PREFIX*{$this->table2}";
  286. $query = OC_DB::prepare("INSERT INTO `$table` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
  287. $query->execute(array('fooBAR', 'foo', 'bar'));
  288. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` LIKE ?");
  289. $result = $query->execute(array('foobar'));
  290. $this->assertCount(0, $result->fetchAll());
  291. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  292. $result = $query->execute(array('foobar'));
  293. $this->assertCount(1, $result->fetchAll());
  294. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  295. $result = $query->execute(array('foo'));
  296. $this->assertCount(0, $result->fetchAll());
  297. }
  298. public function testILIKEWildcard() {
  299. $table = "*PREFIX*{$this->table2}";
  300. $query = OC_DB::prepare("INSERT INTO `$table` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
  301. $query->execute(array('FooBAR', 'foo', 'bar'));
  302. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` LIKE ?");
  303. $result = $query->execute(array('%bar'));
  304. $this->assertCount(0, $result->fetchAll());
  305. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` LIKE ?");
  306. $result = $query->execute(array('foo%'));
  307. $this->assertCount(0, $result->fetchAll());
  308. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` LIKE ?");
  309. $result = $query->execute(array('%ba%'));
  310. $this->assertCount(0, $result->fetchAll());
  311. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  312. $result = $query->execute(array('%bar'));
  313. $this->assertCount(1, $result->fetchAll());
  314. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  315. $result = $query->execute(array('foo%'));
  316. $this->assertCount(1, $result->fetchAll());
  317. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  318. $result = $query->execute(array('%ba%'));
  319. $this->assertCount(1, $result->fetchAll());
  320. }
  321. }