1
0

RemoveDeletedUsersCalendarSubscriptionsTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Thomas Citharel <nextcloud@tcit.fr>
  5. *
  6. * @author Thomas Citharel <nextcloud@tcit.fr>
  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 OCA\DAV\Tests\unit\DAV\Migration;
  25. use OCA\DAV\Migration\RemoveDeletedUsersCalendarSubscriptions;
  26. use OCP\DB\IResult;
  27. use OCP\DB\QueryBuilder\IExpressionBuilder;
  28. use OCP\DB\QueryBuilder\IFunctionBuilder;
  29. use OCP\DB\QueryBuilder\IParameter;
  30. use OCP\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\DB\QueryBuilder\IQueryFunction;
  32. use OCP\IDBConnection;
  33. use OCP\IUserManager;
  34. use OCP\Migration\IOutput;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Test\TestCase;
  37. class RemoveDeletedUsersCalendarSubscriptionsTest extends TestCase {
  38. /**
  39. * @var IDBConnection|MockObject
  40. */
  41. private $dbConnection;
  42. /**
  43. * @var IUserManager|MockObject
  44. */
  45. private $userManager;
  46. /**
  47. * @var IOutput|MockObject
  48. */
  49. private $output;
  50. /**
  51. * @var RemoveDeletedUsersCalendarSubscriptions
  52. */
  53. private $migration;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->dbConnection = $this->createMock(IDBConnection::class);
  57. $this->userManager = $this->createMock(IUserManager::class);
  58. $this->output = $this->createMock(IOutput::class);
  59. $this->migration = new RemoveDeletedUsersCalendarSubscriptions($this->dbConnection, $this->userManager);
  60. }
  61. public function testGetName(): void {
  62. $this->assertEquals(
  63. 'Clean up old calendar subscriptions from deleted users that were not cleaned-up',
  64. $this->migration->getName()
  65. );
  66. }
  67. /**
  68. * @dataProvider dataTestRun
  69. * @param array $subscriptions
  70. * @param array $userExists
  71. * @param int $deletions
  72. * @throws \Exception
  73. */
  74. public function testRun(array $subscriptions, array $userExists, int $deletions): void {
  75. $qb = $this->createMock(IQueryBuilder::class);
  76. $qb->method('select')->willReturn($qb);
  77. $functionBuilder = $this->createMock(IFunctionBuilder::class);
  78. $qb->method('func')->willReturn($functionBuilder);
  79. $functionBuilder->method('count')->willReturn($this->createMock(IQueryFunction::class));
  80. $qb->method('selectDistinct')
  81. ->with(['id', 'principaluri'])
  82. ->willReturn($qb);
  83. $qb->method('from')
  84. ->with('calendarsubscriptions')
  85. ->willReturn($qb);
  86. $qb->method('setMaxResults')
  87. ->willReturn($qb);
  88. $qb->method('setFirstResult')
  89. ->willReturn($qb);
  90. $result = $this->createMock(IResult::class);
  91. $qb->method('execute')
  92. ->willReturn($result);
  93. $result->expects($this->once())
  94. ->method('fetchOne')
  95. ->willReturn(count($subscriptions));
  96. $result
  97. ->method('fetch')
  98. ->willReturnOnConsecutiveCalls(...$subscriptions);
  99. $qb->method('delete')
  100. ->with('calendarsubscriptions')
  101. ->willReturn($qb);
  102. $expr = $this->createMock(IExpressionBuilder::class);
  103. $qb->method('expr')->willReturn($expr);
  104. $qb->method('createNamedParameter')->willReturn($this->createMock(IParameter::class));
  105. $qb->method('where')->willReturn($qb);
  106. // Only when user exists
  107. $qb->expects($this->exactly($deletions))->method('executeStatement');
  108. $this->dbConnection->method('getQueryBuilder')->willReturn($qb);
  109. $this->output->expects($this->once())->method('startProgress');
  110. $this->output->expects($subscriptions === [] ? $this->never(): $this->once())->method('advance');
  111. if (count($subscriptions)) {
  112. $this->userManager->method('userExists')
  113. ->willReturnCallback(function (string $username) use ($userExists) {
  114. return $userExists[$username];
  115. });
  116. }
  117. $this->output->expects($this->once())->method('finishProgress');
  118. $this->output->expects($this->once())->method('info')->with(sprintf('%d calendar subscriptions without an user have been cleaned up', $deletions));
  119. $this->migration->run($this->output);
  120. }
  121. public function dataTestRun(): array {
  122. return [
  123. [[], [], 0],
  124. [[[
  125. 'id' => 1,
  126. 'principaluri' => 'users/principals/foo1',
  127. ],
  128. [
  129. 'id' => 2,
  130. 'principaluri' => 'users/principals/bar1',
  131. ],
  132. [
  133. 'id' => 3,
  134. 'principaluri' => 'users/principals/bar1',
  135. ]], ['foo1' => true, 'bar1' => false], 2]
  136. ];
  137. }
  138. }