RepairDavSharesTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\Repair;
  25. use OC\Repair\RepairDavShares;
  26. use OCP\DB\IResult;
  27. use OCP\DB\QueryBuilder\IExpressionBuilder;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\IConfig;
  30. use OCP\IDBConnection;
  31. use OCP\IGroupManager;
  32. use Psr\Log\LoggerInterface;
  33. use Test\TestCase;
  34. use OCP\Migration\IOutput;
  35. use function in_array;
  36. class RepairDavSharesTest extends TestCase {
  37. /** @var IOutput|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $output;
  39. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $config;
  41. /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  42. protected $dbc;
  43. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  44. protected $groupManager;
  45. /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
  46. protected $logger;
  47. /** @var RepairDavSharesTest */
  48. protected $repair;
  49. public function setUp(): void {
  50. parent::setUp();
  51. $this->output = $this->createMock(IOutput::class);
  52. $this->config = $this->createMock(IConfig::class);
  53. $this->dbc = $this->createMock(IDBConnection::class);
  54. $this->groupManager = $this->createMock(IGroupManager::class);
  55. $this->logger = $this->createMock(LoggerInterface::class);
  56. $this->repair = new RepairDavShares(
  57. $this->config,
  58. $this->dbc,
  59. $this->groupManager,
  60. $this->logger
  61. );
  62. }
  63. public function testRun() {
  64. $this->config->expects($this->any())
  65. ->method('getSystemValue')
  66. ->with('version', '0.0.0')
  67. ->willReturn('20.0.2');
  68. $this->output->expects($this->atLeastOnce())
  69. ->method('info');
  70. $existingGroups = [
  71. 'Innocent',
  72. 'Wants Repair',
  73. 'Well förmed',
  74. 'family+friends',
  75. 'family friends',
  76. ];
  77. $shareResultData = [
  78. [
  79. // No update, nothing to escape
  80. 'id' => 0,
  81. 'principaluri' => 'principals/groups/Innocent',
  82. ],
  83. [
  84. // Update
  85. 'id' => 1,
  86. 'principaluri' => 'principals/groups/Wants Repair',
  87. ],
  88. [
  89. // No update, already proper
  90. 'id' => 2,
  91. 'principaluri' => 'principals/groups/Well+f%C3%B6rmed',
  92. ],
  93. [
  94. // No update, unknown group
  95. 'id' => 3,
  96. 'principaluri' => 'principals/groups/Not known',
  97. ],
  98. [
  99. // No update, unknown group
  100. 'id' => 4,
  101. 'principaluri' => 'principals/groups/Also%2F%2FNot%23Known',
  102. ],
  103. [
  104. // No update, group exists in both forms
  105. 'id' => 5,
  106. 'principaluri' => 'principals/groups/family+friends',
  107. ],
  108. [
  109. // No update, already proper
  110. 'id' => 6,
  111. 'principaluri' => 'principals/groups/family%2Bfriends',
  112. ],
  113. [
  114. // Update
  115. 'id' => 7,
  116. 'principaluri' => 'principals/groups/family friends',
  117. ],
  118. ];
  119. $shareResults = $this->createMock(IResult::class);
  120. $shareResults->expects($this->any())
  121. ->method('fetch')
  122. ->willReturnCallback(function () use (&$shareResultData) {
  123. return array_pop($shareResultData);
  124. });
  125. $expressionBuilder = $this->createMock(IExpressionBuilder::class);
  126. $selectMock = $this->createMock(IQueryBuilder::class);
  127. $selectMock->expects($this->any())
  128. ->method('expr')
  129. ->willReturn($expressionBuilder);
  130. $selectMock->expects($this->once())
  131. ->method('select')
  132. ->willReturnSelf();
  133. $selectMock->expects($this->once())
  134. ->method('from')
  135. ->willReturnSelf();
  136. $selectMock->expects($this->once())
  137. ->method('where')
  138. ->willReturnSelf();
  139. $selectMock->expects($this->once())
  140. ->method('execute')
  141. ->willReturn($shareResults);
  142. $updateMock = $this->createMock(IQueryBuilder::class);
  143. $updateMock->expects($this->any())
  144. ->method('expr')
  145. ->willReturn($expressionBuilder);
  146. $updateMock->expects($this->once())
  147. ->method('update')
  148. ->willReturnSelf();
  149. $updateMock->expects($this->any())
  150. ->method('set')
  151. ->willReturnSelf();
  152. $updateMock->expects($this->once())
  153. ->method('where')
  154. ->willReturnSelf();
  155. $updateMock->expects($this->exactly(4))
  156. ->method('setParameter')
  157. ->withConsecutive(
  158. ['updatedPrincipalUri', 'principals/groups/' . urlencode('family friends')],
  159. ['shareId', 7],
  160. ['updatedPrincipalUri', 'principals/groups/' . urlencode('Wants Repair')],
  161. ['shareId', 1],
  162. )
  163. ->willReturnSelf();
  164. $updateMock->expects($this->exactly(2))
  165. ->method('execute');
  166. $this->dbc->expects($this->atLeast(2))
  167. ->method('getQueryBuilder')
  168. ->willReturnOnConsecutiveCalls($selectMock, $updateMock);
  169. $this->groupManager->expects($this->any())
  170. ->method('groupExists')
  171. ->willReturnCallback(function (string $gid) use ($existingGroups) {
  172. return in_array($gid, $existingGroups);
  173. });
  174. $this->repair->run($this->output);
  175. }
  176. }