Skip to content

Commit eb87f17

Browse files
enjeckblizzz
authored andcommitted
feat(occ): make it possible to remove an arbitrary number of users to a group
Signed-off-by: Enjeck C. <patrathewhiz@gmail.com>
1 parent 8355182 commit eb87f17

2 files changed

Lines changed: 111 additions & 11 deletions

File tree

core/Command/Group/RemoveUser.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 remove the user from'
3535
)->addArgument(
3636
'user',
37-
InputArgument::REQUIRED,
38-
'user to remove from the group'
37+
InputArgument::REQUIRED + InputArgument::IS_ARRAY,
38+
'users to remove from 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->removeUser($user);
61+
unset($user);
62+
$output->writeln('<info>user ' . $userId . ' removed</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 removed from the group.</error>');
67+
return Base::FAILURE;
68+
}
69+
70+
if ($noUserFound) {
71+
return Base::FAILURE;
5272
}
53-
$group->removeUser($user);
54-
return 0;
73+
74+
return Base::SUCCESS;
5575
}
5676

5777
/**

tests/Core/Command/Group/RemoveUserTest.php

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ 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
}
8383

84-
public function testAdd(): void {
84+
public function testRemove(): void {
8585
$group = $this->createMock(IGroup::class);
8686
$this->groupManager->method('get')
8787
->with('myGroup')
@@ -98,4 +98,84 @@ public function testAdd(): void {
9898

9999
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
100100
}
101+
102+
public function testRemoveMultiple(): 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+
$user1 = $this->createMock(IUser::class);
120+
$user2 = $this->createMock(IUser::class);
121+
$this->userManager->method('get')
122+
->willReturnMap([
123+
['myUser', $user1],
124+
['myOtherUser', $user2],
125+
]);
126+
127+
$group->expects($this->exactly(2))
128+
->method('removeUser')
129+
->with($this->callback(static fn(IUser $user): bool => in_array($user, [$user1, $user2], true)));
130+
131+
$this->output->expects($this->exactly(2))
132+
->method('writeln')
133+
->with($this->callback(static fn (string $message): bool => in_array($message,
134+
[
135+
'<info>user myUser removed</info>',
136+
'<info>user myOtherUser removed</info>',
137+
], true)));
138+
139+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
140+
}
141+
142+
public function testRemoveMultiplePartialSuccess(): void {
143+
$this->input->method('getArgument')
144+
->willReturnCallback(function ($arg) {
145+
if ($arg === 'group') {
146+
return 'myGroup';
147+
}
148+
if ($arg === 'user') {
149+
return ['myUser', 'myOtherUser'];
150+
}
151+
throw new \Exception();
152+
});
153+
154+
$group = $this->createMock(IGroup::class);
155+
$this->groupManager->method('get')
156+
->with('myGroup')
157+
->willReturn($group);
158+
159+
$user = $this->createMock(IUser::class);
160+
$this->userManager->method('get')
161+
->willReturnMap([
162+
['myUser', $user],
163+
['myOtherUser', null],
164+
]);
165+
166+
$group->expects($this->once())
167+
->method('removeUser')
168+
->with($user);
169+
170+
$this->output->expects($this->exactly(3))
171+
->method('writeln')
172+
->with($this->callback(static fn (string $message): bool => in_array($message,
173+
[
174+
'<info>user myUser removed</info>',
175+
'<error>user myOtherUser not found</error>',
176+
'<error>Some users were not found, all others where removed from the group.</error>',
177+
], true)));
178+
179+
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
180+
}
101181
}

0 commit comments

Comments
 (0)