2017-08-30 11:56:02 +03:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
|
*
|
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-08-30 11:56:02 +03:00
|
|
|
|
*
|
|
|
|
|
* @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
|
2019-12-03 21:57:53 +03:00
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-08-30 11:56:02 +03:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace OCA\Files_Sharing\Tests\Collaboration;
|
|
|
|
|
|
|
|
|
|
use OCA\Files_Sharing\Collaboration\ShareRecipientSorter;
|
|
|
|
|
use OCP\Files\Folder;
|
2017-10-27 17:03:37 +03:00
|
|
|
|
use OCP\Files\IRootFolder;
|
2017-08-30 11:56:02 +03:00
|
|
|
|
use OCP\Files\Node;
|
2017-10-27 17:03:37 +03:00
|
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\IUserSession;
|
2017-08-30 11:56:02 +03:00
|
|
|
|
use OCP\Share\IManager;
|
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class ShareRecipientSorterTest extends TestCase {
|
2020-08-11 22:32:18 +03:00
|
|
|
|
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
|
2017-08-30 11:56:02 +03:00
|
|
|
|
protected $shareManager;
|
2020-08-11 22:32:18 +03:00
|
|
|
|
/** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
|
2017-10-27 17:03:37 +03:00
|
|
|
|
protected $rootFolder;
|
2020-08-11 22:32:18 +03:00
|
|
|
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2017-10-27 17:03:37 +03:00
|
|
|
|
protected $userSession;
|
2017-08-30 11:56:02 +03:00
|
|
|
|
/** @var ShareRecipientSorter */
|
|
|
|
|
protected $sorter;
|
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
protected function setUp(): void {
|
2017-08-30 11:56:02 +03:00
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->shareManager = $this->createMock(IManager::class);
|
2017-10-27 17:03:37 +03:00
|
|
|
|
$this->rootFolder = $this->createMock(IRootFolder::class);
|
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
2017-08-30 11:56:02 +03:00
|
|
|
|
|
2017-10-27 17:03:37 +03:00
|
|
|
|
$this->sorter = new ShareRecipientSorter($this->shareManager, $this->rootFolder, $this->userSession);
|
2017-08-30 11:56:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider sortDataProvider
|
|
|
|
|
* @param $data
|
|
|
|
|
*/
|
|
|
|
|
public function testSort($data) {
|
|
|
|
|
$node = $this->createMock(Node::class);
|
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
/** @var Folder|\PHPUnit\Framework\MockObject\MockObject $folder */
|
2017-10-27 17:03:37 +03:00
|
|
|
|
$folder = $this->createMock(Folder::class);
|
|
|
|
|
$this->rootFolder->expects($this->any())
|
|
|
|
|
->method('getUserFolder')
|
|
|
|
|
->willReturn($folder);
|
|
|
|
|
|
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
$user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
|
|
|
|
->willReturn('yvonne');
|
|
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('getUser')
|
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
2017-08-30 11:56:02 +03:00
|
|
|
|
if ($data['context']['itemType'] === 'files') {
|
2017-10-27 17:03:37 +03:00
|
|
|
|
$folder->expects($this->once())
|
2017-08-30 11:56:02 +03:00
|
|
|
|
->method('getById')
|
|
|
|
|
->with($data['context']['itemId'])
|
|
|
|
|
->willReturn([$node]);
|
|
|
|
|
|
|
|
|
|
$this->shareManager->expects($this->once())
|
|
|
|
|
->method('getAccessList')
|
|
|
|
|
->with($node)
|
|
|
|
|
->willReturn($data['accessList']);
|
|
|
|
|
} else {
|
2017-10-27 17:03:37 +03:00
|
|
|
|
$folder->expects($this->never())
|
2017-08-30 11:56:02 +03:00
|
|
|
|
->method('getById');
|
|
|
|
|
$this->shareManager->expects($this->never())
|
|
|
|
|
->method('getAccessList');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$workArray = $data['input'];
|
|
|
|
|
$this->sorter->sort($workArray, $data['context']);
|
|
|
|
|
|
2017-10-11 18:25:21 +03:00
|
|
|
|
$this->assertEquals($data['expected'], $workArray);
|
2017-08-30 11:56:02 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSortNoNodes() {
|
2020-08-11 22:32:18 +03:00
|
|
|
|
/** @var Folder|\PHPUnit\Framework\MockObject\MockObject $folder */
|
2017-10-27 17:03:37 +03:00
|
|
|
|
$folder = $this->createMock(Folder::class);
|
|
|
|
|
$this->rootFolder->expects($this->any())
|
|
|
|
|
->method('getUserFolder')
|
|
|
|
|
->willReturn($folder);
|
|
|
|
|
|
|
|
|
|
$folder->expects($this->once())
|
2017-08-30 11:56:02 +03:00
|
|
|
|
->method('getById')
|
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
2017-10-27 17:03:37 +03:00
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
$user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
|
|
|
|
->willReturn('yvonne');
|
|
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
|
->method('getUser')
|
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
2017-08-30 11:56:02 +03:00
|
|
|
|
$this->shareManager->expects($this->never())
|
|
|
|
|
->method('getAccessList');
|
|
|
|
|
|
2017-10-27 17:03:37 +03:00
|
|
|
|
$originalArray = [
|
|
|
|
|
'users' => [
|
|
|
|
|
['value' => ['shareWith' => 'alice']],
|
|
|
|
|
['value' => ['shareWith' => 'bob']],
|
|
|
|
|
]
|
|
|
|
|
];
|
2017-08-30 11:56:02 +03:00
|
|
|
|
$workArray = $originalArray;
|
|
|
|
|
$this->sorter->sort($workArray, ['itemType' => 'files', 'itemId' => 404]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($originalArray, $workArray);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sortDataProvider() {
|
|
|
|
|
return [[
|
|
|
|
|
[
|
2017-10-11 18:25:21 +03:00
|
|
|
|
#0 – sort properly and otherwise keep existing order
|
2017-08-30 11:56:02 +03:00
|
|
|
|
'context' => ['itemType' => 'files', 'itemId' => 42],
|
|
|
|
|
'accessList' => ['users' => ['celia', 'darius', 'faruk', 'gail'], 'bots' => ['r2-d2']],
|
|
|
|
|
'input' => [
|
|
|
|
|
'users' =>
|
|
|
|
|
[
|
|
|
|
|
['value' => ['shareWith' => 'alice']],
|
|
|
|
|
['value' => ['shareWith' => 'bob']],
|
|
|
|
|
['value' => ['shareWith' => 'celia']],
|
|
|
|
|
['value' => ['shareWith' => 'darius']],
|
|
|
|
|
['value' => ['shareWith' => 'elena']],
|
|
|
|
|
['value' => ['shareWith' => 'faruk']],
|
|
|
|
|
['value' => ['shareWith' => 'gail']],
|
|
|
|
|
],
|
|
|
|
|
'bots' => [
|
|
|
|
|
['value' => ['shareWith' => 'c-3po']],
|
|
|
|
|
['value' => ['shareWith' => 'r2-d2']],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'expected' => [
|
|
|
|
|
'users' =>
|
|
|
|
|
[
|
|
|
|
|
['value' => ['shareWith' => 'celia']],
|
|
|
|
|
['value' => ['shareWith' => 'darius']],
|
|
|
|
|
['value' => ['shareWith' => 'faruk']],
|
|
|
|
|
['value' => ['shareWith' => 'gail']],
|
|
|
|
|
['value' => ['shareWith' => 'alice']],
|
|
|
|
|
['value' => ['shareWith' => 'bob']],
|
|
|
|
|
['value' => ['shareWith' => 'elena']],
|
|
|
|
|
],
|
|
|
|
|
'bots' => [
|
|
|
|
|
['value' => ['shareWith' => 'r2-d2']],
|
|
|
|
|
['value' => ['shareWith' => 'c-3po']],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
2017-10-11 18:25:21 +03:00
|
|
|
|
#1 – no recipients
|
2017-08-30 11:56:02 +03:00
|
|
|
|
'context' => ['itemType' => 'files', 'itemId' => 42],
|
|
|
|
|
'accessList' => ['users' => false],
|
|
|
|
|
'input' => [
|
|
|
|
|
'users' =>
|
|
|
|
|
[
|
|
|
|
|
['value' => ['shareWith' => 'alice']],
|
|
|
|
|
['value' => ['shareWith' => 'bob']],
|
|
|
|
|
['value' => ['shareWith' => 'celia']],
|
|
|
|
|
['value' => ['shareWith' => 'darius']],
|
|
|
|
|
['value' => ['shareWith' => 'elena']],
|
|
|
|
|
['value' => ['shareWith' => 'faruk']],
|
|
|
|
|
['value' => ['shareWith' => 'gail']],
|
|
|
|
|
],
|
|
|
|
|
'bots' => [
|
|
|
|
|
['value' => ['shareWith' => 'c-3po']],
|
|
|
|
|
['value' => ['shareWith' => 'r2-d2']],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'expected' => [
|
|
|
|
|
'users' =>
|
|
|
|
|
[
|
|
|
|
|
['value' => ['shareWith' => 'alice']],
|
|
|
|
|
['value' => ['shareWith' => 'bob']],
|
|
|
|
|
['value' => ['shareWith' => 'celia']],
|
|
|
|
|
['value' => ['shareWith' => 'darius']],
|
|
|
|
|
['value' => ['shareWith' => 'elena']],
|
|
|
|
|
['value' => ['shareWith' => 'faruk']],
|
|
|
|
|
['value' => ['shareWith' => 'gail']],
|
|
|
|
|
],
|
|
|
|
|
'bots' => [
|
|
|
|
|
['value' => ['shareWith' => 'c-3po']],
|
|
|
|
|
['value' => ['shareWith' => 'r2-d2']],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
2017-10-11 18:25:21 +03:00
|
|
|
|
#2 – unsupported item type
|
2017-08-30 11:56:02 +03:00
|
|
|
|
'context' => ['itemType' => 'announcements', 'itemId' => 42],
|
|
|
|
|
'accessList' => null, // not needed
|
|
|
|
|
'input' => [
|
|
|
|
|
'users' =>
|
|
|
|
|
[
|
|
|
|
|
['value' => ['shareWith' => 'alice']],
|
|
|
|
|
['value' => ['shareWith' => 'bob']],
|
|
|
|
|
['value' => ['shareWith' => 'celia']],
|
|
|
|
|
['value' => ['shareWith' => 'darius']],
|
|
|
|
|
['value' => ['shareWith' => 'elena']],
|
|
|
|
|
['value' => ['shareWith' => 'faruk']],
|
|
|
|
|
['value' => ['shareWith' => 'gail']],
|
|
|
|
|
],
|
|
|
|
|
'bots' => [
|
|
|
|
|
['value' => ['shareWith' => 'c-3po']],
|
|
|
|
|
['value' => ['shareWith' => 'r2-d2']],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'expected' => [
|
|
|
|
|
'users' =>
|
|
|
|
|
[
|
|
|
|
|
['value' => ['shareWith' => 'alice']],
|
|
|
|
|
['value' => ['shareWith' => 'bob']],
|
|
|
|
|
['value' => ['shareWith' => 'celia']],
|
|
|
|
|
['value' => ['shareWith' => 'darius']],
|
|
|
|
|
['value' => ['shareWith' => 'elena']],
|
|
|
|
|
['value' => ['shareWith' => 'faruk']],
|
|
|
|
|
['value' => ['shareWith' => 'gail']],
|
|
|
|
|
],
|
|
|
|
|
'bots' => [
|
|
|
|
|
['value' => ['shareWith' => 'c-3po']],
|
|
|
|
|
['value' => ['shareWith' => 'r2-d2']],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
2017-10-11 18:25:21 +03:00
|
|
|
|
#3 – no nothing
|
2017-08-30 11:56:02 +03:00
|
|
|
|
'context' => ['itemType' => 'files', 'itemId' => 42],
|
|
|
|
|
'accessList' => [],
|
|
|
|
|
'input' => [],
|
|
|
|
|
'expected' => [],
|
|
|
|
|
],
|
|
|
|
|
]];
|
|
|
|
|
}
|
|
|
|
|
}
|