1
0

RepairDavSharesTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Repair;
  8. use OC\Repair\RepairDavShares;
  9. use OCP\DB\IResult;
  10. use OCP\DB\QueryBuilder\IExpressionBuilder;
  11. use OCP\DB\QueryBuilder\IQueryBuilder;
  12. use OCP\IConfig;
  13. use OCP\IDBConnection;
  14. use OCP\IGroupManager;
  15. use OCP\Migration\IOutput;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Psr\Log\LoggerInterface;
  18. use Test\TestCase;
  19. use function in_array;
  20. class RepairDavSharesTest extends TestCase {
  21. private IOutput&MockObject $output;
  22. private IConfig&MockObject $config;
  23. private IDBConnection&MockObject $dbc;
  24. private LoggerInterface&MockObject $logger;
  25. private IGroupManager&MockObject $groupManager;
  26. private RepairDavShares $repair;
  27. public function setUp(): void {
  28. parent::setUp();
  29. $this->output = $this->createMock(IOutput::class);
  30. $this->config = $this->createMock(IConfig::class);
  31. $this->dbc = $this->createMock(IDBConnection::class);
  32. $this->groupManager = $this->createMock(IGroupManager::class);
  33. $this->logger = $this->createMock(LoggerInterface::class);
  34. $this->repair = new RepairDavShares(
  35. $this->config,
  36. $this->dbc,
  37. $this->groupManager,
  38. $this->logger
  39. );
  40. }
  41. public function testRun(): void {
  42. $this->config->expects($this->any())
  43. ->method('getSystemValueString')
  44. ->with('version', '0.0.0')
  45. ->willReturn('20.0.2');
  46. $this->output->expects($this->atLeastOnce())
  47. ->method('info');
  48. $existingGroups = [
  49. 'Innocent',
  50. 'Wants Repair',
  51. 'Well förmed',
  52. 'family+friends',
  53. 'family friends',
  54. ];
  55. $shareResultData = [
  56. [
  57. // No update, nothing to escape
  58. 'id' => 0,
  59. 'principaluri' => 'principals/groups/Innocent',
  60. ],
  61. [
  62. // Update
  63. 'id' => 1,
  64. 'principaluri' => 'principals/groups/Wants Repair',
  65. ],
  66. [
  67. // No update, already proper
  68. 'id' => 2,
  69. 'principaluri' => 'principals/groups/Well+f%C3%B6rmed',
  70. ],
  71. [
  72. // No update, unknown group
  73. 'id' => 3,
  74. 'principaluri' => 'principals/groups/Not known',
  75. ],
  76. [
  77. // No update, unknown group
  78. 'id' => 4,
  79. 'principaluri' => 'principals/groups/Also%2F%2FNot%23Known',
  80. ],
  81. [
  82. // No update, group exists in both forms
  83. 'id' => 5,
  84. 'principaluri' => 'principals/groups/family+friends',
  85. ],
  86. [
  87. // No update, already proper
  88. 'id' => 6,
  89. 'principaluri' => 'principals/groups/family%2Bfriends',
  90. ],
  91. [
  92. // Update
  93. 'id' => 7,
  94. 'principaluri' => 'principals/groups/family friends',
  95. ],
  96. ];
  97. $shareResults = $this->createMock(IResult::class);
  98. $shareResults->expects($this->any())
  99. ->method('fetch')
  100. ->willReturnCallback(function () use (&$shareResultData) {
  101. return array_pop($shareResultData);
  102. });
  103. $expressionBuilder = $this->createMock(IExpressionBuilder::class);
  104. $selectMock = $this->createMock(IQueryBuilder::class);
  105. $selectMock->expects($this->any())
  106. ->method('expr')
  107. ->willReturn($expressionBuilder);
  108. $selectMock->expects($this->once())
  109. ->method('select')
  110. ->willReturnSelf();
  111. $selectMock->expects($this->once())
  112. ->method('from')
  113. ->willReturnSelf();
  114. $selectMock->expects($this->once())
  115. ->method('where')
  116. ->willReturnSelf();
  117. $selectMock->expects($this->once())
  118. ->method('execute')
  119. ->willReturn($shareResults);
  120. $updateCalls = [];
  121. $updateMock = $this->createMock(IQueryBuilder::class);
  122. $updateMock->expects($this->any())
  123. ->method('expr')
  124. ->willReturn($expressionBuilder);
  125. $updateMock->expects($this->once())
  126. ->method('update')
  127. ->willReturnSelf();
  128. $updateMock->expects($this->any())
  129. ->method('set')
  130. ->willReturnSelf();
  131. $updateMock->expects($this->once())
  132. ->method('where')
  133. ->willReturnSelf();
  134. $updateMock->expects($this->exactly(4))
  135. ->method('setParameter')
  136. ->willReturnCallback(function () use (&$updateCalls, &$updateMock) {
  137. $updateCalls[] = func_get_args();
  138. return $updateMock;
  139. });
  140. $updateMock->expects($this->exactly(2))
  141. ->method('execute');
  142. $this->dbc->expects($this->atLeast(2))
  143. ->method('getQueryBuilder')
  144. ->willReturnOnConsecutiveCalls($selectMock, $updateMock);
  145. $this->groupManager->expects($this->any())
  146. ->method('groupExists')
  147. ->willReturnCallback(function (string $gid) use ($existingGroups) {
  148. return in_array($gid, $existingGroups);
  149. });
  150. $this->repair->run($this->output);
  151. self::assertEquals([
  152. ['updatedPrincipalUri', 'principals/groups/' . urlencode('family friends'), null],
  153. ['shareId', 7, null],
  154. ['updatedPrincipalUri', 'principals/groups/' . urlencode('Wants Repair'), null],
  155. ['shareId', 1, null]
  156. ], $updateCalls);
  157. }
  158. }