RepairSqliteAutoincrementTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
  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\Repair;
  9. use OCP\Migration\IOutput;
  10. /**
  11. * Tests for fixing the SQLite id recycling
  12. *
  13. * @group DB
  14. */
  15. class RepairSqliteAutoincrementTest extends \Test\TestCase {
  16. /**
  17. * @var \OC\Repair\SqliteAutoincrement
  18. */
  19. private $repair;
  20. /**
  21. * @var \Doctrine\DBAL\Connection
  22. */
  23. private $connection;
  24. /**
  25. * @var string
  26. */
  27. private $tableName;
  28. /**
  29. * @var \OCP\IConfig
  30. */
  31. private $config;
  32. protected function setUp() {
  33. parent::setUp();
  34. $this->connection = \OC::$server->getDatabaseConnection();
  35. $this->config = \OC::$server->getConfig();
  36. if (!$this->connection->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
  37. $this->markTestSkipped("Test only relevant on Sqlite");
  38. }
  39. $dbPrefix = $this->config->getSystemValue('dbtableprefix', 'oc_');
  40. $this->tableName = $this->getUniqueID($dbPrefix . 'autoinc_test');
  41. $this->connection->exec('CREATE TABLE ' . $this->tableName . '("someid" INTEGER NOT NULL, "text" VARCHAR(16), PRIMARY KEY("someid"))');
  42. $this->repair = new \OC\Repair\SqliteAutoincrement($this->connection);
  43. }
  44. protected function tearDown() {
  45. $this->connection->getSchemaManager()->dropTable($this->tableName);
  46. parent::tearDown();
  47. }
  48. /**
  49. * Tests whether autoincrement works
  50. *
  51. * @return boolean true if autoincrement works, false otherwise
  52. */
  53. protected function checkAutoincrement() {
  54. $this->connection->executeUpdate('INSERT INTO ' . $this->tableName . ' ("text") VALUES ("test")');
  55. $insertId = $this->connection->lastInsertId();
  56. $this->connection->executeUpdate('DELETE FROM ' . $this->tableName . ' WHERE "someid" = ?', array($insertId));
  57. // insert again
  58. $this->connection->executeUpdate('INSERT INTO ' . $this->tableName . ' ("text") VALUES ("test2")');
  59. $newInsertId = $this->connection->lastInsertId();
  60. return ($insertId !== $newInsertId);
  61. }
  62. public function testConvertIdColumn() {
  63. $this->assertFalse($this->checkAutoincrement());
  64. /** @var IOutput | \PHPUnit_Framework_MockObject_MockObject $outputMock */
  65. $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->repair->run($outputMock);
  69. $this->assertTrue($this->checkAutoincrement());
  70. }
  71. }