@@ -20,138 +20,138 @@ |
||
| 20 | 20 | #[\PHPUnit\Framework\Attributes\Group('DB')] |
| 21 | 21 | class DuplicateAssignmentIntegrationTest extends TestCase { |
| 22 | 22 | |
| 23 | - private AuthorizedGroupService $service; |
|
| 24 | - private AuthorizedGroupMapper $mapper; |
|
| 25 | - |
|
| 26 | - protected function setUp(): void { |
|
| 27 | - parent::setUp(); |
|
| 28 | - |
|
| 29 | - // Use real mapper for integration testing |
|
| 30 | - $this->mapper = \OCP\Server::get(AuthorizedGroupMapper::class); |
|
| 31 | - $this->service = new AuthorizedGroupService($this->mapper); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - protected function tearDown(): void { |
|
| 35 | - // Clean up any test data |
|
| 36 | - try { |
|
| 37 | - $allGroups = $this->mapper->findAll(); |
|
| 38 | - foreach ($allGroups as $group) { |
|
| 39 | - if (str_starts_with($group->getGroupId(), 'test_') |
|
| 40 | - || str_starts_with($group->getClass(), 'TestClass')) { |
|
| 41 | - $this->mapper->delete($group); |
|
| 42 | - } |
|
| 43 | - } |
|
| 44 | - } catch (\Exception $e) { |
|
| 45 | - // Ignore cleanup errors |
|
| 46 | - } |
|
| 47 | - parent::tearDown(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function testDuplicateAssignmentPrevention(): void { |
|
| 51 | - $groupId = 'test_duplicate_group'; |
|
| 52 | - $class = 'TestClass\\DuplicateTest'; |
|
| 53 | - |
|
| 54 | - // First assignment should succeed |
|
| 55 | - $result1 = $this->service->create($groupId, $class); |
|
| 56 | - $this->assertInstanceOf(AuthorizedGroup::class, $result1); |
|
| 57 | - $this->assertEquals($groupId, $result1->getGroupId()); |
|
| 58 | - $this->assertEquals($class, $result1->getClass()); |
|
| 59 | - $this->assertNotNull($result1->getId()); |
|
| 60 | - |
|
| 61 | - // Second assignment of same group to same class should throw ConflictException |
|
| 62 | - $this->expectException(ConflictException::class); |
|
| 63 | - $this->expectExceptionMessage('Group is already assigned to this class'); |
|
| 64 | - |
|
| 65 | - $this->service->create($groupId, $class); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function testDifferentGroupsSameClassAllowed(): void { |
|
| 69 | - $groupId1 = 'test_group_1'; |
|
| 70 | - $groupId2 = 'test_group_2'; |
|
| 71 | - $class = 'TestClass\\MultiGroup'; |
|
| 72 | - |
|
| 73 | - // Both assignments should succeed |
|
| 74 | - $result1 = $this->service->create($groupId1, $class); |
|
| 75 | - $result2 = $this->service->create($groupId2, $class); |
|
| 76 | - |
|
| 77 | - $this->assertEquals($groupId1, $result1->getGroupId()); |
|
| 78 | - $this->assertEquals($groupId2, $result2->getGroupId()); |
|
| 79 | - $this->assertEquals($class, $result1->getClass()); |
|
| 80 | - $this->assertEquals($class, $result2->getClass()); |
|
| 81 | - $this->assertNotEquals($result1->getId(), $result2->getId()); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function testSameGroupDifferentClassesAllowed(): void { |
|
| 85 | - $groupId = 'test_multi_class_group'; |
|
| 86 | - $class1 = 'TestClass\\First'; |
|
| 87 | - $class2 = 'TestClass\\Second'; |
|
| 88 | - |
|
| 89 | - // Both assignments should succeed |
|
| 90 | - $result1 = $this->service->create($groupId, $class1); |
|
| 91 | - $result2 = $this->service->create($groupId, $class2); |
|
| 92 | - |
|
| 93 | - $this->assertEquals($groupId, $result1->getGroupId()); |
|
| 94 | - $this->assertEquals($groupId, $result2->getGroupId()); |
|
| 95 | - $this->assertEquals($class1, $result1->getClass()); |
|
| 96 | - $this->assertEquals($class2, $result2->getClass()); |
|
| 97 | - $this->assertNotEquals($result1->getId(), $result2->getId()); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - public function testCreateAfterDelete(): void { |
|
| 101 | - $groupId = 'test_recreate_group'; |
|
| 102 | - $class = 'TestClass\\Recreate'; |
|
| 103 | - |
|
| 104 | - // Create initial assignment |
|
| 105 | - $result1 = $this->service->create($groupId, $class); |
|
| 106 | - $initialId = $result1->getId(); |
|
| 107 | - |
|
| 108 | - // Delete the assignment |
|
| 109 | - $this->service->delete($initialId); |
|
| 110 | - |
|
| 111 | - // Verify it's deleted by trying to find it |
|
| 112 | - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); |
|
| 113 | - try { |
|
| 114 | - $this->service->find($initialId); |
|
| 115 | - } catch (\OCA\Settings\Service\NotFoundException $e) { |
|
| 116 | - // Expected - now create the same assignment again, which should succeed |
|
| 117 | - $result2 = $this->service->create($groupId, $class); |
|
| 118 | - |
|
| 119 | - $this->assertEquals($groupId, $result2->getGroupId()); |
|
| 120 | - $this->assertEquals($class, $result2->getClass()); |
|
| 121 | - $this->assertNotEquals($initialId, $result2->getId()); |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - $this->fail('Expected NotFoundException when finding deleted group'); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Test the mapper's findByGroupIdAndClass method behavior with duplicates |
|
| 130 | - */ |
|
| 131 | - public function testMapperFindByGroupIdAndClassBehavior(): void { |
|
| 132 | - $groupId = 'test_mapper_group'; |
|
| 133 | - $class = 'TestClass\\MapperTest'; |
|
| 134 | - |
|
| 135 | - // Initially should throw DoesNotExistException |
|
| 136 | - $this->expectException(DoesNotExistException::class); |
|
| 137 | - $this->mapper->findByGroupIdAndClass($groupId, $class); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Test that mapper returns existing record after creation |
|
| 142 | - */ |
|
| 143 | - public function testMapperFindsExistingRecord(): void { |
|
| 144 | - $groupId = 'test_existing_group'; |
|
| 145 | - $class = 'TestClass\\Existing'; |
|
| 146 | - |
|
| 147 | - // Create the record first |
|
| 148 | - $created = $this->service->create($groupId, $class); |
|
| 149 | - |
|
| 150 | - // Now mapper should find it |
|
| 151 | - $found = $this->mapper->findByGroupIdAndClass($groupId, $class); |
|
| 152 | - |
|
| 153 | - $this->assertEquals($created->getId(), $found->getId()); |
|
| 154 | - $this->assertEquals($groupId, $found->getGroupId()); |
|
| 155 | - $this->assertEquals($class, $found->getClass()); |
|
| 156 | - } |
|
| 23 | + private AuthorizedGroupService $service; |
|
| 24 | + private AuthorizedGroupMapper $mapper; |
|
| 25 | + |
|
| 26 | + protected function setUp(): void { |
|
| 27 | + parent::setUp(); |
|
| 28 | + |
|
| 29 | + // Use real mapper for integration testing |
|
| 30 | + $this->mapper = \OCP\Server::get(AuthorizedGroupMapper::class); |
|
| 31 | + $this->service = new AuthorizedGroupService($this->mapper); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + protected function tearDown(): void { |
|
| 35 | + // Clean up any test data |
|
| 36 | + try { |
|
| 37 | + $allGroups = $this->mapper->findAll(); |
|
| 38 | + foreach ($allGroups as $group) { |
|
| 39 | + if (str_starts_with($group->getGroupId(), 'test_') |
|
| 40 | + || str_starts_with($group->getClass(), 'TestClass')) { |
|
| 41 | + $this->mapper->delete($group); |
|
| 42 | + } |
|
| 43 | + } |
|
| 44 | + } catch (\Exception $e) { |
|
| 45 | + // Ignore cleanup errors |
|
| 46 | + } |
|
| 47 | + parent::tearDown(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function testDuplicateAssignmentPrevention(): void { |
|
| 51 | + $groupId = 'test_duplicate_group'; |
|
| 52 | + $class = 'TestClass\\DuplicateTest'; |
|
| 53 | + |
|
| 54 | + // First assignment should succeed |
|
| 55 | + $result1 = $this->service->create($groupId, $class); |
|
| 56 | + $this->assertInstanceOf(AuthorizedGroup::class, $result1); |
|
| 57 | + $this->assertEquals($groupId, $result1->getGroupId()); |
|
| 58 | + $this->assertEquals($class, $result1->getClass()); |
|
| 59 | + $this->assertNotNull($result1->getId()); |
|
| 60 | + |
|
| 61 | + // Second assignment of same group to same class should throw ConflictException |
|
| 62 | + $this->expectException(ConflictException::class); |
|
| 63 | + $this->expectExceptionMessage('Group is already assigned to this class'); |
|
| 64 | + |
|
| 65 | + $this->service->create($groupId, $class); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function testDifferentGroupsSameClassAllowed(): void { |
|
| 69 | + $groupId1 = 'test_group_1'; |
|
| 70 | + $groupId2 = 'test_group_2'; |
|
| 71 | + $class = 'TestClass\\MultiGroup'; |
|
| 72 | + |
|
| 73 | + // Both assignments should succeed |
|
| 74 | + $result1 = $this->service->create($groupId1, $class); |
|
| 75 | + $result2 = $this->service->create($groupId2, $class); |
|
| 76 | + |
|
| 77 | + $this->assertEquals($groupId1, $result1->getGroupId()); |
|
| 78 | + $this->assertEquals($groupId2, $result2->getGroupId()); |
|
| 79 | + $this->assertEquals($class, $result1->getClass()); |
|
| 80 | + $this->assertEquals($class, $result2->getClass()); |
|
| 81 | + $this->assertNotEquals($result1->getId(), $result2->getId()); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function testSameGroupDifferentClassesAllowed(): void { |
|
| 85 | + $groupId = 'test_multi_class_group'; |
|
| 86 | + $class1 = 'TestClass\\First'; |
|
| 87 | + $class2 = 'TestClass\\Second'; |
|
| 88 | + |
|
| 89 | + // Both assignments should succeed |
|
| 90 | + $result1 = $this->service->create($groupId, $class1); |
|
| 91 | + $result2 = $this->service->create($groupId, $class2); |
|
| 92 | + |
|
| 93 | + $this->assertEquals($groupId, $result1->getGroupId()); |
|
| 94 | + $this->assertEquals($groupId, $result2->getGroupId()); |
|
| 95 | + $this->assertEquals($class1, $result1->getClass()); |
|
| 96 | + $this->assertEquals($class2, $result2->getClass()); |
|
| 97 | + $this->assertNotEquals($result1->getId(), $result2->getId()); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + public function testCreateAfterDelete(): void { |
|
| 101 | + $groupId = 'test_recreate_group'; |
|
| 102 | + $class = 'TestClass\\Recreate'; |
|
| 103 | + |
|
| 104 | + // Create initial assignment |
|
| 105 | + $result1 = $this->service->create($groupId, $class); |
|
| 106 | + $initialId = $result1->getId(); |
|
| 107 | + |
|
| 108 | + // Delete the assignment |
|
| 109 | + $this->service->delete($initialId); |
|
| 110 | + |
|
| 111 | + // Verify it's deleted by trying to find it |
|
| 112 | + $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); |
|
| 113 | + try { |
|
| 114 | + $this->service->find($initialId); |
|
| 115 | + } catch (\OCA\Settings\Service\NotFoundException $e) { |
|
| 116 | + // Expected - now create the same assignment again, which should succeed |
|
| 117 | + $result2 = $this->service->create($groupId, $class); |
|
| 118 | + |
|
| 119 | + $this->assertEquals($groupId, $result2->getGroupId()); |
|
| 120 | + $this->assertEquals($class, $result2->getClass()); |
|
| 121 | + $this->assertNotEquals($initialId, $result2->getId()); |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + $this->fail('Expected NotFoundException when finding deleted group'); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Test the mapper's findByGroupIdAndClass method behavior with duplicates |
|
| 130 | + */ |
|
| 131 | + public function testMapperFindByGroupIdAndClassBehavior(): void { |
|
| 132 | + $groupId = 'test_mapper_group'; |
|
| 133 | + $class = 'TestClass\\MapperTest'; |
|
| 134 | + |
|
| 135 | + // Initially should throw DoesNotExistException |
|
| 136 | + $this->expectException(DoesNotExistException::class); |
|
| 137 | + $this->mapper->findByGroupIdAndClass($groupId, $class); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Test that mapper returns existing record after creation |
|
| 142 | + */ |
|
| 143 | + public function testMapperFindsExistingRecord(): void { |
|
| 144 | + $groupId = 'test_existing_group'; |
|
| 145 | + $class = 'TestClass\\Existing'; |
|
| 146 | + |
|
| 147 | + // Create the record first |
|
| 148 | + $created = $this->service->create($groupId, $class); |
|
| 149 | + |
|
| 150 | + // Now mapper should find it |
|
| 151 | + $found = $this->mapper->findByGroupIdAndClass($groupId, $class); |
|
| 152 | + |
|
| 153 | + $this->assertEquals($created->getId(), $found->getId()); |
|
| 154 | + $this->assertEquals($groupId, $found->getGroupId()); |
|
| 155 | + $this->assertEquals($class, $found->getClass()); |
|
| 156 | + } |
|
| 157 | 157 | } |
@@ -19,140 +19,140 @@ |
||
| 19 | 19 | |
| 20 | 20 | class AuthorizedGroupServiceTest extends TestCase { |
| 21 | 21 | |
| 22 | - private AuthorizedGroupMapper&MockObject $mapper; |
|
| 23 | - private AuthorizedGroupService $service; |
|
| 24 | - |
|
| 25 | - protected function setUp(): void { |
|
| 26 | - parent::setUp(); |
|
| 27 | - $this->mapper = $this->createMock(AuthorizedGroupMapper::class); |
|
| 28 | - $this->service = new AuthorizedGroupService($this->mapper); |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - public function testCreateSuccessWhenNoDuplicateExists(): void { |
|
| 32 | - $groupId = 'testgroup'; |
|
| 33 | - $class = 'TestClass'; |
|
| 34 | - |
|
| 35 | - // Mock that no existing assignment is found (throws DoesNotExistException) |
|
| 36 | - $this->mapper->expects($this->once()) |
|
| 37 | - ->method('findByGroupIdAndClass') |
|
| 38 | - ->with($groupId, $class) |
|
| 39 | - ->willThrowException(new DoesNotExistException('Not found')); |
|
| 40 | - |
|
| 41 | - // Mock the successful creation |
|
| 42 | - $expectedGroup = new AuthorizedGroup(); |
|
| 43 | - $expectedGroup->setGroupId($groupId); |
|
| 44 | - $expectedGroup->setClass($class); |
|
| 45 | - $expectedGroup->setId(123); |
|
| 46 | - |
|
| 47 | - $this->mapper->expects($this->once()) |
|
| 48 | - ->method('insert') |
|
| 49 | - ->willReturn($expectedGroup); |
|
| 50 | - |
|
| 51 | - $result = $this->service->create($groupId, $class); |
|
| 52 | - |
|
| 53 | - $this->assertInstanceOf(AuthorizedGroup::class, $result); |
|
| 54 | - $this->assertEquals($groupId, $result->getGroupId()); |
|
| 55 | - $this->assertEquals($class, $result->getClass()); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - public function testCreateThrowsConflictExceptionWhenDuplicateExists(): void { |
|
| 59 | - $groupId = 'testgroup'; |
|
| 60 | - $class = 'TestClass'; |
|
| 61 | - |
|
| 62 | - // Mock that an existing assignment is found |
|
| 63 | - $existingGroup = new AuthorizedGroup(); |
|
| 64 | - $existingGroup->setGroupId($groupId); |
|
| 65 | - $existingGroup->setClass($class); |
|
| 66 | - $existingGroup->setId(42); |
|
| 67 | - |
|
| 68 | - $this->mapper->expects($this->once()) |
|
| 69 | - ->method('findByGroupIdAndClass') |
|
| 70 | - ->with($groupId, $class) |
|
| 71 | - ->willReturn($existingGroup); |
|
| 72 | - |
|
| 73 | - // Mapper insert should never be called when duplicate exists |
|
| 74 | - $this->mapper->expects($this->never()) |
|
| 75 | - ->method('insert'); |
|
| 76 | - |
|
| 77 | - $this->expectException(ConflictException::class); |
|
| 78 | - $this->expectExceptionMessage('Group is already assigned to this class'); |
|
| 79 | - |
|
| 80 | - $this->service->create($groupId, $class); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public function testCreateAllowsDifferentGroupsSameClass(): void { |
|
| 84 | - $groupId1 = 'testgroup1'; |
|
| 85 | - $groupId2 = 'testgroup2'; |
|
| 86 | - $class = 'TestClass'; |
|
| 87 | - |
|
| 88 | - // Mock that no duplicate exists for group1 |
|
| 89 | - $this->mapper->expects($this->exactly(2)) |
|
| 90 | - ->method('findByGroupIdAndClass') |
|
| 91 | - ->willReturnCallback(function ($groupId, $classArg) use ($groupId1, $groupId2, $class) { |
|
| 92 | - $this->assertContains($groupId, [$groupId1, $groupId2]); |
|
| 93 | - $this->assertEquals($class, $classArg); |
|
| 94 | - throw new DoesNotExistException('Not found'); |
|
| 95 | - }); |
|
| 96 | - |
|
| 97 | - $expectedGroup1 = new AuthorizedGroup(); |
|
| 98 | - $expectedGroup1->setGroupId($groupId1); |
|
| 99 | - $expectedGroup1->setClass($class); |
|
| 100 | - $expectedGroup1->setId(123); |
|
| 101 | - |
|
| 102 | - $expectedGroup2 = new AuthorizedGroup(); |
|
| 103 | - $expectedGroup2->setGroupId($groupId2); |
|
| 104 | - $expectedGroup2->setClass($class); |
|
| 105 | - $expectedGroup2->setId(124); |
|
| 106 | - |
|
| 107 | - $this->mapper->expects($this->exactly(2)) |
|
| 108 | - ->method('insert') |
|
| 109 | - ->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2); |
|
| 110 | - |
|
| 111 | - // Both creations should succeed |
|
| 112 | - $result1 = $this->service->create($groupId1, $class); |
|
| 113 | - $this->assertEquals($groupId1, $result1->getGroupId()); |
|
| 114 | - $this->assertEquals($class, $result1->getClass()); |
|
| 115 | - |
|
| 116 | - $result2 = $this->service->create($groupId2, $class); |
|
| 117 | - $this->assertEquals($groupId2, $result2->getGroupId()); |
|
| 118 | - $this->assertEquals($class, $result2->getClass()); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - public function testCreateAllowsSameGroupDifferentClasses(): void { |
|
| 122 | - $groupId = 'testgroup'; |
|
| 123 | - $class1 = 'TestClass1'; |
|
| 124 | - $class2 = 'TestClass2'; |
|
| 125 | - |
|
| 126 | - // Mock that no duplicate exists for either class |
|
| 127 | - $this->mapper->expects($this->exactly(2)) |
|
| 128 | - ->method('findByGroupIdAndClass') |
|
| 129 | - ->willReturnCallback(function ($groupIdArg, $class) use ($groupId, $class1, $class2) { |
|
| 130 | - $this->assertEquals($groupId, $groupIdArg); |
|
| 131 | - $this->assertContains($class, [$class1, $class2]); |
|
| 132 | - throw new DoesNotExistException('Not found'); |
|
| 133 | - }); |
|
| 134 | - |
|
| 135 | - $expectedGroup1 = new AuthorizedGroup(); |
|
| 136 | - $expectedGroup1->setGroupId($groupId); |
|
| 137 | - $expectedGroup1->setClass($class1); |
|
| 138 | - $expectedGroup1->setId(123); |
|
| 139 | - |
|
| 140 | - $expectedGroup2 = new AuthorizedGroup(); |
|
| 141 | - $expectedGroup2->setGroupId($groupId); |
|
| 142 | - $expectedGroup2->setClass($class2); |
|
| 143 | - $expectedGroup2->setId(124); |
|
| 144 | - |
|
| 145 | - $this->mapper->expects($this->exactly(2)) |
|
| 146 | - ->method('insert') |
|
| 147 | - ->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2); |
|
| 148 | - |
|
| 149 | - // Both creations should succeed |
|
| 150 | - $result1 = $this->service->create($groupId, $class1); |
|
| 151 | - $result2 = $this->service->create($groupId, $class2); |
|
| 152 | - |
|
| 153 | - $this->assertEquals($groupId, $result1->getGroupId()); |
|
| 154 | - $this->assertEquals($groupId, $result2->getGroupId()); |
|
| 155 | - $this->assertEquals($class1, $result1->getClass()); |
|
| 156 | - $this->assertEquals($class2, $result2->getClass()); |
|
| 157 | - } |
|
| 22 | + private AuthorizedGroupMapper&MockObject $mapper; |
|
| 23 | + private AuthorizedGroupService $service; |
|
| 24 | + |
|
| 25 | + protected function setUp(): void { |
|
| 26 | + parent::setUp(); |
|
| 27 | + $this->mapper = $this->createMock(AuthorizedGroupMapper::class); |
|
| 28 | + $this->service = new AuthorizedGroupService($this->mapper); |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + public function testCreateSuccessWhenNoDuplicateExists(): void { |
|
| 32 | + $groupId = 'testgroup'; |
|
| 33 | + $class = 'TestClass'; |
|
| 34 | + |
|
| 35 | + // Mock that no existing assignment is found (throws DoesNotExistException) |
|
| 36 | + $this->mapper->expects($this->once()) |
|
| 37 | + ->method('findByGroupIdAndClass') |
|
| 38 | + ->with($groupId, $class) |
|
| 39 | + ->willThrowException(new DoesNotExistException('Not found')); |
|
| 40 | + |
|
| 41 | + // Mock the successful creation |
|
| 42 | + $expectedGroup = new AuthorizedGroup(); |
|
| 43 | + $expectedGroup->setGroupId($groupId); |
|
| 44 | + $expectedGroup->setClass($class); |
|
| 45 | + $expectedGroup->setId(123); |
|
| 46 | + |
|
| 47 | + $this->mapper->expects($this->once()) |
|
| 48 | + ->method('insert') |
|
| 49 | + ->willReturn($expectedGroup); |
|
| 50 | + |
|
| 51 | + $result = $this->service->create($groupId, $class); |
|
| 52 | + |
|
| 53 | + $this->assertInstanceOf(AuthorizedGroup::class, $result); |
|
| 54 | + $this->assertEquals($groupId, $result->getGroupId()); |
|
| 55 | + $this->assertEquals($class, $result->getClass()); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + public function testCreateThrowsConflictExceptionWhenDuplicateExists(): void { |
|
| 59 | + $groupId = 'testgroup'; |
|
| 60 | + $class = 'TestClass'; |
|
| 61 | + |
|
| 62 | + // Mock that an existing assignment is found |
|
| 63 | + $existingGroup = new AuthorizedGroup(); |
|
| 64 | + $existingGroup->setGroupId($groupId); |
|
| 65 | + $existingGroup->setClass($class); |
|
| 66 | + $existingGroup->setId(42); |
|
| 67 | + |
|
| 68 | + $this->mapper->expects($this->once()) |
|
| 69 | + ->method('findByGroupIdAndClass') |
|
| 70 | + ->with($groupId, $class) |
|
| 71 | + ->willReturn($existingGroup); |
|
| 72 | + |
|
| 73 | + // Mapper insert should never be called when duplicate exists |
|
| 74 | + $this->mapper->expects($this->never()) |
|
| 75 | + ->method('insert'); |
|
| 76 | + |
|
| 77 | + $this->expectException(ConflictException::class); |
|
| 78 | + $this->expectExceptionMessage('Group is already assigned to this class'); |
|
| 79 | + |
|
| 80 | + $this->service->create($groupId, $class); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public function testCreateAllowsDifferentGroupsSameClass(): void { |
|
| 84 | + $groupId1 = 'testgroup1'; |
|
| 85 | + $groupId2 = 'testgroup2'; |
|
| 86 | + $class = 'TestClass'; |
|
| 87 | + |
|
| 88 | + // Mock that no duplicate exists for group1 |
|
| 89 | + $this->mapper->expects($this->exactly(2)) |
|
| 90 | + ->method('findByGroupIdAndClass') |
|
| 91 | + ->willReturnCallback(function ($groupId, $classArg) use ($groupId1, $groupId2, $class) { |
|
| 92 | + $this->assertContains($groupId, [$groupId1, $groupId2]); |
|
| 93 | + $this->assertEquals($class, $classArg); |
|
| 94 | + throw new DoesNotExistException('Not found'); |
|
| 95 | + }); |
|
| 96 | + |
|
| 97 | + $expectedGroup1 = new AuthorizedGroup(); |
|
| 98 | + $expectedGroup1->setGroupId($groupId1); |
|
| 99 | + $expectedGroup1->setClass($class); |
|
| 100 | + $expectedGroup1->setId(123); |
|
| 101 | + |
|
| 102 | + $expectedGroup2 = new AuthorizedGroup(); |
|
| 103 | + $expectedGroup2->setGroupId($groupId2); |
|
| 104 | + $expectedGroup2->setClass($class); |
|
| 105 | + $expectedGroup2->setId(124); |
|
| 106 | + |
|
| 107 | + $this->mapper->expects($this->exactly(2)) |
|
| 108 | + ->method('insert') |
|
| 109 | + ->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2); |
|
| 110 | + |
|
| 111 | + // Both creations should succeed |
|
| 112 | + $result1 = $this->service->create($groupId1, $class); |
|
| 113 | + $this->assertEquals($groupId1, $result1->getGroupId()); |
|
| 114 | + $this->assertEquals($class, $result1->getClass()); |
|
| 115 | + |
|
| 116 | + $result2 = $this->service->create($groupId2, $class); |
|
| 117 | + $this->assertEquals($groupId2, $result2->getGroupId()); |
|
| 118 | + $this->assertEquals($class, $result2->getClass()); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + public function testCreateAllowsSameGroupDifferentClasses(): void { |
|
| 122 | + $groupId = 'testgroup'; |
|
| 123 | + $class1 = 'TestClass1'; |
|
| 124 | + $class2 = 'TestClass2'; |
|
| 125 | + |
|
| 126 | + // Mock that no duplicate exists for either class |
|
| 127 | + $this->mapper->expects($this->exactly(2)) |
|
| 128 | + ->method('findByGroupIdAndClass') |
|
| 129 | + ->willReturnCallback(function ($groupIdArg, $class) use ($groupId, $class1, $class2) { |
|
| 130 | + $this->assertEquals($groupId, $groupIdArg); |
|
| 131 | + $this->assertContains($class, [$class1, $class2]); |
|
| 132 | + throw new DoesNotExistException('Not found'); |
|
| 133 | + }); |
|
| 134 | + |
|
| 135 | + $expectedGroup1 = new AuthorizedGroup(); |
|
| 136 | + $expectedGroup1->setGroupId($groupId); |
|
| 137 | + $expectedGroup1->setClass($class1); |
|
| 138 | + $expectedGroup1->setId(123); |
|
| 139 | + |
|
| 140 | + $expectedGroup2 = new AuthorizedGroup(); |
|
| 141 | + $expectedGroup2->setGroupId($groupId); |
|
| 142 | + $expectedGroup2->setClass($class2); |
|
| 143 | + $expectedGroup2->setId(124); |
|
| 144 | + |
|
| 145 | + $this->mapper->expects($this->exactly(2)) |
|
| 146 | + ->method('insert') |
|
| 147 | + ->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2); |
|
| 148 | + |
|
| 149 | + // Both creations should succeed |
|
| 150 | + $result1 = $this->service->create($groupId, $class1); |
|
| 151 | + $result2 = $this->service->create($groupId, $class2); |
|
| 152 | + |
|
| 153 | + $this->assertEquals($groupId, $result1->getGroupId()); |
|
| 154 | + $this->assertEquals($groupId, $result2->getGroupId()); |
|
| 155 | + $this->assertEquals($class1, $result1->getClass()); |
|
| 156 | + $this->assertEquals($class2, $result2->getClass()); |
|
| 157 | + } |
|
| 158 | 158 | } |
@@ -22,127 +22,127 @@ |
||
| 22 | 22 | |
| 23 | 23 | class AddTest extends TestCase { |
| 24 | 24 | |
| 25 | - private IManager&MockObject $settingManager; |
|
| 26 | - private AuthorizedGroupService&MockObject $authorizedGroupService; |
|
| 27 | - private IGroupManager&MockObject $groupManager; |
|
| 28 | - private Add $command; |
|
| 29 | - private InputInterface&MockObject $input; |
|
| 30 | - private OutputInterface&MockObject $output; |
|
| 31 | - |
|
| 32 | - protected function setUp(): void { |
|
| 33 | - parent::setUp(); |
|
| 34 | - |
|
| 35 | - $this->settingManager = $this->createMock(IManager::class); |
|
| 36 | - $this->authorizedGroupService = $this->createMock(AuthorizedGroupService::class); |
|
| 37 | - $this->groupManager = $this->createMock(IGroupManager::class); |
|
| 38 | - |
|
| 39 | - $this->command = new Add( |
|
| 40 | - $this->settingManager, |
|
| 41 | - $this->authorizedGroupService, |
|
| 42 | - $this->groupManager |
|
| 43 | - ); |
|
| 44 | - |
|
| 45 | - $this->input = $this->createMock(InputInterface::class); |
|
| 46 | - $this->output = $this->createMock(OutputInterface::class); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function testExecuteSuccessfulDelegation(): void { |
|
| 50 | - $settingClass = Server::class; |
|
| 51 | - $groupId = 'testgroup'; |
|
| 52 | - |
|
| 53 | - // Mock valid delegated settings class |
|
| 54 | - $this->input->expects($this->exactly(2)) |
|
| 55 | - ->method('getArgument') |
|
| 56 | - ->willReturnMap([ |
|
| 57 | - ['settingClass', $settingClass], |
|
| 58 | - ['groupId', $groupId] |
|
| 59 | - ]); |
|
| 60 | - |
|
| 61 | - // Mock group exists |
|
| 62 | - $this->groupManager->expects($this->once()) |
|
| 63 | - ->method('groupExists') |
|
| 64 | - ->with($groupId) |
|
| 65 | - ->willReturn(true); |
|
| 66 | - |
|
| 67 | - // Mock successful creation |
|
| 68 | - $authorizedGroup = new AuthorizedGroup(); |
|
| 69 | - $authorizedGroup->setGroupId($groupId); |
|
| 70 | - $authorizedGroup->setClass($settingClass); |
|
| 71 | - |
|
| 72 | - $this->authorizedGroupService->expects($this->once()) |
|
| 73 | - ->method('create') |
|
| 74 | - ->with($groupId, $settingClass) |
|
| 75 | - ->willReturn($authorizedGroup); |
|
| 76 | - |
|
| 77 | - $result = $this->command->execute($this->input, $this->output); |
|
| 78 | - |
|
| 79 | - $this->assertEquals(0, $result); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - public function testExecuteHandlesDuplicateAssignment(): void { |
|
| 83 | - $settingClass = 'OCA\\Settings\\Settings\\Admin\\Server'; |
|
| 84 | - $groupId = 'testgroup'; |
|
| 85 | - |
|
| 86 | - // Mock valid delegated settings class |
|
| 87 | - $this->input->expects($this->exactly(2)) |
|
| 88 | - ->method('getArgument') |
|
| 89 | - ->willReturnMap([ |
|
| 90 | - ['settingClass', $settingClass], |
|
| 91 | - ['groupId', $groupId] |
|
| 92 | - ]); |
|
| 93 | - |
|
| 94 | - // Mock group exists |
|
| 95 | - $this->groupManager->expects($this->once()) |
|
| 96 | - ->method('groupExists') |
|
| 97 | - ->with($groupId) |
|
| 98 | - ->willReturn(true); |
|
| 99 | - |
|
| 100 | - // Mock ConflictException when trying to create duplicate |
|
| 101 | - $this->authorizedGroupService->expects($this->once()) |
|
| 102 | - ->method('create') |
|
| 103 | - ->with($groupId, $settingClass) |
|
| 104 | - ->willThrowException(new ConflictException('Group is already assigned to this class')); |
|
| 105 | - |
|
| 106 | - $result = $this->command->execute($this->input, $this->output); |
|
| 107 | - |
|
| 108 | - $this->assertEquals(4, $result, 'Duplicate assignment should return exit code 4'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - public function testExecuteInvalidSettingClass(): void { |
|
| 112 | - // Use a real class that exists but doesn't implement IDelegatedSettings |
|
| 113 | - $settingClass = 'stdClass'; |
|
| 114 | - |
|
| 115 | - $this->input->expects($this->once()) |
|
| 116 | - ->method('getArgument') |
|
| 117 | - ->with('settingClass') |
|
| 118 | - ->willReturn($settingClass); |
|
| 119 | - |
|
| 120 | - $result = $this->command->execute($this->input, $this->output); |
|
| 121 | - |
|
| 122 | - // Should return exit code 2 for invalid setting class |
|
| 123 | - $this->assertEquals(2, $result); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - public function testExecuteNonExistentGroup(): void { |
|
| 127 | - $settingClass = Server::class; |
|
| 128 | - $groupId = 'nonexistentgroup'; |
|
| 129 | - |
|
| 130 | - $this->input->expects($this->exactly(2)) |
|
| 131 | - ->method('getArgument') |
|
| 132 | - ->willReturnMap([ |
|
| 133 | - ['settingClass', $settingClass], |
|
| 134 | - ['groupId', $groupId] |
|
| 135 | - ]); |
|
| 136 | - |
|
| 137 | - // Mock group does not exist |
|
| 138 | - $this->groupManager->expects($this->once()) |
|
| 139 | - ->method('groupExists') |
|
| 140 | - ->with($groupId) |
|
| 141 | - ->willReturn(false); |
|
| 142 | - |
|
| 143 | - $result = $this->command->execute($this->input, $this->output); |
|
| 144 | - |
|
| 145 | - // Should return exit code 3 for non-existent group |
|
| 146 | - $this->assertEquals(3, $result); |
|
| 147 | - } |
|
| 25 | + private IManager&MockObject $settingManager; |
|
| 26 | + private AuthorizedGroupService&MockObject $authorizedGroupService; |
|
| 27 | + private IGroupManager&MockObject $groupManager; |
|
| 28 | + private Add $command; |
|
| 29 | + private InputInterface&MockObject $input; |
|
| 30 | + private OutputInterface&MockObject $output; |
|
| 31 | + |
|
| 32 | + protected function setUp(): void { |
|
| 33 | + parent::setUp(); |
|
| 34 | + |
|
| 35 | + $this->settingManager = $this->createMock(IManager::class); |
|
| 36 | + $this->authorizedGroupService = $this->createMock(AuthorizedGroupService::class); |
|
| 37 | + $this->groupManager = $this->createMock(IGroupManager::class); |
|
| 38 | + |
|
| 39 | + $this->command = new Add( |
|
| 40 | + $this->settingManager, |
|
| 41 | + $this->authorizedGroupService, |
|
| 42 | + $this->groupManager |
|
| 43 | + ); |
|
| 44 | + |
|
| 45 | + $this->input = $this->createMock(InputInterface::class); |
|
| 46 | + $this->output = $this->createMock(OutputInterface::class); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function testExecuteSuccessfulDelegation(): void { |
|
| 50 | + $settingClass = Server::class; |
|
| 51 | + $groupId = 'testgroup'; |
|
| 52 | + |
|
| 53 | + // Mock valid delegated settings class |
|
| 54 | + $this->input->expects($this->exactly(2)) |
|
| 55 | + ->method('getArgument') |
|
| 56 | + ->willReturnMap([ |
|
| 57 | + ['settingClass', $settingClass], |
|
| 58 | + ['groupId', $groupId] |
|
| 59 | + ]); |
|
| 60 | + |
|
| 61 | + // Mock group exists |
|
| 62 | + $this->groupManager->expects($this->once()) |
|
| 63 | + ->method('groupExists') |
|
| 64 | + ->with($groupId) |
|
| 65 | + ->willReturn(true); |
|
| 66 | + |
|
| 67 | + // Mock successful creation |
|
| 68 | + $authorizedGroup = new AuthorizedGroup(); |
|
| 69 | + $authorizedGroup->setGroupId($groupId); |
|
| 70 | + $authorizedGroup->setClass($settingClass); |
|
| 71 | + |
|
| 72 | + $this->authorizedGroupService->expects($this->once()) |
|
| 73 | + ->method('create') |
|
| 74 | + ->with($groupId, $settingClass) |
|
| 75 | + ->willReturn($authorizedGroup); |
|
| 76 | + |
|
| 77 | + $result = $this->command->execute($this->input, $this->output); |
|
| 78 | + |
|
| 79 | + $this->assertEquals(0, $result); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + public function testExecuteHandlesDuplicateAssignment(): void { |
|
| 83 | + $settingClass = 'OCA\\Settings\\Settings\\Admin\\Server'; |
|
| 84 | + $groupId = 'testgroup'; |
|
| 85 | + |
|
| 86 | + // Mock valid delegated settings class |
|
| 87 | + $this->input->expects($this->exactly(2)) |
|
| 88 | + ->method('getArgument') |
|
| 89 | + ->willReturnMap([ |
|
| 90 | + ['settingClass', $settingClass], |
|
| 91 | + ['groupId', $groupId] |
|
| 92 | + ]); |
|
| 93 | + |
|
| 94 | + // Mock group exists |
|
| 95 | + $this->groupManager->expects($this->once()) |
|
| 96 | + ->method('groupExists') |
|
| 97 | + ->with($groupId) |
|
| 98 | + ->willReturn(true); |
|
| 99 | + |
|
| 100 | + // Mock ConflictException when trying to create duplicate |
|
| 101 | + $this->authorizedGroupService->expects($this->once()) |
|
| 102 | + ->method('create') |
|
| 103 | + ->with($groupId, $settingClass) |
|
| 104 | + ->willThrowException(new ConflictException('Group is already assigned to this class')); |
|
| 105 | + |
|
| 106 | + $result = $this->command->execute($this->input, $this->output); |
|
| 107 | + |
|
| 108 | + $this->assertEquals(4, $result, 'Duplicate assignment should return exit code 4'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + public function testExecuteInvalidSettingClass(): void { |
|
| 112 | + // Use a real class that exists but doesn't implement IDelegatedSettings |
|
| 113 | + $settingClass = 'stdClass'; |
|
| 114 | + |
|
| 115 | + $this->input->expects($this->once()) |
|
| 116 | + ->method('getArgument') |
|
| 117 | + ->with('settingClass') |
|
| 118 | + ->willReturn($settingClass); |
|
| 119 | + |
|
| 120 | + $result = $this->command->execute($this->input, $this->output); |
|
| 121 | + |
|
| 122 | + // Should return exit code 2 for invalid setting class |
|
| 123 | + $this->assertEquals(2, $result); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + public function testExecuteNonExistentGroup(): void { |
|
| 127 | + $settingClass = Server::class; |
|
| 128 | + $groupId = 'nonexistentgroup'; |
|
| 129 | + |
|
| 130 | + $this->input->expects($this->exactly(2)) |
|
| 131 | + ->method('getArgument') |
|
| 132 | + ->willReturnMap([ |
|
| 133 | + ['settingClass', $settingClass], |
|
| 134 | + ['groupId', $groupId] |
|
| 135 | + ]); |
|
| 136 | + |
|
| 137 | + // Mock group does not exist |
|
| 138 | + $this->groupManager->expects($this->once()) |
|
| 139 | + ->method('groupExists') |
|
| 140 | + ->with($groupId) |
|
| 141 | + ->willReturn(false); |
|
| 142 | + |
|
| 143 | + $result = $this->command->execute($this->input, $this->output); |
|
| 144 | + |
|
| 145 | + // Should return exit code 3 for non-existent group |
|
| 146 | + $this->assertEquals(3, $result); |
|
| 147 | + } |
|
| 148 | 148 | } |
@@ -16,92 +16,92 @@ |
||
| 16 | 16 | |
| 17 | 17 | class AuthorizedGroupService { |
| 18 | 18 | |
| 19 | - public function __construct( |
|
| 20 | - private AuthorizedGroupMapper $mapper, |
|
| 21 | - ) { |
|
| 22 | - } |
|
| 19 | + public function __construct( |
|
| 20 | + private AuthorizedGroupMapper $mapper, |
|
| 21 | + ) { |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @return AuthorizedGroup[] |
|
| 26 | - */ |
|
| 27 | - public function findAll(): array { |
|
| 28 | - return $this->mapper->findAll(); |
|
| 29 | - } |
|
| 24 | + /** |
|
| 25 | + * @return AuthorizedGroup[] |
|
| 26 | + */ |
|
| 27 | + public function findAll(): array { |
|
| 28 | + return $this->mapper->findAll(); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Find AuthorizedGroup by id. |
|
| 33 | - * |
|
| 34 | - * @param int $id |
|
| 35 | - */ |
|
| 36 | - public function find(int $id): ?AuthorizedGroup { |
|
| 37 | - return $this->mapper->find($id); |
|
| 38 | - } |
|
| 31 | + /** |
|
| 32 | + * Find AuthorizedGroup by id. |
|
| 33 | + * |
|
| 34 | + * @param int $id |
|
| 35 | + */ |
|
| 36 | + public function find(int $id): ?AuthorizedGroup { |
|
| 37 | + return $this->mapper->find($id); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param $e |
|
| 42 | - * @throws NotFoundException |
|
| 43 | - */ |
|
| 44 | - private function handleException(\Exception $e): void { |
|
| 45 | - if ($e instanceof DoesNotExistException |
|
| 46 | - || $e instanceof MultipleObjectsReturnedException) { |
|
| 47 | - throw new NotFoundException('AuthorizedGroup not found'); |
|
| 48 | - } else { |
|
| 49 | - throw $e; |
|
| 50 | - } |
|
| 51 | - } |
|
| 40 | + /** |
|
| 41 | + * @param $e |
|
| 42 | + * @throws NotFoundException |
|
| 43 | + */ |
|
| 44 | + private function handleException(\Exception $e): void { |
|
| 45 | + if ($e instanceof DoesNotExistException |
|
| 46 | + || $e instanceof MultipleObjectsReturnedException) { |
|
| 47 | + throw new NotFoundException('AuthorizedGroup not found'); |
|
| 48 | + } else { |
|
| 49 | + throw $e; |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Create a new AuthorizedGroup |
|
| 55 | - * |
|
| 56 | - * @param string $groupId |
|
| 57 | - * @param string $class |
|
| 58 | - * @return AuthorizedGroup |
|
| 59 | - * @throws Exception |
|
| 60 | - * @throws ConflictException |
|
| 61 | - */ |
|
| 62 | - public function create(string $groupId, string $class): AuthorizedGroup { |
|
| 63 | - // Check if the group is already assigned to this class |
|
| 64 | - try { |
|
| 65 | - $existing = $this->mapper->findByGroupIdAndClass($groupId, $class); |
|
| 66 | - if ($existing) { |
|
| 67 | - throw new ConflictException('Group is already assigned to this class'); |
|
| 68 | - } |
|
| 69 | - } catch (DoesNotExistException $e) { |
|
| 70 | - // This is expected when no duplicate exists, continue with creation |
|
| 71 | - } |
|
| 53 | + /** |
|
| 54 | + * Create a new AuthorizedGroup |
|
| 55 | + * |
|
| 56 | + * @param string $groupId |
|
| 57 | + * @param string $class |
|
| 58 | + * @return AuthorizedGroup |
|
| 59 | + * @throws Exception |
|
| 60 | + * @throws ConflictException |
|
| 61 | + */ |
|
| 62 | + public function create(string $groupId, string $class): AuthorizedGroup { |
|
| 63 | + // Check if the group is already assigned to this class |
|
| 64 | + try { |
|
| 65 | + $existing = $this->mapper->findByGroupIdAndClass($groupId, $class); |
|
| 66 | + if ($existing) { |
|
| 67 | + throw new ConflictException('Group is already assigned to this class'); |
|
| 68 | + } |
|
| 69 | + } catch (DoesNotExistException $e) { |
|
| 70 | + // This is expected when no duplicate exists, continue with creation |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $authorizedGroup = new AuthorizedGroup(); |
|
| 74 | - $authorizedGroup->setGroupId($groupId); |
|
| 75 | - $authorizedGroup->setClass($class); |
|
| 76 | - return $this->mapper->insert($authorizedGroup); |
|
| 77 | - } |
|
| 73 | + $authorizedGroup = new AuthorizedGroup(); |
|
| 74 | + $authorizedGroup->setGroupId($groupId); |
|
| 75 | + $authorizedGroup->setClass($class); |
|
| 76 | + return $this->mapper->insert($authorizedGroup); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * @throws NotFoundException |
|
| 81 | - */ |
|
| 82 | - public function delete(int $id): void { |
|
| 83 | - try { |
|
| 84 | - $authorizedGroup = $this->mapper->find($id); |
|
| 85 | - $this->mapper->delete($authorizedGroup); |
|
| 86 | - } catch (\Exception $e) { |
|
| 87 | - $this->handleException($e); |
|
| 88 | - } |
|
| 89 | - } |
|
| 79 | + /** |
|
| 80 | + * @throws NotFoundException |
|
| 81 | + */ |
|
| 82 | + public function delete(int $id): void { |
|
| 83 | + try { |
|
| 84 | + $authorizedGroup = $this->mapper->find($id); |
|
| 85 | + $this->mapper->delete($authorizedGroup); |
|
| 86 | + } catch (\Exception $e) { |
|
| 87 | + $this->handleException($e); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - public function findExistingGroupsForClass(string $class): array { |
|
| 92 | - try { |
|
| 93 | - $authorizedGroup = $this->mapper->findExistingGroupsForClass($class); |
|
| 94 | - return $authorizedGroup; |
|
| 95 | - } catch (\Exception $e) { |
|
| 96 | - return []; |
|
| 97 | - } |
|
| 98 | - } |
|
| 91 | + public function findExistingGroupsForClass(string $class): array { |
|
| 92 | + try { |
|
| 93 | + $authorizedGroup = $this->mapper->findExistingGroupsForClass($class); |
|
| 94 | + return $authorizedGroup; |
|
| 95 | + } catch (\Exception $e) { |
|
| 96 | + return []; |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - public function removeAuthorizationAssociatedTo(IGroup $group): void { |
|
| 101 | - try { |
|
| 102 | - $this->mapper->removeGroup($group->getGID()); |
|
| 103 | - } catch (\Exception $e) { |
|
| 104 | - $this->handleException($e); |
|
| 105 | - } |
|
| 106 | - } |
|
| 100 | + public function removeAuthorizationAssociatedTo(IGroup $group): void { |
|
| 101 | + try { |
|
| 102 | + $this->mapper->removeGroup($group->getGID()); |
|
| 103 | + } catch (\Exception $e) { |
|
| 104 | + $this->handleException($e); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -19,47 +19,47 @@ |
||
| 19 | 19 | use Symfony\Component\Console\Style\SymfonyStyle; |
| 20 | 20 | |
| 21 | 21 | class Add extends Base { |
| 22 | - public function __construct( |
|
| 23 | - private IManager $settingManager, |
|
| 24 | - private AuthorizedGroupService $authorizedGroupService, |
|
| 25 | - private IGroupManager $groupManager, |
|
| 26 | - ) { |
|
| 27 | - parent::__construct(); |
|
| 28 | - } |
|
| 22 | + public function __construct( |
|
| 23 | + private IManager $settingManager, |
|
| 24 | + private AuthorizedGroupService $authorizedGroupService, |
|
| 25 | + private IGroupManager $groupManager, |
|
| 26 | + ) { |
|
| 27 | + parent::__construct(); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - protected function configure(): void { |
|
| 31 | - $this |
|
| 32 | - ->setName('admin-delegation:add') |
|
| 33 | - ->setDescription('add setting delegation to a group') |
|
| 34 | - ->addArgument('settingClass', InputArgument::REQUIRED, 'Admin setting class') |
|
| 35 | - ->addArgument('groupId', InputArgument::REQUIRED, 'Delegate to group ID') |
|
| 36 | - ->addUsage('\'OCA\Settings\Settings\Admin\Server\' mygroup') |
|
| 37 | - ; |
|
| 38 | - } |
|
| 30 | + protected function configure(): void { |
|
| 31 | + $this |
|
| 32 | + ->setName('admin-delegation:add') |
|
| 33 | + ->setDescription('add setting delegation to a group') |
|
| 34 | + ->addArgument('settingClass', InputArgument::REQUIRED, 'Admin setting class') |
|
| 35 | + ->addArgument('groupId', InputArgument::REQUIRED, 'Delegate to group ID') |
|
| 36 | + ->addUsage('\'OCA\Settings\Settings\Admin\Server\' mygroup') |
|
| 37 | + ; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 41 | - $io = new SymfonyStyle($input, $output); |
|
| 42 | - $settingClass = $input->getArgument('settingClass'); |
|
| 43 | - if (!in_array(IDelegatedSettings::class, (array)class_implements($settingClass), true)) { |
|
| 44 | - $io->error('The specified class isn’t a valid delegated setting.'); |
|
| 45 | - return 2; |
|
| 46 | - } |
|
| 40 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 41 | + $io = new SymfonyStyle($input, $output); |
|
| 42 | + $settingClass = $input->getArgument('settingClass'); |
|
| 43 | + if (!in_array(IDelegatedSettings::class, (array)class_implements($settingClass), true)) { |
|
| 44 | + $io->error('The specified class isn’t a valid delegated setting.'); |
|
| 45 | + return 2; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - $groupId = $input->getArgument('groupId'); |
|
| 49 | - if (!$this->groupManager->groupExists($groupId)) { |
|
| 50 | - $io->error('The specified group didn’t exist.'); |
|
| 51 | - return 3; |
|
| 52 | - } |
|
| 48 | + $groupId = $input->getArgument('groupId'); |
|
| 49 | + if (!$this->groupManager->groupExists($groupId)) { |
|
| 50 | + $io->error('The specified group didn’t exist.'); |
|
| 51 | + return 3; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - try { |
|
| 55 | - $this->authorizedGroupService->create($groupId, $settingClass); |
|
| 56 | - } catch (ConflictException) { |
|
| 57 | - $io->warning('Administration of ' . $settingClass . ' is already delegated to ' . $groupId . '.'); |
|
| 58 | - return 4; |
|
| 59 | - } |
|
| 54 | + try { |
|
| 55 | + $this->authorizedGroupService->create($groupId, $settingClass); |
|
| 56 | + } catch (ConflictException) { |
|
| 57 | + $io->warning('Administration of ' . $settingClass . ' is already delegated to ' . $groupId . '.'); |
|
| 58 | + return 4; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - $io->success('Administration of ' . $settingClass . ' delegated to ' . $groupId . '.'); |
|
| 61 | + $io->success('Administration of ' . $settingClass . ' delegated to ' . $groupId . '.'); |
|
| 62 | 62 | |
| 63 | - return 0; |
|
| 64 | - } |
|
| 63 | + return 0; |
|
| 64 | + } |
|
| 65 | 65 | } |