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.

107 lines
2.9 KiB

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