Skip to content

Commit

Permalink
Increase test coverage, including meta version nodes
Browse files Browse the repository at this point in the history
Added more tests for meta node collection
Add tests for comments activity listener
Add activity listener test for system tags
Add test coverage for files' ActivityHelper
Test for legacy shares in fed share provider
  • Loading branch information
Vincent Petry committed Dec 11, 2018
1 parent 1be6207 commit bcb66aa
Show file tree
Hide file tree
Showing 7 changed files with 821 additions and 8 deletions.
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);
}
}
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
137 changes: 137 additions & 0 deletions apps/files/tests/ActivityHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?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/>
*
*/

use OCA\Files\ActivityHelper;
use OCP\ITagManager;
use OCP\ITags;
use Test\Traits\UserTrait;

/**
* Class ActivityHelperTest
*
* @group DB
*/
class ActivityHelperTest extends \Test\TestCase {
use UserTrait;

/**
* @var ITagManager
*/
private $tagManager;

/**
* @var ITags
*/
private $tags;

/**
* @var ActivityHelper
*/
private $helper;

/**
* @var string
*/
private $user;

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

$this->tags = $this->createMock(ITags::class);

$this->user = $this->getUniqueID('files_activityhelpertest_user_');

// because \OC::$server->getUserFolder()
$this->createUser($this->user);
$this->loginAsUser($this->user);

$this->tagManager = $this->createMock(ITagManager::class);
$this->tagManager->expects($this->once())
->method('load')
->with('files', [], false, $this->user)
->willReturn($this->tags);

$this->helper = new ActivityHelper($this->tagManager);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage No favorites
*/
public function testGetFavoriteFilePathsNoFavorites() {
$this->tags->method('getFavorites')->willReturn([]);
$this->helper->getFavoriteFilePaths($this->user);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Too many favorites
*/
public function testGetFavoriteFilePathsTooManyFavorites() {
$tooManyFavorites = [];
for ($i = 0; $i < ActivityHelper::FAVORITE_LIMIT + 1; $i++) {
$tooManyFavorites[] = [];
}

$this->tags->method('getFavorites')->willReturn($tooManyFavorites);

$this->helper->getFavoriteFilePaths($this->user);
}

public function testGetFavoriteFilePaths() {
$userFolder = \OC::$server->getUserFolder();
$fav1 = $userFolder->newFolder('fav1');
$fav2 = $userFolder->newFile('fav2.txt');
$userFolder->newFolder('nonfav1');
$userFolder->newFolder('nonfav2');

$favorites = [
$fav1->getId(),
$fav2->getId(),
$fav2->getId() + 999, // non-existing
];

$this->tags->method('getFavorites')->willReturn($favorites);

$result = $this->helper->getFavoriteFilePaths($this->user);

$this->assertEquals(['/fav1', '/fav2.txt'], $result['items']);
$this->assertEquals(['/fav1'], $result['folders']);
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage No favorites
*/
public function testGetFavoriteFilePathsMissingFolder() {
$userFolder = \OC::$server->getUserFolder();
$aFolder = $userFolder->newFolder('x');

$favorites = [
// non-existing
$aFolder->getId() + 999,
];

$this->tags->method('getFavorites')->willReturn($favorites);

$result = $this->helper->getFavoriteFilePaths($this->user);
}
}
Loading

0 comments on commit bcb66aa

Please sign in to comment.