mirror of
https://github.com/nextcloud/server.git
synced 2026-06-08 16:26:59 -04:00
Merge pull request #56044 from nextcloud/backport/55676/stable32
[stable32] Add unit tests for AdminDelegation command and AuthorizedGroupService
This commit is contained in:
commit
92d8ca31cc
3 changed files with 204 additions and 1 deletions
|
|
@ -36,7 +36,7 @@ class Add extends Base {
|
|||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
public function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$settingClass = $input->getArgument('settingClass');
|
||||
if (!in_array(IDelegatedSettings::class, (array)class_implements($settingClass), true)) {
|
||||
|
|
|
|||
117
apps/settings/tests/Command/AdminDelegation/AddTest.php
Normal file
117
apps/settings/tests/Command/AdminDelegation/AddTest.php
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Settings\Tests\Command\AdminDelegation;
|
||||
|
||||
use OC\Settings\AuthorizedGroup;
|
||||
use OCA\Settings\Command\AdminDelegation\Add;
|
||||
use OCA\Settings\Service\AuthorizedGroupService;
|
||||
use OCP\IGroupManager;
|
||||
use OCP\Settings\IManager;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Test\TestCase;
|
||||
|
||||
class AddTest extends TestCase {
|
||||
|
||||
private IManager&MockObject $settingManager;
|
||||
private AuthorizedGroupService&MockObject $authorizedGroupService;
|
||||
private IGroupManager&MockObject $groupManager;
|
||||
private Add $command;
|
||||
private InputInterface&MockObject $input;
|
||||
private OutputInterface&MockObject $output;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->settingManager = $this->createMock(IManager::class);
|
||||
$this->authorizedGroupService = $this->createMock(AuthorizedGroupService::class);
|
||||
$this->groupManager = $this->createMock(IGroupManager::class);
|
||||
|
||||
$this->command = new Add(
|
||||
$this->settingManager,
|
||||
$this->authorizedGroupService,
|
||||
$this->groupManager
|
||||
);
|
||||
|
||||
$this->input = $this->createMock(InputInterface::class);
|
||||
$this->output = $this->createMock(OutputInterface::class);
|
||||
}
|
||||
|
||||
public function testExecuteSuccessfulDelegation(): void {
|
||||
$settingClass = \OCA\Settings\Settings\Admin\Server::class;
|
||||
$groupId = 'testgroup';
|
||||
|
||||
// Mock valid delegated settings class
|
||||
$this->input->expects($this->exactly(2))
|
||||
->method('getArgument')
|
||||
->willReturnMap([
|
||||
['settingClass', $settingClass],
|
||||
['groupId', $groupId]
|
||||
]);
|
||||
|
||||
// Mock group exists
|
||||
$this->groupManager->expects($this->once())
|
||||
->method('groupExists')
|
||||
->with($groupId)
|
||||
->willReturn(true);
|
||||
|
||||
// Mock successful creation
|
||||
$authorizedGroup = new AuthorizedGroup();
|
||||
$authorizedGroup->setGroupId($groupId);
|
||||
$authorizedGroup->setClass($settingClass);
|
||||
|
||||
$this->authorizedGroupService->expects($this->once())
|
||||
->method('create')
|
||||
->with($groupId, $settingClass)
|
||||
->willReturn($authorizedGroup);
|
||||
|
||||
$result = $this->command->execute($this->input, $this->output);
|
||||
|
||||
$this->assertEquals(0, $result);
|
||||
}
|
||||
|
||||
public function testExecuteInvalidSettingClass(): void {
|
||||
// Use a real class that exists but doesn't implement IDelegatedSettings
|
||||
$settingClass = 'stdClass';
|
||||
|
||||
$this->input->expects($this->once())
|
||||
->method('getArgument')
|
||||
->with('settingClass')
|
||||
->willReturn($settingClass);
|
||||
|
||||
$result = $this->command->execute($this->input, $this->output);
|
||||
|
||||
// Should return exit code 2 for invalid setting class
|
||||
$this->assertEquals(2, $result);
|
||||
}
|
||||
|
||||
public function testExecuteNonExistentGroup(): void {
|
||||
$settingClass = \OCA\Settings\Settings\Admin\Server::class;
|
||||
$groupId = 'nonexistentgroup';
|
||||
|
||||
$this->input->expects($this->exactly(2))
|
||||
->method('getArgument')
|
||||
->willReturnMap([
|
||||
['settingClass', $settingClass],
|
||||
['groupId', $groupId]
|
||||
]);
|
||||
|
||||
// Mock group does not exist
|
||||
$this->groupManager->expects($this->once())
|
||||
->method('groupExists')
|
||||
->with($groupId)
|
||||
->willReturn(false);
|
||||
|
||||
$result = $this->command->execute($this->input, $this->output);
|
||||
|
||||
// Should return exit code 3 for non-existent group
|
||||
$this->assertEquals(3, $result);
|
||||
}
|
||||
}
|
||||
86
apps/settings/tests/Service/AuthorizedGroupServiceTest.php
Normal file
86
apps/settings/tests/Service/AuthorizedGroupServiceTest.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace OCA\Settings\Tests\Service;
|
||||
|
||||
use OC\Settings\AuthorizedGroup;
|
||||
use OC\Settings\AuthorizedGroupMapper;
|
||||
use OCA\Settings\Service\AuthorizedGroupService;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Test\TestCase;
|
||||
|
||||
class AuthorizedGroupServiceTest extends TestCase {
|
||||
|
||||
private AuthorizedGroupMapper&MockObject $mapper;
|
||||
private AuthorizedGroupService $service;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->mapper = $this->createMock(AuthorizedGroupMapper::class);
|
||||
$this->service = new AuthorizedGroupService($this->mapper);
|
||||
}
|
||||
|
||||
public function testCreateAllowsDifferentGroupsSameClass(): void {
|
||||
$groupId1 = 'testgroup1';
|
||||
$groupId2 = 'testgroup2';
|
||||
$class = 'TestClass';
|
||||
|
||||
$expectedGroup1 = new AuthorizedGroup();
|
||||
$expectedGroup1->setGroupId($groupId1);
|
||||
$expectedGroup1->setClass($class);
|
||||
$expectedGroup1->setId(123);
|
||||
|
||||
$expectedGroup2 = new AuthorizedGroup();
|
||||
$expectedGroup2->setGroupId($groupId2);
|
||||
$expectedGroup2->setClass($class);
|
||||
$expectedGroup2->setId(124);
|
||||
|
||||
$this->mapper->expects($this->exactly(2))
|
||||
->method('insert')
|
||||
->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2);
|
||||
|
||||
// Both creations should succeed
|
||||
$result1 = $this->service->create($groupId1, $class);
|
||||
$this->assertEquals($groupId1, $result1->getGroupId());
|
||||
$this->assertEquals($class, $result1->getClass());
|
||||
|
||||
$result2 = $this->service->create($groupId2, $class);
|
||||
$this->assertEquals($groupId2, $result2->getGroupId());
|
||||
$this->assertEquals($class, $result2->getClass());
|
||||
}
|
||||
|
||||
public function testCreateAllowsSameGroupDifferentClasses(): void {
|
||||
$groupId = 'testgroup';
|
||||
$class1 = 'TestClass1';
|
||||
$class2 = 'TestClass2';
|
||||
|
||||
$expectedGroup1 = new AuthorizedGroup();
|
||||
$expectedGroup1->setGroupId($groupId);
|
||||
$expectedGroup1->setClass($class1);
|
||||
$expectedGroup1->setId(123);
|
||||
|
||||
$expectedGroup2 = new AuthorizedGroup();
|
||||
$expectedGroup2->setGroupId($groupId);
|
||||
$expectedGroup2->setClass($class2);
|
||||
$expectedGroup2->setId(124);
|
||||
|
||||
$this->mapper->expects($this->exactly(2))
|
||||
->method('insert')
|
||||
->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2);
|
||||
|
||||
// Both creations should succeed
|
||||
$result1 = $this->service->create($groupId, $class1);
|
||||
$result2 = $this->service->create($groupId, $class2);
|
||||
|
||||
$this->assertEquals($groupId, $result1->getGroupId());
|
||||
$this->assertEquals($groupId, $result2->getGroupId());
|
||||
$this->assertEquals($class1, $result1->getClass());
|
||||
$this->assertEquals($class2, $result2->getClass());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue