Skip to content

Commit 92d8ca3

Browse files
Merge pull request #56044 from nextcloud/backport/55676/stable32
[stable32] Add unit tests for AdminDelegation command and AuthorizedGroupService
2 parents 72a3754 + 170d936 commit 92d8ca3

File tree

3 files changed

+204
-1
lines changed

3 files changed

+204
-1
lines changed

apps/settings/lib/Command/AdminDelegation/Add.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function configure(): void {
3636
;
3737
}
3838

39-
protected function execute(InputInterface $input, OutputInterface $output): int {
39+
public function execute(InputInterface $input, OutputInterface $output): int {
4040
$io = new SymfonyStyle($input, $output);
4141
$settingClass = $input->getArgument('settingClass');
4242
if (!in_array(IDelegatedSettings::class, (array)class_implements($settingClass), true)) {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Settings\Tests\Command\AdminDelegation;
10+
11+
use OC\Settings\AuthorizedGroup;
12+
use OCA\Settings\Command\AdminDelegation\Add;
13+
use OCA\Settings\Service\AuthorizedGroupService;
14+
use OCP\IGroupManager;
15+
use OCP\Settings\IManager;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Test\TestCase;
20+
21+
class AddTest extends TestCase {
22+
23+
private IManager&MockObject $settingManager;
24+
private AuthorizedGroupService&MockObject $authorizedGroupService;
25+
private IGroupManager&MockObject $groupManager;
26+
private Add $command;
27+
private InputInterface&MockObject $input;
28+
private OutputInterface&MockObject $output;
29+
30+
protected function setUp(): void {
31+
parent::setUp();
32+
33+
$this->settingManager = $this->createMock(IManager::class);
34+
$this->authorizedGroupService = $this->createMock(AuthorizedGroupService::class);
35+
$this->groupManager = $this->createMock(IGroupManager::class);
36+
37+
$this->command = new Add(
38+
$this->settingManager,
39+
$this->authorizedGroupService,
40+
$this->groupManager
41+
);
42+
43+
$this->input = $this->createMock(InputInterface::class);
44+
$this->output = $this->createMock(OutputInterface::class);
45+
}
46+
47+
public function testExecuteSuccessfulDelegation(): void {
48+
$settingClass = \OCA\Settings\Settings\Admin\Server::class;
49+
$groupId = 'testgroup';
50+
51+
// Mock valid delegated settings class
52+
$this->input->expects($this->exactly(2))
53+
->method('getArgument')
54+
->willReturnMap([
55+
['settingClass', $settingClass],
56+
['groupId', $groupId]
57+
]);
58+
59+
// Mock group exists
60+
$this->groupManager->expects($this->once())
61+
->method('groupExists')
62+
->with($groupId)
63+
->willReturn(true);
64+
65+
// Mock successful creation
66+
$authorizedGroup = new AuthorizedGroup();
67+
$authorizedGroup->setGroupId($groupId);
68+
$authorizedGroup->setClass($settingClass);
69+
70+
$this->authorizedGroupService->expects($this->once())
71+
->method('create')
72+
->with($groupId, $settingClass)
73+
->willReturn($authorizedGroup);
74+
75+
$result = $this->command->execute($this->input, $this->output);
76+
77+
$this->assertEquals(0, $result);
78+
}
79+
80+
public function testExecuteInvalidSettingClass(): void {
81+
// Use a real class that exists but doesn't implement IDelegatedSettings
82+
$settingClass = 'stdClass';
83+
84+
$this->input->expects($this->once())
85+
->method('getArgument')
86+
->with('settingClass')
87+
->willReturn($settingClass);
88+
89+
$result = $this->command->execute($this->input, $this->output);
90+
91+
// Should return exit code 2 for invalid setting class
92+
$this->assertEquals(2, $result);
93+
}
94+
95+
public function testExecuteNonExistentGroup(): void {
96+
$settingClass = \OCA\Settings\Settings\Admin\Server::class;
97+
$groupId = 'nonexistentgroup';
98+
99+
$this->input->expects($this->exactly(2))
100+
->method('getArgument')
101+
->willReturnMap([
102+
['settingClass', $settingClass],
103+
['groupId', $groupId]
104+
]);
105+
106+
// Mock group does not exist
107+
$this->groupManager->expects($this->once())
108+
->method('groupExists')
109+
->with($groupId)
110+
->willReturn(false);
111+
112+
$result = $this->command->execute($this->input, $this->output);
113+
114+
// Should return exit code 3 for non-existent group
115+
$this->assertEquals(3, $result);
116+
}
117+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Settings\Tests\Service;
11+
12+
use OC\Settings\AuthorizedGroup;
13+
use OC\Settings\AuthorizedGroupMapper;
14+
use OCA\Settings\Service\AuthorizedGroupService;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use Test\TestCase;
17+
18+
class AuthorizedGroupServiceTest extends TestCase {
19+
20+
private AuthorizedGroupMapper&MockObject $mapper;
21+
private AuthorizedGroupService $service;
22+
23+
protected function setUp(): void {
24+
parent::setUp();
25+
$this->mapper = $this->createMock(AuthorizedGroupMapper::class);
26+
$this->service = new AuthorizedGroupService($this->mapper);
27+
}
28+
29+
public function testCreateAllowsDifferentGroupsSameClass(): void {
30+
$groupId1 = 'testgroup1';
31+
$groupId2 = 'testgroup2';
32+
$class = 'TestClass';
33+
34+
$expectedGroup1 = new AuthorizedGroup();
35+
$expectedGroup1->setGroupId($groupId1);
36+
$expectedGroup1->setClass($class);
37+
$expectedGroup1->setId(123);
38+
39+
$expectedGroup2 = new AuthorizedGroup();
40+
$expectedGroup2->setGroupId($groupId2);
41+
$expectedGroup2->setClass($class);
42+
$expectedGroup2->setId(124);
43+
44+
$this->mapper->expects($this->exactly(2))
45+
->method('insert')
46+
->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2);
47+
48+
// Both creations should succeed
49+
$result1 = $this->service->create($groupId1, $class);
50+
$this->assertEquals($groupId1, $result1->getGroupId());
51+
$this->assertEquals($class, $result1->getClass());
52+
53+
$result2 = $this->service->create($groupId2, $class);
54+
$this->assertEquals($groupId2, $result2->getGroupId());
55+
$this->assertEquals($class, $result2->getClass());
56+
}
57+
58+
public function testCreateAllowsSameGroupDifferentClasses(): void {
59+
$groupId = 'testgroup';
60+
$class1 = 'TestClass1';
61+
$class2 = 'TestClass2';
62+
63+
$expectedGroup1 = new AuthorizedGroup();
64+
$expectedGroup1->setGroupId($groupId);
65+
$expectedGroup1->setClass($class1);
66+
$expectedGroup1->setId(123);
67+
68+
$expectedGroup2 = new AuthorizedGroup();
69+
$expectedGroup2->setGroupId($groupId);
70+
$expectedGroup2->setClass($class2);
71+
$expectedGroup2->setId(124);
72+
73+
$this->mapper->expects($this->exactly(2))
74+
->method('insert')
75+
->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2);
76+
77+
// Both creations should succeed
78+
$result1 = $this->service->create($groupId, $class1);
79+
$result2 = $this->service->create($groupId, $class2);
80+
81+
$this->assertEquals($groupId, $result1->getGroupId());
82+
$this->assertEquals($groupId, $result2->getGroupId());
83+
$this->assertEquals($class1, $result1->getClass());
84+
$this->assertEquals($class2, $result2->getClass());
85+
}
86+
}

0 commit comments

Comments
 (0)