Skip to content

Commit

Permalink
Test edge case in presencemapper
Browse files Browse the repository at this point in the history
  • Loading branch information
LEDfan committed Feb 9, 2018
1 parent 3deb657 commit 8bd8215
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 24 deletions.
4 changes: 0 additions & 4 deletions lib/ContactsStoreUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ public function hasUserForUserByUID($uid1, $uid2)
return !is_null($this->contactsStore->findOne($this->userManager->get($uid1), 0, $uid2));
}

/**
* @param string $userId
* @return bool whether $userId is excluded from chatting
*/
public function isUserExcluded($userId)
{
if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') {
Expand Down
2 changes: 1 addition & 1 deletion lib/IUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function hasUserForUserByUID($uid1, $uid2);

/**
* @param string $userId
* @return bool
* @return bool whether $userId is excluded from chatting
*/
public function isUserExcluded($userId);
}
2 changes: 1 addition & 1 deletion lib/db/presencemapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PresenceMapper extends Mapper
/**
* @var null|string the userId of the current user
*/
private $userId;
private $userIdPresenceMapperPresenceMapperPresenceMapper;


/**
Expand Down
32 changes: 17 additions & 15 deletions tests/integration/ContactsStoreUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
use Sabre\VObject\Component\VCard;
use OCA\OJSXC\Utility\TestCase;

class ContactsStoreUserProviderTest extends TestCase {
class ContactsStoreUserProviderTest extends TestCase
{

/**
* @var ContactsStoreUserProvider
Expand Down Expand Up @@ -59,7 +60,8 @@ class ContactsStoreUserProviderTest extends TestCase {
*/
private $contactsManager;

