2016-12-29 05:26:49 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-12-29 05:26:49 -05:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-12-29 05:26:49 -05:00
|
|
|
*/
|
2019-11-22 14:52:10 -05:00
|
|
|
|
2016-12-29 05:26:49 -05:00
|
|
|
namespace Test\Core\Command\Group;
|
|
|
|
|
|
|
|
|
|
use OC\Core\Command\Group\RemoveUser;
|
|
|
|
|
use OCP\IGroup;
|
|
|
|
|
use OCP\IGroupManager;
|
|
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\IUserManager;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class RemoveUserTest extends TestCase {
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-29 05:26:49 -05:00
|
|
|
private $groupManager;
|
|
|
|
|
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-29 05:26:49 -05:00
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
|
|
/** @var RemoveUser */
|
|
|
|
|
private $command;
|
|
|
|
|
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var InputInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-29 05:26:49 -05:00
|
|
|
private $input;
|
|
|
|
|
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var OutputInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2016-12-29 05:26:49 -05:00
|
|
|
private $output;
|
|
|
|
|
|
2026-04-28 13:29:54 -04:00
|
|
|
#[\Override]
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-12-29 05:26:49 -05:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->groupManager = $this->createMock(IGroupManager::class);
|
2020-10-05 09:12:57 -04:00
|
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
2016-12-29 05:26:49 -05:00
|
|
|
$this->command = new RemoveUser($this->userManager, $this->groupManager);
|
2026-04-09 16:16:52 -04:00
|
|
|
$this->output = $this->createMock(OutputInterface::class);
|
|
|
|
|
}
|
2016-12-29 05:26:49 -05:00
|
|
|
|
2026-04-09 16:16:52 -04:00
|
|
|
protected function configureInput(array|string $returnGroup, array|string $returnUser): void {
|
2016-12-29 05:26:49 -05:00
|
|
|
$this->input = $this->createMock(InputInterface::class);
|
|
|
|
|
$this->input->method('getArgument')
|
2026-04-09 16:16:52 -04:00
|
|
|
->willReturnCallback(function ($arg) use ($returnGroup, $returnUser) {
|
2016-12-29 05:26:49 -05:00
|
|
|
if ($arg === 'group') {
|
2026-04-09 16:16:52 -04:00
|
|
|
return $returnGroup;
|
|
|
|
|
}
|
|
|
|
|
if ($arg === 'user') {
|
|
|
|
|
return $returnUser;
|
2016-12-29 05:26:49 -05:00
|
|
|
}
|
|
|
|
|
throw new \Exception();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNoGroup(): void {
|
2026-04-09 16:16:52 -04:00
|
|
|
$this->configureInput('myGroup', 'myUser');
|
|
|
|
|
|
2016-12-29 05:26:49 -05:00
|
|
|
$this->groupManager->method('get')
|
|
|
|
|
->with('myGroup')
|
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('writeln')
|
|
|
|
|
->with('<error>group not found</error>');
|
|
|
|
|
|
|
|
|
|
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNoUser(): void {
|
2026-04-09 16:16:52 -04:00
|
|
|
$this->configureInput('myGroup', 'myUser');
|
|
|
|
|
|
2016-12-29 05:26:49 -05:00
|
|
|
$group = $this->createMock(IGroup::class);
|
|
|
|
|
$this->groupManager->method('get')
|
|
|
|
|
->with('myGroup')
|
|
|
|
|
->willReturn($group);
|
|
|
|
|
|
|
|
|
|
$this->userManager->method('get')
|
|
|
|
|
->with('myUser')
|
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
|
|
$this->output->expects($this->once())
|
|
|
|
|
->method('writeln')
|
2026-04-09 16:16:52 -04:00
|
|
|
->with('<error>user myUser not found</error>');
|
2016-12-29 05:26:49 -05:00
|
|
|
|
|
|
|
|
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-09 16:16:52 -04:00
|
|
|
public function testRemove(): void {
|
|
|
|
|
$this->configureInput('myGroup', 'myUser');
|
|
|
|
|
|
2016-12-29 05:26:49 -05:00
|
|
|
$group = $this->createMock(IGroup::class);
|
|
|
|
|
$this->groupManager->method('get')
|
|
|
|
|
->with('myGroup')
|
|
|
|
|
->willReturn($group);
|
|
|
|
|
|
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
$this->userManager->method('get')
|
|
|
|
|
->with('myUser')
|
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
|
|
$group->expects($this->once())
|
|
|
|
|
->method('removeUser')
|
|
|
|
|
->with($user);
|
|
|
|
|
|
|
|
|
|
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
|
|
|
|
|
}
|
2026-04-09 16:16:52 -04:00
|
|
|
|
|
|
|
|
public function testRemoveMultiple(): void {
|
|
|
|
|
$this->configureInput('myGroup', ['myUser', 'myOtherUser']);
|
|
|
|
|
|
|
|
|
|
$group = $this->createMock(IGroup::class);
|
|
|
|
|
$this->groupManager->method('get')
|
|
|
|
|
->with('myGroup')
|
|
|
|
|
->willReturn($group);
|
|
|
|
|
|
|
|
|
|
$user1 = $this->createMock(IUser::class);
|
|
|
|
|
$user2 = $this->createMock(IUser::class);
|
|
|
|
|
$this->userManager->method('get')
|
|
|
|
|
->willReturnMap([
|
|
|
|
|
['myUser', $user1],
|
|
|
|
|
['myOtherUser', $user2],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$group->expects($this->exactly(2))
|
|
|
|
|
->method('removeUser')
|
|
|
|
|
->with($this->callback(static fn (IUser $user): bool => in_array($user, [$user1, $user2], true)));
|
|
|
|
|
|
|
|
|
|
$this->output->expects($this->exactly(2))
|
|
|
|
|
->method('writeln')
|
|
|
|
|
->with($this->callback(static fn (string $message): bool => in_array($message,
|
|
|
|
|
[
|
|
|
|
|
'<info>user myUser removed</info>',
|
|
|
|
|
'<info>user myOtherUser removed</info>',
|
|
|
|
|
], true)));
|
|
|
|
|
|
|
|
|
|
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRemoveMultiplePartialSuccess(): void {
|
|
|
|
|
$this->configureInput('myGroup', ['myUser', 'myOtherUser']);
|
|
|
|
|
|
|
|
|
|
$group = $this->createMock(IGroup::class);
|
|
|
|
|
$this->groupManager->method('get')
|
|
|
|
|
->with('myGroup')
|
|
|
|
|
->willReturn($group);
|
|
|
|
|
|
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
$this->userManager->method('get')
|
|
|
|
|
->willReturnMap([
|
|
|
|
|
['myUser', $user],
|
|
|
|
|
['myOtherUser', null],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$group->expects($this->once())
|
|
|
|
|
->method('removeUser')
|
|
|
|
|
->with($user);
|
|
|
|
|
|
|
|
|
|
$this->output->expects($this->exactly(3))
|
|
|
|
|
->method('writeln')
|
|
|
|
|
->with($this->callback(static fn (string $message): bool => in_array($message,
|
|
|
|
|
[
|
|
|
|
|
'<info>user myUser removed</info>',
|
|
|
|
|
'<error>user myOtherUser not found</error>',
|
|
|
|
|
'<error>Some users were not found, all others where removed from the group.</error>',
|
|
|
|
|
], true)));
|
|
|
|
|
|
|
|
|
|
$this->invokePrivate($this->command, 'execute', [$this->input, $this->output]);
|
|
|
|
|
}
|
2016-12-29 05:26:49 -05:00
|
|
|
}
|