Skip to content

Commit

Permalink
Don't show contacts an entry for themselves
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Apr 20, 2017
1 parent 239eb11 commit cc3edca
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
10 changes: 8 additions & 2 deletions lib/private/Contacts/ContactsMenu/ContactsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use OCP\Contacts\ContactsMenu\IEntry;
use OCP\Contacts\IManager;
use OCP\IUser;

class ContactsStore {

Expand All @@ -40,17 +41,22 @@ public function __construct(IManager $contactsManager) {
}

/**
* @param IUser $user
* @param string|null $filter
* @return IEntry[]
*/
public function getContacts($filter) {
public function getContacts(IUser $user, $filter) {
$allContacts = $this->contactsManager->search($filter ?: '', [
'FN',
]);

return array_map(function(array $contact) {
$self = $user->getUID();
$entries = array_map(function(array $contact) {
return $this->contactArrayToEntry($contact);
}, $allContacts);
return array_filter($entries, function(IEntry $entry) use ($self) {
return $entry->getProperty('UID') !== $self;
});
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/private/Contacts/ContactsMenu/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

use OCP\App\IAppManager;
use OCP\Contacts\ContactsMenu\IEntry;
use OCP\IURLGenerator;
use OCP\IUser;

class Manager {
Expand Down Expand Up @@ -57,7 +56,7 @@ public function __construct(ContactsStore $store, ActionProviderStore $actionPro
* @return array
*/
public function getEntries(IUser $user, $filter) {
$entries = $this->store->getContacts($filter);
$entries = $this->store->getContacts($user, $filter);

$sortedEntries = $this->sortEntries($entries);
$topEntries = array_slice($sortedEntries, 0, 25);
Expand Down
1 change: 1 addition & 0 deletions lib/public/Contacts/ContactsMenu/IProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface IProvider {
/**
* @since 12.0
* @param IEntry $entry
* @return void
*/
public function process(IEntry $entry);
}
57 changes: 48 additions & 9 deletions tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use OC\Contacts\ContactsMenu\ContactsStore;
use OCP\Contacts\IManager;
use OCP\IUser;
use PHPUnit_Framework_MockObject_MockObject;
use Test\TestCase;

Expand All @@ -46,73 +47,111 @@ protected function setUp() {
}

public function testGetContactsWithoutFilter() {
$user = $this->createMock(IUser::class);
$this->contactsManager->expects($this->once())
->method('search')
->with($this->equalTo(''), $this->equalTo(['FN']))
->willReturn([
[
'id' => 123,
'UID' => 123,
],
[
'id' => 567,
'UID' => 567,
'FN' => 'Darren Roner',
'EMAIL' => [
'[email protected]'
],
],
]);
$user->expects($this->once())
->method('getUID')
->willReturn('user123');

$entries = $this->contactsStore->getContacts('');
$entries = $this->contactsStore->getContacts($user, '');

$this->assertCount(2, $entries);
$this->assertEquals([
'[email protected]'
], $entries[1]->getEMailAddresses());
}

public function testGetContactsHidesOwnEntry() {
$user = $this->createMock(IUser::class);
$this->contactsManager->expects($this->once())
->method('search')
->with($this->equalTo(''), $this->equalTo(['FN']))
->willReturn([
[
'UID' => 'user123',
],
[
'UID' => 567,
'FN' => 'Darren Roner',
'EMAIL' => [
'[email protected]'
],
],
]);
$user->expects($this->once())
->method('getUID')
->willReturn('user123');

$entries = $this->contactsStore->getContacts($user, '');

$this->assertCount(1, $entries);
}

public function testGetContactsWithoutBinaryImage() {
$user = $this->createMock(IUser::class);
$this->contactsManager->expects($this->once())
->method('search')
->with($this->equalTo(''), $this->equalTo(['FN']))
->willReturn([
[
'id' => 123,
'UID' => 123,
],
[
'id' => 567,
'UID' => 567,
'FN' => 'Darren Roner',
'EMAIL' => [
'[email protected]'
],
'PHOTO' => base64_encode('photophotophoto'),
],
]);
$user->expects($this->once())
->method('getUID')
->willReturn('user123');

$entries = $this->contactsStore->getContacts('');
$entries = $this->contactsStore->getContacts($user, '');

$this->assertCount(2, $entries);
$this->assertNull($entries[1]->getAvatar());
}

public function testGetContactsWithoutAvatarURI() {
$user = $this->createMock(IUser::class);
$this->contactsManager->expects($this->once())
->method('search')
->with($this->equalTo(''), $this->equalTo(['FN']))
->willReturn([
[
'id' => 123,
'UID' => 123,
],
[
'id' => 567,
'UID' => 567,
'FN' => 'Darren Roner',
'EMAIL' => [
'[email protected]'
],
'PHOTO' => 'VALUE=uri:https://photo',
],
]);
$user->expects($this->once())
->method('getUID')
->willReturn('user123');

$entries = $this->contactsStore->getContacts('');
$entries = $this->contactsStore->getContacts($user, '');

$this->assertCount(2, $entries);
$this->assertEquals('https://photo', $entries[1]->getAvatar());
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/Contacts/ContactsMenu/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testGetFilteredEntries() {
$provider = $this->createMock(IProvider::class);
$this->contactsStore->expects($this->once())
->method('getContacts')
->with($filter)
->with($user, $filter)
->willReturn($entries);
$this->actionProviderStore->expects($this->once())
->method('getProviders')
Expand Down

0 comments on commit cc3edca

Please sign in to comment.