RepairSqliteAutoincrementTest.php 2.4 KB

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