|
| 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 | +} |
0 commit comments