DBSchemaTest.php 3.1 KB

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