DBSchemaTest.php 2.9 KB

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