Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/comments/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function () {

$eventDispatcher->addListener(\OCP\Comments\CommentsEntityEvent::EVENT_ENTITY, function (\OCP\Comments\CommentsEntityEvent $event) {
$event->addEntityCollection('files', function ($name) {
$nodes = \OC::$server->getUserFolder()->getById(\intval($name));
$nodes = \OC::$server->getUserFolder()->getById((int)$name, true);
return !empty($nodes);
});
});
7 changes: 3 additions & 4 deletions apps/comments/lib/Activity/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ public function commentEvent(CommentsEvent $event) {
foreach ($mounts as $mount) {
$owner = $mount->getUser()->getUID();
$ownerFolder = $this->rootFolder->getUserFolder($owner);
$nodes = $ownerFolder->getById($event->getComment()->getObjectId());
if (!empty($nodes)) {
/** @var Node $node */
$node = \array_shift($nodes);
$nodes = $ownerFolder->getById($event->getComment()->getObjectId(), true);
$node = $nodes[0] ?? null;
if ($node) {
$path = $node->getPath();
if (\strpos($path, '/' . $owner . '/files/') === 0) {
$path = \substr($path, \strlen('/' . $owner . '/files'));
Expand Down
151 changes: 151 additions & 0 deletions apps/comments/tests/unit/ActivityListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* @author Vincent Petry <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Comments\Tests\unit;

use OCA\Comments\Activity\Listener;
use OCP\Comments\CommentsEntityEvent;
use Test\Traits\UserTrait;
use OCP\IUserSession;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\Files\Config\IMountProviderCollection;
use OCP\Files\IRootFolder;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Config\ICachedMountInfo;
use OCP\IUser;
use OCP\Files\Folder;
use OCP\Activity\IEvent;
use OCA\Comments\Activity\Extension;
use OCP\Comments\IComment;
use OCP\Comments\CommentsEvent;

/**
* Tests for the activity listener
*
* @group DB
*/
class ActivityListenerTest extends \Test\TestCase {
use UserTrait;

/**
* @var Listener
*/
private $listener;

/**
* @var IUserMountCache
*/
private $userMountCache;

/**
* @var IRootFolder
*/
private $rootFolder;

/**
* @var IUserSession
*/
private $userSession;

/**
* @var IManager
*/
private $activityManager;

protected function setUp() {
parent::setUp();

$this->activityManager = $this->createMock(IManager::class);
$this->userSession = $this->createMock(IUserSession::class);
$appManager = $this->createMock(IAppManager::class);
$appManager->method('isInstalled')->with('activity')->willReturn(true);

$this->userMountCache = $this->createMock(IUserMountCache::class);

$mountProviderCollection = $this->createMock(IMountProviderCollection::class);
$mountProviderCollection->method('getMountCache')->willReturn($this->userMountCache);

$this->rootFolder = $this->createMock(IRootFolder::class);

// needed for the one unmockable static method "Share::getUsersSharingFile"...
$this->createUser('user1');
$this->createUser('actor1');

$this->listener = new Listener(
$this->activityManager,
$this->userSession,
$appManager,
$mountProviderCollection,
$this->rootFolder
);
}

public function testActivityOnFilesComment() {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('user1');

$actor = $this->createMock(IUser::class);
$actor->method('getUID')->willReturn('actor1');

$this->userSession->method('getUser')->willReturn($actor);

$cachedMountInfo = $this->createMock(ICachedMountInfo::class);
$cachedMountInfo->method('getUser')->willReturn($user);

$node = $this->createMock(Folder::class);
$node->method('getPath')->willReturn('/user1/files/folder');

$ownerFolder = $this->createMock(Folder::class);
$ownerFolder->method('getById')
->with(123, true)
->willReturn([$node]);

$this->rootFolder->method('getUserFolder')
->with('user1')
->willReturn($ownerFolder);

$this->userMountCache->method('getMountsForFileId')
->with(123)
->willReturn([$cachedMountInfo]);

$activityEvent = $this->createMock(IEvent::class);
$activityEvent->expects($this->once())->method('setApp')->with('comments')->willReturn($activityEvent);
$activityEvent->expects($this->once())->method('setType')->with('comments')->willReturn($activityEvent);
$activityEvent->expects($this->once())->method('setAuthor')->with('actor1')->willReturn($activityEvent);
$activityEvent->expects($this->once())->method('setObject')->with('files', 123)->willReturn($activityEvent);
$activityEvent->expects($this->once())->method('setMessage')->with(Extension::ADD_COMMENT_MESSAGE, [111])->willReturn($activityEvent);

$this->activityManager->method('generateEvent')
->willReturn($activityEvent);
$this->activityManager->expects($this->once())
->method('publish')
->with($activityEvent);

$comment = $this->createMock(IComment::class);
$comment->method('getObjectType')->willReturn('files');
$comment->method('getObjectId')->willReturn(123);
$comment->method('getId')->willReturn(111);

$commentsEvent = new CommentsEvent(CommentsEvent::EVENT_ADD, $comment);
$this->listener->commentEvent($commentsEvent);
}
}
4 changes: 2 additions & 2 deletions apps/dav/lib/Connector/Sabre/FilesReportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ public function findNodesByFileIds($rootNode, $fileIds) {

$results = [];
foreach ($fileIds as $fileId) {
$entry = $folder->getById($fileId);
$entries = $folder->getById($fileId, true);
$entry = $entries[0] ?? null;
if ($entry) {
$entry = \current($entry);
$node = $this->makeSabreNode($entry);
if ($node) {
$results[] = $node;
Expand Down
4 changes: 2 additions & 2 deletions apps/dav/lib/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function __construct() {
\OC::$server->getSystemTagObjectMapper(),
\OC::$server->getUserSession(),
\OC::$server->getGroupManager(),
\OC::$server->getRootFolder()
\OC::$server->getLazyRootFolder()
);

$usersCardDavBackend = new CardDavBackend($db, $userPrincipalBackend, $groupPrincipalBackend, $dispatcher);
Expand Down Expand Up @@ -109,7 +109,7 @@ public function __construct() {
$systemTagRelationsCollection,
$uploadCollection,
$avatarCollection,
new \OCA\DAV\Meta\RootCollection(\OC::$server->getRootFolder()),
new \OCA\DAV\Meta\RootCollection(\OC::$server->getLazyRootFolder()),
$queueCollection
];

Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/SystemTag/SystemTagsObjectTypeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function childExists($name) {
if ($this->objectType === 'files') {
// make sure the object is reachable for the current user
$userId = $this->userSession->getUser()->getUID();
$nodes = $this->fileRoot->getUserFolder($userId)->getById(\intval($name));
$nodes = $this->fileRoot->getUserFolder($userId)->getById((int)$name, true);
return !empty($nodes);
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion apps/federatedfilesharing/lib/FederatedShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ private function getNode($userId, $id) {
throw new InvalidShare();
}

$nodes = $userFolder->getById($id);
$nodes = $userFolder->getById($id, true);

if (empty($nodes)) {
throw new InvalidShare();
Expand Down
86 changes: 86 additions & 0 deletions apps/federatedfilesharing/tests/FederatedShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
use OCP\IUserManager;
use OCP\Share\IManager;
use OCP\Share\IShare;
use OCP\Files\Folder;
use OCP\IUser;

/**
* Class FederatedShareProviderTest
Expand Down Expand Up @@ -188,6 +190,90 @@ public function testCreate() {
$this->assertEquals('token', $share->getToken());
}

public function testCreateLegacy() {
$share = $this->shareManager->newShare();

$node = $this->createMock('\OCP\Files\File');
$node->method('getId')->willReturn(42);
$node->method('getName')->willReturn('myFile');

$share->setSharedWith('[email protected]')
->setShareOwner('shareOwner')
->setPermissions(19)
->setNode($node);

$this->tokenHandler->method('generateToken')->willReturn('token');

$shareWithAddress = new Address('[email protected]');
$ownerAddress = new Address('shareOwner@http://localhost/');
$sharedByAddress = new Address('sharedBy@http://localhost/');
$this->addressHandler->expects($this->any())->method('getLocalUserFederatedAddress')
->will($this->onConsecutiveCalls($ownerAddress, $sharedByAddress, $ownerAddress));

$this->addressHandler->expects($this->any())->method('splitUserRemote')
->willReturn(['user', 'server.com']);

$this->notifications->expects($this->once())
->method('sendRemoteShare')
->with(
$this->equalTo($shareWithAddress),
$this->equalTo($ownerAddress),
$this->equalTo($sharedByAddress),
$this->equalTo('token'),
$this->equalTo('myFile'),
$this->anything()
)->willReturn(true);

$folderOwner = $this->createMock(IUser::class);
$folderOwner->method('getUID')->willReturn('folderOwner');
$node = $this->createMock(Folder::class);
$node->method('getOwner')->willReturn($folderOwner);

$userFolder = $this->createMock(Folder::class);
$userFolder->method('getById')
->with(42, true)
->willReturn([$node]);
$this->rootFolder->expects($this->once())
->method('getUserFolder')
->with('shareOwner')
->willReturn($userFolder);

$share = $this->provider->create($share);

$qb = $this->connection->getQueryBuilder();
$stmt = $qb->select('*')
->from('share')
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
->execute();

$data = $stmt->fetch();
$stmt->closeCursor();

$expected = [
'share_type' => \OCP\Share::SHARE_TYPE_REMOTE,
'share_with' => '[email protected]',
'uid_owner' => 'shareOwner',
'uid_initiator' => null,
'item_type' => 'file',
'item_source' => 42,
'file_source' => 42,
'permissions' => 19,
'accepted' => 0,
'token' => 'token',
];
$this->assertArraySubset($expected, $data);

$this->assertEquals($data['id'], $share->getId());
$this->assertEquals(\OCP\Share::SHARE_TYPE_REMOTE, $share->getShareType());
$this->assertEquals('[email protected]', $share->getSharedWith());
$this->assertEquals('shareOwner', $share->getSharedBy());
$this->assertEquals('folderOwner', $share->getShareOwner());
$this->assertEquals('file', $share->getNodeType());
$this->assertEquals(42, $share->getNodeId());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals('token', $share->getToken());
}

public function testCreateCouldNotFindServer() {
$share = $this->shareManager->newShare();

Expand Down
7 changes: 3 additions & 4 deletions apps/files/lib/ActivityHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ public function getFavoriteFilePaths($user) {
$rootFolder = \OC::$server->getUserFolder($user);
$folders = $items = [];
foreach ($favorites as $favorite) {
$nodes = $rootFolder->getById($favorite);
if (!empty($nodes)) {
/** @var \OCP\Files\Node $node */
$node = \array_shift($nodes);
$nodes = $rootFolder->getById($favorite, true);
$node = $nodes[0] ?? null;
if ($node) {
$path = \substr($node->getPath(), \strlen($user . '/files/'));

$items[] = $path;
Expand Down
Loading