|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | +namespace OCA\Settings\Tests\Integration; |
| 8 | + |
| 9 | +use OC\Settings\AuthorizedGroup; |
| 10 | +use OC\Settings\AuthorizedGroupMapper; |
| 11 | +use OCA\Settings\Service\AuthorizedGroupService; |
| 12 | +use OCA\Settings\Service\ConflictException; |
| 13 | +use OCP\AppFramework\Db\DoesNotExistException; |
| 14 | +use Test\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Integration test for duplicate prevention in AuthorizedGroupService |
| 18 | + * This test verifies the complete flow of duplicate detection and prevention |
| 19 | + */ |
| 20 | +#[\PHPUnit\Framework\Attributes\Group('DB')] |
| 21 | +class DuplicateAssignmentIntegrationTest extends TestCase { |
| 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 | + } |
| 157 | +} |
0 commit comments