RepairCollationTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Doctrine\DBAL\Platforms\MySqlPlatform;
  9. use OC\Repair\Collation;
  10. use OCP\IDBConnection;
  11. use OCP\Migration\IOutput;
  12. use Psr\Log\LoggerInterface;
  13. use Test\TestCase;
  14. class TestCollationRepair extends Collation {
  15. /**
  16. * @param IDBConnection $connection
  17. * @return string[]
  18. */
  19. public function getAllNonUTF8BinTables(IDBConnection $connection) {
  20. return parent::getAllNonUTF8BinTables($connection);
  21. }
  22. }
  23. /**
  24. * Tests for the converting of MySQL tables to InnoDB engine
  25. *
  26. * @group DB
  27. *
  28. * @see \OC\Repair\RepairMimeTypes
  29. */
  30. class RepairCollationTest extends TestCase {
  31. /**
  32. * @var TestCollationRepair
  33. */
  34. private $repair;
  35. /**
  36. * @var IDBConnection
  37. */
  38. private $connection;
  39. /**
  40. * @var string
  41. */
  42. private $tableName;
  43. /**
  44. * @var \OCP\IConfig
  45. */
  46. private $config;
  47. /** @var LoggerInterface */
  48. private $logger;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->connection = \OC::$server->get(IDBConnection::class);
  52. $this->logger = $this->createMock(LoggerInterface::class);
  53. $this->config = \OC::$server->getConfig();
  54. if (!$this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
  55. $this->markTestSkipped("Test only relevant on MySql");
  56. }
  57. $dbPrefix = $this->config->getSystemValueString("dbtableprefix");
  58. $this->tableName = $this->getUniqueID($dbPrefix . "_collation_test");
  59. $this->connection->prepare("CREATE TABLE $this->tableName(text VARCHAR(16)) COLLATE utf8_unicode_ci")->execute();
  60. $this->repair = new TestCollationRepair($this->config, $this->logger, $this->connection, false);
  61. }
  62. protected function tearDown(): void {
  63. $this->connection->getInner()->getSchemaManager()->dropTable($this->tableName);
  64. parent::tearDown();
  65. }
  66. public function testCollationConvert() {
  67. $tables = $this->repair->getAllNonUTF8BinTables($this->connection);
  68. $this->assertGreaterThanOrEqual(1, count($tables));
  69. /** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */
  70. $outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->repair->run($outputMock);
  74. $tables = $this->repair->getAllNonUTF8BinTables($this->connection);
  75. $this->assertCount(0, $tables);
  76. }
  77. }