1
0

RegistryTest.php 7.7 KB

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