Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Improve unit testing and stability of inactive users command
  • Loading branch information
tomneedham committed Jul 4, 2017
commit c7cbe37a5de58f3f2d18e9075d5e8232d92e6871
10 changes: 6 additions & 4 deletions core/Command/User/Inactive.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,17 @@ protected function configure() {

protected function execute(InputInterface $input, OutputInterface $output) {
$days = $input->getArgument('days');
if($days < 1) {
throw new InvalidArgumentException('Days must be above zero');
if(!is_int($days) || $days < 1) {
throw new InvalidArgumentException('Days must be integer and above zero');
}

$now = time();

$inactive = [];
$this->userManager->callForSeenUsers(function(IUser $user) use (&$inactive, $days) {
$this->userManager->callForSeenUsers(function(IUser $user) use (&$inactive, $days, $now) {
$lastLogin = $user->getLastLogin();
// Difference between now and last login, into days, and rounded down
$daysSinceLastLogin = floor((time() - $lastLogin) / (60*60*24));
$daysSinceLastLogin = floor(($now - $lastLogin) / (60*60*24));
if($daysSinceLastLogin >= $days) {
$inactive[] = [
'uid' => $user->getUID(),
Expand Down
44 changes: 40 additions & 4 deletions tests/Core/Command/User/InactiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,41 @@ public function testWithInvalidDays($days) {

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

protected function dummyUser($lastLogin) {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getLastLogin')
->willReturn($lastLogin);

return $user;
}

public function validDays() {
return [
'no users' => [[], 10, 0],
'1 recent user, excluded' => [[$this->dummyUser(time()-2*24*60*60)], 10, 0],
'1 recent user, included' => [[$this->dummyUser(time()-2*24*60*60)], 1, 1],
'2 users 1 includd' => [
[
$this->dummyUser(time()-5*24*60*60),
$this->dummyUser(time()-10*24*60*60)
], 7, 1],
];
}

public function testWithValidDays() {
/**
* @dataProvider validDays
*
* @param $users
* @param $days
* @param $expectedCount
*/
public function testWithValidDays($users, $days, $expectedCount) {
$this->consoleInput->expects($this->once())
->method('getArgument')
->with('days')
->willReturn(1);
->willReturn($days);

// Work with json output
$this->consoleInput->expects($this->once())
Expand All @@ -87,11 +116,18 @@ public function testWithValidDays() {
->willReturn(Base::OUTPUT_FORMAT_JSON);

$this->userManager->expects($this->once())
->method('callForSeenUsers');
->method('callForSeenUsers')
->willReturnCallback(function($callback) use ($users, $expectedCount) {
foreach($users as $user) {
$callback($user);
}
});

$this->consoleOutput->expects($this->once())
->method('writeLn')
->with($this->stringContains('['));
->with($this->callback(function($output) use ($expectedCount) {
return self::isJson($output) && count(json_decode($output)) === $expectedCount;
}));

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