You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.3 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
  5. *
  6. * @author Vitor Mattos <vitor@php.rio>
  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\Talk\Tests\BackgroundJob;
  25. use OCA\Talk\BackgroundJob\RemoveEmptyRooms;
  26. use OCA\Talk\Federation\FederationManager;
  27. use OCA\Talk\Manager;
  28. use OCA\Talk\Room;
  29. use OCA\Talk\Service\ParticipantService;
  30. use OCA\Talk\Service\RoomService;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Files\Config\IUserMountCache;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. use Psr\Log\LoggerInterface;
  35. use Test\TestCase;
  36. class RemoveEmptyRoomsTest extends TestCase {
  37. protected ITimeFactory&MockObject $timeFactory;
  38. protected Manager&MockObject $manager;
  39. protected RoomService&MockObject $roomService;
  40. protected ParticipantService&MockObject $participantService;
  41. protected FederationManager&MockObject $federationManager;
  42. protected LoggerInterface&MockObject $loggerInterface;
  43. protected IUserMountCache&MockObject $userMountCache;
  44. public function setUp(): void {
  45. parent::setUp();
  46. $this->timeFactory = $this->createMock(ITimeFactory::class);
  47. $this->manager = $this->createMock(Manager::class);
  48. $this->roomService = $this->createMock(RoomService::class);
  49. $this->participantService = $this->createMock(ParticipantService::class);
  50. $this->federationManager = $this->createMock(FederationManager::class);
  51. $this->loggerInterface = $this->createMock(LoggerInterface::class);
  52. $this->userMountCache = $this->createMock(IUserMountCache::class);
  53. }
  54. public function getBackgroundJob(): RemoveEmptyRooms {
  55. return new RemoveEmptyRooms(
  56. $this->timeFactory,
  57. $this->manager,
  58. $this->roomService,
  59. $this->participantService,
  60. $this->federationManager,
  61. $this->loggerInterface,
  62. $this->userMountCache,
  63. );
  64. }
  65. public function testDoDeleteRoom(): void {
  66. $backgroundJob = $this->getBackgroundJob();
  67. $room = $this->createMock(Room::class);
  68. $room->method('getType')
  69. ->willReturn(Room::TYPE_GROUP);
  70. $numDeletedRooms = self::invokePrivate($backgroundJob, 'numDeletedRooms');
  71. $this->assertEquals(0, $numDeletedRooms, 'Invalid default quantity of rooms');
  72. self::invokePrivate($backgroundJob, 'doDeleteRoom', [$room]);
  73. $numDeletedRooms = self::invokePrivate($backgroundJob, 'numDeletedRooms');
  74. $this->assertEquals(1, $numDeletedRooms, 'Invalid final quantity of rooms');
  75. }
  76. public static function dataDeleteIfFileIsRemoved(): array {
  77. return [
  78. ['', [], 0],
  79. ['email', [], 0],
  80. ['file', ['fileExists'], 0],
  81. ['file', [], 1],
  82. ];
  83. }
  84. /**
  85. * @dataProvider dataDeleteIfFileIsRemoved
  86. */
  87. public function testDeleteIfFileIsRemoved(string $objectType, array $fileList, int $numDeletedRoomsExpected): void {
  88. $backgroundJob = $this->getBackgroundJob();
  89. $numDeletedRoomsActual = self::invokePrivate($backgroundJob, 'numDeletedRooms');
  90. $this->assertEquals(0, $numDeletedRoomsActual, 'Invalid default quantity of rooms');
  91. $room = $this->createMock(Room::class);
  92. $room->method('getType')
  93. ->willReturn(Room::TYPE_GROUP);
  94. $room->method('getObjectType')
  95. ->willReturn($objectType);
  96. $userMountCache = self::invokePrivate($backgroundJob, 'userMountCache');
  97. $userMountCache->method('getMountsForFileId')
  98. ->willReturn($fileList);
  99. self::invokePrivate($backgroundJob, 'deleteIfFileIsRemoved', [$room]);
  100. $numDeletedRoomsActual = self::invokePrivate($backgroundJob, 'numDeletedRooms');
  101. $this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
  102. }
  103. public static function dataDeleteIfIsEmpty(): array {
  104. return [
  105. 'room with user' => ['', 1, 0, 0],
  106. 'room with fed invite' => ['', 0, 1, 0],
  107. 'room to delete' => ['', 0, 0, 1],
  108. 'file room with user' => ['file', 1, 0, 0],
  109. 'email room with user' => ['email', 1, 0, 0],
  110. 'email room without user' => ['email', 0, 0, 1]
  111. ];
  112. }
  113. /**
  114. * @dataProvider dataDeleteIfIsEmpty
  115. */
  116. public function testDeleteIfIsEmpty(string $objectType, int $actorsCount, int $inviteCount, int $numDeletedRoomsExpected): void {
  117. $backgroundJob = $this->getBackgroundJob();
  118. $numDeletedRoomsActual = self::invokePrivate($backgroundJob, 'numDeletedRooms');
  119. $this->assertEquals(0, $numDeletedRoomsActual, 'Invalid default quantity of rooms');
  120. $room = $this->createMock(Room::class);
  121. $room->method('getType')
  122. ->willReturn(Room::TYPE_GROUP);
  123. $room->method('getObjectType')
  124. ->willReturn($objectType);
  125. $room->method('isFederatedConversation')
  126. ->willReturn($inviteCount > 0);
  127. $this->federationManager->method('getNumberOfInvitations')
  128. ->with($room)
  129. ->willReturn($inviteCount);
  130. $participantService = self::invokePrivate($backgroundJob, 'participantService');
  131. $participantService->method('getNumberOfActors')
  132. ->willReturn($actorsCount);
  133. self::invokePrivate($backgroundJob, 'deleteIfIsEmpty', [$room]);
  134. $numDeletedRoomsActual = self::invokePrivate($backgroundJob, 'numDeletedRooms');
  135. $this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
  136. }
  137. /**
  138. * @dataProvider dataCallback
  139. */
  140. public function testCallback(int $roomType, string $objectType, int $numDeletedRoomsExpected): void {
  141. $backgroundJob = $this->getBackgroundJob();
  142. $room = $this->createMock(Room::class);
  143. $room->method('getType')
  144. ->willReturn($roomType);
  145. $room->method('getObjectType')
  146. ->willReturn($objectType);
  147. $backgroundJob->callback($room);
  148. $numDeletedRoomsActual = self::invokePrivate($backgroundJob, 'numDeletedRooms');
  149. $this->assertEquals($numDeletedRoomsExpected, $numDeletedRoomsActual, 'Invalid final quantity of rooms');
  150. }
  151. public static function dataCallback(): array {
  152. return [
  153. [Room::TYPE_CHANGELOG, '', 0],
  154. [Room::TYPE_GROUP, 'file', 1],
  155. ];
  156. }
  157. }