X Tutup
Skip to content

Commit 59dacae

Browse files
author
Vincent Petry
committed
Increase test coverage, including meta version nodes
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
1 parent 0510f13 commit 59dacae

File tree

10 files changed

+847
-93
lines changed

10 files changed

+847
-93
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* @author Vincent Petry <pvince81@owncloud.com>
4+
*
5+
* @copyright Copyright (c) 2018, ownCloud GmbH
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
22+
namespace OCA\Comments\Tests\unit;
23+
24+
use OCA\Comments\Activity\Listener;
25+
use OCP\Comments\CommentsEntityEvent;
26+
use Test\Traits\UserTrait;
27+
use OCP\IUserSession;
28+
use OCP\Activity\IManager;
29+
use OCP\App\IAppManager;
30+
use OCP\Files\Config\IMountProviderCollection;
31+
use OCP\Files\IRootFolder;
32+
use OCP\Files\Config\IUserMountCache;
33+
use OCP\Files\Config\ICachedMountInfo;
34+
use OCP\IUser;
35+
use OCP\Files\Folder;
36+
use OCP\Activity\IEvent;
37+
use OCA\Comments\Activity\Extension;
38+
use OCP\Comments\IComment;
39+
use OCP\Comments\CommentsEvent;
40+
41+
/**
42+
* Tests for the activity listener
43+
*
44+
* @group DB
45+
*/
46+
class ActivityListenerTest extends \Test\TestCase {
47+
use UserTrait;
48+
49+
/**
50+
* @var Listener
51+
*/
52+
private $listener;
53+
54+
/**
55+
* @var IUserMountCache
56+
*/
57+
private $userMountCache;
58+
59+
/**
60+
* @var IRootFolder
61+
*/
62+
private $rootFolder;
63+
64+
/**
65+
* @var IUserSession
66+
*/
67+
private $userSession;
68+
69+
/**
70+
* @var IManager
71+
*/
72+
private $activityManager;
73+
74+
protected function setUp() {
75+
parent::setUp();
76+
77+
$this->activityManager = $this->createMock(IManager::class);
78+
$this->userSession = $this->createMock(IUserSession::class);
79+
$appManager = $this->createMock(IAppManager::class);
80+
$appManager->method('isInstalled')->with('activity')->willReturn(true);
81+
82+
$this->userMountCache = $this->createMock(IUserMountCache::class);
83+
84+
$mountProviderCollection = $this->createMock(IMountProviderCollection::class);
85+
$mountProviderCollection->method('getMountCache')->willReturn($this->userMountCache);
86+
87+
$this->rootFolder = $this->createMock(IRootFolder::class);
88+
89+
// needed for the one unmockable static method "Share::getUsersSharingFile"...
90+
$this->createUser('user1');
91+
$this->createUser('actor1');
92+
93+
$this->listener = new Listener(
94+
$this->activityManager,
95+
$this->userSession,
96+
$appManager,
97+
$mountProviderCollection,
98+
$this->rootFolder
99+
);
100+
}
101+
102+
public function testActivityOnFilesComment() {
103+
$user = $this->createMock(IUser::class);
104+
$user->method('getUID')->willReturn('user1');
105+
106+
$actor = $this->createMock(IUser::class);
107+
$actor->method('getUID')->willReturn('actor1');
108+
109+
$this->userSession->method('getUser')->willReturn($actor);
110+
111+
$cachedMountInfo = $this->createMock(ICachedMountInfo::class);
112+
$cachedMountInfo->method('getUser')->willReturn($user);
113+
114+
$node = $this->createMock(Folder::class);
115+
$node->method('getPath')->willReturn('/user1/files/folder');
116+
117+
$ownerFolder = $this->createMock(Folder::class);
118+
$ownerFolder->method('getById')
119+
->with(123, true)
120+
->willReturn([$node]);
121+
122+
$this->rootFolder->method('getUserFolder')
123+
->with('user1')
124+
->willReturn($ownerFolder);
125+
126+
$this->userMountCache->method('getMountsForFileId')
127+
->with(123)
128+
->willReturn([$cachedMountInfo]);
129+
130+
$activityEvent = $this->createMock(IEvent::class);
131+
$activityEvent->expects($this->once())->method('setApp')->with('comments')->willReturn($activityEvent);
132+
$activityEvent->expects($this->once())->method('setType')->with('comments')->willReturn($activityEvent);
133+
$activityEvent->expects($this->once())->method('setAuthor')->with('actor1')->willReturn($activityEvent);
134+
$activityEvent->expects($this->once())->method('setObject')->with('files', 123)->willReturn($activityEvent);
135+
$activityEvent->expects($this->once())->method('setMessage')->with(Extension::ADD_COMMENT_MESSAGE, [111])->willReturn($activityEvent);
136+
137+
$this->activityManager->method('generateEvent')
138+
->willReturn($activityEvent);
139+
$this->activityManager->expects($this->once())
140+
->method('publish')
141+
->with($activityEvent);
142+
143+
$comment = $this->createMock(IComment::class);
144+
$comment->method('getObjectType')->willReturn('files');
145+
$comment->method('getObjectId')->willReturn(123);
146+
$comment->method('getId')->willReturn(111);
147+
148+
$commentsEvent = new CommentsEvent(CommentsEvent::EVENT_ADD, $comment);
149+
$this->listener->commentEvent($commentsEvent);
150+
}
151+
}

