db.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }