RepairCollationTest.php 2.3 KB

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