123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace Test\Collaboration\Collaborators;
- use OC\Collaboration\Collaborators\RemotePlugin;
- use OC\Collaboration\Collaborators\SearchResult;
- use OC\Federation\CloudIdManager;
- use OCP\Collaboration\Collaborators\SearchResultType;
- use OCP\Contacts\IManager;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Federation\ICloudIdManager;
- use OCP\ICacheFactory;
- use OCP\IConfig;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\Share\IShare;
- use Test\TestCase;
- class RemotePluginTest extends TestCase {
- /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
- protected $userManager;
- /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
- protected $config;
- /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
- protected $contactsManager;
- /** @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject */
- protected $cloudIdManager;
- /** @var RemotePlugin */
- protected $plugin;
- /** @var SearchResult */
- protected $searchResult;
- protected function setUp(): void {
- parent::setUp();
- $this->userManager = $this->createMock(IUserManager::class);
- $this->config = $this->createMock(IConfig::class);
- $this->contactsManager = $this->createMock(IManager::class);
- $this->cloudIdManager = new CloudIdManager(
- $this->contactsManager,
- $this->createMock(IURLGenerator::class),
- $this->createMock(IUserManager::class),
- $this->createMock(ICacheFactory::class),
- $this->createMock(IEventDispatcher::class)
- );
- $this->searchResult = new SearchResult();
- }
- public function instantiatePlugin() {
- $user = $this->createMock(IUser::class);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('admin');
- $userSession = $this->createMock(IUserSession::class);
- $userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($user);
- $this->plugin = new RemotePlugin($this->contactsManager, $this->cloudIdManager, $this->config, $this->userManager, $userSession);
- }
- /**
- * @dataProvider dataGetRemote
- *
- * @param string $searchTerm
- * @param array $contacts
- * @param bool $shareeEnumeration
- * @param array $expected
- * @param bool $exactIdMatch
- * @param bool $reachedEnd
- */
- public function testSearch($searchTerm, array $contacts, $shareeEnumeration, array $expected, $exactIdMatch, $reachedEnd): void {
- $this->config->expects($this->any())
- ->method('getAppValue')
- ->willReturnCallback(
- function ($appName, $key, $default) use ($shareeEnumeration) {
- if ($appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration') {
- return $shareeEnumeration ? 'yes' : 'no';
- }
- return $default;
- }
- );
- $this->instantiatePlugin();
- $this->contactsManager->expects($this->any())
- ->method('search')
- ->willReturnCallback(function ($search, $searchAttributes) use ($searchTerm, $contacts) {
- if ($search === $searchTerm) {
- return $contacts;
- }
- return [];
- });
- $moreResults = $this->plugin->search($searchTerm, 2, 0, $this->searchResult);
- $result = $this->searchResult->asArray();
- $this->assertSame($exactIdMatch, $this->searchResult->hasExactIdMatch(new SearchResultType('remotes')));
- $this->assertEquals($expected, $result);
- $this->assertSame($reachedEnd, $moreResults);
- }
- /**
- * @dataProvider dataTestSplitUserRemote
- *
- * @param string $remote
- * @param string $expectedUser
- * @param string $expectedUrl
- */
- public function testSplitUserRemote($remote, $expectedUser, $expectedUrl): void {
- $this->instantiatePlugin();
- $this->contactsManager->expects($this->any())
- ->method('search')
- ->willReturn([]);
- [$remoteUser, $remoteUrl] = $this->plugin->splitUserRemote($remote);
- $this->assertSame($expectedUser, $remoteUser);
- $this->assertSame($expectedUrl, $remoteUrl);
- }
- /**
- * @dataProvider dataTestSplitUserRemoteError
- *
- * @param string $id
- */
- public function testSplitUserRemoteError($id): void {
- $this->expectException(\Exception::class);
- $this->instantiatePlugin();
- $this->plugin->splitUserRemote($id);
- }
- public function dataGetRemote() {
- return [
- ['test', [], true, ['remotes' => [], 'exact' => ['remotes' => []]], false, true],
- ['test', [], false, ['remotes' => [], 'exact' => ['remotes' => []]], false, true],
- [
- 'test@remote',
- [],
- true,
- ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'test@remote', 'server' => 'remote'], 'uuid' => 'test', 'name' => 'test']]]],
- false,
- true,
- ],
- [
- 'test@remote',
- [],
- false,
- ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'test@remote', 'server' => 'remote'], 'uuid' => 'test', 'name' => 'test']]]],
- false,
- true,
- ],
- [
- 'test',
- [
- [
- 'UID' => 'uid',
- 'FN' => 'User3 @ Localhost',
- ],
- [
- 'UID' => 'uid',
- 'FN' => 'User2 @ Localhost',
- 'CLOUD' => [
- ],
- ],
- [
- 'UID' => 'uid1',
- 'FN' => 'User @ Localhost',
- 'CLOUD' => [
- 'username@localhost',
- ],
- ],
- ],
- true,
- ['remotes' => [['name' => 'User @ Localhost', 'label' => 'User @ Localhost (username@localhost)', 'uuid' => 'uid1', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exact' => ['remotes' => []]],
- false,
- true,
- ],
- [
- 'test',
- [
- [
- 'UID' => 'uid',
- 'FN' => 'User3 @ Localhost',
- ],
- [
- 'UID' => 'uid',
- 'FN' => 'User2 @ Localhost',
- 'CLOUD' => [
- ],
- ],
- [
- 'UID' => 'uid',
- 'FN' => 'User @ Localhost',
- 'CLOUD' => [
- 'username@localhost',
- ],
- ],
- ],
- false,
- ['remotes' => [], 'exact' => ['remotes' => []]],
- false,
- true,
- ],
- [
- 'test@remote',
- [
- [
- 'UID' => 'uid',
- 'FN' => 'User3 @ Localhost',
- ],
- [
- 'UID' => 'uid',
- 'FN' => 'User2 @ Localhost',
- 'CLOUD' => [
- ],
- ],
- [
- 'UID' => 'uid',
- 'FN' => 'User @ Localhost',
- 'CLOUD' => [
- 'username@localhost',
- ],
- ],
- ],
- true,
- ['remotes' => [['name' => 'User @ Localhost', 'label' => 'User @ Localhost (username@localhost)', 'uuid' => 'uid', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]], 'exact' => ['remotes' => [['label' => 'test (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'test@remote', 'server' => 'remote'], 'uuid' => 'test', 'name' => 'test']]]],
- false,
- true,
- ],
- [
- 'test@remote',
- [
- [
- 'UID' => 'uid',
- 'FN' => 'User3 @ Localhost',
- ],
- [
- 'UID' => 'uid',
- 'FN' => 'User2 @ Localhost',
- 'CLOUD' => [
- ],
- ],
- [
- 'UID' => 'uid',
- 'FN' => 'User @ Localhost',
- 'CLOUD' => [
- 'username@localhost',
- ],
- ],
- ],
- false,
- ['remotes' => [], 'exact' => ['remotes' => [['label' => 'test (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'test@remote', 'server' => 'remote'], 'uuid' => 'test', 'name' => 'test']]]],
- false,
- true,
- ],
- [
- 'username@localhost',
- [
- [
- 'UID' => 'uid3',
- 'FN' => 'User3 @ Localhost',
- ],
- [
- 'UID' => '2',
- 'FN' => 'User2 @ Localhost',
- 'CLOUD' => [
- ],
- ],
- [
- 'UID' => 'uid1',
- 'FN' => 'User @ Localhost',
- 'CLOUD' => [
- 'username@localhost',
- ],
- ],
- ],
- true,
- ['remotes' => [], 'exact' => ['remotes' => [['name' => 'User @ Localhost', 'label' => 'User @ Localhost (username@localhost)', 'uuid' => 'uid1', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]]]],
- true,
- true,
- ],
- [
- 'username@localhost',
- [
- [
- 'UID' => 'uid3',
- 'FN' => 'User3 @ Localhost',
- ],
- [
- 'UID' => 'uid2',
- 'FN' => 'User2 @ Localhost',
- 'CLOUD' => [
- ],
- ],
- [
- 'UID' => 'uid1',
- 'FN' => 'User @ Localhost',
- 'CLOUD' => [
- 'username@localhost',
- ],
- ],
- ],
- false,
- ['remotes' => [], 'exact' => ['remotes' => [['name' => 'User @ Localhost', 'label' => 'User @ Localhost (username@localhost)', 'uuid' => 'uid1', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']]]]],
- true,
- true,
- ],
- // contact with space
- [
- 'user name@localhost',
- [
- [
- 'UID' => 'uid1',
- 'FN' => 'User3 @ Localhost',
- ],
- [
- 'UID' => 'uid2',
- 'FN' => 'User2 @ Localhost',
- 'CLOUD' => [
- ],
- ],
- [
- 'UID' => 'uid3',
- 'FN' => 'User Name @ Localhost',
- 'CLOUD' => [
- 'user name@localhost',
- ],
- ],
- ],
- false,
- ['remotes' => [], 'exact' => ['remotes' => [['name' => 'User Name @ Localhost', 'label' => 'User Name @ Localhost (user name@localhost)', 'uuid' => 'uid3', 'type' => '', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'user name@localhost', 'server' => 'localhost']]]]],
- true,
- true,
- ],
- // remote with space, no contact
- [
- 'user space@remote',
- [
- [
- 'UID' => 'uid3',
- 'FN' => 'User3 @ Localhost',
- ],
- [
- 'UID' => 'uid2',
- 'FN' => 'User2 @ Localhost',
- 'CLOUD' => [
- ],
- ],
- [
- 'UID' => 'uid1',
- 'FN' => 'User @ Localhost',
- 'CLOUD' => [
- 'username@localhost',
- ],
- ],
- ],
- false,
- ['remotes' => [], 'exact' => ['remotes' => [['label' => 'user space (remote)', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'user space@remote', 'server' => 'remote'], 'uuid' => 'user space', 'name' => 'user space']]]],
- false,
- true,
- ],
- ];
- }
- public function dataTestSplitUserRemote() {
- $userPrefix = ['user@name', 'username'];
- $protocols = ['', 'http://', 'https://'];
- $remotes = [
- 'localhost',
- 'local.host',
- 'dev.local.host',
- 'dev.local.host/path',
- 'dev.local.host/at@inpath',
- '127.0.0.1',
- '::1',
- '::192.0.2.128',
- '::192.0.2.128/at@inpath',
- ];
- $testCases = [];
- foreach ($userPrefix as $user) {
- foreach ($remotes as $remote) {
- foreach ($protocols as $protocol) {
- $baseUrl = $user . '@' . $protocol . $remote;
- if ($protocol === 'https://') {
- // https:// protocol is not expected in the final result
- $protocol = '';
- }
- $testCases[] = [$baseUrl, $user, $protocol . $remote];
- $testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
- $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
- $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
- }
- }
- }
- return $testCases;
- }
- public function dataTestSplitUserRemoteError() {
- return [
- // Invalid path
- ['user@'],
- // Invalid user
- ['@server'],
- ['us/er@server'],
- ['us:er@server'],
- // Invalid splitting
- ['user'],
- [''],
- ['us/erserver'],
- ['us:erserver'],
- ];
- }
- }
|