You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbschema.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. use OCP\Security\ISecureRandom;
  9. /**
  10. * Class Test_DBSchema
  11. *
  12. * @group DB
  13. */
  14. class Test_DBSchema extends \Test\TestCase {
  15. protected $schema_file = 'static://test_db_scheme';
  16. protected $schema_file2 = 'static://test_db_scheme2';
  17. protected $table1;
  18. protected $table2;
  19. protected function setUp() {
  20. parent::setUp();
  21. $dbfile = OC::$SERVERROOT.'/tests/data/db_structure.xml';
  22. $dbfile2 = OC::$SERVERROOT.'/tests/data/db_structure2.xml';
  23. $r = '_' . \OC::$server->getSecureRandom()->
  24. generate(4, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS) . '_';
  25. $content = file_get_contents( $dbfile );
  26. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  27. file_put_contents( $this->schema_file, $content );
  28. $content = file_get_contents( $dbfile2 );
  29. $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content );
  30. file_put_contents( $this->schema_file2, $content );
  31. $this->table1 = $r.'cntcts_addrsbks';
  32. $this->table2 = $r.'cntcts_cards';
  33. }
  34. protected function tearDown() {
  35. unlink($this->schema_file);
  36. unlink($this->schema_file2);
  37. parent::tearDown();
  38. }
  39. // everything in one test, they depend on each other
  40. /**
  41. * @medium
  42. */
  43. public function testSchema() {
  44. $platform = \OC::$server->getDatabaseConnection()->getDatabasePlatform();
  45. $this->doTestSchemaCreating();
  46. $this->doTestSchemaChanging();
  47. $this->doTestSchemaDumping();
  48. $this->doTestSchemaRemoving();
  49. }
  50. public function doTestSchemaCreating() {
  51. OC_DB::createDbFromStructure($this->schema_file);
  52. $this->assertTableExist($this->table1);
  53. $this->assertTableExist($this->table2);
  54. }
  55. public function doTestSchemaChanging() {
  56. OC_DB::updateDbFromStructure($this->schema_file2);
  57. $this->assertTableExist($this->table2);
  58. }
  59. public function doTestSchemaDumping() {
  60. $outfile = 'static://db_out.xml';
  61. OC_DB::getDbStructure($outfile);
  62. $content = file_get_contents($outfile);
  63. $this->assertContains($this->table1, $content);
  64. $this->assertContains($this->table2, $content);
  65. }
  66. public function doTestSchemaRemoving() {
  67. OC_DB::removeDBStructure($this->schema_file);
  68. $this->assertTableNotExist($this->table1);
  69. $this->assertTableNotExist($this->table2);
  70. }
  71. /**
  72. * @param string $table
  73. */
  74. public function assertTableExist($table) {
  75. $this->assertTrue(OC_DB::tableExists($table), 'Table ' . $table . ' does not exist');
  76. }
  77. /**
  78. * @param string $table
  79. */
  80. public function assertTableNotExist($table) {
  81. $platform = \OC::$server->getDatabaseConnection()->getDatabasePlatform();
  82. if ($platform instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
  83. // sqlite removes the tables after closing the DB
  84. $this->assertTrue(true);
  85. } else {
  86. $this->assertFalse(OC_DB::tableExists($table), 'Table ' . $table . ' exists.');
  87. }
  88. }
  89. }