ManagerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace Test\Notification;
  9. use OC\AppFramework\Bootstrap\Coordinator;
  10. use OC\AppFramework\Bootstrap\RegistrationContext;
  11. use OC\AppFramework\Bootstrap\ServiceRegistration;
  12. use OC\Notification\Manager;
  13. use OCP\ICache;
  14. use OCP\ICacheFactory;
  15. use OCP\IUserManager;
  16. use OCP\Notification\IManager;
  17. use OCP\Notification\INotification;
  18. use OCP\RichObjectStrings\IValidator;
  19. use OCP\Support\Subscription\IRegistry;
  20. use PHPUnit\Framework\MockObject\MockObject;
  21. use Psr\Log\LoggerInterface;
  22. use Test\TestCase;
  23. class ManagerTest extends TestCase {
  24. /** @var IManager */
  25. protected $manager;
  26. /** @var IValidator|MockObject */
  27. protected $validator;
  28. /** @var IUserManager|MockObject */
  29. protected $userManager;
  30. /** @var ICacheFactory|MockObject */
  31. protected $cacheFactory;
  32. /** @var ICache|MockObject */
  33. protected $cache;
  34. /** @var IRegistry|MockObject */
  35. protected $subscriptionRegistry;
  36. /** @var LoggerInterface|MockObject */
  37. protected $logger;
  38. /** @var Coordinator|MockObject */
  39. protected $coordinator;
  40. /** @var RegistrationContext|MockObject */
  41. protected $registrationContext;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->validator = $this->createMock(IValidator::class);
  45. $this->userManager = $this->createMock(IUserManager::class);
  46. $this->cache = $this->createMock(ICache::class);
  47. $this->subscriptionRegistry = $this->createMock(IRegistry::class);
  48. $this->logger = $this->createMock(LoggerInterface::class);
  49. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  50. $this->cacheFactory->method('createDistributed')
  51. ->with('notifications')
  52. ->willReturn($this->cache);
  53. $this->registrationContext = $this->createMock(RegistrationContext::class);
  54. $this->coordinator = $this->createMock(Coordinator::class);
  55. $this->coordinator->method('getRegistrationContext')
  56. ->willReturn($this->registrationContext);
  57. $this->manager = new Manager($this->validator, $this->userManager, $this->cacheFactory, $this->subscriptionRegistry, $this->logger, $this->coordinator);
  58. }
  59. public function testRegisterApp(): void {
  60. $this->assertEquals([], self::invokePrivate($this->manager, 'getApps'));
  61. $this->manager->registerApp(DummyApp::class);
  62. $this->assertCount(1, self::invokePrivate($this->manager, 'getApps'));
  63. $this->assertCount(1, self::invokePrivate($this->manager, 'getApps'));
  64. $this->manager->registerApp(DummyApp::class);
  65. $this->assertCount(2, self::invokePrivate($this->manager, 'getApps'));
  66. }
  67. public function testRegisterAppInvalid(): void {
  68. $this->manager->registerApp(DummyNotifier::class);
  69. $this->logger->expects($this->once())
  70. ->method('error');
  71. self::invokePrivate($this->manager, 'getApps');
  72. }
  73. public function testRegisterNotifier(): void {
  74. $this->assertEquals([], self::invokePrivate($this->manager, 'getNotifiers'));
  75. $this->manager->registerNotifierService(DummyNotifier::class);
  76. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  77. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  78. $this->manager->registerNotifierService(DummyNotifier::class);
  79. $this->assertCount(2, self::invokePrivate($this->manager, 'getNotifiers'));
  80. }
  81. public function testRegisterNotifierBootstrap(): void {
  82. $this->registrationContext->method('getNotifierServices')
  83. ->willReturn([
  84. new ServiceRegistration('app', DummyNotifier::class),
  85. ]);
  86. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  87. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  88. }
  89. public function testRegisterNotifierInvalid(): void {
  90. $this->manager->registerNotifierService(DummyApp::class);
  91. $this->logger->expects($this->once())
  92. ->method('error');
  93. self::invokePrivate($this->manager, 'getNotifiers');
  94. }
  95. public function testCreateNotification(): void {
  96. $action = $this->manager->createNotification();
  97. $this->assertInstanceOf(INotification::class, $action);
  98. }
  99. public function testNotify(): void {
  100. /** @var INotification|MockObject $notification */
  101. $notification = $this->getMockBuilder(INotification::class)
  102. ->disableOriginalConstructor()
  103. ->getMock();
  104. $notification->expects($this->once())
  105. ->method('isValid')
  106. ->willReturn(true);
  107. $manager = $this->getMockBuilder(Manager::class)
  108. ->setConstructorArgs([
  109. $this->validator,
  110. $this->userManager,
  111. $this->cacheFactory,
  112. $this->subscriptionRegistry,
  113. $this->logger,
  114. $this->coordinator,
  115. ])
  116. ->setMethods(['getApps'])
  117. ->getMock();
  118. $manager->expects($this->once())
  119. ->method('getApps')
  120. ->willReturn([]);
  121. $manager->notify($notification);
  122. }
  123. public function testNotifyInvalid(): void {
  124. $this->expectException(\InvalidArgumentException::class);
  125. /** @var INotification|MockObject $notification */
  126. $notification = $this->getMockBuilder(INotification::class)
  127. ->disableOriginalConstructor()
  128. ->getMock();
  129. $notification->expects($this->once())
  130. ->method('isValid')
  131. ->willReturn(false);
  132. $manager = $this->getMockBuilder(Manager::class)
  133. ->setConstructorArgs([
  134. $this->validator,
  135. $this->userManager,
  136. $this->cacheFactory,
  137. $this->subscriptionRegistry,
  138. $this->logger,
  139. $this->coordinator,
  140. ])
  141. ->setMethods(['getApps'])
  142. ->getMock();
  143. $manager->expects($this->never())
  144. ->method('getApps');
  145. $manager->notify($notification);
  146. }
  147. public function testMarkProcessed(): void {
  148. /** @var INotification|MockObject $notification */
  149. $notification = $this->getMockBuilder(INotification::class)
  150. ->disableOriginalConstructor()
  151. ->getMock();
  152. $manager = $this->getMockBuilder(Manager::class)
  153. ->setConstructorArgs([
  154. $this->validator,
  155. $this->userManager,
  156. $this->cacheFactory,
  157. $this->subscriptionRegistry,
  158. $this->logger,
  159. $this->coordinator,
  160. ])
  161. ->setMethods(['getApps'])
  162. ->getMock();
  163. $manager->expects($this->once())
  164. ->method('getApps')
  165. ->willReturn([]);
  166. $manager->markProcessed($notification);
  167. }
  168. public function testGetCount(): void {
  169. /** @var INotification|MockObject $notification */
  170. $notification = $this->getMockBuilder(INotification::class)
  171. ->disableOriginalConstructor()
  172. ->getMock();
  173. $manager = $this->getMockBuilder(Manager::class)
  174. ->setConstructorArgs([
  175. $this->validator,
  176. $this->userManager,
  177. $this->cacheFactory,
  178. $this->subscriptionRegistry,
  179. $this->logger,
  180. $this->coordinator,
  181. ])
  182. ->setMethods(['getApps'])
  183. ->getMock();
  184. $manager->expects($this->once())
  185. ->method('getApps')
  186. ->willReturn([]);
  187. $manager->getCount($notification);
  188. }
  189. public function dataIsFairUseOfFreePushService(): array {
  190. return [
  191. [true, 999, true],
  192. [true, 1000, true],
  193. [false, 999, true],
  194. [false, 1000, false],
  195. ];
  196. }
  197. /**
  198. * @dataProvider dataIsFairUseOfFreePushService
  199. * @param bool $hasValidSubscription
  200. * @param int $userCount
  201. * @param bool $isFair
  202. */
  203. public function testIsFairUseOfFreePushService(bool $hasValidSubscription, int $userCount, bool $isFair): void {
  204. $this->subscriptionRegistry->method('delegateHasValidSubscription')
  205. ->willReturn($hasValidSubscription);
  206. $this->userManager->method('countSeenUsers')
  207. ->willReturn($userCount);
  208. $this->assertSame($isFair, $this->manager->isFairUseOfFreePushService());
  209. }
  210. }