Skip to content

Commit

Permalink
test(sharing): Add unit tests proofing the chunking
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Feb 9, 2024
1 parent 792560b commit d844f22
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
* @package OC\Share20
*/
class DefaultShareProvider implements IShareProvider {
protected int $chunkSize = 1000;

// Special share type for user modified group shares
public const SHARE_TYPE_USERGROUP = 2;

Expand Down Expand Up @@ -1095,7 +1097,7 @@ public function getAllSharedWithUser(string $userId): array {
->where($queryFileCache->expr()->in('f.fileid', $queryFileCache->createParameter('fileIds')));

$allFileIds = array_keys($fileData);
foreach (array_chunk($allFileIds, 1000) as $fileIds) {
foreach (array_chunk($allFileIds, $this->chunkSize) as $fileIds) {
// Filecache and storage info
$queryFileCache->setParameter('fileIds', $fileIds, IQueryBuilder::PARAM_INT_ARRAY);

Expand Down Expand Up @@ -1145,7 +1147,7 @@ public function getAllSharedWithGroups($userId): array {
/** @var array<int, ?array> $fileData */
$fileData = [];

foreach (array_chunk($allGroups, 1000) as $groups) {
foreach (array_chunk($allGroups, $this->chunkSize) as $groups) {
$query->setParameter('groups', $groups, IQueryBuilder::PARAM_STR_ARRAY);

$result = $query->executeQuery();
Expand All @@ -1158,6 +1160,7 @@ public function getAllSharedWithGroups($userId): array {
$fileData[(int)$row['file_source']] = null;
}
$result->closeCursor();

}

if (empty($fileData)) {
Expand All @@ -1174,7 +1177,7 @@ public function getAllSharedWithGroups($userId): array {
->where($queryFileCache->expr()->in('f.fileid', $queryFileCache->createParameter('fileIds')));

$allFileIds = array_keys($fileData);
foreach (array_chunk($allFileIds, 1000) as $fileIds) {
foreach (array_chunk($allFileIds, $this->chunkSize) as $fileIds) {
// Filecache and storage info
$queryFileCache->setParameter('fileIds', $fileIds, IQueryBuilder::PARAM_INT_ARRAY);

Expand Down
124 changes: 124 additions & 0 deletions tests/lib/Share20/DefaultShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,130 @@ public function testGetSharedWithUser($storageStringId, $fileName1, $fileName2)
$this->assertEquals(IShare::TYPE_USER, $share->getShareType());
}

public function testGetAllSharedWithUser(): void {
$storageId = $this->createTestStorageEntry('home::shareOwner');
$fileIds = [];
for ($i = 0; $i < 50; $i++) {
$fileIds[] = $this->createTestFileEntry('files/test-' . $i . '.txt', $storageId);
}

$shareIds = [];
foreach ($fileIds as $fileId) {
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('share')
->values([
'share_type' => $qb->expr()->literal(IShare::TYPE_USER),
'share_with' => $qb->expr()->literal('sharedWith'),
'uid_owner' => $qb->expr()->literal('shareOwner'),
'uid_initiator' => $qb->expr()->literal('sharedBy'),
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal($fileId),
'file_target' => $qb->expr()->literal('myTarget'),
'permissions' => $qb->expr()->literal(13),
]);
$qb->executeStatement();
$shareIds[$fileId] = $qb->getLastInsertId();
}

$file = $this->createMock(File::class);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
$this->rootFolder->method('getById')->willReturn([$file]);

self::invokePrivate($this->provider, 'chunkSize', [5]);
$shares = $this->provider->getSharedWith('sharedWith', IShare::TYPE_USER, null, -1, 0);
$this->assertCount(50, $shares);

foreach ($shares as $share) {
$this->assertEquals($shareIds[$share->getNodeId()], $share->getId());
$this->assertEquals('sharedWith', $share->getSharedWith());
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals(IShare::TYPE_USER, $share->getShareType());
}
}
public function testGetAllSharedWithGroup() {
$storageId = $this->createTestStorageEntry('home::shareOwner');
$fileIds = [];
for ($i = 0; $i < 50; $i++) {
$fileIds[] = $this->createTestFileEntry('files/test-' . $i . '.txt', $storageId);
}

$shareIdToFileIds = [];
$shareIdToGroupId = [];
for ($i = 0; $i < 50; $i++) {
$qb = $this->dbConn->getQueryBuilder();
$qb->insert('share')
->values([
'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
'share_with' => $qb->expr()->literal('sharedWith'),
'uid_owner' => $qb->expr()->literal('shareOwner'),
'uid_initiator' => $qb->expr()->literal('sharedBy'),
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal($fileIds[$i]),
'file_target' => $qb->expr()->literal('myTarget' . $i . 'shareWith'),
'permissions' => $qb->expr()->literal(13),
]);
$qb->executeStatement();
$shareId = $qb->getLastInsertId();
$shareIdToFileIds[$shareId] = $fileIds[$i];
$shareIdToGroupId[$shareId] = 'sharedWith';

$qb = $this->dbConn->getQueryBuilder();
$qb->insert('share')
->values([
'share_type' => $qb->expr()->literal(IShare::TYPE_GROUP),
'share_with' => $qb->expr()->literal('group' . $i),
'uid_owner' => $qb->expr()->literal('shareOwner'),
'uid_initiator' => $qb->expr()->literal('sharedBy'),
'item_type' => $qb->expr()->literal('file'),
'file_source' => $qb->expr()->literal($fileIds[$i]),
'file_target' => $qb->expr()->literal('myTarget' . $i . 'group'),
'permissions' => $qb->expr()->literal(13),
]);
$qb->executeStatement();
$shareId = $qb->getLastInsertId();
$shareIdToFileIds[$shareId] = $fileIds[$i];
$shareIdToGroupId[$shareId] = 'group' . $i;
}

$groups = [];
foreach (range(0, 100) as $i) {
$groups[] = 'group'.$i;
}

$groups[] = 'sharedWith';

$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('sharedWith');
$owner = $this->createMock(IUser::class);
$owner->method('getUID')->willReturn('shareOwner');
$initiator = $this->createMock(IUser::class);
$initiator->method('getUID')->willReturn('sharedBy');

$this->userManager->method('get')->willReturnMap([
['sharedWith', $user],
['shareOwner', $owner],
['sharedBy', $initiator],
]);
$this->groupManager->method('getUserGroupIds')->with($user)->willReturn($groups);

$file = $this->createMock(File::class);
$this->rootFolder->method('getUserFolder')->with('shareOwner')->willReturnSelf();
$this->rootFolder->method('getById')->willReturn([$file]);

self::invokePrivate($this->provider, 'chunkSize', [5]);
$shares = $this->provider->getSharedWith('sharedWith', IShare::TYPE_GROUP, null, -1, 0);
$this->assertCount(100, $shares);

foreach ($shares as $share) {
$this->assertEquals($shareIdToFileIds[$share->getId()], $share->getNodeId());
$this->assertEquals($shareIdToGroupId[$share->getId()], $share->getSharedWith());
$this->assertEquals('shareOwner', $share->getShareOwner());
$this->assertEquals('sharedBy', $share->getSharedBy());
$this->assertEquals(IShare::TYPE_GROUP, $share->getShareType());
}
}

/**
* @dataProvider storageAndFileNameProvider
*/
Expand Down

0 comments on commit d844f22

Please sign in to comment.