1
0

FixMountStoragesTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Repair\NC11;
  7. use OC\Repair\NC11\FixMountStorages;
  8. use OCP\DB\QueryBuilder\IQueryBuilder;
  9. use OCP\IDBConnection;
  10. use OCP\Migration\IOutput;
  11. use Test\TestCase;
  12. /**
  13. * Class FixMountStoragesTest
  14. *
  15. * @package Test\Repair\NC11
  16. * @group DB
  17. */
  18. class FixMountStoragesTest extends TestCase {
  19. /** @var IDBConnection */
  20. private $db;
  21. /** @var FixMountStorages */
  22. private $repair;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->db = \OC::$server->getDatabaseConnection();
  26. $this->repair = new FixMountStorages(
  27. $this->db
  28. );
  29. }
  30. public function testGetName() {
  31. $this->assertSame('Fix potential broken mount points', $this->repair->getName());
  32. }
  33. public function testRun() {
  34. // Valid mount
  35. $file1 = $this->createFile(42);
  36. $mount1 = $this->createMount($file1, 42);
  37. $this->assertStorage($mount1, 42);
  38. // Broken mount
  39. $file2 = $this->createFile(23);
  40. $mount2 = $this->createMount($file2, 1337);
  41. $this->assertStorage($mount2, 1337);
  42. /** @var IOutput|\PHPUnit\Framework\MockObject\MockObject $output */
  43. $output = $this->createMock(IOutput::class);
  44. $output->expects($this->exactly(2))
  45. ->method('info')
  46. ->withConsecutive(
  47. ['1 mounts updated'],
  48. ['No mounts updated']
  49. );
  50. $this->repair->run($output);
  51. $this->assertStorage($mount1, 42);
  52. $this->assertStorage($mount2, 23);
  53. $this->repair->run($output);
  54. $this->assertStorage($mount1, 42);
  55. $this->assertStorage($mount2, 23);
  56. }
  57. protected function createFile($storage) {
  58. $query = $this->db->getQueryBuilder();
  59. $query->insert('filecache')
  60. ->values([
  61. 'storage' => $query->createNamedParameter($storage, IQueryBuilder::PARAM_INT),
  62. 'path_hash' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR),
  63. 'encrypted' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  64. 'size' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  65. 'unencrypted_size' => $query->createNamedParameter(0, IQueryBuilder::PARAM_INT),
  66. ]);
  67. $query->execute();
  68. return $query->getLastInsertId();
  69. }
  70. protected function createMount($fileId, $storage) {
  71. $query = $this->db->getQueryBuilder();
  72. $query->insert('mounts')
  73. ->values([
  74. 'storage_id' => $query->createNamedParameter($storage, IQueryBuilder::PARAM_INT),
  75. 'root_id' => $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT),
  76. 'user_id' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR),
  77. 'mount_point' => $query->createNamedParameter(static::getUniqueID(), IQueryBuilder::PARAM_STR),
  78. ]);
  79. $query->execute();
  80. return $query->getLastInsertId();
  81. }
  82. protected function assertStorage($mount, $storage) {
  83. $query = $this->db->getQueryBuilder();
  84. $query->select('storage_id')
  85. ->from('mounts')
  86. ->where($query->expr()->eq('id', $query->createNamedParameter($mount, IQueryBuilder::PARAM_INT)));
  87. $result = $query->execute();
  88. $row = $result->fetch();
  89. $result->closeCursor();
  90. $this->assertEquals($storage, $row['storage_id']);
  91. }
  92. }