1
0

LegacyDBTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. namespace Test\DB;
  9. use OC_DB;
  10. /**
  11. * Class LegacyDBTest
  12. *
  13. * @group DB
  14. */
  15. class LegacyDBTest extends \Test\TestCase {
  16. protected $backupGlobals = false;
  17. protected static $schema_file;
  18. protected $test_prefix;
  19. public static function setUpBeforeClass(): void {
  20. self::$schema_file = \OC::$server->getTempManager()->getTemporaryFile();
  21. }
  22. /**
  23. * @var string
  24. */
  25. private $table1;
  26. /**
  27. * @var string
  28. */
  29. private $table2;
  30. /**
  31. * @var string
  32. */
  33. private $table3;
  34. /**
  35. * @var string
  36. */
  37. private $table4;
  38. /**
  39. * @var string
  40. */
  41. private $table5;
  42. /**
  43. * @var string
  44. */
  45. private $text_table;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $dbFile = \OC::$SERVERROOT.'/tests/data/db_structure.xml';
  49. $r = $this->getUniqueID('_', 4).'_';
  50. $content = file_get_contents($dbFile);
  51. $content = str_replace('*dbprefix*', '*dbprefix*'.$r, $content);
  52. file_put_contents(self::$schema_file, $content);
  53. OC_DB::createDbFromStructure(self::$schema_file);
  54. $this->test_prefix = $r;
  55. $this->table1 = $this->test_prefix.'cntcts_addrsbks';
  56. $this->table2 = $this->test_prefix.'cntcts_cards';
  57. $this->table3 = $this->test_prefix.'vcategory';
  58. $this->table4 = $this->test_prefix.'decimal';
  59. $this->table5 = $this->test_prefix.'uniconst';
  60. $this->text_table = $this->test_prefix.'text_table';
  61. }
  62. protected function tearDown(): void {
  63. OC_DB::removeDBStructure(self::$schema_file);
  64. unlink(self::$schema_file);
  65. parent::tearDown();
  66. }
  67. public function testQuotes() {
  68. $query = OC_DB::prepare('SELECT `fullname` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  69. $result = $query->execute(['uri_1']);
  70. $this->assertTrue((bool)$result);
  71. $row = $result->fetchRow();
  72. $this->assertFalse($row);
  73. $result->closeCursor();
  74. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)');
  75. $result = $query->execute(['fullname test', 'uri_1']);
  76. $this->assertEquals(1, $result);
  77. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  78. $result = $query->execute(['uri_1']);
  79. $this->assertTrue((bool)$result);
  80. $row = $result->fetchRow();
  81. $this->assertArrayHasKey('fullname', $row);
  82. $this->assertEquals($row['fullname'], 'fullname test');
  83. $row = $result->fetchRow();
  84. $this->assertFalse((bool)$row); //PDO returns false, MDB2 returns null
  85. $result->closeCursor();
  86. }
  87. /**
  88. * @medium
  89. */
  90. public function testNOW() {
  91. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (NOW(),?)');
  92. $result = $query->execute(['uri_2']);
  93. $this->assertEquals(1, $result);
  94. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  95. $result = $query->execute(['uri_2']);
  96. $this->assertTrue((bool)$result);
  97. $result->closeCursor();
  98. }
  99. public function testUNIX_TIMESTAMP() {
  100. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (UNIX_TIMESTAMP(),?)');
  101. $result = $query->execute(['uri_3']);
  102. $this->assertEquals(1, $result);
  103. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?');
  104. $result = $query->execute(['uri_3']);
  105. $this->assertTrue((bool)$result);
  106. $result->closeCursor();
  107. }
  108. public function testLastInsertId() {
  109. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)');
  110. $result1 = OC_DB::executeAudited($query, ['insertid 1','uri_1']);
  111. $id1 = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*'.$this->table2);
  112. // we don't know the id we should expect, so insert another row
  113. $result2 = OC_DB::executeAudited($query, ['insertid 2','uri_2']);
  114. $id2 = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*'.$this->table2);
  115. // now we can check if the two ids are in correct order
  116. $this->assertGreaterThan($id1, $id2);
  117. }
  118. public function testUtf8Data() {
  119. $table = "*PREFIX*{$this->table2}";
  120. $expected = "Ћö雙喜\xE2\x80\xA2";
  121. $query = OC_DB::prepare("INSERT INTO `$table` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
  122. $result = $query->execute([$expected, 'uri_1', 'This is a vCard']);
  123. $this->assertEquals(1, $result);
  124. $query = OC_DB::prepare("SELECT `fullname` FROM `$table`");
  125. $result = $query->execute();
  126. $actual = $result->fetchOne();
  127. $result->closeCursor();
  128. $this->assertSame($expected, $actual);
  129. }
  130. /**
  131. * Insert, select and delete decimal(12,2) values
  132. * @dataProvider decimalData
  133. */
  134. public function XtestDecimal($insert, $expect) {
  135. $table = "*PREFIX*" . $this->table4;
  136. $rowname = 'decimaltest';
  137. $query = OC_DB::prepare('INSERT INTO `' . $table . '` (`' . $rowname . '`) VALUES (?)');
  138. $result = $query->execute([$insert]);
  139. $this->assertEquals(1, $result);
  140. $query = OC_DB::prepare('SELECT `' . $rowname . '` FROM `' . $table . '`');
  141. $result = $query->execute();
  142. $this->assertTrue((bool)$result);
  143. $row = $result->fetchRow();
  144. $result->closeCursor();
  145. $this->assertArrayHasKey($rowname, $row);
  146. $this->assertEquals($expect, $row[$rowname]);
  147. $query = OC_DB::prepare('DELETE FROM `' . $table . '`');
  148. $result = $query->execute();
  149. $this->assertTrue((bool)$result);
  150. }
  151. public function decimalData() {
  152. return [
  153. ['1337133713.37', '1337133713.37'],
  154. ['1234567890', '1234567890.00'],
  155. ];
  156. }
  157. public function testUpdateAffectedRowsNoMatch() {
  158. $this->insertCardData('fullname1', 'uri1');
  159. // The WHERE clause does not match any rows
  160. $this->assertSame(0, $this->updateCardData('fullname3', 'uri2'));
  161. }
  162. public function testUpdateAffectedRowsDifferent() {
  163. $this->insertCardData('fullname1', 'uri1');
  164. // The WHERE clause matches a single row and the value we are updating
  165. // is different from the one already present.
  166. $this->assertSame(1, $this->updateCardData('fullname1', 'uri2'));
  167. }
  168. public function testUpdateAffectedRowsSame() {
  169. $this->insertCardData('fullname1', 'uri1');
  170. // The WHERE clause matches a single row and the value we are updating
  171. // to is the same as the one already present. MySQL reports 0 here when
  172. // the PDO::MYSQL_ATTR_FOUND_ROWS flag is not specified.
  173. $this->assertSame(1, $this->updateCardData('fullname1', 'uri1'));
  174. }
  175. public function testUpdateAffectedRowsMultiple() {
  176. $this->insertCardData('fullname1', 'uri1');
  177. $this->insertCardData('fullname2', 'uri2');
  178. // The WHERE clause matches two rows. One row contains a value that
  179. // needs to be updated, the other one already contains the value we are
  180. // updating to. MySQL reports 1 here when the PDO::MYSQL_ATTR_FOUND_ROWS
  181. // flag is not specified.
  182. $query = OC_DB::prepare("UPDATE `*PREFIX*{$this->table2}` SET `uri` = ?");
  183. $this->assertSame(2, $query->execute(['uri1']));
  184. }
  185. protected function insertCardData($fullname, $uri) {
  186. $query = OC_DB::prepare("INSERT INTO `*PREFIX*{$this->table2}` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
  187. $this->assertSame(1, $query->execute([$fullname, $uri, $this->getUniqueID()]));
  188. }
  189. protected function updateCardData($fullname, $uri) {
  190. $query = OC_DB::prepare("UPDATE `*PREFIX*{$this->table2}` SET `uri` = ? WHERE `fullname` = ?");
  191. return $query->execute([$uri, $fullname]);
  192. }
  193. public function testILIKE() {
  194. $table = "*PREFIX*{$this->table2}";
  195. $query = OC_DB::prepare("INSERT INTO `$table` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
  196. $query->execute(['fooBAR', 'foo', 'bar']);
  197. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` LIKE ?");
  198. $result = $query->execute(['foobar']);
  199. $this->assertCount(0, $result->fetchAll());
  200. $result->closeCursor();
  201. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  202. $result = $query->execute(['foobar']);
  203. $this->assertCount(1, $result->fetchAll());
  204. $result->closeCursor();
  205. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  206. $result = $query->execute(['foo']);
  207. $this->assertCount(0, $result->fetchAll());
  208. $result->closeCursor();
  209. }
  210. public function testILIKEWildcard() {
  211. $table = "*PREFIX*{$this->table2}";
  212. $query = OC_DB::prepare("INSERT INTO `$table` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)");
  213. $query->execute(['FooBAR', 'foo', 'bar']);
  214. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` LIKE ?");
  215. $result = $query->execute(['%bar']);
  216. $this->assertCount(0, $result->fetchAll());
  217. $result->closeCursor();
  218. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` LIKE ?");
  219. $result = $query->execute(['foo%']);
  220. $this->assertCount(0, $result->fetchAll());
  221. $result->closeCursor();
  222. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` LIKE ?");
  223. $result = $query->execute(['%ba%']);
  224. $this->assertCount(0, $result->fetchAll());
  225. $result->closeCursor();
  226. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  227. $result = $query->execute(['%bar']);
  228. $this->assertCount(1, $result->fetchAll());
  229. $result->closeCursor();
  230. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  231. $result = $query->execute(['foo%']);
  232. $this->assertCount(1, $result->fetchAll());
  233. $result->closeCursor();
  234. $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `fullname` ILIKE ?");
  235. $result = $query->execute(['%ba%']);
  236. $this->assertCount(1, $result->fetchAll());
  237. $result->closeCursor();
  238. }
  239. /**
  240. * @dataProvider insertAndSelectDataProvider
  241. */
  242. public function testInsertAndSelectData($expected, $throwsOnMysqlWithoutUTF8MB4) {
  243. $table = "*PREFIX*{$this->text_table}";
  244. $config = \OC::$server->getConfig();
  245. $query = OC_DB::prepare("INSERT INTO `$table` (`textfield`) VALUES (?)");
  246. if ($throwsOnMysqlWithoutUTF8MB4 && $config->getSystemValue('dbtype', 'sqlite') === 'mysql' && $config->getSystemValue('mysql.utf8mb4', false) === false) {
  247. $this->markTestSkipped('MySQL requires UTF8mb4 to store value: ' . $expected);
  248. }
  249. $result = $query->execute([$expected]);
  250. $this->assertEquals(1, $result);
  251. $query = OC_DB::prepare("SELECT `textfield` FROM `$table`");
  252. $result = $query->execute();
  253. $actual = $result->fetchOne();
  254. $result->closeCursor();
  255. $this->assertSame($expected, $actual);
  256. }
  257. public function insertAndSelectDataProvider() {
  258. return [
  259. ['abcdefghijklmnopqrstuvwxyzABCDEFGHIKLMNOPQRSTUVWXYZ', false],
  260. ['0123456789', false],
  261. ['äöüÄÖÜß!"§$%&/()=?#\'+*~°^`´', false],
  262. ['²³¼½¬{[]}\\', false],
  263. ['♡⚗', false],
  264. ['💩', true], # :hankey: on github
  265. ];
  266. }
  267. }