ManagerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Notification;
  23. use OC\AppFramework\Bootstrap\Coordinator;
  24. use OC\AppFramework\Bootstrap\RegistrationContext;
  25. use OC\AppFramework\Bootstrap\ServiceRegistration;
  26. use OC\Notification\Manager;
  27. use OCP\ICache;
  28. use OCP\ICacheFactory;
  29. use OCP\IUserManager;
  30. use OCP\Notification\IManager;
  31. use OCP\Notification\INotification;
  32. use OCP\RichObjectStrings\IValidator;
  33. use OCP\Support\Subscription\IRegistry;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Psr\Log\LoggerInterface;
  36. use Test\TestCase;
  37. class ManagerTest extends TestCase {
  38. /** @var IManager */
  39. protected $manager;
  40. /** @var IValidator|MockObject */
  41. protected $validator;
  42. /** @var IUserManager|MockObject */
  43. protected $userManager;
  44. /** @var ICacheFactory|MockObject */
  45. protected $cacheFactory;
  46. /** @var ICache|MockObject */
  47. protected $cache;
  48. /** @var IRegistry|MockObject */
  49. protected $subscriptionRegistry;
  50. /** @var LoggerInterface|MockObject */
  51. protected $logger;
  52. /** @var Coordinator|MockObject */
  53. protected $coordinator;
  54. /** @var RegistrationContext|MockObject */
  55. protected $registrationContext;
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->validator = $this->createMock(IValidator::class);
  59. $this->userManager = $this->createMock(IUserManager::class);
  60. $this->cache = $this->createMock(ICache::class);
  61. $this->subscriptionRegistry = $this->createMock(IRegistry::class);
  62. $this->logger = $this->createMock(LoggerInterface::class);
  63. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  64. $this->cacheFactory->method('createDistributed')
  65. ->with('notifications')
  66. ->willReturn($this->cache);
  67. $this->registrationContext = $this->createMock(RegistrationContext::class);
  68. $this->coordinator = $this->createMock(Coordinator::class);
  69. $this->coordinator->method('getRegistrationContext')
  70. ->willReturn($this->registrationContext);
  71. $this->manager = new Manager($this->validator, $this->userManager, $this->cacheFactory, $this->subscriptionRegistry, $this->logger, $this->coordinator);
  72. }
  73. public function testRegisterApp(): void {
  74. $this->assertEquals([], self::invokePrivate($this->manager, 'getApps'));
  75. $this->manager->registerApp(DummyApp::class);
  76. $this->assertCount(1, self::invokePrivate($this->manager, 'getApps'));
  77. $this->assertCount(1, self::invokePrivate($this->manager, 'getApps'));
  78. $this->manager->registerApp(DummyApp::class);
  79. $this->assertCount(2, self::invokePrivate($this->manager, 'getApps'));
  80. }
  81. public function testRegisterAppInvalid(): void {
  82. $this->manager->registerApp(DummyNotifier::class);
  83. $this->logger->expects($this->once())
  84. ->method('error');
  85. self::invokePrivate($this->manager, 'getApps');
  86. }
  87. public function testRegisterNotifier(): void {
  88. $this->assertEquals([], self::invokePrivate($this->manager, 'getNotifiers'));
  89. $this->manager->registerNotifierService(DummyNotifier::class);
  90. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  91. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  92. $this->manager->registerNotifierService(DummyNotifier::class);
  93. $this->assertCount(2, self::invokePrivate($this->manager, 'getNotifiers'));
  94. }
  95. public function testRegisterNotifierBootstrap(): void {
  96. $this->registrationContext->method('getNotifierServices')
  97. ->willReturn([
  98. new ServiceRegistration('app', DummyNotifier::class),
  99. ]);
  100. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  101. $this->assertCount(1, self::invokePrivate($this->manager, 'getNotifiers'));
  102. }
  103. public function testRegisterNotifierInvalid(): void {
  104. $this->manager->registerNotifierService(DummyApp::class);
  105. $this->logger->expects($this->once())
  106. ->method('error');
  107. self::invokePrivate($this->manager, 'getNotifiers');
  108. }
  109. public function testCreateNotification(): void {
  110. $action = $this->manager->createNotification();
  111. $this->assertInstanceOf(INotification::class, $action);
  112. }
  113. public function testNotify(): void {
  114. /** @var INotification|MockObject $notification */
  115. $notification = $this->getMockBuilder(INotification::class)
  116. ->disableOriginalConstructor()
  117. ->getMock();
  118. $notification->expects($this->once())
  119. ->method('isValid')
  120. ->willReturn(true);
  121. $manager = $this->getMockBuilder(Manager::class)
  122. ->setConstructorArgs([
  123. $this->validator,
  124. $this->userManager,
  125. $this->cacheFactory,
  126. $this->subscriptionRegistry,
  127. $this->logger,
  128. $this->coordinator,
  129. ])
  130. ->setMethods(['getApps'])
  131. ->getMock();
  132. $manager->expects($this->once())
  133. ->method('getApps')
  134. ->willReturn([]);
  135. $manager->notify($notification);
  136. }
  137. public function testNotifyInvalid(): void {
  138. $this->expectException(\InvalidArgumentException::class);
  139. /** @var INotification|MockObject $notification */
  140. $notification = $this->getMockBuilder(INotification::class)
  141. ->disableOriginalConstructor()
  142. ->getMock();
  143. $notification->expects($this->once())
  144. ->method('isValid')
  145. ->willReturn(false);
  146. $manager = $this->getMockBuilder(Manager::class)
  147. ->setConstructorArgs([
  148. $this->validator,
  149. $this->userManager,
  150. $this->cacheFactory,
  151. $this->subscriptionRegistry,
  152. $this->logger,
  153. $this->coordinator,
  154. ])
  155. ->setMethods(['getApps'])
  156. ->getMock();
  157. $manager->expects($this->never())
  158. ->method('getApps');
  159. $manager->notify($notification);
  160. }
  161. public function testMarkProcessed(): void {
  162. /** @var INotification|MockObject $notification */
  163. $notification = $this->getMockBuilder(INotification::class)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $manager = $this->getMockBuilder(Manager::class)
  167. ->setConstructorArgs([
  168. $this->validator,
  169. $this->userManager,
  170. $this->cacheFactory,
  171. $this->subscriptionRegistry,
  172. $this->logger,
  173. $this->coordinator,
  174. ])
  175. ->setMethods(['getApps'])
  176. ->getMock();
  177. $manager->expects($this->once())
  178. ->method('getApps')
  179. ->willReturn([]);
  180. $manager->markProcessed($notification);
  181. }
  182. public function testGetCount(): void {
  183. /** @var INotification|MockObject $notification */
  184. $notification = $this->getMockBuilder(INotification::class)
  185. ->disableOriginalConstructor()
  186. ->getMock();
  187. $manager = $this->getMockBuilder(Manager::class)
  188. ->setConstructorArgs([
  189. $this->validator,
  190. $this->userManager,
  191. $this->cacheFactory,
  192. $this->subscriptionRegistry,
  193. $this->logger,
  194. $this->coordinator,
  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. }