Skip to content

Commit ae0b10f

Browse files
committed
feat(occ): make it possible to add an arbitrary number of users to a group
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent fdfcc00 commit ae0b10f

2 files changed

Lines changed: 109 additions & 10 deletions

File tree

core/Command/Group/AddUser.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,44 @@ protected function configure() {
3434
'group to add the user to'
3535
)->addArgument(
3636
'user',
37-
InputArgument::REQUIRED,
38-
'user to add to the group'
37+
InputArgument::REQUIRED + InputArgument::IS_ARRAY,
38+
'users to add to the group',
3939
);
4040
}
4141

4242
protected function execute(InputInterface $input, OutputInterface $output): int {
4343
$group = $this->groupManager->get($input->getArgument('group'));
4444
if (is_null($group)) {
4545
$output->writeln('<error>group not found</error>');
46-
return 1;
46+
return Base::FAILURE;
47+
}
48+
49+
$allUsersFound = true;
50+
$noUserFound = true;
51+
$users = (array)$input->getArgument('user');
52+
foreach ($users as $userId) {
53+
$user = $this->userManager->get($userId);
54+
if (is_null($user)) {
55+
$output->writeln('<error>user ' . $userId . ' not found</error>');
56+
$allUsersFound = false;
57+
continue;
58+
}
59+
$noUserFound = false;
60+
$group->addUser($user);
61+
unset($user);
62+
$output->writeln('<info>user ' . $userId . ' added</info>');
4763
}
48-
$user = $this->userManager->get($input->getArgument('user'));
49-
if (is_null($user)) {
50-
$output->writeln('<error>user not found</error>');
51-
return 1;
64+
65+
if (!$allUsersFound && !$noUserFound) {
66+
$output->writeln('<error>Some users were not found, all others where added to the group.</error>');
67+
return Base::FAILURE;
68+
}
69+
70+
if ($noUserFound) {
71+
return Base::FAILURE;
5272
}
53-
$group->addUser($user);
54-
return 0;
73+
74+
return Base::SUCCESS;
5575
}
5676

5777
/**

tests/Core/Command/Group/AddUserTest.php

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testNoUser(): void {
7676

7777
$this->output->expects($this->once())
7878
->method('writeln')
79-
->with('<error>user not found</error>');
79+
->with('<error>user myUser not found</error>');
8080

8181
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
8282
}
@@ -98,4 +98,83 @@ public function testAdd(): void {
9898

9999
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
100100
}
101+
102+
public function testAddMultiple(): void {
103+
$this->input->method('getArgument')
104+
->willReturnCallback(function ($arg) {
105+
if ($arg === 'group') {
106+
return 'myGroup';
107+
}
108+
if ($arg === 'user') {
109+
return ['myUser', 'myOtherUser'];
110+
}
111+
throw new \Exception();
112+
});
113+
114+
$group = $this->createMock(IGroup::class);
115+
$this->groupManager->method('get')
116+
->with('myGroup')
117+
->willReturn($group);
118+
119+
$user = $this->createMock(IUser::class);
120+
$this->userManager->method('get')
121+
->willReturnMap([
122+
['myUser', $user],
123+
['myOtherUser', clone $user],
124+
]);
125+
126+
$group->expects($this->exactly(2))
127+
->method('addUser')
128+
->with($user);
129+
130+
$this->output->expects($this->exactly(2))
131+
->method('writeln')
132+
->with($this->callback(static fn (string $message): bool => in_array($message,
133+
[
134+
'<info>user myUser added</info>',
135+
'<info>user myOtherUser added</info>',
136+
], true)));
137+
138+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
139+
}
140+
141+
public function testAddMultiplePartialSuccess(): void {
142+
$this->input->method('getArgument')
143+
->willReturnCallback(function ($arg) {
144+
if ($arg === 'group') {
145+
return 'myGroup';
146+
}
147+
if ($arg === 'user') {
148+
return ['myUser', 'myOtherUser'];
149+
}
150+
throw new \Exception();
151+
});
152+
153+
$group = $this->createMock(IGroup::class);
154+
$this->groupManager->method('get')
155+
->with('myGroup')
156+
->willReturn($group);
157+
158+
$user = $this->createMock(IUser::class);
159+
$this->userManager->method('get')
160+
->willReturnMap([
161+
['myUser', $user],
162+
['myOtherUser', null],
163+
]);
164+
165+
$group->expects($this->once())
166+
->method('addUser')
167+
->with($user);
168+
169+
$this->output->expects($this->exactly(3))
170+
->method('writeln')
171+
->with($this->callback(static fn (string $message): bool => in_array($message,
172+
[
173+
'<info>user myUser added</info>',
174+
'<error>user myOtherUser not found</error>',
175+
'<error>Some users were not found, all others where added to the group.</error>',
176+
], true)));
177+
178+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
179+
}
101180
}

0 commit comments

Comments
 (0)