RegistryTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Support\Subscription;
  22. use OC\Support\Subscription\Registry;
  23. use OCP\IConfig;
  24. use OCP\IGroup;
  25. use OCP\IGroupManager;
  26. use OCP\IServerContainer;
  27. use OCP\IUserManager;
  28. use OCP\Notification\IManager;
  29. use OCP\Support\Subscription\ISubscription;
  30. use OCP\Support\Subscription\ISupportedApps;
  31. use PHPUnit\Framework\MockObject\MockObject;
  32. use Psr\Log\LoggerInterface;
  33. use Test\TestCase;
  34. class RegistryTest extends TestCase {
  35. /** @var Registry */
  36. private $registry;
  37. /** @var MockObject|IConfig */
  38. private $config;
  39. /** @var MockObject|IServerContainer */
  40. private $serverContainer;
  41. /** @var MockObject|IUserManager */
  42. private $userManager;
  43. /** @var MockObject|IGroupManager */
  44. private $groupManager;
  45. /** @var MockObject|LoggerInterface */
  46. private $logger;
  47. /** @var MockObject|IManager */
  48. private $notificationManager;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->config = $this->createMock(IConfig::class);
  52. $this->serverContainer = $this->createMock(IServerContainer::class);
  53. $this->userManager = $this->createMock(IUserManager::class);
  54. $this->groupManager = $this->createMock(IGroupManager::class);
  55. $this->logger = $this->createMock(LoggerInterface::class);
  56. $this->notificationManager = $this->createMock(IManager::class);
  57. $this->registry = new Registry(
  58. $this->config,
  59. $this->serverContainer,
  60. $this->userManager,
  61. $this->groupManager,
  62. $this->logger,
  63. $this->notificationManager
  64. );
  65. }
  66. /**
  67. * Doesn't assert anything, just checks whether anything "explodes"
  68. */
  69. public function testDelegateToNone() {
  70. $this->registry->delegateHasValidSubscription();
  71. $this->addToAssertionCount(1);
  72. }
  73. public function testDoubleRegistration() {
  74. $this->expectException(\OCP\Support\Subscription\Exception\AlreadyRegisteredException::class);
  75. /* @var ISubscription $subscription1 */
  76. $subscription1 = $this->createMock(ISubscription::class);
  77. /* @var ISubscription $subscription2 */
  78. $subscription2 = $this->createMock(ISubscription::class);
  79. $this->registry->register($subscription1);
  80. $this->registry->register($subscription2);
  81. }
  82. public function testNoSupportApp() {
  83. $this->assertSame([], $this->registry->delegateGetSupportedApps());
  84. $this->assertSame(false, $this->registry->delegateHasValidSubscription());
  85. }
  86. public function testDelegateHasValidSubscription() {
  87. /* @var ISubscription|\PHPUnit\Framework\MockObject\MockObject $subscription */
  88. $subscription = $this->createMock(ISubscription::class);
  89. $subscription->expects($this->once())
  90. ->method('hasValidSubscription')
  91. ->willReturn(true);
  92. $this->registry->register($subscription);
  93. $this->assertSame(true, $this->registry->delegateHasValidSubscription());
  94. }
  95. public function testDelegateHasValidSubscriptionConfig() {
  96. /* @var ISubscription|\PHPUnit\Framework\MockObject\MockObject $subscription */
  97. $this->config->expects($this->once())
  98. ->method('getSystemValueBool')
  99. ->with('has_valid_subscription')
  100. ->willReturn(true);
  101. $this->assertSame(true, $this->registry->delegateHasValidSubscription());
  102. }
  103. public function testDelegateHasExtendedSupport() {
  104. /* @var ISubscription|\PHPUnit\Framework\MockObject\MockObject $subscription */
  105. $subscription = $this->createMock(ISubscription::class);
  106. $subscription->expects($this->once())
  107. ->method('hasExtendedSupport')
  108. ->willReturn(true);
  109. $this->registry->register($subscription);
  110. $this->assertSame(true, $this->registry->delegateHasExtendedSupport());
  111. }
  112. public function testDelegateGetSupportedApps() {
  113. /* @var ISupportedApps|\PHPUnit\Framework\MockObject\MockObject $subscription */
  114. $subscription = $this->createMock(ISupportedApps::class);
  115. $subscription->expects($this->once())
  116. ->method('getSupportedApps')
  117. ->willReturn(['abc']);
  118. $this->registry->register($subscription);
  119. $this->assertSame(['abc'], $this->registry->delegateGetSupportedApps());
  120. }
  121. public function testSubscriptionService() {
  122. $this->serverContainer->method('query')
  123. ->with(DummySubscription::class)
  124. ->willReturn(new DummySubscription(true, false, false));
  125. $this->registry->registerService(DummySubscription::class);
  126. $this->assertTrue($this->registry->delegateHasValidSubscription());
  127. $this->assertFalse($this->registry->delegateHasExtendedSupport());
  128. }
  129. public function testDelegateIsHardUserLimitReached() {
  130. /* @var ISubscription|\PHPUnit\Framework\MockObject\MockObject $subscription */
  131. $subscription = $this->createMock(ISubscription::class);
  132. $subscription->expects($this->once())
  133. ->method('hasValidSubscription')
  134. ->willReturn(true);
  135. $subscription->expects($this->once())
  136. ->method('isHardUserLimitReached')
  137. ->willReturn(true);
  138. $this->registry->register($subscription);
  139. $dummyGroup = $this->createMock(IGroup::class);
  140. $dummyGroup->expects($this->once())
  141. ->method('getUsers')
  142. ->willReturn([]);
  143. $this->groupManager->expects($this->once())
  144. ->method('get')
  145. ->willReturn($dummyGroup);
  146. $this->assertSame(true, $this->registry->delegateIsHardUserLimitReached());
  147. }
  148. public function testDelegateIsHardUserLimitReachedWithoutSupportApp() {
  149. $this->config->expects($this->once())
  150. ->method('getSystemValueBool')
  151. ->with('one-click-instance')
  152. ->willReturn(false);
  153. $this->assertSame(false, $this->registry->delegateIsHardUserLimitReached());
  154. }
  155. public function dataForUserLimitCheck() {
  156. return [
  157. // $userLimit, $userCount, $disabledUsers, $expectedResult
  158. [35, 15, 2, false],
  159. [35, 45, 15, false],
  160. [35, 45, 5, true],
  161. [35, 45, 55, false],
  162. ];
  163. }
  164. /**
  165. * @dataProvider dataForUserLimitCheck
  166. */
  167. public function testDelegateIsHardUserLimitReachedWithoutSupportAppAndUserCount($userLimit, $userCount, $disabledUsers, $expectedResult) {
  168. $this->config->expects($this->once())
  169. ->method('getSystemValueBool')
  170. ->with('one-click-instance')
  171. ->willReturn(true);
  172. $this->config->expects($this->once())
  173. ->method('getSystemValue')
  174. ->with('one-click-instance.user-limit')
  175. ->willReturn($userLimit);
  176. $this->config->expects($this->once())
  177. ->method('getUsersForUserValue')
  178. ->with('core', 'enabled', 'false')
  179. ->willReturn(array_fill(0, $disabledUsers, ''));
  180. /* @var UserInterface|\PHPUnit\Framework\MockObject\MockObject $dummyBackend */
  181. $dummyBackend = $this->createMock(UserInterface::class);
  182. $dummyBackend->expects($this->once())
  183. ->method('implementsActions')
  184. ->willReturn(true);
  185. $dummyBackend->expects($this->once())
  186. ->method('countUsers')
  187. ->willReturn($userCount);
  188. $this->userManager->expects($this->once())
  189. ->method('getBackends')
  190. ->willReturn([$dummyBackend]);
  191. if ($expectedResult) {
  192. $dummyGroup = $this->createMock(IGroup::class);
  193. $dummyGroup->expects($this->once())
  194. ->method('getUsers')
  195. ->willReturn([]);
  196. $this->groupManager->expects($this->once())
  197. ->method('get')
  198. ->willReturn($dummyGroup);
  199. }
  200. $this->assertSame($expectedResult, $this->registry->delegateIsHardUserLimitReached());
  201. }
  202. }