2017-09-13 15:29:57 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Collaboration\Collaborators;
|
|
|
|
|
|
|
|
use OC\Collaboration\Collaborators\Search;
|
|
|
|
use OC\Collaboration\Collaborators\SearchResult;
|
|
|
|
use OCP\Collaboration\Collaborators\ISearch;
|
|
|
|
use OCP\Collaboration\Collaborators\ISearchPlugin;
|
|
|
|
use OCP\Collaboration\Collaborators\SearchResultType;
|
|
|
|
use OCP\IContainer;
|
2020-06-24 17:49:16 +03:00
|
|
|
use OCP\Share\IShare;
|
2017-09-13 15:29:57 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class SearchTest extends TestCase {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IContainer|\PHPUnit\Framework\MockObject\MockObject */
|
2017-09-13 15:29:57 +03:00
|
|
|
protected $container;
|
|
|
|
/** @var ISearch */
|
|
|
|
protected $search;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-09-13 15:29:57 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->container = $this->createMock(IContainer::class);
|
|
|
|
|
|
|
|
$this->search = new Search($this->container);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSearchSharees
|
|
|
|
*/
|
|
|
|
public function testSearch(
|
2021-04-13 23:49:42 +03:00
|
|
|
string $searchTerm,
|
2017-09-13 15:29:57 +03:00
|
|
|
array $shareTypes,
|
2021-04-13 23:49:42 +03:00
|
|
|
int $page,
|
2021-04-15 13:45:40 +03:00
|
|
|
int $perPage,
|
2017-09-13 15:29:57 +03:00
|
|
|
array $mockedUserResult,
|
|
|
|
array $mockedGroupsResult,
|
|
|
|
array $mockedRemotesResult,
|
2021-04-13 23:49:42 +03:00
|
|
|
array $mockedMailResult,
|
2017-09-13 15:29:57 +03:00
|
|
|
array $expected,
|
2021-04-13 23:49:42 +03:00
|
|
|
bool $expectedMoreResults
|
2017-09-13 15:29:57 +03:00
|
|
|
) {
|
|
|
|
$searchResult = new SearchResult();
|
|
|
|
|
|
|
|
$userPlugin = $this->createMock(ISearchPlugin::class);
|
|
|
|
$userPlugin->expects($this->any())
|
|
|
|
->method('search')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function () use ($searchResult, $mockedUserResult, $expectedMoreResults) {
|
2017-09-13 15:29:57 +03:00
|
|
|
$type = new SearchResultType('users');
|
|
|
|
$searchResult->addResultSet($type, $mockedUserResult);
|
|
|
|
return $expectedMoreResults;
|
|
|
|
});
|
|
|
|
|
|
|
|
$groupPlugin = $this->createMock(ISearchPlugin::class);
|
|
|
|
$groupPlugin->expects($this->any())
|
|
|
|
->method('search')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function () use ($searchResult, $mockedGroupsResult, $expectedMoreResults) {
|
2017-09-13 15:29:57 +03:00
|
|
|
$type = new SearchResultType('groups');
|
|
|
|
$searchResult->addResultSet($type, $mockedGroupsResult);
|
|
|
|
return $expectedMoreResults;
|
|
|
|
});
|
|
|
|
|
|
|
|
$remotePlugin = $this->createMock(ISearchPlugin::class);
|
|
|
|
$remotePlugin->expects($this->any())
|
|
|
|
->method('search')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function () use ($searchResult, $mockedRemotesResult, $expectedMoreResults) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($mockedRemotesResult !== null) {
|
2017-09-13 15:29:57 +03:00
|
|
|
$type = new SearchResultType('remotes');
|
|
|
|
$searchResult->addResultSet($type, $mockedRemotesResult['results'], $mockedRemotesResult['exact']);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($mockedRemotesResult['exactIdMatch'] === true) {
|
2017-09-13 15:29:57 +03:00
|
|
|
$searchResult->markExactIdMatch($type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $expectedMoreResults;
|
|
|
|
});
|
|
|
|
|
2021-04-13 23:49:42 +03:00
|
|
|
$mailPlugin = $this->createMock(ISearchPlugin::class);
|
|
|
|
$mailPlugin->expects($this->any())
|
|
|
|
->method('search')
|
|
|
|
->willReturnCallback(function () use ($searchResult, $mockedMailResult, $expectedMoreResults) {
|
|
|
|
$type = new SearchResultType('emails');
|
|
|
|
$searchResult->addResultSet($type, $mockedMailResult);
|
|
|
|
return $expectedMoreResults;
|
|
|
|
});
|
|
|
|
|
2017-09-13 15:29:57 +03:00
|
|
|
$this->container->expects($this->any())
|
|
|
|
->method('resolve')
|
2021-04-13 23:49:42 +03:00
|
|
|
->willReturnCallback(function ($class) use ($searchResult, $userPlugin, $groupPlugin, $remotePlugin, $mailPlugin) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($class === SearchResult::class) {
|
2017-09-13 15:29:57 +03:00
|
|
|
return $searchResult;
|
|
|
|
} elseif ($class === $userPlugin) {
|
|
|
|
return $userPlugin;
|
|
|
|
} elseif ($class === $groupPlugin) {
|
|
|
|
return $groupPlugin;
|
|
|
|
} elseif ($class === $remotePlugin) {
|
|
|
|
return $remotePlugin;
|
2021-04-13 23:49:42 +03:00
|
|
|
} elseif ($class === $mailPlugin) {
|
|
|
|
return $mailPlugin;
|
2017-09-13 15:29:57 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_USER', 'class' => $userPlugin]);
|
|
|
|
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_GROUP', 'class' => $groupPlugin]);
|
|
|
|
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_REMOTE', 'class' => $remotePlugin]);
|
2021-04-13 23:49:42 +03:00
|
|
|
$this->search->registerPlugin(['shareType' => 'SHARE_TYPE_EMAIL', 'class' => $mailPlugin]);
|
2017-09-13 15:29:57 +03:00
|
|
|
|
2021-01-12 12:15:48 +03:00
|
|
|
[$results, $moreResults] = $this->search->search($searchTerm, $shareTypes, false, $perPage, $perPage * ($page - 1));
|
2017-09-13 15:29:57 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $results);
|
|
|
|
$this->assertSame($expectedMoreResults, $moreResults);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataSearchSharees() {
|
|
|
|
return [
|
2021-04-13 23:49:42 +03:00
|
|
|
// #0
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
2020-06-24 17:49:16 +03:00
|
|
|
'test', [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE], 1, 2, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
|
2021-04-13 23:49:42 +03:00
|
|
|
[],
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
|
|
|
'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
|
|
|
|
'users' => [],
|
|
|
|
'groups' => [],
|
|
|
|
'remotes' => [],
|
2021-04-13 23:49:42 +03:00
|
|
|
],
|
|
|
|
false
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
2021-04-13 23:49:42 +03:00
|
|
|
// #1
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
2020-06-24 17:49:16 +03:00
|
|
|
'test', [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE], 1, 2, [], [], ['results' => [], 'exact' => [], 'exactIdMatch' => false],
|
2021-04-13 23:49:42 +03:00
|
|
|
[],
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
|
|
|
'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
|
|
|
|
'users' => [],
|
|
|
|
'groups' => [],
|
|
|
|
'remotes' => [],
|
2021-04-13 23:49:42 +03:00
|
|
|
],
|
|
|
|
false
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
2021-04-13 23:49:42 +03:00
|
|
|
// #2
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
2020-06-24 17:49:16 +03:00
|
|
|
'test', [IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE], 1, 2, [
|
|
|
|
['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
2020-04-09 10:22:29 +03:00
|
|
|
], [
|
2020-06-24 17:49:16 +03:00
|
|
|
['label' => 'testgroup1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'testgroup1']],
|
2020-04-09 10:22:29 +03:00
|
|
|
], [
|
2020-06-24 17:49:16 +03:00
|
|
|
'results' => [['label' => 'testz@remote', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false,
|
2020-04-09 10:22:29 +03:00
|
|
|
],
|
2021-04-13 23:49:42 +03:00
|
|
|
[],
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
|
|
|
'exact' => ['users' => [], 'groups' => [], 'remotes' => []],
|
|
|
|
'users' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
|
|
|
'groups' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
['label' => 'testgroup1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'testgroup1']],
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
|
|
|
'remotes' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
['label' => 'testz@remote', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote']],
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
|
|
|
], true,
|
|
|
|
],
|
2021-04-13 23:49:42 +03:00
|
|
|
// #3 No groups requested
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
2020-06-24 17:49:16 +03:00
|
|
|
'test', [IShare::TYPE_USER, IShare::TYPE_REMOTE], 1, 2, [
|
|
|
|
['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
2020-04-09 10:22:29 +03:00
|
|
|
], [], [
|
2020-06-24 17:49:16 +03:00
|
|
|
'results' => [['label' => 'testz@remote', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote']]], 'exact' => [], 'exactIdMatch' => false
|
2020-04-09 10:22:29 +03:00
|
|
|
],
|
2021-04-13 23:49:42 +03:00
|
|
|
[],
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
|
|
|
'exact' => ['users' => [], 'remotes' => []],
|
|
|
|
'users' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
|
|
|
'remotes' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
['label' => 'testz@remote', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote']],
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
|
|
|
], false,
|
|
|
|
],
|
2021-04-13 23:49:42 +03:00
|
|
|
// #4 Share type restricted to user - Only one user
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
2020-06-24 17:49:16 +03:00
|
|
|
'test', [IShare::TYPE_USER], 1, 2, [
|
|
|
|
['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
2021-04-13 23:49:42 +03:00
|
|
|
], [], [], [],
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
|
|
|
'exact' => ['users' => []],
|
|
|
|
'users' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
|
|
|
], false,
|
|
|
|
],
|
2021-04-13 23:49:42 +03:00
|
|
|
// #5 Share type restricted to user - Multipage result
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
2020-06-24 17:49:16 +03:00
|
|
|
'test', [IShare::TYPE_USER], 1, 2, [
|
|
|
|
['label' => 'test 1', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
|
|
|
['label' => 'test 2', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2']],
|
2021-04-13 23:49:42 +03:00
|
|
|
], [], [], [],
|
2017-09-13 15:29:57 +03:00
|
|
|
[
|
|
|
|
'exact' => ['users' => []],
|
|
|
|
'users' => [
|
2020-06-24 17:49:16 +03:00
|
|
|
['label' => 'test 1', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
|
|
|
['label' => 'test 2', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test2']],
|
2017-09-13 15:29:57 +03:00
|
|
|
],
|
|
|
|
], true,
|
|
|
|
],
|
2021-04-13 23:49:42 +03:00
|
|
|
// #6 Mail shares filtered out in favor of remote shares
|
|
|
|
[
|
|
|
|
'test', // search term
|
|
|
|
[IShare::TYPE_USER, IShare::TYPE_GROUP, IShare::TYPE_REMOTE, IShare::TYPE_EMAIL], // plugins
|
|
|
|
1, // page
|
|
|
|
10, // per page
|
|
|
|
[ // user result
|
|
|
|
['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
|
|
|
],
|
|
|
|
[ // group result
|
|
|
|
['label' => 'testgroup1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'testgroup1']],
|
|
|
|
],
|
|
|
|
[ // remote result
|
|
|
|
'results' => [
|
|
|
|
['label' => 'testz@remote.tld', 'uuid' => 'f3d78140-abcc-46df-b58d-c7cc1176aadf','value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote.tld']]
|
|
|
|
],
|
|
|
|
'exact' => [],
|
|
|
|
'exactIdMatch' => false,
|
|
|
|
],
|
|
|
|
[ // mail result
|
|
|
|
['label' => 'test Two', 'uuid' => 'b2321e9e-31af-43ac-a406-583fb26d1964', 'value' => ['shareType' => IShare::TYPE_EMAIL, 'shareWith' => 'test2@remote.tld']],
|
|
|
|
['label' => 'test One', 'uuid' => 'f3d78140-abcc-46df-b58d-c7cc1176aadf', 'value' => ['shareType' => IShare::TYPE_EMAIL, 'shareWith' => 'testz@remote.tld']],
|
|
|
|
],
|
|
|
|
[ // expected result
|
|
|
|
'exact' => ['users' => [], 'groups' => [], 'remotes' => [], 'emails' => []],
|
|
|
|
'users' => [
|
|
|
|
['label' => 'test One', 'value' => ['shareType' => IShare::TYPE_USER, 'shareWith' => 'test1']],
|
|
|
|
],
|
|
|
|
'groups' => [
|
|
|
|
['label' => 'testgroup1', 'value' => ['shareType' => IShare::TYPE_GROUP, 'shareWith' => 'testgroup1']],
|
|
|
|
],
|
|
|
|
'remotes' => [
|
|
|
|
['label' => 'testz@remote.tld', 'uuid' => 'f3d78140-abcc-46df-b58d-c7cc1176aadf', 'value' => ['shareType' => IShare::TYPE_REMOTE, 'shareWith' => 'testz@remote.tld']],
|
|
|
|
],
|
|
|
|
'emails' => [
|
|
|
|
// one passes, another is filtered out
|
|
|
|
['label' => 'test Two', 'uuid' => 'b2321e9e-31af-43ac-a406-583fb26d1964', 'value' => ['shareType' => IShare::TYPE_EMAIL, 'shareWith' => 'test2@remote.tld']]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
false, // expected more results indicator
|
|
|
|
],
|
2017-09-13 15:29:57 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|