RegistryTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Support\Subscription;
  7. use OC\Support\Subscription\Registry;
  8. use OC\User\Database;
  9. use OCP\IConfig;
  10. use OCP\IGroup;
  11. use OCP\IGroupManager;
  12. use OCP\IServerContainer;
  13. use OCP\IUserManager;
  14. use OCP\Notification\IManager;
  15. use OCP\Support\Subscription\ISubscription;
  16. use OCP\Support\Subscription\ISupportedApps;
  17. use OCP\User\Backend\ICountUsersBackend;
  18. use OCP\UserInterface;
  19. use PHPUnit\Framework\MockObject\MockObject;
  20. use Psr\Log\LoggerInterface;
  21. use Test\TestCase;
  22. class RegistryTest extends TestCase {
  23. /** @var Registry */
  24. private $registry;
  25. /** @var MockObject|IConfig */
  26. private $config;
  27. /** @var MockObject|IServerContainer */
  28. private $serverContainer;
  29. /** @var MockObject|IUserManager */
  30. private $userManager;
  31. /** @var MockObject|IGroupManager */
  32. private $groupManager;
  33. /** @var MockObject|LoggerInterface */
  34. private $logger;
  35. /** @var MockObject|IManager */
  36. private $notificationManager;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->config = $this->createMock(IConfig::class);
  40. $this->serverContainer = $this->createMock(IServerContainer::class);
  41. $this->userManager = $this->createMock(IUserManager::class);
  42. $this->groupManager = $this->createMock(IGroupManager::class);
  43. $this->logger = $this->createMock(LoggerInterface::class);
  44. $this->notificationManager = $this->createMock(IManager::class);
  45. $this->registry = new Registry(
  46. $this->config,
  47. $this->serverContainer,
  48. $this->userManager,
  49. $this->groupManager,
  50. $this->logger
  51. );
  52. }
  53. /**
  54. * Doesn't assert anything, just checks whether anything "explodes"
  55. */
  56. public function testDelegateToNone() {
  57. $this->registry->delegateHasValidSubscription();
  58. $this->addToAssertionCount(1);
  59. }
  60. public function testDoubleRegistration() {
  61. $this->expectException(\OCP\Support\Subscription\Exception\AlreadyRegisteredException::class);
  62. /* @var ISubscription $subscription1 */
  63. $subscription1 = $this->createMock(ISubscription::class);
  64. /* @var ISubscription $subscription2 */
  65. $subscription2 = $this->createMock(ISubscription::class);
  66. $this->registry->register($subscription1);
  67. $this->registry->register($subscription2);
  68. }
  69. public function testNoSupportApp() {
  70. $this->assertSame([], $this->registry->delegateGetSupportedApps());
  71. $this->assertSame(false, $this->registry->delegateHasValidSubscription());
  72. }
  73. public function testDelegateHasValidSubscription() {
  74. /* @var ISubscription|\PHPUnit\Framework\MockObject\MockObject $subscription */
  75. $subscription = $this->createMock(ISubscription::class);
  76. $subscription->expects($this->once())
  77. ->method('hasValidSubscription')
  78. ->willReturn(true);
  79. $this->registry->register($subscription);
  80. $this->assertSame(true, $this->registry->delegateHasValidSubscription());
  81. }
  82. public function testDelegateHasValidSubscriptionConfig() {
  83. /* @var ISubscription|\PHPUnit\Framework\MockObject\MockObject $subscription */
  84. $this->config->expects($this->once())
  85. ->method('getSystemValueBool')
  86. ->with('has_valid_subscription')
  87. ->willReturn(true);
  88. $this->assertSame(true, $this->registry->delegateHasValidSubscription());
  89. }
  90. public function testDelegateHasExtendedSupport() {
  91. /* @var ISubscription|\PHPUnit\Framework\MockObject\MockObject $subscription */
  92. $subscription = $this->createMock(ISubscription::class);
  93. $subscription->expects($this->once())
  94. ->method('hasExtendedSupport')
  95. ->willReturn(true);
  96. $this->registry->register($subscription);
  97. $this->assertSame(true, $this->registry->delegateHasExtendedSupport());
  98. }
  99. public function testDelegateGetSupportedApps() {
  100. /* @var ISupportedApps|\PHPUnit\Framework\MockObject\MockObject $subscription */
  101. $subscription = $this->createMock(ISupportedApps::class);
  102. $subscription->expects($this->once())
  103. ->method('getSupportedApps')
  104. ->willReturn(['abc']);
  105. $this->registry->register($subscription);
  106. $this->assertSame(['abc'], $this->registry->delegateGetSupportedApps());
  107. }
  108. public function testSubscriptionService() {
  109. $this->serverContainer->method('query')
  110. ->with(DummySubscription::class)
  111. ->willReturn(new DummySubscription(true, false, false));
  112. $this->registry->registerService(DummySubscription::class);
  113. $this->assertTrue($this->registry->delegateHasValidSubscription());
  114. $this->assertFalse($this->registry->delegateHasExtendedSupport());
  115. }
  116. public function testDelegateIsHardUserLimitReached() {
  117. /* @var ISubscription|\PHPUnit\Framework\MockObject\MockObject $subscription */
  118. $subscription = $this->createMock(ISubscription::class);
  119. $subscription->expects($this->once())
  120. ->method('hasValidSubscription')
  121. ->willReturn(true);
  122. $subscription->expects($this->once())
  123. ->method('isHardUserLimitReached')
  124. ->willReturn(true);
  125. $this->registry->register($subscription);
  126. $dummyGroup = $this->createMock(IGroup::class);
  127. $dummyGroup->expects($this->once())
  128. ->method('getUsers')
  129. ->willReturn([]);
  130. $this->groupManager->expects($this->once())
  131. ->method('get')
  132. ->willReturn($dummyGroup);
  133. $this->assertSame(true, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
  134. }
  135. public function testDelegateIsHardUserLimitReachedWithoutSupportApp() {
  136. $this->config->expects($this->once())
  137. ->method('getSystemValueBool')
  138. ->with('one-click-instance')
  139. ->willReturn(false);
  140. $this->assertSame(false, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
  141. }
  142. public function dataForUserLimitCheck() {
  143. return [
  144. // $userLimit, $userCount, $disabledUsers, $expectedResult
  145. [35, 15, 2, false],
  146. [35, 45, 15, false],
  147. [35, 45, 5, true],
  148. [35, 45, 55, false],
  149. ];
  150. }
  151. /**
  152. * @dataProvider dataForUserLimitCheck
  153. */
  154. public function testDelegateIsHardUserLimitReachedWithoutSupportAppAndUserCount($userLimit, $userCount, $disabledUsers, $expectedResult) {
  155. $this->config->expects($this->once())
  156. ->method('getSystemValueBool')
  157. ->with('one-click-instance')
  158. ->willReturn(true);
  159. $this->config->expects($this->once())
  160. ->method('getSystemValueInt')
  161. ->with('one-click-instance.user-limit')
  162. ->willReturn($userLimit);
  163. $this->config->expects($this->once())
  164. ->method('getUsersForUserValue')
  165. ->with('core', 'enabled', 'false')
  166. ->willReturn(array_fill(0, $disabledUsers, ''));
  167. /* @var UserInterface|ICountUsersBackend|\PHPUnit\Framework\MockObject\MockObject $dummyBackend */
  168. $dummyBackend = $this->createMock(Database::class);
  169. $dummyBackend->expects($this->once())
  170. ->method('implementsActions')
  171. ->willReturn(true);
  172. $dummyBackend->expects($this->once())
  173. ->method('countUsers')
  174. ->willReturn($userCount);
  175. $this->userManager->expects($this->once())
  176. ->method('getBackends')
  177. ->willReturn([$dummyBackend]);
  178. if ($expectedResult) {
  179. $dummyGroup = $this->createMock(IGroup::class);
  180. $dummyGroup->expects($this->once())
  181. ->method('getUsers')
  182. ->willReturn([]);
  183. $this->groupManager->expects($this->once())
  184. ->method('get')
  185. ->willReturn($dummyGroup);
  186. }
  187. $this->assertSame($expectedResult, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
  188. }
  189. }