ManagerTest.php 8.6 KB

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