public function setUp() {
public function setUp()
{
if (!Application::contactsStoreApiSupported()) {
$this->markTestSkipped();
return;
Expand All @@ -80,7 +82,7 @@ public function setUp() {
$this->userSession = \OC::$server->getUserSession();
$this->userSession->setUser($currentUser);
$this->userManager = \OC::$server->getUserManager();
$this->groupManager= \OC::$server->getGroupManager();
$this->groupManager = \OC::$server->getGroupManager();
$this->contactsStore = \OC::$server->query(ContactsStore::class);
$this->contactsManager = \OC::$server->getContactsManager();

Expand Down Expand Up @@ -119,11 +121,10 @@ public function setUp() {
$this->groupManager,
$this->config
);


}

private function setupAddressBook($userId) {
private function setupAddressBook($userId)
{
$addressBooks = $this->cardDavBackend->getAddressBooksForUser("principals/users/$userId");
foreach ($addressBooks as $addressBookInfo) {
$this->cardDavBackend->deleteAddressBook($addressBookInfo['id']);
Expand All @@ -147,7 +148,8 @@ private function setupAddressBook($userId) {
return $addressBookId;
}

protected function tearDown() {
protected function tearDown()
{
$this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'no');
$this->config->setAppValue('core', 'shareapi_exclude_groups', 'no');
$this->config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode([]));
Expand All @@ -156,7 +158,8 @@ protected function tearDown() {
}
}

public function testNormalSituation() {
public function testNormalSituation()
{

// no special settings set
$derp = new User('derp', 'derp', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'derp')[0]);
Expand All @@ -183,10 +186,10 @@ public function testNormalSituation() {
$this->assertTrue($this->contactsStoreUserProvider->hasUserByUID('derpina'));
$this->assertTrue($this->contactsStoreUserProvider->hasUser($derpina));
$this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('derpina'));

}

public function testGroupsOnly() {
public function testGroupsOnly()
{
$this->setValueOfPrivateProperty($this->contactsStoreUserProvider, 'cache', null);
$group1 = $this->groupManager->createGroup('group1');
$group2 = $this->groupManager->createGroup('group2');
Expand Down Expand Up @@ -222,10 +225,10 @@ public function testGroupsOnly() {
$this->assertFalse($this->contactsStoreUserProvider->hasUserByUID('derpina'));
$this->assertFalse($this->contactsStoreUserProvider->hasUser($derpina));
$this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('derpina'));

}

public function testExcluded() {
public function testExcluded()
{
$this->setValueOfPrivateProperty($this->contactsStoreUserProvider, 'cache', null);
$groupExcluded = $this->groupManager->createGroup('excluded');
$this->config->setAppValue('core', 'shareapi_exclude_groups', 'yes');
Expand All @@ -250,7 +253,8 @@ public function testExcluded() {
$this->config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode([]));
}

public function testDisabled() {
public function testDisabled()
{
$this->setValueOfPrivateProperty($this->contactsStoreUserProvider, 'cache', null);
$derpina = new User('derpina', 'derpina', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'derpina')[0]);
$herp = new User('herp', 'herp', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'herp')[0]);
Expand All @@ -267,7 +271,6 @@ public function testDisabled() {
];

$this->assertCanonicalizeEquals($expected, $this->contactsStoreUserProvider->getAllUsers());

}

private function assertCanonicalizeEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $ignoreCase = false)
Expand All @@ -282,5 +285,4 @@ private function assertCanonicalizeEquals($expected, $actual, $message = '', $de

static::assertThat($actual, $constraint, $message);
}

}
48 changes: 47 additions & 1 deletion tests/integration/db/PresenceMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function getPresenceProvider()
]
];
}

/**
* @dataProvider getPresenceProvider
* @param $inputs
Expand Down Expand Up @@ -343,6 +343,52 @@ public function testGetConnectedUsers($inputs, $expected)
$this->assertEquals($expected, $result);
}

public function testGetConnectedUsersIfUserHasNot()
{
$this->setValueOfPrivateProperty($this->mapper, 'connectedUsers', []);
$this->setupContactsStoreAPI();

$group = \OC::$server->getGroupManager()->createGroup('group1');
$group->addUser(\OC::$server->getUserManager()->get('derp'));

$group2 = \OC::$server->getGroupManager()->createGroup('group2');
$group2->addUser(\OC::$server->getUserManager()->get('autotest'));
$group2->addUser(\OC::$server->getUserManager()->get('foo'));
$group2->addUser(\OC::$server->getUserManager()->get('admin'));

\OC::$server->getConfig()->setAppValue('core', 'shareapi_only_share_with_group_members', 'yes');

$input1 = new PresenceEntity();
$input1->setPresence('online');
$input1->setUserid('foo');
$input1->setLastActive(23434475);

$input2 = new PresenceEntity();
$input2->setPresence('online');
$input2->setUserid('derp');
$input2->setLastActive(23434475);

$this->mapper->setPresence($input1);
$this->mapper->setPresence($input2);

$expected = ['foo'];

$result = $this->mapper->getConnectedUsers();

$this->assertCount(count($expected), $result);
sort($expected);
sort($result);
$this->assertEquals($expected, $result);

\OC::$server->getConfig()->setAppValue('core', 'shareapi_only_share_with_group_members', 'no');

$group->removeUser(\OC::$server->getUserManager()->get('derp'));
$group->delete();
$group2->removeUser(\OC::$server->getUserManager()->get('autotest'));
$group2->removeUser(\OC::$server->getUserManager()->get('admin'));
$group2->delete();
}

public function updatePresenceProvider()
{
$input1 = new PresenceEntity();
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/stanzahandlers/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public function testMessage(array $stanza, $expected)
$this->message->handle($stanza);
}

public function testNotAllowedToChat() {
public function testNotAllowedToChat()
{
$this->messageMapper->expects($this->never())
->method('insert');

Expand All @@ -135,6 +136,5 @@ public function testNotAllowedToChat() {
];

$this->message->handle($stanza);

}
}

0 comments on commit 8bd8215

Please sign in to comment.