db.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. }
  23. public function tearDown() {
  24. OC_DB::removeDBStructure(self::$schema_file);
  25. unlink(self::$schema_file);
  26. }
  27. public function testQuotes() {
  28. $query = OC_DB::prepare('SELECT `fullname` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
  29. $result = $query->execute(array('uri_1'));
  30. $this->assertTrue($result);
  31. $row = $result->fetchRow();
  32. $this->assertFalse($row);
  33. $query = OC_DB::prepare('INSERT INTO *PREFIX*'.$this->table2.' (`fullname`,`uri`) VALUES (?,?)');
  34. $result = $query->execute(array('fullname test', 'uri_1'));
  35. $this->assertTrue($result);
  36. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
  37. $result = $query->execute(array('uri_1'));
  38. $this->assertTrue($result);
  39. $row = $result->fetchRow();
  40. $this->assertArrayHasKey('fullname', $row);
  41. $this->assertEqual($row['fullname'], 'fullname test');
  42. $row = $result->fetchRow();
  43. $this->assertFalse($row);
  44. }
  45. public function testNOW() {
  46. $query = OC_DB::prepare('INSERT INTO *PREFIX*'.$this->table2.' (`fullname`,`uri`) VALUES (NOW(),?)');
  47. $result = $query->execute(array('uri_2'));
  48. $this->assertTrue($result);
  49. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
  50. $result = $query->execute(array('uri_2'));
  51. $this->assertTrue($result);
  52. }
  53. public function testUNIX_TIMESTAMP() {
  54. $query = OC_DB::prepare('INSERT INTO *PREFIX*'.$this->table2.' (`fullname`,`uri`) VALUES (UNIX_TIMESTAMP(),?)');
  55. $result = $query->execute(array('uri_3'));
  56. $this->assertTrue($result);
  57. $query = OC_DB::prepare('SELECT `fullname`,`uri` FROM *PREFIX*'.$this->table2.' WHERE `uri` = ?');
  58. $result = $query->execute(array('uri_3'));
  59. $this->assertTrue($result);
  60. }
  61. }