/** * Tests merging shares. * * Happens when sharing the same entry to a user through multiple ways, * like several groups and also direct shares at the same time. * * @dataProvider mergeSharesDataProvider * * @param array $userShares array of user share specs * @param array $groupShares array of group share specs * @param array $expectedShares array of expected supershare specs */ public function testMergeShares($userShares, $groupShares, $expectedShares) { $rootFolder = $this->getMock('\\OCP\\Files\\IRootFolder'); $userManager = $this->getMock('\\OCP\\IUserManager'); $userShares = array_map(function ($shareSpec) { return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4]); }, $userShares); $groupShares = array_map(function ($shareSpec) { return $this->makeMockShare($shareSpec[0], $shareSpec[1], $shareSpec[2], $shareSpec[3], $shareSpec[4]); }, $groupShares); $this->user->expects($this->any())->method('getUID')->will($this->returnValue('user1')); $this->shareManager->expects($this->at(0))->method('getSharedWith')->with('user1', \OCP\Share::SHARE_TYPE_USER)->will($this->returnValue($userShares)); $this->shareManager->expects($this->at(1))->method('getSharedWith')->with('user1', \OCP\Share::SHARE_TYPE_GROUP, null, -1)->will($this->returnValue($groupShares)); $this->shareManager->expects($this->any())->method('newShare')->will($this->returnCallback(function () use($rootFolder, $userManager) { return new \OC\Share20\Share($rootFolder, $userManager); })); $mounts = $this->provider->getMountsForUser($this->user, $this->loader); $this->assertCount(count($expectedShares), $mounts); foreach ($mounts as $index => $mount) { $expectedShare = $expectedShares[$index]; $this->assertInstanceOf('OCA\\Files_Sharing\\SharedMount', $mount); // supershare $share = $mount->getShare(); $this->assertEquals($expectedShare[0], $share->getId()); $this->assertEquals($expectedShare[1], $share->getNodeId()); $this->assertEquals($expectedShare[2], $share->getShareOwner()); $this->assertEquals($expectedShare[3], $share->getTarget()); $this->assertEquals($expectedShare[4], $share->getPermissions()); } }
public function testSharePasswordLinkInvalidSession() { $share = $this->getMock('OCP\\Share\\IShare'); $share->method('getPassword')->willReturn('password'); $share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK); $share->method('getId')->willReturn('42'); $this->shareManager->expects($this->once())->method('getShareByToken')->willReturn($share); $this->shareManager->method('checkPassword')->with($this->equalTo($share), $this->equalTo('password'))->willReturn(false); $this->session->method('exists')->with('public_link_authenticated')->willReturn(true); $this->session->method('get')->with('public_link_authenticated')->willReturn('43'); $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']); $this->assertFalse($result); }
public function testGetFilesByTagSingle() { $tagName = 'MyTagName'; $fileInfo = new FileInfo('/root.txt', $this->getMockBuilder('\\OC\\Files\\Storage\\Storage')->disableOriginalConstructor()->getMock(), '/var/www/root.txt', ['mtime' => 55, 'mimetype' => 'application/pdf', 'permissions' => 31, 'size' => 1234, 'etag' => 'MyEtag'], $this->getMockBuilder('\\OCP\\Files\\Mount\\IMountPoint')->disableOriginalConstructor()->getMock()); $node = $this->getMockBuilder('\\OC\\Files\\Node\\File')->disableOriginalConstructor()->getMock(); $node->expects($this->once())->method('getFileInfo')->will($this->returnValue($fileInfo)); $this->tagService->expects($this->once())->method('getFilesByTag')->with($this->equalTo([$tagName]))->will($this->returnValue([$node])); $this->shareManager->expects($this->any())->method('getSharesBy')->with($this->equalTo('user1'), $this->anything(), $node, $this->equalTo(false), $this->equalTo(1))->will($this->returnCallback(function ($userId, $shareType) { if ($shareType === \OCP\Share::SHARE_TYPE_USER || $shareType === \OCP\Share::SHARE_TYPE_LINK) { return ['dummy_share']; } return []; })); $expected = new DataResponse(['files' => [['id' => null, 'parentId' => null, 'mtime' => 55000, 'name' => 'root.txt', 'permissions' => 31, 'mimetype' => 'application/pdf', 'size' => 1234, 'type' => 'file', 'etag' => 'MyEtag', 'path' => '/', 'tags' => [['MyTagName']], 'shareTypes' => [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_LINK]]]]); $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName])); }
/** * @dataProvider sharesGetPropertiesDataProvider */ public function testPreloadThenGetProperties($shareTypes) { $sabreNode1 = $this->getMockBuilder('\\OCA\\DAV\\Connector\\Sabre\\File')->disableOriginalConstructor()->getMock(); $sabreNode1->expects($this->any())->method('getId')->will($this->returnValue(111)); $sabreNode1->expects($this->never())->method('getPath'); $sabreNode2 = $this->getMockBuilder('\\OCA\\DAV\\Connector\\Sabre\\File')->disableOriginalConstructor()->getMock(); $sabreNode2->expects($this->any())->method('getId')->will($this->returnValue(222)); $sabreNode2->expects($this->never())->method('getPath'); $sabreNode = $this->getMockBuilder('\\OCA\\DAV\\Connector\\Sabre\\Directory')->disableOriginalConstructor()->getMock(); $sabreNode->expects($this->any())->method('getId')->will($this->returnValue(123)); // never, because we use getDirectoryListing from the Node API instead $sabreNode->expects($this->never())->method('getChildren'); $sabreNode->expects($this->any())->method('getPath')->will($this->returnValue('/subdir')); // node API nodes $node = $this->getMock('\\OCP\\Files\\Folder'); $node->expects($this->any())->method('getId')->will($this->returnValue(123)); $node1 = $this->getMock('\\OCP\\Files\\File'); $node1->expects($this->any())->method('getId')->will($this->returnValue(111)); $node2 = $this->getMock('\\OCP\\Files\\File'); $node2->expects($this->any())->method('getId')->will($this->returnValue(222)); $node->expects($this->once())->method('getDirectoryListing')->will($this->returnValue([$node1, $node2])); $this->userFolder->expects($this->once())->method('get')->with('/subdir')->will($this->returnValue($node)); $this->shareManager->expects($this->any())->method('getSharesBy')->with($this->equalTo('user1'), $this->anything(), $this->anything(), $this->equalTo(false), $this->equalTo(1))->will($this->returnCallback(function ($userId, $requestedShareType, $node, $flag, $limit) use($shareTypes) { if ($node->getId() === 111 && in_array($requestedShareType, $shareTypes)) { return ['dummyshare']; } return []; })); // simulate sabre recursive PROPFIND traversal $propFindRoot = new \Sabre\DAV\PropFind('/subdir', [self::SHARETYPES_PROPERTYNAME], 1); $propFind1 = new \Sabre\DAV\PropFind('/subdir/test.txt', [self::SHARETYPES_PROPERTYNAME], 0); $propFind2 = new \Sabre\DAV\PropFind('/subdir/test2.txt', [self::SHARETYPES_PROPERTYNAME], 0); $this->plugin->handleGetProperties($propFindRoot, $sabreNode); $this->plugin->handleGetProperties($propFind1, $sabreNode1); $this->plugin->handleGetProperties($propFind2, $sabreNode2); $result = $propFind1->getResultForMultiStatus(); $this->assertEmpty($result[404]); unset($result[404]); $this->assertEquals($shareTypes, $result[200][self::SHARETYPES_PROPERTYNAME]->getShareTypes()); }
/** * @dataProvider dataSearch * * @param array $getData * @param string $apiSetting * @param string $enumSetting * @param bool $remoteSharingEnabled * @param string $search * @param string $itemType * @param array $shareTypes * @param int $page * @param int $perPage * @param bool $shareWithGroupOnly * @param bool $shareeEnumeration * @param bool $allowGroupSharing */ public function testSearch($getData, $apiSetting, $enumSetting, $remoteSharingEnabled, $search, $itemType, $shareTypes, $page, $perPage, $shareWithGroupOnly, $shareeEnumeration, $allowGroupSharing) { $oldGet = $_GET; $_GET = $getData; $config = $this->getMockBuilder('OCP\\IConfig')->disableOriginalConstructor()->getMock(); $config->expects($this->exactly(2))->method('getAppValue')->with('core', $this->anything(), $this->anything())->willReturnMap([['core', 'shareapi_only_share_with_group_members', 'no', $apiSetting], ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', $enumSetting]]); $this->shareManager->expects($this->once())->method('allowGroupSharing')->willReturn($allowGroupSharing); $sharees = $this->getMockBuilder('\\OCA\\Files_Sharing\\API\\Sharees')->setConstructorArgs([$this->groupManager, $this->userManager, $this->contactsManager, $config, $this->session, $this->getMockBuilder('OCP\\IURLGenerator')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OCP\\IRequest')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OCP\\ILogger')->disableOriginalConstructor()->getMock(), $this->shareManager])->setMethods(array('searchSharees', 'isRemoteSharingAllowed'))->getMock(); $sharees->expects($this->once())->method('searchSharees')->with($search, $itemType, $shareTypes, $page, $perPage)->willReturnCallback(function ($isearch, $iitemType, $ishareTypes, $ipage, $iperPage) use($search, $itemType, $shareTypes, $page, $perPage) { // We are doing strict comparisons here, so we can differ 0/'' and null on shareType/itemType $this->assertSame($search, $isearch); $this->assertSame($itemType, $iitemType); $this->assertSame($shareTypes, $ishareTypes); $this->assertSame($page, $ipage); $this->assertSame($perPage, $iperPage); return new \OC_OCS_Result([]); }); $sharees->expects($this->any())->method('isRemoteSharingAllowed')->with($itemType)->willReturn($remoteSharingEnabled); /** @var \PHPUnit_Framework_MockObject_MockObject|\OCA\Files_Sharing\API\Sharees $sharees */ $this->assertInstanceOf('\\OC_OCS_Result', $sharees->search()); $this->assertSame($shareWithGroupOnly, $this->invokePrivate($sharees, 'shareWithGroupOnly')); $this->assertSame($shareeEnumeration, $this->invokePrivate($sharees, 'shareeEnumeration')); $_GET = $oldGet; }