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