@@ -18,42 +18,42 @@ |
||
18 | 18 | use Symfony\Component\Console\Style\SymfonyStyle; |
19 | 19 | |
20 | 20 | class Add extends Base { |
21 | - public function __construct( |
|
22 | - private IManager $settingManager, |
|
23 | - private AuthorizedGroupService $authorizedGroupService, |
|
24 | - private IGroupManager $groupManager, |
|
25 | - ) { |
|
26 | - parent::__construct(); |
|
27 | - } |
|
28 | - |
|
29 | - protected function configure(): void { |
|
30 | - $this |
|
31 | - ->setName('admin-delegation:add') |
|
32 | - ->setDescription('add setting delegation to a group') |
|
33 | - ->addArgument('settingClass', InputArgument::REQUIRED, 'Admin setting class') |
|
34 | - ->addArgument('groupId', InputArgument::REQUIRED, 'Delegate to group ID') |
|
35 | - ->addUsage('\'OCA\Settings\Settings\Admin\Server\' mygroup') |
|
36 | - ; |
|
37 | - } |
|
38 | - |
|
39 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
40 | - $io = new SymfonyStyle($input, $output); |
|
41 | - $settingClass = $input->getArgument('settingClass'); |
|
42 | - if (!in_array(IDelegatedSettings::class, (array)class_implements($settingClass), true)) { |
|
43 | - $io->error('The specified class isn’t a valid delegated setting.'); |
|
44 | - return 2; |
|
45 | - } |
|
46 | - |
|
47 | - $groupId = $input->getArgument('groupId'); |
|
48 | - if (!$this->groupManager->groupExists($groupId)) { |
|
49 | - $io->error('The specified group didn’t exist.'); |
|
50 | - return 3; |
|
51 | - } |
|
52 | - |
|
53 | - $this->authorizedGroupService->create($groupId, $settingClass); |
|
54 | - |
|
55 | - $io->success('Administration of ' . $settingClass . ' delegated to ' . $groupId . '.'); |
|
56 | - |
|
57 | - return 0; |
|
58 | - } |
|
21 | + public function __construct( |
|
22 | + private IManager $settingManager, |
|
23 | + private AuthorizedGroupService $authorizedGroupService, |
|
24 | + private IGroupManager $groupManager, |
|
25 | + ) { |
|
26 | + parent::__construct(); |
|
27 | + } |
|
28 | + |
|
29 | + protected function configure(): void { |
|
30 | + $this |
|
31 | + ->setName('admin-delegation:add') |
|
32 | + ->setDescription('add setting delegation to a group') |
|
33 | + ->addArgument('settingClass', InputArgument::REQUIRED, 'Admin setting class') |
|
34 | + ->addArgument('groupId', InputArgument::REQUIRED, 'Delegate to group ID') |
|
35 | + ->addUsage('\'OCA\Settings\Settings\Admin\Server\' mygroup') |
|
36 | + ; |
|
37 | + } |
|
38 | + |
|
39 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
40 | + $io = new SymfonyStyle($input, $output); |
|
41 | + $settingClass = $input->getArgument('settingClass'); |
|
42 | + if (!in_array(IDelegatedSettings::class, (array)class_implements($settingClass), true)) { |
|
43 | + $io->error('The specified class isn’t a valid delegated setting.'); |
|
44 | + return 2; |
|
45 | + } |
|
46 | + |
|
47 | + $groupId = $input->getArgument('groupId'); |
|
48 | + if (!$this->groupManager->groupExists($groupId)) { |
|
49 | + $io->error('The specified group didn’t exist.'); |
|
50 | + return 3; |
|
51 | + } |
|
52 | + |
|
53 | + $this->authorizedGroupService->create($groupId, $settingClass); |
|
54 | + |
|
55 | + $io->success('Administration of ' . $settingClass . ' delegated to ' . $groupId . '.'); |
|
56 | + |
|
57 | + return 0; |
|
58 | + } |
|
59 | 59 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | /** |
5 | 5 | * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
6 | 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | public function execute(InputInterface $input, OutputInterface $output): int { |
40 | 40 | $io = new SymfonyStyle($input, $output); |
41 | 41 | $settingClass = $input->getArgument('settingClass'); |
42 | - if (!in_array(IDelegatedSettings::class, (array)class_implements($settingClass), true)) { |
|
42 | + if (!in_array(IDelegatedSettings::class, (array) class_implements($settingClass), true)) { |
|
43 | 43 | $io->error('The specified class isn’t a valid delegated setting.'); |
44 | 44 | return 2; |
45 | 45 | } |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | |
53 | 53 | $this->authorizedGroupService->create($groupId, $settingClass); |
54 | 54 | |
55 | - $io->success('Administration of ' . $settingClass . ' delegated to ' . $groupId . '.'); |
|
55 | + $io->success('Administration of '.$settingClass.' delegated to '.$groupId.'.'); |
|
56 | 56 | |
57 | 57 | return 0; |
58 | 58 | } |
@@ -20,98 +20,98 @@ |
||
20 | 20 | |
21 | 21 | class AddTest extends TestCase { |
22 | 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 | - } |
|
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 | 117 | } |
@@ -17,70 +17,70 @@ |
||
17 | 17 | |
18 | 18 | class AuthorizedGroupServiceTest extends TestCase { |
19 | 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 | - } |
|
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 | 86 | } |