|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OCA\Text\Controller; |
| 4 | + |
| 5 | +use OCA\Text\Service\SessionService; |
| 6 | +use OCP\Collaboration\Collaborators\ISearch; |
| 7 | +use OCP\AppFramework\Http\DataResponse; |
| 8 | +use \OCP\IRequest; |
| 9 | +use OCP\IUserManager; |
| 10 | +use OCP\IUserSession; |
| 11 | +use OCA\Text\Db\Session; |
| 12 | +use Test\TestCase; |
| 13 | + |
| 14 | +class UserApiControllerTest extends TestCase { |
| 15 | + /** |
| 16 | + * @var \PHPUnit\Framework\MockObject\MockObject |
| 17 | + */ |
| 18 | + private $collaboratorSearch; |
| 19 | + /** |
| 20 | + * @var \PHPUnit\Framework\MockObject\MockObject |
| 21 | + */ |
| 22 | + private $userSession; |
| 23 | + /** |
| 24 | + * @var \PHPUnit\Framework\MockObject\MockObject |
| 25 | + */ |
| 26 | + private $userManager; |
| 27 | + /** |
| 28 | + * @var \PHPUnit\Framework\MockObject\MockObject |
| 29 | + */ |
| 30 | + private $sessionService; |
| 31 | + /** |
| 32 | + * @var UserApiController |
| 33 | + */ |
| 34 | + private $userApiController; |
| 35 | + /** |
| 36 | + * @var IRequest|\PHPUnit\Framework\MockObject\MockObject |
| 37 | + */ |
| 38 | + private $request; |
| 39 | + |
| 40 | + protected function setUp(): void { |
| 41 | + parent::setUp(); |
| 42 | + $this->request = $this->createMock(IRequest::class); |
| 43 | + $this->collaboratorSearch = $this->createMock(ISearch::class); |
| 44 | + $this->userSession = $this->createMock(IUserSession::class); |
| 45 | + $this->userManager = $this->createMock(IUserManager::class); |
| 46 | + $this->sessionService = $this->createMock(SessionService::class); |
| 47 | + $this->userApiController = new UserApiController( |
| 48 | + 'text', |
| 49 | + $this->request, |
| 50 | + $this->sessionService, |
| 51 | + $this->collaboratorSearch, |
| 52 | + $this->userManager, |
| 53 | + $this->userSession |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @dataProvider dataTestUsersIndex |
| 59 | + */ |
| 60 | + public function testUsersIndex(int $documentId, int $sessionId, string $sessionToken, string $filter) { |
| 61 | + $session = new Session(); |
| 62 | + $session->setUserId('admin'); |
| 63 | + $this->sessionService |
| 64 | + ->expects($this->once()) |
| 65 | + ->method('isValidSession')->willReturn(true); |
| 66 | + $this->sessionService |
| 67 | + ->expects($this->once()) |
| 68 | + ->method('getAllSessions')->willReturn([[ |
| 69 | + 'userId' => 'admin', |
| 70 | + 'displayName' => 'admin', |
| 71 | + ]]); |
| 72 | + $this->sessionService |
| 73 | + ->expects($this->once()) |
| 74 | + ->method('getSession')->willReturn($session); |
| 75 | + $this->collaboratorSearch |
| 76 | + ->expects($this->once()) |
| 77 | + ->method('search')->willReturn([ |
| 78 | + [ |
| 79 | + 'exact' => [ |
| 80 | + 'users' => [] |
| 81 | + ], |
| 82 | + 'users' => [ |
| 83 | + [ |
| 84 | + 'label' => 'admin', |
| 85 | + 'subline' => '', |
| 86 | + 'icon' => 'icon-user', |
| 87 | + 'value' => [ |
| 88 | + 'shareType' => 0, |
| 89 | + 'shareWith' => 'admin' |
| 90 | + ], |
| 91 | + 'shareWithDisplayNameUnique' => 'admin', |
| 92 | + 'status' => [] |
| 93 | + ], |
| 94 | + ] |
| 95 | + ] |
| 96 | + ]); |
| 97 | + |
| 98 | + $response = $this->userApiController->index( |
| 99 | + $documentId, |
| 100 | + $sessionId, |
| 101 | + $sessionToken, |
| 102 | + $filter |
| 103 | + ); |
| 104 | + $this->assertInstanceOf(DataResponse::class, $response); |
| 105 | + $this->assertIsArray($response->getData()); |
| 106 | + $this->assertSame(['admin' => 'admin'], $response->getData()); |
| 107 | + } |
| 108 | + |
| 109 | + public function dataTestUsersIndex() { |
| 110 | + return [ |
| 111 | + [103, 44, 'token1', 'admin'], |
| 112 | + [103, 44, 'token2', 'admin'], |
| 113 | + [103, 44, 'token3', 'admin'], |
| 114 | + ]; |
| 115 | + } |
| 116 | +} |
0 commit comments