db.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 UnitTestCase {
  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::generate_random_bytes('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.'contacts_addressbooks';
  21. $this->table2 = $this->test_prefix.'contacts_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($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->assertTrue($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($result);
  40. $row = $result->fetchRow();
  41. $this->assertArrayHasKey('fullname', $row);
  42. $this->assertEqual($row['fullname'], 'fullname test');
  43. $row = $result->fetchRow();
  44. $this->assertFalse($row);
  45. }
  46. public function testNOW() {
  47. $query = OC_DB::prepare('INSERT INTO *PREFIX*'.$this->table2.' (`fullname`,`uri`) VALUES (NOW(),?)');
  48. $result = $query->execute(array('uri_2'));
  49. $this->assertTrue($result);
  50. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
  51. $result = $query->execute(array('uri_2'));
  52. $this->assertTrue($result);
  53. }
  54. public function testUNIX_TIMESTAMP() {
  55. $query = OC_DB::prepare('INSERT INTO *PREFIX*'.$this->table2.' (`fullname`,`uri`) VALUES (UNIX_TIMESTAMP(),?)');
  56. $result = $query->execute(array('uri_3'));
  57. $this->assertTrue($result);
  58. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
  59. $result = $query->execute(array('uri_3'));
  60. $this->assertTrue($result);
  61. }
  62. public function testinsertIfNotExist() {
  63. $categoryentries = array(
  64. array('user' => 'test', 'type' => 'contact', 'category' => 'Family'),
  65. array('user' => 'test', 'type' => 'contact', 'category' => 'Friends'),
  66. array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers'),
  67. array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers'),
  68. array('user' => 'test', 'type' => 'contact', 'category' => 'School'),
  69. );
  70. foreach($categoryentries as $entry) {
  71. $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table3,
  72. array(
  73. 'uid' => $entry['user'],
  74. 'type' => $entry['type'],
  75. 'category' => $entry['category'],
  76. ));
  77. $this->assertTrue($result);
  78. }
  79. $query = OC_DB::prepare('SELECT * FROM *PREFIX*'.$this->table3);
  80. $result = $query->execute();
  81. $this->assertTrue($result);
  82. $this->assertEqual('4', $result->numRows());
  83. }
  84. public function testinsertIfNotExistDontOverwrite() {
  85. $fullname = 'fullname test';
  86. $uri = 'uri_1';
  87. $carddata = 'This is a vCard';
  88. // Normal test to have same known data inserted.
  89. $query = OC_DB::prepare('INSERT INTO *PREFIX*'.$this->table2.' (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)');
  90. $result = $query->execute(array($fullname, $uri, $carddata));
  91. $this->assertTrue($result);
  92. $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
  93. $result = $query->execute(array($uri));
  94. $this->assertTrue($result);
  95. $row = $result->fetchRow();
  96. $this->assertArrayHasKey('carddata', $row);
  97. $this->assertEqual($carddata, $row['carddata']);
  98. $this->assertEqual('1', $result->numRows());
  99. // Try to insert a new row
  100. $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2,
  101. array(
  102. 'fullname' => $fullname,
  103. 'uri' => $uri,
  104. ));
  105. $this->assertTrue($result);
  106. $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
  107. $result = $query->execute(array($uri));
  108. $this->assertTrue($result);
  109. $row = $result->fetchRow();
  110. $this->assertArrayHasKey('carddata', $row);
  111. // Test that previously inserted data isn't overwritten
  112. $this->assertEqual($carddata, $row['carddata']);
  113. // And that a new row hasn't been inserted.
  114. $this->assertEqual('1', $result->numRows());
  115. }
  116. }