1
0

RepairCollationTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Repair;
  8. use OC\DB\ConnectionAdapter;
  9. use OC\Repair\Collation;
  10. use OCP\IConfig;
  11. use OCP\IDBConnection;
  12. use OCP\Migration\IOutput;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Psr\Log\LoggerInterface;
  15. use Test\TestCase;
  16. class TestCollationRepair extends Collation {
  17. /**
  18. * @param IDBConnection $connection
  19. * @return string[]
  20. */
  21. public function getAllNonUTF8BinTables(IDBConnection $connection) {
  22. return parent::getAllNonUTF8BinTables($connection);
  23. }
  24. }
  25. /**
  26. * Tests for the converting of MySQL tables to InnoDB engine
  27. *
  28. * @group DB
  29. *
  30. * @see \OC\Repair\RepairMimeTypes
  31. */
  32. class RepairCollationTest extends TestCase {
  33. private TestCollationRepair $repair;
  34. private ConnectionAdapter $connection;
  35. private string $tableName;
  36. private IConfig $config;
  37. private LoggerInterface&MockObject $logger;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->connection = \OCP\Server::get(ConnectionAdapter::class);
  41. $this->config = \OCP\Server::get(IConfig::class);
  42. if ($this->connection->getDatabaseProvider() !== IDBConnection::PLATFORM_MYSQL) {
  43. $this->markTestSkipped('Test only relevant on MySql');
  44. }
  45. $this->logger = $this->createMock(LoggerInterface::class);
  46. $dbPrefix = $this->config->getSystemValueString('dbtableprefix');
  47. $this->tableName = $this->getUniqueID($dbPrefix . '_collation_test');
  48. $this->connection->prepare("CREATE TABLE $this->tableName(text VARCHAR(16)) COLLATE utf8_unicode_ci")->execute();
  49. $this->repair = new TestCollationRepair($this->config, $this->logger, $this->connection, false);
  50. }
  51. protected function tearDown(): void {
  52. $this->connection->getInner()->createSchemaManager()->dropTable($this->tableName);
  53. parent::tearDown();
  54. }
  55. public function testCollationConvert(): void {
  56. $tables = $this->repair->getAllNonUTF8BinTables($this->connection);
  57. $this->assertGreaterThanOrEqual(1, count($tables));
  58. $outputMock = $this->getMockBuilder(IOutput::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $this->repair->run($outputMock);
  62. $tables = $this->repair->getAllNonUTF8BinTables($this->connection);
  63. $this->assertCount(0, $tables);
  64. }
  65. }