Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UserApiControllerTest.php « Controller « unit « tests - github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 96462edc8240d0dc0c069505272030b5a555da57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

namespace OCA\Text\Controller;

use OCA\Text\Service\SessionService;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\AppFramework\Http\DataResponse;
use \OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCA\Text\Db\Session;
use Test\TestCase;

class UserApiControllerTest extends TestCase {
	/**
	 * @var \PHPUnit\Framework\MockObject\MockObject
	 */
	private $collaboratorSearch;
	/**
	 * @var \PHPUnit\Framework\MockObject\MockObject
	 */
	private $userSession;
	/**
	 * @var \PHPUnit\Framework\MockObject\MockObject
	 */
	private $userManager;
	/**
	 * @var \PHPUnit\Framework\MockObject\MockObject
	 */
	private $sessionService;
	/**
	 * @var UserApiController
	 */
	private $userApiController;
	/**
	 * @var IRequest|\PHPUnit\Framework\MockObject\MockObject
	 */
	private $request;

	protected function setUp(): void {
		parent::setUp();
		$this->request = $this->createMock(IRequest::class);
		$this->collaboratorSearch = $this->createMock(ISearch::class);
		$this->userSession = $this->createMock(IUserSession::class);
		$this->userManager = $this->createMock(IUserManager::class);
		$this->sessionService = $this->createMock(SessionService::class);
		$this->userApiController = new UserApiController(
			'text',
			$this->request,
			$this->sessionService,
			$this->collaboratorSearch,
			$this->userManager,
			$this->userSession
		);
	}

	/**
	 * @dataProvider dataTestUsersIndex
	 */
	public function testUsersIndex(int $documentId, int $sessionId, string $sessionToken, string $filter) {
		$session = new Session();
		$session->setUserId('admin');
		$this->sessionService
			->expects($this->once())
			->method('isValidSession')->willReturn(true);
		$this->sessionService
			->expects($this->once())
			->method('getAllSessions')->willReturn([[
				'userId' => 'admin',
				'displayName' => 'admin',
			]]);
		$this->sessionService
			->expects($this->once())
			->method('getSession')->willReturn($session);
		$this->collaboratorSearch
			->expects($this->once())
			->method('search')->willReturn([
				[
					'exact' => [
						'users' => []
					],
					'users' => [
						[
							'label' => 'admin',
							'subline' => '',
							'icon' => 'icon-user',
							'value' => [
								'shareType' => 0,
								'shareWith' => 'admin'
							],
							'shareWithDisplayNameUnique' => 'admin',
							'status' => []
						],
					]
				]
			]);

		$response = $this->userApiController->index(
			$documentId,
			$sessionId,
			$sessionToken,
			$filter
		);
		$this->assertInstanceOf(DataResponse::class, $response);
		$this->assertIsArray($response->getData());
		$this->assertSame(['admin' => 'admin'], $response->getData());
	}

	public function dataTestUsersIndex() {
		return [
			[103, 44, 'token1', 'admin'],
			[103, 44, 'token2', 'admin'],
			[103, 44, 'token3', 'admin'],
		];
	}
}