apps/federatedfilesharing/tests/FederatedShareProviderTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
use OCP\IUserManager;
3838
use OCP\Share\IManager;
3939
use OCP\Share\IShare;
40+
use OCP\Files\Folder;
41+
use OCP\IUser;
4042

4143
/**
4244
* Class FederatedShareProviderTest
@@ -188,6 +190,90 @@ public function testCreate() {
188190
$this->assertEquals('token', $share->getToken());
189191
}
190192

193+
public function testCreateLegacy() {
194+
$share = $this->shareManager->newShare();
195+
196+
$node = $this->createMock('\OCP\Files\File');
197+
$node->method('getId')->willReturn(42);
198+
$node->method('getName')->willReturn('myFile');
199+
200+
$share->setSharedWith('user@server.com')
201+
->setShareOwner('shareOwner')
202+
->setPermissions(19)
203+
->setNode($node);
204+
205+
$this->tokenHandler->method('generateToken')->willReturn('token');
206+
207+
$shareWithAddress = new Address('user@server.com');
208+
$ownerAddress = new Address('shareOwner@http://localhost/');
209+
$sharedByAddress = new Address('sharedBy@http://localhost/');
210+
$this->addressHandler->expects($this->any())->method('getLocalUserFederatedAddress')
211+
->will($this->onConsecutiveCalls($ownerAddress, $sharedByAddress, $ownerAddress));
212+
213+
$this->addressHandler->expects($this->any())->method('splitUserRemote')
214+
->willReturn(['user', 'server.com']);
215+
216+
$this->notifications->expects($this->once())
217+
->method('sendRemoteShare')
218+
->with(
219+
$this->equalTo($shareWithAddress),
220+
$this->equalTo($ownerAddress),
221+
$this->equalTo($sharedByAddress),
222+
$this->equalTo('token'),
223+
$this->equalTo('myFile'),
224+
$this->anything()
225+
)->willReturn(true);
226+
227+
$folderOwner = $this->createMock(IUser::class);
228+
$folderOwner->method('getUID')->willReturn('folderOwner');
229+
$node = $this->createMock(Folder::class);
230+
$node->method('getOwner')->willReturn($folderOwner);
231+
232+
$userFolder = $this->createMock(Folder::class);
233+
$userFolder->method('getById')
234+
->with(42, true)
235+
->willReturn([$node]);
236+
$this->rootFolder->expects($this->once())
237+
->method('getUserFolder')
238+
->with('shareOwner')
239+
->willReturn($userFolder);
240+
241+
$share = $this->provider->create($share);
242+
243+
$qb = $this->connection->getQueryBuilder();
244+
$stmt = $qb->select('*')
245+
->from('share')
246+
->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
247+
->execute();
248+
249+
$data = $stmt->fetch();
250+
$stmt->closeCursor();
251+
252+
$expected = [
253+
'share_type' => \OCP\Share::SHARE_TYPE_REMOTE,
254+
'share_with' => 'user@server.com',
255+
'uid_owner' => 'shareOwner',
256+
'uid_initiator' => null,
257+
'item_type' => 'file',
258+
'item_source' => 42,
259+
'file_source' => 42,
260+
'permissions' => 19,
261+
'accepted' => 0,
262+
'token' => 'token',
263+
];
264+
$this->assertArraySubset($expected, $data);
265+
266+
$this->assertEquals($data['id'], $share->getId());
267+
$this->assertEquals(\OCP\Share::SHARE_TYPE_REMOTE, $share->getShareType());
268+
$this->assertEquals('user@server.com', $share->getSharedWith());
269+
$this->assertEquals('shareOwner', $share->getSharedBy());
270+
$this->assertEquals('folderOwner', $share->getShareOwner());
271+
$this->assertEquals('file', $share->getNodeType());
272+
$this->assertEquals(42, $share->getNodeId());
273+
$this->assertEquals(19, $share->getPermissions());
274+
$this->assertEquals('token', $share->getToken());
275+
}
276+
191277
public function testCreateCouldNotFindServer() {
192278
$share = $this->shareManager->newShare();
193279

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* @author Vincent Petry <pvince81@owncloud.com>
4+
*
5+
* @copyright Copyright (c) 2018, ownCloud GmbH
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
22+
use OCA\Files\ActivityHelper;
23+
use OCP\ITagManager;
24+
use OCP\ITags;
25+
use Test\Traits\UserTrait;
26+
27+
/**
28+
* Class ActivityHelperTest
29+
*
30+
* @group DB
31+
*/
32+
class ActivityHelperTest extends \Test\TestCase {
33+
use UserTrait;
34+
35+
/**
36+
* @var ITagManager
37+
*/
38+
private $tagManager;
39+
40+
/**
41+
* @var ITags
42+
*/
43+
private $tags;
44+
45+
/**
46+
* @var ActivityHelper
47+
*/
48+
private $helper;
49+
50+
/**
51+
* @var string
52+
*/
53+
private $user;
54+
55+
protected function setUp() {
56+
parent::setUp();
57+
58+
$this->tags = $this->createMock(ITags::class);
59+
60+
$this->user = $this->getUniqueID('files_activityhelpertest_user_');
61+
62+
// because \OC::$server->getUserFolder()
63+
$this->createUser($this->user);
64+
$this->loginAsUser($this->user);
65+
66+
$this->tagManager = $this->createMock(ITagManager::class);
67+
$this->tagManager->expects($this->once())
68+
->method('load')
69+
->with('files', [], false, $this->user)
70+
->willReturn($this->tags);
71+
72+
$this->helper = new ActivityHelper($this->tagManager);
73+
}
74+
75+
/**
76+
* @expectedException \RuntimeException
77+
* @expectedExceptionMessage No favorites
78+
*/
79+
public function testGetFavoriteFilePathsNoFavorites() {
80+
$this->tags->method('getFavorites')->willReturn([]);
81+
$this->helper->getFavoriteFilePaths($this->user);
82+
}
83+
84+
/**
85+
* @expectedException \RuntimeException
86+
* @expectedExceptionMessage Too many favorites
87+
*/
88+
public function testGetFavoriteFilePathsTooManyFavorites() {
89+
$tooManyFavorites = [];
90+
for ($i = 0; $i < ActivityHelper::FAVORITE_LIMIT + 1; $i++) {
91+
$tooManyFavorites[] = [];
92+
}
93+
94+
$this->tags->method('getFavorites')->willReturn($tooManyFavorites);
95+
96+
$this->helper->getFavoriteFilePaths($this->user);
97+
}
98+
99+
public function testGetFavoriteFilePaths() {
100+
$userFolder = \OC::$server->getUserFolder();
101+
$fav1 = $userFolder->newFolder('fav1');
102+
$fav2 = $userFolder->newFile('fav2.txt');
103+
$userFolder->newFolder('nonfav1');
104+
$userFolder->newFolder('nonfav2');
105+
106+
$favorites = [
107+
$fav1->getId(),
108+
$fav2->getId(),
109+
$fav2->getId() + 999, // non-existing
110+
];
111+
112+
$this->tags->method('getFavorites')->willReturn($favorites);
113+
114+
$result = $this->helper->getFavoriteFilePaths($this->user);
115+
116+
$this->assertEquals(['/fav1', '/fav2.txt'], $result['items']);
117+
$this->assertEquals(['/fav1'], $result['folders']);
118+
}
119+
120+
/**
121+
* @expectedException \RuntimeException
122+
* @expectedExceptionMessage No favorites
123+
*/
124+
public function testGetFavoriteFilePathsMissingFolder() {
125+
$userFolder = \OC::$server->getUserFolder();
126+
$aFolder = $userFolder->newFolder('x');
127+
128+
$favorites = [
129+
// non-existing
130+
$aFolder->getId() + 999,
131+
];
132+
133+
$this->tags->method('getFavorites')->willReturn($favorites);
134+
135+
$result = $this->helper->getFavoriteFilePaths($this->user);
136+
}
137+
}

0 commit comments

Comments
 (0)
X Tutup