ManagerTest.php 7.6 KB

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