Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/Command/User/LastSeen.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ protected function configure(): void {
InputOption::VALUE_NONE,
'shows a list of when all users were last logged in'
)
->addOption(
'exclude-disabled',
null,
InputOption::VALUE_NONE,
'exclude disabled users from the list (only works with --all)'
)
;
}

Expand Down Expand Up @@ -68,7 +74,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

$this->userManager->callForAllUsers(static function (IUser $user) use ($output): void {
$excludeDisabled = $input->getOption('exclude-disabled');
$this->userManager->callForAllUsers(static function (IUser $user) use ($output, $excludeDisabled): void {
if ($excludeDisabled && !$user->isEnabled()) {
return;
}
$lastLogin = $user->getLastLogin();
if ($lastLogin === 0) {
$output->writeln($user->getUID() . ' has never logged in.');
Expand Down
94 changes: 94 additions & 0 deletions tests/Core/Command/User/LastSeenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,98 @@ public function testInvalidUser(): void {

self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}

public function testAllUsersWithoutExcludeDisabled(): void {
$enabledUser = $this->getMockBuilder(IUser::class)->getMock();
$enabledUser->expects($this->once())
->method('getLastLogin')
->willReturn(time());
$enabledUser->expects($this->once())
->method('getUID')
->willReturn('enabled_user');
$enabledUser->expects($this->never())
->method('isEnabled');

$disabledUser = $this->getMockBuilder(IUser::class)->getMock();
$disabledUser->expects($this->once())
->method('getLastLogin')
->willReturn(time());
$disabledUser->expects($this->once())
->method('getUID')
->willReturn('disabled_user');
$disabledUser->expects($this->never())
->method('isEnabled');

$this->consoleInput->expects($this->once())
->method('getArgument')
->with('uid')
->willReturn(null);

$this->consoleInput->expects($this->exactly(2))
->method('getOption')
->willReturnMap([
['all', true],
['exclude-disabled', false],
]);

$this->userManager->expects($this->once())
->method('callForAllUsers')
->willReturnCallback(function ($callback) use ($enabledUser, $disabledUser) {
$callback($enabledUser);
$callback($disabledUser);
});

$this->consoleOutput->expects($this->exactly(2))
->method('writeln')
->with($this->stringContains("'s last login:"));

self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}

public function testAllUsersWithExcludeDisabled(): void {
$enabledUser = $this->getMockBuilder(IUser::class)->getMock();
$enabledUser->expects($this->once())
->method('getLastLogin')
->willReturn(time());
$enabledUser->expects($this->once())
->method('getUID')
->willReturn('enabled_user');
$enabledUser->expects($this->once())
->method('isEnabled')
->willReturn(true);

$disabledUser = $this->getMockBuilder(IUser::class)->getMock();
$disabledUser->expects($this->never())
->method('getLastLogin');
$disabledUser->expects($this->never())
->method('getUID');
$disabledUser->expects($this->once())
->method('isEnabled')
->willReturn(false);

$this->consoleInput->expects($this->once())
->method('getArgument')
->with('uid')
->willReturn(null);

$this->consoleInput->expects($this->exactly(2))
->method('getOption')
->willReturnMap([
['all', true],
['exclude-disabled', true],
]);

$this->userManager->expects($this->once())
->method('callForAllUsers')
->willReturnCallback(function ($callback) use ($enabledUser, $disabledUser) {
$callback($enabledUser);
$callback($disabledUser);
});

$this->consoleOutput->expects($this->once())
->method('writeln')
->with($this->stringContains("enabled_user's last login:"));

self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}
}
Loading