123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace Test\Support\Subscription;
- use OC\Support\Subscription\Registry;
- use OC\User\Database;
- use OCP\IConfig;
- use OCP\IGroup;
- use OCP\IGroupManager;
- use OCP\IServerContainer;
- use OCP\IUserManager;
- use OCP\Notification\IManager;
- use OCP\Support\Subscription\ISubscription;
- use OCP\Support\Subscription\ISupportedApps;
- use OCP\User\Backend\ICountUsersBackend;
- use OCP\UserInterface;
- use PHPUnit\Framework\MockObject\MockObject;
- use Psr\Log\LoggerInterface;
- use Test\TestCase;
- class RegistryTest extends TestCase {
-
- private $registry;
-
- private $config;
-
- private $serverContainer;
-
- private $userManager;
-
- private $groupManager;
-
- private $logger;
-
- private $notificationManager;
- protected function setUp(): void {
- parent::setUp();
- $this->config = $this->createMock(IConfig::class);
- $this->serverContainer = $this->createMock(IServerContainer::class);
- $this->userManager = $this->createMock(IUserManager::class);
- $this->groupManager = $this->createMock(IGroupManager::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->notificationManager = $this->createMock(IManager::class);
- $this->registry = new Registry(
- $this->config,
- $this->serverContainer,
- $this->userManager,
- $this->groupManager,
- $this->logger
- );
- }
-
- public function testDelegateToNone() {
- $this->registry->delegateHasValidSubscription();
- $this->addToAssertionCount(1);
- }
- public function testDoubleRegistration() {
- $this->expectException(\OCP\Support\Subscription\Exception\AlreadyRegisteredException::class);
-
- $subscription1 = $this->createMock(ISubscription::class);
-
- $subscription2 = $this->createMock(ISubscription::class);
- $this->registry->register($subscription1);
- $this->registry->register($subscription2);
- }
- public function testNoSupportApp() {
- $this->assertSame([], $this->registry->delegateGetSupportedApps());
- $this->assertSame(false, $this->registry->delegateHasValidSubscription());
- }
- public function testDelegateHasValidSubscription() {
-
- $subscription = $this->createMock(ISubscription::class);
- $subscription->expects($this->once())
- ->method('hasValidSubscription')
- ->willReturn(true);
- $this->registry->register($subscription);
- $this->assertSame(true, $this->registry->delegateHasValidSubscription());
- }
- public function testDelegateHasValidSubscriptionConfig() {
-
- $this->config->expects($this->once())
- ->method('getSystemValueBool')
- ->with('has_valid_subscription')
- ->willReturn(true);
- $this->assertSame(true, $this->registry->delegateHasValidSubscription());
- }
- public function testDelegateHasExtendedSupport() {
-
- $subscription = $this->createMock(ISubscription::class);
- $subscription->expects($this->once())
- ->method('hasExtendedSupport')
- ->willReturn(true);
- $this->registry->register($subscription);
- $this->assertSame(true, $this->registry->delegateHasExtendedSupport());
- }
- public function testDelegateGetSupportedApps() {
-
- $subscription = $this->createMock(ISupportedApps::class);
- $subscription->expects($this->once())
- ->method('getSupportedApps')
- ->willReturn(['abc']);
- $this->registry->register($subscription);
- $this->assertSame(['abc'], $this->registry->delegateGetSupportedApps());
- }
- public function testSubscriptionService() {
- $this->serverContainer->method('query')
- ->with(DummySubscription::class)
- ->willReturn(new DummySubscription(true, false, false));
- $this->registry->registerService(DummySubscription::class);
- $this->assertTrue($this->registry->delegateHasValidSubscription());
- $this->assertFalse($this->registry->delegateHasExtendedSupport());
- }
- public function testDelegateIsHardUserLimitReached() {
-
- $subscription = $this->createMock(ISubscription::class);
- $subscription->expects($this->once())
- ->method('hasValidSubscription')
- ->willReturn(true);
- $subscription->expects($this->once())
- ->method('isHardUserLimitReached')
- ->willReturn(true);
- $this->registry->register($subscription);
- $dummyGroup = $this->createMock(IGroup::class);
- $dummyGroup->expects($this->once())
- ->method('getUsers')
- ->willReturn([]);
- $this->groupManager->expects($this->once())
- ->method('get')
- ->willReturn($dummyGroup);
- $this->assertSame(true, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
- }
- public function testDelegateIsHardUserLimitReachedWithoutSupportApp() {
- $this->config->expects($this->once())
- ->method('getSystemValueBool')
- ->with('one-click-instance')
- ->willReturn(false);
- $this->assertSame(false, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
- }
- public function dataForUserLimitCheck() {
- return [
-
- [35, 15, 2, false],
- [35, 45, 15, false],
- [35, 45, 5, true],
- [35, 45, 55, false],
- ];
- }
-
- public function testDelegateIsHardUserLimitReachedWithoutSupportAppAndUserCount($userLimit, $userCount, $disabledUsers, $expectedResult) {
- $this->config->expects($this->once())
- ->method('getSystemValueBool')
- ->with('one-click-instance')
- ->willReturn(true);
- $this->config->expects($this->once())
- ->method('getSystemValueInt')
- ->with('one-click-instance.user-limit')
- ->willReturn($userLimit);
- $this->config->expects($this->once())
- ->method('getUsersForUserValue')
- ->with('core', 'enabled', 'false')
- ->willReturn(array_fill(0, $disabledUsers, ''));
-
- $dummyBackend = $this->createMock(Database::class);
- $dummyBackend->expects($this->once())
- ->method('implementsActions')
- ->willReturn(true);
- $dummyBackend->expects($this->once())
- ->method('countUsers')
- ->willReturn($userCount);
- $this->userManager->expects($this->once())
- ->method('getBackends')
- ->willReturn([$dummyBackend]);
- if ($expectedResult) {
- $dummyGroup = $this->createMock(IGroup::class);
- $dummyGroup->expects($this->once())
- ->method('getUsers')
- ->willReturn([]);
- $this->groupManager->expects($this->once())
- ->method('get')
- ->willReturn($dummyGroup);
- }
- $this->assertSame($expectedResult, $this->registry->delegateIsHardUserLimitReached($this->notificationManager));
- }
- }
|