RemoveDeletedUsersCalendarSubscriptionsTest.php 4.0 KB

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