Skip to content

Commit

Permalink
Bring the coverage back to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
nickvergessen committed Aug 26, 2015
1 parent 2a6e676 commit 199d1dc
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 25 deletions.
38 changes: 21 additions & 17 deletions apps/files_sharing/api/sharees.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ protected function getUsers($search) {

$foundUserById = false;
foreach ($users as $uid => $userDisplayName) {
if ($uid === $search || $userDisplayName === $search) {
if ($uid === $search) {
if (strtolower($uid) === $search || strtolower($userDisplayName) === $search) {
if (strtolower($uid) === $search) {
$foundUserById = true;
}
$this->result['exact']['users'][] = [
Expand Down Expand Up @@ -199,7 +199,7 @@ protected function getGroups($search) {
}

foreach ($groups as $gid) {
if ($gid === $search) {
if (strtolower($gid) === $search) {
$this->result['exact']['groups'][] = [
'label' => $search,
'value' => [
Expand All @@ -222,8 +222,8 @@ protected function getGroups($search) {
// On page one we try if the search result has a direct hit on the
// user id and if so, we add that to the exact match list
$group = $this->groupManager->get($search);
if ($group instanceof IGroup && (!$this->shareWithGroupOnly || array_intersect([$group], $userGroups))) {
array_push($this->result['exact']['users'], [
if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
array_push($this->result['exact']['groups'], [
'label' => $group->getGID(),
'value' => [
'shareType' => Share::SHARE_TYPE_GROUP,
Expand All @@ -241,23 +241,17 @@ protected function getGroups($search) {
protected function getRemote($search) {
$this->result['remotes'] = [];

if (substr_count($search, '@') >= 1 && $this->offset === 0) {
$this->result['exact']['remotes'][] = [
'label' => $search,
'value' => [
'shareType' => Share::SHARE_TYPE_REMOTE,
'shareWith' => $search,
],
];
}

// Search in contacts
//@todo Pagination missing
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']);
$foundRemoteById = false;
foreach ($addressBookContacts as $contact) {
if (isset($contact['CLOUD'])) {
foreach ($contact['CLOUD'] as $cloudId) {
if ($contact['FN'] === $search || $cloudId === $search) {
if (strtolower($contact['FN']) === $search || strtolower($cloudId) === $search) {
if (strtolower($cloudId) === $search) {
$foundRemoteById = true;
}
$this->result['exact']['remotes'][] = [
'label' => $contact['FN'],
'value' => [
Expand All @@ -278,6 +272,16 @@ protected function getRemote($search) {
}
}

if (!$foundRemoteById && substr_count($search, '@') >= 1 && substr_count($search, ' ') === 0 && $this->offset === 0) {
$this->result['exact']['remotes'][] = [
'label' => $search,
'value' => [
'shareType' => Share::SHARE_TYPE_REMOTE,
'shareWith' => $search,
],
];
}

$this->reachedEndFor[] = 'remotes';
}

Expand Down Expand Up @@ -313,7 +317,7 @@ public function search() {
$this->limit = (int) $perPage;
$this->offset = $perPage * ($page - 1);

return $this->searchSharees($search, $itemType, $shareTypes, $page, $perPage);
return $this->searchSharees(strtolower($search), $itemType, $shareTypes, $page, $perPage);
}

/**
Expand Down
156 changes: 148 additions & 8 deletions apps/files_sharing/tests/api/shareestest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,20 @@ protected function getGroupMock($gid) {

public function dataGetUsers() {
return [
['test', false, [], [], [], [], true],
['test', true, [], [], [], [], true],
['test', false, [], [], [], [], true, false],
['test', true, [], [], [], [], true, false],
[
'test', false, [], [],
[
['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']],
], [], true, $this->getUserMock('test', 'Test')
],
[
'test', true, [], [],
[
['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']],
], [], true, $this->getUserMock('test', 'Test')
],
[
'test',
false,
Expand All @@ -125,6 +137,7 @@ public function dataGetUsers() {
['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
],
true,
false,
],
[
'test',
Expand All @@ -140,24 +153,26 @@ public function dataGetUsers() {
['label' => 'Test Two', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
],
false,
false,
],
[
'test',
false,
[],
[
$this->getUserMock('test', 'Test'),
$this->getUserMock('test0', 'Test'),
$this->getUserMock('test1', 'Test One'),
$this->getUserMock('test2', 'Test Two'),
],
[
['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test']],
['label' => 'Test', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test0']],
],
[
['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
['label' => 'Test Two', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
],
false,
false,
],
[
'test',
Expand All @@ -172,6 +187,7 @@ public function dataGetUsers() {
['label' => 'Test One', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test1']],
],
true,
false,
],
[
'test',
Expand All @@ -193,6 +209,7 @@ public function dataGetUsers() {
['label' => 'Test Two', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
],
false,
false,
],
[
'test',
Expand All @@ -213,6 +230,7 @@ public function dataGetUsers() {
['label' => 'Test Two', 'value' => ['shareType' => Share::SHARE_TYPE_USER, 'shareWith' => 'test2']],
],
false,
false,
],
];
}
Expand All @@ -227,8 +245,9 @@ public function dataGetUsers() {
* @param array $exactExpected
* @param array $expected
* @param bool $reachedEnd
* @param mixed $singleUser
*/
public function testGetUsers($searchTerm, $shareWithGroupOnly, $groupResponse, $userResponse, $exactExpected, $expected, $reachedEnd) {
public function testGetUsers($searchTerm, $shareWithGroupOnly, $groupResponse, $userResponse, $exactExpected, $expected, $reachedEnd, $singleUser) {
$this->invokePrivate($this->sharees, 'limit', [2]);
$this->invokePrivate($this->sharees, 'offset', [0]);
$this->invokePrivate($this->sharees, 'shareWithGroupOnly', [$shareWithGroupOnly]);
Expand All @@ -255,6 +274,13 @@ public function testGetUsers($searchTerm, $shareWithGroupOnly, $groupResponse, $
->willReturnMap($userResponse);
}

if ($singleUser !== false) {
$this->userManager->expects($this->once())
->method('get')
->with($searchTerm)
->willReturn($singleUser);
}

$this->invokePrivate($this->sharees, 'getUsers', [$searchTerm]);
$result = $this->invokePrivate($this->sharees, 'result');

Expand All @@ -265,14 +291,15 @@ public function testGetUsers($searchTerm, $shareWithGroupOnly, $groupResponse, $

public function dataGetGroups() {
return [
['test', false, [], [], [], [], true],
['test', false, [], [], [], [], true, false],
[
'test', false,
[$this->getGroupMock('test1')],
[],
[],
[['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
true,
false,
],
[
'test', false,
Expand All @@ -284,8 +311,41 @@ public function dataGetGroups() {
[['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]],
[['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
false,
false,
],
['test', true, [], [], [], [], true],
[
'test', false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
],
[],
[],
[
['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']],
['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']],
],
false,
null,
],
[
'test', false,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
],
[],
[
['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']],
],
[
['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']],
['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']],
],
false,
$this->getGroupMock('test'),
],
['test', true, [], [], [], [], true, false],
[
'test', true,
[
Expand All @@ -296,6 +356,7 @@ public function dataGetGroups() {
[],
[['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
false,
false,
],
[
'test', true,
Expand All @@ -307,6 +368,7 @@ public function dataGetGroups() {
[['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]],
[],
false,
false,
],
[
'test', true,
Expand All @@ -318,6 +380,51 @@ public function dataGetGroups() {
[],
[['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
false,
false,
],
[
'test', true,
[
$this->getGroupMock('test'),
$this->getGroupMock('test1'),
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']]],
[['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
false,
false,
],
[
'test', true,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[],
[
['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']],
['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']],
],
false,
null,
],
[
'test', true,
[
$this->getGroupMock('test0'),
$this->getGroupMock('test1'),
],
[$this->getGroupMock('test'), $this->getGroupMock('test0'), $this->getGroupMock('test1')],
[
['label' => 'test', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test']],
],
[
['label' => 'test0', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test0']],
['label' => 'test1', 'value' => ['shareType' => Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']],
],
false,
$this->getGroupMock('test'),
],
];
}
Expand All @@ -332,8 +439,9 @@ public function dataGetGroups() {
* @param array $exactExpected
* @param array $expected
* @param bool $reachedEnd
* @param mixed $singleGroup
*/
public function testGetGroups($searchTerm, $shareWithGroupOnly, $groupResponse, $userGroupsResponse, $exactExpected, $expected, $reachedEnd) {
public function testGetGroups($searchTerm, $shareWithGroupOnly, $groupResponse, $userGroupsResponse, $exactExpected, $expected, $reachedEnd, $singleGroup) {
$this->invokePrivate($this->sharees, 'limit', [2]);
$this->invokePrivate($this->sharees, 'offset', [0]);
$this->invokePrivate($this->sharees, 'shareWithGroupOnly', [$shareWithGroupOnly]);
Expand All @@ -343,6 +451,13 @@ public function testGetGroups($searchTerm, $shareWithGroupOnly, $groupResponse,
->with($searchTerm, $this->invokePrivate($this->sharees, 'limit'), $this->invokePrivate($this->sharees, 'offset'))
->willReturn($groupResponse);

if ($singleGroup !== false) {
$this->groupManager->expects($this->once())
->method('get')
->with($searchTerm)
->willReturn($singleGroup);
}

if ($shareWithGroupOnly) {
$user = $this->getUserMock('admin', 'Administrator');
$this->session->expects($this->any())
Expand Down Expand Up @@ -426,6 +541,31 @@ public function dataGetRemote() {
],
true,
],
[
'username@localhost',
[
[
'FN' => 'User3 @ Localhost',
],
[
'FN' => 'User2 @ Localhost',
'CLOUD' => [
],
],
[
'FN' => 'User @ Localhost',
'CLOUD' => [
'username@localhost',
],
],
],
[
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost']],
],
[
],
true,
],
];
}

Expand Down

0 comments on commit 199d1dc

Please sign in to comment.