dbschema.php 3.0 KB

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