From 20e7a0c07b4259a22231b70a1b66514be2adeed7 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 27 Oct 2017 16:03:37 +0200 Subject: [PATCH] fix sharerecipientssorter tests Signed-off-by: Arthur Schiwon --- .../Collaboration/ShareRecipientSorter.php | 2 +- .../ShareRecipientSorterTest.php | 60 +++++++++++++++---- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php b/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php index 5d19ed5d42..db21339811 100644 --- a/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php +++ b/apps/files_sharing/lib/Collaboration/ShareRecipientSorter.php @@ -55,12 +55,12 @@ class ShareRecipientSorter implements ISorter { if($context['itemType'] !== 'files' && $context['itemType'] !== 'file') { return; } - /** @var Node[] $nodes */ $user = $this->userSession->getUser(); if($user === null) { return; } $userFolder = $this->rootFolder->getUserFolder($user->getUID()); + /** @var Node[] $nodes */ $nodes = $userFolder->getById((int)$context['itemId']); if(count($nodes) === 0) { return; diff --git a/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php b/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php index 42487e6162..8f51678876 100644 --- a/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php +++ b/apps/files_sharing/tests/Collaboration/ShareRecipientSorterTest.php @@ -26,15 +26,20 @@ namespace OCA\Files_Sharing\Tests\Collaboration; use OCA\Files_Sharing\Collaboration\ShareRecipientSorter; use OCP\Files\Folder; +use OCP\Files\IRootFolder; use OCP\Files\Node; +use OCP\IUser; +use OCP\IUserSession; use OCP\Share\IManager; use Test\TestCase; class ShareRecipientSorterTest extends TestCase { /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ protected $shareManager; - /** @var Folder|\PHPUnit_Framework_MockObject_MockObject */ - protected $userFolder; + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */ + protected $rootFolder; + /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */ + protected $userSession; /** @var ShareRecipientSorter */ protected $sorter; @@ -42,9 +47,10 @@ class ShareRecipientSorterTest extends TestCase { parent::setUp(); $this->shareManager = $this->createMock(IManager::class); - $this->userFolder = $this->createMock(Folder::class); + $this->rootFolder = $this->createMock(IRootFolder::class); + $this->userSession = $this->createMock(IUserSession::class); - $this->sorter = new ShareRecipientSorter($this->shareManager, $this->userFolder); + $this->sorter = new ShareRecipientSorter($this->shareManager, $this->rootFolder, $this->userSession); } /** @@ -54,8 +60,23 @@ class ShareRecipientSorterTest extends TestCase { public function testSort($data) { $node = $this->createMock(Node::class); + /** @var Folder|\PHPUnit_Framework_MockObject_MockObject $folder */ + $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); + if ($data['context']['itemType'] === 'files') { - $this->userFolder->expects($this->once()) + $folder->expects($this->once()) ->method('getById') ->with($data['context']['itemId']) ->willReturn([$node]); @@ -65,7 +86,7 @@ class ShareRecipientSorterTest extends TestCase { ->with($node) ->willReturn($data['accessList']); } else { - $this->userFolder->expects($this->never()) + $folder->expects($this->never()) ->method('getById'); $this->shareManager->expects($this->never()) ->method('getAccessList'); @@ -78,17 +99,34 @@ class ShareRecipientSorterTest extends TestCase { } public function testSortNoNodes() { - $this->userFolder->expects($this->once()) + /** @var Folder|\PHPUnit_Framework_MockObject_MockObject $folder */ + $folder = $this->createMock(Folder::class); + $this->rootFolder->expects($this->any()) + ->method('getUserFolder') + ->willReturn($folder); + + $folder->expects($this->once()) ->method('getById') ->willReturn([]); + $user = $this->createMock(IUser::class); + $user->expects($this->any()) + ->method('getUID') + ->willReturn('yvonne'); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->willReturn($user); + $this->shareManager->expects($this->never()) ->method('getAccessList'); - $originalArray = ['users' => [ - ['value' => ['shareWith' => 'alice']], - ['value' => ['shareWith' => 'bob']], - ]]; + $originalArray = [ + 'users' => [ + ['value' => ['shareWith' => 'alice']], + ['value' => ['shareWith' => 'bob']], + ] + ]; $workArray = $originalArray; $this->sorter->sort($workArray, ['itemType' => 'files', 'itemId' => 404]);