LastSeenTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Tests\Core\Command\User;
  8. use OC\Core\Command\User\LastSeen;
  9. use OCP\IUser;
  10. use OCP\IUserManager;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Test\TestCase;
  14. class LastSeenTest extends TestCase {
  15. /** @var \PHPUnit\Framework\MockObject\MockObject */
  16. protected $userManager;
  17. /** @var \PHPUnit\Framework\MockObject\MockObject */
  18. protected $consoleInput;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject */
  20. protected $consoleOutput;
  21. /** @var \Symfony\Component\Console\Command\Command */
  22. protected $command;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $userManager = $this->userManager = $this->getMockBuilder(IUserManager::class)
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  29. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  30. /** @var \OCP\IUserManager $userManager */
  31. $this->command = new LastSeen($userManager);
  32. }
  33. public function validUserLastSeen() {
  34. return [
  35. [0, 'never logged in'],
  36. [time(), 'last login'],
  37. ];
  38. }
  39. /**
  40. * @dataProvider validUserLastSeen
  41. *
  42. * @param int $lastSeen
  43. * @param string $expectedString
  44. */
  45. public function testValidUser($lastSeen, $expectedString): void {
  46. $user = $this->getMockBuilder(IUser::class)->getMock();
  47. $user->expects($this->once())
  48. ->method('getLastLogin')
  49. ->willReturn($lastSeen);
  50. $this->userManager->expects($this->once())
  51. ->method('get')
  52. ->with('user')
  53. ->willReturn($user);
  54. $this->consoleInput->expects($this->once())
  55. ->method('getArgument')
  56. ->with('uid')
  57. ->willReturn('user');
  58. $this->consoleOutput->expects($this->once())
  59. ->method('writeln')
  60. ->with($this->stringContains($expectedString));
  61. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  62. }
  63. public function testInvalidUser(): void {
  64. $this->userManager->expects($this->once())
  65. ->method('get')
  66. ->with('user')
  67. ->willReturn(null);
  68. $this->consoleInput->expects($this->once())
  69. ->method('getArgument')
  70. ->with('uid')
  71. ->willReturn('user');
  72. $this->consoleOutput->expects($this->once())
  73. ->method('writeln')
  74. ->with($this->stringContains('User does not exist'));
  75. self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  76. }
  77. }