Skip to content

Commit 38ce28f

Browse files
authored
Merge pull request #57081 from NoahOksuz/excludedisabled
added a exclude disabled option to last seen
2 parents 6337d22 + 4b94fd4 commit 38ce28f

2 files changed

Lines changed: 105 additions & 1 deletion

File tree

core/Command/User/LastSeen.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ protected function configure(): void {
4040
InputOption::VALUE_NONE,
4141
'shows a list of when all users were last logged in'
4242
)
43+
->addOption(
44+
'exclude-disabled',
45+
null,
46+
InputOption::VALUE_NONE,
47+
'exclude disabled users from the list (only works with --all)'
48+
)
4349
;
4450
}
4551

@@ -71,7 +77,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7177
return 1;
7278
}
7379

74-
$this->userManager->callForAllUsers(static function (IUser $user) use ($output): void {
80+
$excludeDisabled = $input->getOption('exclude-disabled');
81+
$this->userManager->callForAllUsers(static function (IUser $user) use ($output, $excludeDisabled): void {
82+
if ($excludeDisabled && !$user->isEnabled()) {
83+
return;
84+
}
7585
$lastLogin = $user->getLastLogin();
7686
if ($lastLogin === 0) {
7787
$output->writeln($user->getUID() . ' has never logged in.');

tests/Core/Command/User/LastSeenTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,98 @@ public function testInvalidUser(): void {
9393

9494
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
9595
}
96+
97+
public function testAllUsersWithoutExcludeDisabled(): void {
98+
$enabledUser = $this->getMockBuilder(IUser::class)->getMock();
99+
$enabledUser->expects($this->once())
100+
->method('getLastLogin')
101+
->willReturn(time());
102+
$enabledUser->expects($this->once())
103+
->method('getUID')
104+
->willReturn('enabled_user');
105+
$enabledUser->expects($this->never())
106+
->method('isEnabled');
107+
108+
$disabledUser = $this->getMockBuilder(IUser::class)->getMock();
109+
$disabledUser->expects($this->once())
110+
->method('getLastLogin')
111+
->willReturn(time());
112+
$disabledUser->expects($this->once())
113+
->method('getUID')
114+
->willReturn('disabled_user');
115+
$disabledUser->expects($this->never())
116+
->method('isEnabled');
117+
118+
$this->consoleInput->expects($this->once())
119+
->method('getArgument')
120+
->with('uid')
121+
->willReturn(null);
122+
123+
$this->consoleInput->expects($this->exactly(2))
124+
->method('getOption')
125+
->willReturnMap([
126+
['all', true],
127+
['exclude-disabled', false],
128+
]);
129+
130+
$this->userManager->expects($this->once())
131+
->method('callForAllUsers')
132+
->willReturnCallback(function ($callback) use ($enabledUser, $disabledUser) {
133+
$callback($enabledUser);
134+
$callback($disabledUser);
135+
});
136+
137+
$this->consoleOutput->expects($this->exactly(2))
138+
->method('writeln')
139+
->with($this->stringContains("'s last login:"));
140+
141+
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
142+
}
143+
144+
public function testAllUsersWithExcludeDisabled(): void {
145+
$enabledUser = $this->getMockBuilder(IUser::class)->getMock();
146+
$enabledUser->expects($this->once())
147+
->method('getLastLogin')
148+
->willReturn(time());
149+
$enabledUser->expects($this->once())
150+
->method('getUID')
151+
->willReturn('enabled_user');
152+
$enabledUser->expects($this->once())
153+
->method('isEnabled')
154+
->willReturn(true);
155+
156+
$disabledUser = $this->getMockBuilder(IUser::class)->getMock();
157+
$disabledUser->expects($this->never())
158+
->method('getLastLogin');
159+
$disabledUser->expects($this->never())
160+
->method('getUID');
161+
$disabledUser->expects($this->once())
162+
->method('isEnabled')
163+
->willReturn(false);
164+
165+
$this->consoleInput->expects($this->once())
166+
->method('getArgument')
167+
->with('uid')
168+
->willReturn(null);
169+
170+
$this->consoleInput->expects($this->exactly(2))
171+
->method('getOption')
172+
->willReturnMap([
173+
['all', true],
174+
['exclude-disabled', true],
175+
]);
176+
177+
$this->userManager->expects($this->once())
178+
->method('callForAllUsers')
179+
->willReturnCallback(function ($callback) use ($enabledUser, $disabledUser) {
180+
$callback($enabledUser);
181+
$callback($disabledUser);
182+
});
183+
184+
$this->consoleOutput->expects($this->once())
185+
->method('writeln')
186+
->with($this->stringContains("enabled_user's last login:"));
187+
188+
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
189+
}
96190
}

0 commit comments

Comments
 (0)