UserControllerTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Core\Controller;
  7. use OC\Core\Controller\UserController;
  8. use OCP\AppFramework\Http\JSONResponse;
  9. use OCP\IRequest;
  10. use OCP\IUser;
  11. use OCP\IUserManager;
  12. use Test\TestCase;
  13. class UserControllerTest extends TestCase {
  14. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  15. private $userManager;
  16. /** @var UserController */
  17. private $controller;
  18. protected function setUp(): void {
  19. parent::setUp();
  20. $this->userManager = $this->createMock(IUserManager::class);
  21. $this->controller = new UserController(
  22. 'core',
  23. $this->createMock(IRequest::class),
  24. $this->userManager
  25. );
  26. }
  27. public function testGetDisplayNames() {
  28. $user = $this->createMock(IUser::class);
  29. $user->method('getDisplayName')
  30. ->willReturn('FooDisplay Name');
  31. $this->userManager
  32. ->method('get')
  33. ->willReturnCallback(function ($uid) use ($user) {
  34. if ($uid === 'foo') {
  35. return $user;
  36. }
  37. return null;
  38. });
  39. $expected = new JSONResponse([
  40. 'users' => [
  41. 'foo' => 'FooDisplay Name',
  42. 'bar' => 'bar',
  43. ],
  44. 'status' => 'success'
  45. ]);
  46. $result = $this->controller->getDisplayNames(['foo', 'bar']);
  47. $this->assertEquals($expected, $result);
  48. }
  49. }