-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathAuthorizedGroupServiceTest.php
More file actions
158 lines (125 loc) · 5.05 KB
/
Copy pathAuthorizedGroupServiceTest.php
File metadata and controls
158 lines (125 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Settings\Tests\Service;
use OC\Settings\AuthorizedGroup;
use OC\Settings\AuthorizedGroupMapper;
use OCA\Settings\Service\AuthorizedGroupService;
use OCA\Settings\Service\ConflictException;
use OCP\AppFramework\Db\DoesNotExistException;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class AuthorizedGroupServiceTest extends TestCase {
private AuthorizedGroupMapper&MockObject $mapper;
private AuthorizedGroupService $service;
protected function setUp(): void {
parent::setUp();
$this->mapper = $this->createMock(AuthorizedGroupMapper::class);
$this->service = new AuthorizedGroupService($this->mapper);
}
public function testCreateSuccessWhenNoDuplicateExists(): void {
$groupId = 'testgroup';
$class = 'TestClass';
// Mock that no existing assignment is found (throws DoesNotExistException)
$this->mapper->expects($this->once())
->method('findByGroupIdAndClass')
->with($groupId, $class)
->willThrowException(new DoesNotExistException('Not found'));
// Mock the successful creation
$expectedGroup = new AuthorizedGroup();
$expectedGroup->setGroupId($groupId);
$expectedGroup->setClass($class);
$expectedGroup->setId(123);
$this->mapper->expects($this->once())
->method('insert')
->willReturn($expectedGroup);
$result = $this->service->create($groupId, $class);
$this->assertInstanceOf(AuthorizedGroup::class, $result);
$this->assertEquals($groupId, $result->getGroupId());
$this->assertEquals($class, $result->getClass());
}
public function testCreateThrowsConflictExceptionWhenDuplicateExists(): void {
$groupId = 'testgroup';
$class = 'TestClass';
// Mock that an existing assignment is found
$existingGroup = new AuthorizedGroup();
$existingGroup->setGroupId($groupId);
$existingGroup->setClass($class);
$existingGroup->setId(42);
$this->mapper->expects($this->once())
->method('findByGroupIdAndClass')
->with($groupId, $class)
->willReturn($existingGroup);
// Mapper insert should never be called when duplicate exists
$this->mapper->expects($this->never())
->method('insert');
$this->expectException(ConflictException::class);
$this->expectExceptionMessage('Group is already assigned to this class');
$this->service->create($groupId, $class);
}
public function testCreateAllowsDifferentGroupsSameClass(): void {
$groupId1 = 'testgroup1';
$groupId2 = 'testgroup2';
$class = 'TestClass';
// Mock that no duplicate exists for group1
$this->mapper->expects($this->exactly(2))
->method('findByGroupIdAndClass')
->willReturnCallback(function ($groupId, $classArg) use ($groupId1, $groupId2, $class): void {
$this->assertContains($groupId, [$groupId1, $groupId2]);
$this->assertEquals($class, $classArg);
throw new DoesNotExistException('Not found');
});
$expectedGroup1 = new AuthorizedGroup();
$expectedGroup1->setGroupId($groupId1);
$expectedGroup1->setClass($class);
$expectedGroup1->setId(123);
$expectedGroup2 = new AuthorizedGroup();
$expectedGroup2->setGroupId($groupId2);
$expectedGroup2->setClass($class);
$expectedGroup2->setId(124);
$this->mapper->expects($this->exactly(2))
->method('insert')
->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2);
// Both creations should succeed
$result1 = $this->service->create($groupId1, $class);
$this->assertEquals($groupId1, $result1->getGroupId());
$this->assertEquals($class, $result1->getClass());
$result2 = $this->service->create($groupId2, $class);
$this->assertEquals($groupId2, $result2->getGroupId());
$this->assertEquals($class, $result2->getClass());
}
public function testCreateAllowsSameGroupDifferentClasses(): void {
$groupId = 'testgroup';
$class1 = 'TestClass1';
$class2 = 'TestClass2';
// Mock that no duplicate exists for either class
$this->mapper->expects($this->exactly(2))
->method('findByGroupIdAndClass')
->willReturnCallback(function ($groupIdArg, $class) use ($groupId, $class1, $class2): void {
$this->assertEquals($groupId, $groupIdArg);
$this->assertContains($class, [$class1, $class2]);
throw new DoesNotExistException('Not found');
});
$expectedGroup1 = new AuthorizedGroup();
$expectedGroup1->setGroupId($groupId);
$expectedGroup1->setClass($class1);
$expectedGroup1->setId(123);
$expectedGroup2 = new AuthorizedGroup();
$expectedGroup2->setGroupId($groupId);
$expectedGroup2->setClass($class2);
$expectedGroup2->setId(124);
$this->mapper->expects($this->exactly(2))
->method('insert')
->willReturnOnConsecutiveCalls($expectedGroup1, $expectedGroup2);
// Both creations should succeed
$result1 = $this->service->create($groupId, $class1);
$result2 = $this->service->create($groupId, $class2);
$this->assertEquals($groupId, $result1->getGroupId());
$this->assertEquals($groupId, $result2->getGroupId());
$this->assertEquals($class1, $result1->getClass());
$this->assertEquals($class2, $result2->getClass());
}
}