diff options
author | sualko <klaus@jsxc.org> | 2020-06-30 15:53:20 +0300 |
---|---|---|
committer | sualko <klaus@jsxc.org> | 2020-06-30 15:53:20 +0300 |
commit | bce9f1900271a9af7ba2fcac30c12f2ea37c2b64 (patch) | |
tree | 822750b67829d835397c75129e5f026d59099cd9 /tests/integration | |
parent | c083263a500ae1664377d94b38528e7c10e570f2 (diff) |
test: fix integration tests
Diffstat (limited to 'tests/integration')
-rw-r--r-- | tests/integration/ContactsStoreUserProviderTest.php | 281 | ||||
-rw-r--r-- | tests/integration/DbLockTest.php | 108 | ||||
-rw-r--r-- | tests/integration/MemLockTest.php | 98 | ||||
-rw-r--r-- | tests/integration/RosterPushTest.php | 174 | ||||
-rw-r--r-- | tests/integration/db/IqRosterPushTest.php | 37 | ||||
-rw-r--r-- | tests/integration/db/IqRosterTest.php | 57 | ||||
-rw-r--r-- | tests/integration/db/MessageMapperTest.php | 173 | ||||
-rw-r--r-- | tests/integration/db/PresenceMapperTest.php | 651 | ||||
-rw-r--r-- | tests/integration/db/PresenceTest.php | 145 | ||||
-rw-r--r-- | tests/integration/db/StanzaMapperTest.php | 145 |
10 files changed, 0 insertions, 1869 deletions
diff --git a/tests/integration/ContactsStoreUserProviderTest.php b/tests/integration/ContactsStoreUserProviderTest.php deleted file mode 100644 index 0073c17..0000000 --- a/tests/integration/ContactsStoreUserProviderTest.php +++ /dev/null @@ -1,281 +0,0 @@ -<?php - -namespace OCA\OJSXC; - -use OC\Contacts\ContactsMenu\ContactsStore; -use OCA\DAV\CardDAV\AddressBookImpl; -use OCA\OJSXC\AppInfo\Application; -use OCP\Contacts\ContactsMenu\IContactsStore; -use OCP\Contacts\IManager; -use OCP\IConfig; -use OCP\IGroupManager; -use OCP\IUserManager; -use OCP\IUserSession; -use OCA\DAV\AppInfo\Application as DavApp; -use OCA\DAV\CardDAV\CardDavBackend; -use PHPUnit\Framework\Constraint\IsEqual; -use Sabre\VObject\Component\VCard; -use OCA\OJSXC\Utility\TestCase; - -class ContactsStoreUserProviderTest extends TestCase -{ - - /** - * @var ContactsStoreUserProvider - */ - private $contactsStoreUserProvider; - - /** - * @var IContactsStore - */ - private $contactsStore; - - /** - * @var IUserSession - */ - private $userSession; - - /** - * @var IUserManager - */ - private $userManager; - - /** - * @var IGroupManager - */ - private $groupManager; - - /** - * @var IConfig - */ - private $config; - - /** - * @var CardDavBackend - */ - private $cardDavBackend; - - /** - * @var IManager - */ - private $contactsManager; - - public function setUp(): void - { - if (!Application::contactsStoreApiSupported()) { - $this->markTestSkipped(); - return; - } - $this->config = \OC::$server->getConfig(); - $this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'no'); - foreach (\OC::$server->getUserManager()->search('') as $user) { - $user->delete(); - } - - $users[] = \OC::$server->getUserManager()->createUser('admin', 'admin'); - $users[] = \OC::$server->getUserManager()->createUser('derp', 'derp'); - $users[] = \OC::$server->getUserManager()->createUser('derpina', 'derpina'); - $users[] = \OC::$server->getUserManager()->createUser('herp', 'herp'); - $users[] = \OC::$server->getUserManager()->createUser('foo', 'foo'); - - $currentUser = \OC::$server->getUserManager()->createUser('autotest', 'autotest'); - $this->userSession = \OC::$server->getUserSession(); - $this->userSession->setUser($currentUser); - $this->userManager = \OC::$server->getUserManager(); - $this->groupManager = \OC::$server->getGroupManager(); - $this->contactsStore = \OC::$server->query(ContactsStore::class); - $this->contactsManager = \OC::$server->getContactsManager(); - - /** @var \OCA\DAV\CardDAV\SyncService $syncService */ - $syncService = \OC::$server->query('CardDAVSyncService'); - $syncService->getLocalSystemAddressBook(); - $syncService->updateUser($currentUser); - - - $davApp = new DavApp(); - - $this->cardDavBackend = $davApp->getContainer()->query(CardDavBackend::class); - - // create some contacts - $vCard = new VCard(); - $vCard->VERSION = '3.0'; - $vCard->UID = 'Test1'; - $vCard->FN = 'Test1'; - - $id = $this->setupAddressBook('autotest'); - $this->cardDavBackend->createCard($id, 'Alice.Test1.vcf', $vCard->serialize()); - - foreach ($users as $user) { - $syncService->updateUser($user); - } - - $davApp->setupSystemContactsProvider($this->contactsManager); - \OC_User::setIncognitoMode(false); - \OC::$server->getDatabaseConnection()->executeQuery("DELETE FROM *PREFIX*ojsxc_stanzas"); - - - $this->contactsStoreUserProvider = new ContactsStoreUserProvider( - $this->contactsStore, - $this->userSession, - $this->userManager, - $this->groupManager, - $this->config - ); - } - - private function setupAddressBook($userId) - { - $addressBooks = $this->cardDavBackend->getAddressBooksForUser("principals/users/$userId"); - foreach ($addressBooks as $addressBookInfo) { - $this->cardDavBackend->deleteAddressBook($addressBookInfo['id']); - } - $addressBookId = $this->cardDavBackend->createAddressBook('principals/users/' . $userId, 'principals/users/' . $userId, []); - - $addressBooks = $this->cardDavBackend->getAddressBooksForUser("principals/users/$userId"); - - foreach ($addressBooks as $addressBookInfo) { - $addressBook = new \OCA\DAV\CardDAV\AddressBook($this->cardDavBackend, $addressBookInfo, \OC::$server->getL10N('dav')); - $this->contactsManager->registerAddressBook( - new AddressBookImpl( - $addressBook, - $addressBookInfo, - $this->cardDavBackend, - \OC::$server->getURLGenerator() - ) - ); - } - - return $addressBookId; - } - - protected function tearDown(): void - { - $config = \OC::$server->getConfig(); - $config->setAppValue('core', 'shareapi_only_share_with_group_members', 'no'); - $config->setAppValue('core', 'shareapi_exclude_groups', 'no'); - $config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode([])); - foreach (\OC::$server->getUserManager()->search('') as $user) { - $user->delete(); - } - } - - public function testNormalSituation() - { - - // no special settings set - $derp = new User('derp', 'derp', $this->contactsStore->findOne($this->userManager->get('autotest'), 6, 'derp')); - $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]); - $foo = new User('foo', 'foo', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'foo')[0]); - $admin = new User('admin', 'admin', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'admin')[0]); - $autotest = new User('autotest', 'autotest', $this->contactsStore->getContacts($this->userManager->get('derpina'), 'autotest')[0]); - - $expected = [ - $foo, - $admin, - $derp, - $derpina, - $herp - ]; - - $this->assertCanonicalizeEquals($expected, $this->contactsStoreUserProvider->getAllUsers()); - $this->assertCanonicalizeEquals([$autotest, $derp, $herp, $foo, $admin], $this->contactsStoreUserProvider->getAllUsersForUserByUID('derpina')); - $this->assertCanonicalizeEquals([$autotest, $derp, $herp, $foo, $admin], $this->contactsStoreUserProvider->getAllUsersForUser($derpina)); - - $this->assertTrue($this->contactsStoreUserProvider->hasUserForUserByUID('derp', 'derpina')); - $this->assertTrue($this->contactsStoreUserProvider->hasUserForUser($derp, $derpina)); - $this->assertTrue($this->contactsStoreUserProvider->hasUserByUID('derpina')); - $this->assertTrue($this->contactsStoreUserProvider->hasUser($derpina)); - $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('derpina')); - } - - public function testGroupsOnly() - { - $this->setValueOfPrivateProperty($this->contactsStoreUserProvider, 'cache', null); - $group1 = $this->groupManager->createGroup('group1'); - $group2 = $this->groupManager->createGroup('group2'); - $group1->addUser($this->userManager->get('derp')); - $group1->addUser($this->userManager->get('foo')); - $group2->addUser($this->userManager->get('derpina')); - $group2->addUser($this->userManager->get('herp')); - - $this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'yes'); - - // no special settings set - $derp = new User('derp', 'derp', $this->contactsStore->getContacts($this->userManager->get('foo'), 'derp')[0]); - $derpina = new User('derpina', 'derpina', $this->contactsStore->getContacts($this->userManager->get('herp'), 'derpina')[0]); - $herp = new User('herp', 'herp', $this->contactsStore->getContacts($this->userManager->get('derpina'), 'herp')[0]); - $foo = new User('foo', 'foo', $this->contactsStore->getContacts($this->userManager->get('derp'), 'foo')[0]); - - $this->assertCanonicalizeEquals([], $this->contactsStoreUserProvider->getAllUsers()); // running as autotest -> not in any group - $this->assertCanonicalizeEquals([$herp], $this->contactsStoreUserProvider->getAllUsersForUserByUID('derpina')); - $this->assertCanonicalizeEquals([$herp], $this->contactsStoreUserProvider->getAllUsersForUser($derpina)); - $this->assertCanonicalizeEquals([$foo], $this->contactsStoreUserProvider->getAllUsersForUserByUID('derp')); - $this->assertCanonicalizeEquals([$foo], $this->contactsStoreUserProvider->getAllUsersForUser($derp)); - - $this->assertFalse($this->contactsStoreUserProvider->hasUserForUserByUID('derp', 'derpina')); - $this->assertFalse($this->contactsStoreUserProvider->hasUserForUser($derp, $derpina)); - $this->assertFalse($this->contactsStoreUserProvider->hasUserForUserByUID('derpina', 'derp')); - $this->assertFalse($this->contactsStoreUserProvider->hasUserForUser($derpina, $derp)); - - $this->assertTrue($this->contactsStoreUserProvider->hasUserForUserByUID('derp', 'foo')); - $this->assertTrue($this->contactsStoreUserProvider->hasUserForUser($derp, $foo)); - $this->assertTrue($this->contactsStoreUserProvider->hasUserForUserByUID('foo', 'derp')); - $this->assertTrue($this->contactsStoreUserProvider->hasUserForUser($foo, $derp)); - - $this->assertFalse($this->contactsStoreUserProvider->hasUserByUID('derpina')); - $this->assertFalse($this->contactsStoreUserProvider->hasUser($derpina)); - $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('derpina')); - } - - public function testExcluded() - { - $this->setValueOfPrivateProperty($this->contactsStoreUserProvider, 'cache', null); - $groupExcluded = $this->groupManager->createGroup('excluded'); - $this->config->setAppValue('core', 'shareapi_exclude_groups', 'yes'); - $this->config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode(['excluded'])); - $groupExcluded->addUser($this->userManager->get('derp')); - $groupExcluded->addUser($this->userManager->get('derpina')); - - $herp = new User('herp', 'herp', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'herp')[0]); - $foo = new User('foo', 'foo', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'foo')[0]); - $admin = new User('admin', 'admin', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'admin')[0]); - $autotest = new User('autotest', 'autotest', $this->contactsStore->getContacts($this->userManager->get('admin'), 'autotest')[0]); - - $this->assertCanonicalizeEquals([$admin, $herp, $foo], $this->contactsStoreUserProvider->getAllUsers()); - $this->assertTrue($this->contactsStoreUserProvider->isUserExcluded('derp')); - $this->assertTrue($this->contactsStoreUserProvider->isUserExcluded('derpina')); - $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('autotest')); - $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('admin')); - $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('foo')); - $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('herp')); - - $this->config->setAppValue('core', 'shareapi_exclude_groups', 'no'); - $this->config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode([])); - } - - 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]); - $foo = new User('foo', 'foo', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'foo')[0]); - $admin = new User('admin', 'admin', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'admin')[0]); - - $this->userManager->get('derp')->setEnabled(false); - - $expected = [ - $admin, - $derpina, - $herp, - $foo - ]; - - $this->assertCanonicalizeEquals($expected, $this->contactsStoreUserProvider->getAllUsers()); - } - - private function assertCanonicalizeEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $ignoreCase = false) - { - $this->assertEquals($expected, $actual, $message, $delta, $maxDepth, true, $ignoreCase); - } -} diff --git a/tests/integration/DbLockTest.php b/tests/integration/DbLockTest.php deleted file mode 100644 index e071990..0000000 --- a/tests/integration/DbLockTest.php +++ /dev/null @@ -1,108 +0,0 @@ -<?php -namespace OCA\OJSXC; - -use OCP\AppFramework\Db\DoesNotExistException; -use Test\TestCase; -use OCA\OJSXC\AppInfo\Application; -use OCA\OJSXC\DbLock; - -$time = 0; - -function time() -{ - global $time; - return $time; -} - -/** - * @group DB - */ -class DbLockTest extends TestCase -{ - - /** - * @var \OCA\OJSXC\DbLock - */ - private $dbLock; - - /** - * @var \OCA\OJSXC\DbLock - */ - private $dbLock2; - - /** - * @var \OCP\IDb - */ - private $con; - - - /** - * @var \OCP\AppFramework\IAppContainer - */ - private $container; - - public function setUp(): void - { - parent::setUp(); - $app = new Application(); - $this->container = $app->getContainer(); - $this->con = $this->container->getServer()->getDatabaseConnection(); - $this->con->executeQuery("DELETE FROM `*PREFIX*preferences` WHERE `appid`='ojsxc' AND `configkey`='longpolling'"); - } - - /** - * Tests the setLock and stillLocked function by setting up and lock - * and then setting a new lock. - */ - public function testLock() - { - global $time; - $time = 4; - $this->dbLock = new DbLock( - 'john', - $this->container->getServer()->getConfig(), - $this->container->getServer()->getDatabaseConnection() - ); - $this->dbLock->setLock(); - $result = $this->fetchLocks(); - $this->assertCount(1, $result); - $this->assertEquals($result[0]['userid'], 'john'); - $this->assertEquals($result[0]['appid'], 'ojsxc'); - $this->assertEquals($result[0]['configkey'], 'longpolling'); - $this->assertEquals($result[0]['configvalue'], '4'); - $this->assertTrue($this->dbLock->stillLocked()); - - - $time = 5; - $this->dbLock2 = new DbLock( - 'john', - $this->container->getServer()->getConfig(), - $this->container->getServer()->getDatabaseConnection() - ); // simulate new lock/request - $this->dbLock2->setLock(); - - $this->assertFalse($this->dbLock->stillLocked()); - $this->assertTrue($this->dbLock2->stillLocked()); - $result = $this->fetchLocks(); - $this->assertCount(1, $result); - $this->assertEquals($result[0]['userid'], 'john'); - $this->assertEquals($result[0]['appid'], 'ojsxc'); - $this->assertEquals($result[0]['configkey'], 'longpolling'); - $this->assertEquals($result[0]['configvalue'], '5'); - $this->assertTrue($this->dbLock2->stillLocked()); - } - - private function fetchLocks() - { - $stmt = $this->con->executeQuery("SELECT * FROM `*PREFIX*preferences` WHERE `appid`='ojsxc' AND `configkey`='longpolling'"); - - $reuslt = []; - - while ($row = $stmt->fetch()) { - $result[] = $row; - } - - - return $result; - } -} diff --git a/tests/integration/MemLockTest.php b/tests/integration/MemLockTest.php deleted file mode 100644 index 728c2ec..0000000 --- a/tests/integration/MemLockTest.php +++ /dev/null @@ -1,98 +0,0 @@ -<?php -namespace OCA\OJSXC; - -use OCP\AppFramework\Db\DoesNotExistException; -use Test\TestCase; -use OCA\OJSXC\AppInfo\Application; -use OCA\OJSXC\MemLock; - -//function time() { -// return DbLockTest::$time; -//} - -/** - * @group DB - */ -class MemLockTest extends TestCase -{ - - /** - * @var \OCA\OJSXC\MemLock - */ - private $memLock; - - /** - * @var \OCA\OJSXC\MemLock - */ - private $memLock2; - - /** - * @var \OCP\AppFramework\IAppContainer - */ - private $container; - - /** - * @var \OCP\ICache - */ - private $memCache; - - public static $time; - - public function setUp(): void - { - parent::setUp(); - $app = new Application(); - $this->container = $app->getContainer(); - - $version = \OC::$server->getSession()->get('OC_Version'); - if ($version[0] === 8 && $version[1] == 0) { - $this->markTestSkipped(); - } - } - - /** - * Tests the setLock and stillLocked function by setting up and lock - * and then setting a new lock. - */ - public function testLock() - { - global $time; - $time = 4; - $cache = $this->container->getServer()->getMemCacheFactory(); - if ($cache->isAvailable()) { - $this->memCache = $cache->create('ojsxc'); - } else { - die('No memcache available'); - } - - $this->memLock = new MemLock( - 'john', - $this->memCache - ); - $this->memLock->setLock(); - $this->assertTrue($this->memLock->stillLocked()); - - - $result = $this->fetchLock(); - $this->assertEquals('4', $result); - - - global $time; - $time = 5; - $this->memLock2 = new MemLock( - 'john', - $this->memCache - ); // simulate new lock/request - $this->memLock2->setLock(); - - $this->assertFalse($this->memLock->stillLocked()); - $this->assertTrue($this->memLock2->stillLocked()); - $result = $this->fetchLock(); - $this->assertEquals('5', $result); - } - - private function fetchLock() - { - return $this->memCache->get('-john-ojxsc-lock'); - } -} diff --git a/tests/integration/RosterPushTest.php b/tests/integration/RosterPushTest.php deleted file mode 100644 index b6b9e43..0000000 --- a/tests/integration/RosterPushTest.php +++ /dev/null @@ -1,174 +0,0 @@ -<?php - -namespace OCA\OJSXC; - -use OCA\OJSXC\AppInfo\Application; -use OCA\OJSXC\Db\IQRosterPushMapper; -use OCP\Contacts\ContactsMenu\IContactsStore; -use OCP\IConfig; -use OCP\IDBConnection; -use OCP\IGroupManager; -use OCP\IUserManager; -use OCP\IUserSession; -use PHPUnit\Framework\TestCase; -use PHPUnit\Framework\MockObject\MockObject; - -class RosterPushTest extends TestCase -{ - - /** - * @var RosterPush - */ - private $rosterPush; - - /** - * @var IUserManager - */ - private $userManager; - - /** - * @var IGroupManager - */ - private $groupManager; - - /** - * @var IUserSession - */ - private $userSession; - - /** - * @var MockObject | IQRosterPushMapper - */ - private $iqRosterPushMapper; - - /** - * @var IDBConnection - */ - private $dbConnection; - - /** - * @var IUserProvider - */ - private $userProvider; - - /** - * @var IConfig - */ - private $config; - - /** - * @var MockObject | IContactsStore - */ - private $contactsStore; - - public function setUp(): void - { - if (!Application::contactsStoreApiSupported()) { - $this->markTestSkipped(); - return; - } - - $this->userManager = \OC::$server->getUserManager(); - $this->groupManager = \OC::$server->getGroupManager(); - $this->userSession = \OC::$server->getUserSession(); - $this->iqRosterPushMapper = $this->getMockBuilder(IQRosterPushMapper::class)->disableOriginalConstructor()->getMock(); - $this->dbConnection = \OC::$server->getDatabaseConnection(); - $this->config = \OC::$server->getConfig(); - $this->contactsStore = $this->getMockBuilder(IContactsStore::class)->disableOriginalConstructor()->getMock(); - - $this->userProvider = new ContactsStoreUserProvider( - $this->contactsStore, - $this->userSession, - $this->userManager, - $this->groupManager, - $this->config - ); - - $this->rosterPush = new RosterPush( - $this->userManager, - $this->userSession, - 'localhost', - $this->iqRosterPushMapper, - $this->dbConnection, - $this->userProvider - ); - - foreach (\OC::$server->getUserManager()->search('') as $user) { - $user->delete(); - } - foreach (\OC::$server->getGroupManager()->search('') as $group) { - $group->delete(); - } - } - - public function testRemoveRosterItemForUsersInGroup() - { - $group1 = $this->groupManager->createGroup('group1'); - $group2 = $this->groupManager->createGroup('group2'); - $group3 = $this->groupManager->createGroup('group3'); - $user1 = $this->userManager->createUser('user1', 'user1'); - $user2 = $this->userManager->createUser('user2', 'user2'); - $user3 = $this->userManager->createUser('user3', 'user3'); - $user4 = $this->userManager->createUser('user4', 'user4'); - - $group1->addUser($user1); - $group1->addUser($user2); - $group1->addUser($user3); - $group2->addUser($user1); - $group2->addUser($user2); - $group3->addUser($user4); - - // remove $user1 from $group1 - // when no special settings are set this should result in no rosterMessages - $this->iqRosterPushMapper->expects($this->never())->method('insert'); - $this->contactsStore->expects($this->at(0)) - ->method('findOne') - ->with($user2, 0, 'user1') - ->willReturn([$user2]); - $this->contactsStore->expects($this->at(1)) - ->method('findOne') - ->with($user3, 0, 'user1') - ->willReturn([$user3]); - $this->rosterPush->removeRosterItemForUsersInGroup($group1, 'user1'); - } - public function testRemoveRosterItemForUsersInGroupOwnGroups() - { - $group1 = $this->groupManager->createGroup('group1'); - $group2 = $this->groupManager->createGroup('group2'); - $group3 = $this->groupManager->createGroup('group3'); - $user1 = $this->userManager->createUser('user1', 'user1'); - $user2 = $this->userManager->createUser('user2', 'user2'); - $user3 = $this->userManager->createUser('user3', 'user3'); - $user4 = $this->userManager->createUser('user4', 'user4'); - - $group1->addUser($user1); - $group1->addUser($user2); - $group1->addUser($user3); - $group2->addUser($user1); - $group2->addUser($user2); - $group3->addUser($user4); - - $this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'yes'); - - // remove $user1 from $group1 - // users can only chat with users in their groups - // $user2 should still be reachable by $group2 - // $user3 should be become unreachable - // for $user4 nothing changes - $this->iqRosterPushMapper->expects($this->once())->method('insert'); - - $this->contactsStore->expects($this->at(0)) - ->method('findOne') - ->with($user2, 0, 'user1') - ->willReturn([$user2]); - - $this->contactsStore->expects($this->at(1)) - ->method('findOne') - ->with($user3, 0, 'user1') - ->willReturn(null); - - $this->rosterPush->removeRosterItemForUsersInGroup($group1, 'user1'); - - $this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'no'); - } -} diff --git a/tests/integration/db/IqRosterPushTest.php b/tests/integration/db/IqRosterPushTest.php deleted file mode 100644 index 02ecbb9..0000000 --- a/tests/integration/db/IqRosterPushTest.php +++ /dev/null @@ -1,37 +0,0 @@ -<?php -namespace OCA\OJSXC\Db; - -use Sabre\Xml\Writer; -use OCA\OJSXC\Utility\TestCase; - -class IqRosterPushTest extends TestCase -{ - public function testIqRoster() - { - $expected = '<body xmlns="http://jabber.org/protocol/httpbind"><iq to="jan@localhost" type="set" id="4"><query xmlns="jabber:iq:roster"><item jid="john@localhost" name="john" subscription="both"></item></query></iq></body>'; - - $writer = new Writer(); - $writer->openMemory(); - $writer->startElement('body'); - $writer->writeAttribute('xmlns', 'http://jabber.org/protocol/httpbind'); - - $iqRosterPush = new IQRosterPush(); - $iqRosterPush->setJid('john', 'localhost'); - $iqRosterPush->setTo('jan', 'localhost'); - $iqRosterPush->setName('john'); - $iqRosterPush->setSubscription('both'); - - $this->assertEquals('john@localhost', $iqRosterPush->getJid()); - $this->assertEquals('jan', $iqRosterPush->getUnSanitizedTo()); - $this->assertEquals('jan@localhost', $iqRosterPush->to); - $this->assertEquals('john', $iqRosterPush->getName()); - $this->assertEquals('both', $iqRosterPush->getSubscription()); - - $writer->write($iqRosterPush); // needed to test the xmlSerialize function - - $writer->endElement(); - $result = $writer->outputMemory(); - - $this->assertEquals($expected, $result); - } -} diff --git a/tests/integration/db/IqRosterTest.php b/tests/integration/db/IqRosterTest.php deleted file mode 100644 index 377acc8..0000000 --- a/tests/integration/db/IqRosterTest.php +++ /dev/null @@ -1,57 +0,0 @@ -<?php -namespace OCA\OJSXC\Db; - -use Sabre\Xml\Writer; -use OCA\OJSXC\Utility\TestCase; - -class IqRosterTest extends TestCase -{ - public function testIqRoster() - { - $expected = '<body xmlns="http://jabber.org/protocol/httpbind"><iq to="john@localhost" type="result" id="4434"><query xmlns="jabber:iq:roster"><item jid="test@test.be" name="Test Test" subscription="both"></item><item jid="test2@test.be" name="Test2 Test" subscription="both"></item></query></iq></body>'; - - $writer = new Writer(); - $writer->openMemory(); - $writer->startElement('body'); - $writer->writeAttribute('xmlns', 'http://jabber.org/protocol/httpbind'); - - $iqRoster = new IQRoster(); - $iqRoster->setType('result'); - $iqRoster->setTo('john', 'localhost'); - $iqRoster->setQid(4434); - $iqRoster->addItem('test@test.be', 'Test Test'); - $iqRoster->addItem('test2@test.be', 'Test2 Test'); - - $this->assertEquals('result', $iqRoster->getType()); - $this->assertEquals('john', $iqRoster->getUnSanitizedTo()); - $this->assertEquals('john@localhost', $iqRoster->to); - $this->assertEquals(4434, $iqRoster->getQid()); - $this->assertEquals([ - [ - "name" => "item", - "attributes" => [ - "jid" => "test@test.be", - "name" => "Test Test", - "subscription" => "both" - ], - "value" => '' - ], - [ - "name" => "item", - "attributes" => [ - "jid" => "test2@test.be", - "name" => "Test2 Test", - "subscription" => "both" - ], - "value" => '' - ], - ], $iqRoster->getItems()); - - $writer->write($iqRoster); // needed to test the xmlSerialize function - - $writer->endElement(); - $result = $writer->outputMemory(); - - $this->assertEquals($expected, $result); - } -} diff --git a/tests/integration/db/MessageMapperTest.php b/tests/integration/db/MessageMapperTest.php deleted file mode 100644 index 52ca3b4..0000000 --- a/tests/integration/db/MessageMapperTest.php +++ /dev/null @@ -1,173 +0,0 @@ -<?php - -namespace OCA\OJSXC\Db; - -use OCA\OJSXC\AppInfo\Application; -use OCA\OJSXC\Utility\MapperTestUtility; -use OCP\AppFramework\Db\DoesNotExistException; - -function uniqid() -{ - return 4; // chosen by fair dice roll. - // guaranteed to be unique. -} - -/** - * @group DB - */ -class MessageMapperTest extends MapperTestUtility -{ - - /** - * @var StanzaMapper - */ - protected $mapper; - - protected function setUp(): void - { - $this->entityName = 'OCA\OJSXC\Db\Message'; - $this->mapperName = 'MessageMapper'; - parent::setUp(); - } - - public function insertProvider() - { - return [ - [ - ['john', 'localhost'], - ['thomas', 'localhost'], - 'abcd', - 'test', - 'Test Message', - // save stanza without host or resource - '<message to="thomas" from="john" type="test" xmlns="jabber:client" id="4-msg">Test Message</message>' - ] - ]; - } - - /** - * @dataProvider insertProvider - */ - public function testInsert($from, $to, $data, $type, $msg, $expectedStanza) - { - $stanza = new Message(); - $stanza->setFrom($from[0]); - $stanza->setTo($to[0]); - $stanza->setStanza($data); - $stanza->setType($type); - $stanza->setValue($msg); - - $this->assertEquals($stanza->getUnSanitizedFrom(), $from[0]); - $this->assertEquals($stanza->getUnSanitizedTo(), $to[0]); - $this->assertEquals($stanza->getStanza(), $data); - $this->assertEquals($stanza->getType(), $type); - - $this->mapper->insert($stanza); - - $result = $this->fetchAll(); - - $this->assertCount(1, $result); - $this->assertEquals($stanza->getUnSanitizedFrom(), $result[0]->getFrom()); - $this->assertEquals($stanza->getUnSanitizedTo(), $result[0]->getTo()); - $this->assertEquals($expectedStanza, $result[0]->getStanza()); - $this->assertEquals(null, $result[0]->getType()); // type is saved into the XML string, not the DB. - } - - /** - * @expectedException \OCP\AppFramework\Db\DoesNotExistException - */ - public function testFindByToNotFound() - { - $this->mapper->findByTo('test'); - } - - /** - * @expectedException \OCP\AppFramework\Db\DoesNotExistException - */ - public function testFindByToNotFound2() - { - $stanza = new Message(); - $stanza->setFrom('john', 'localhost'); - $stanza->setTo('john', 'localhost'); - $stanza->setStanza('abcd'); - $stanza->setType('test'); - $stanza->setValue('message abc'); - $this->mapper->insert($stanza); - - $this->mapper->findByTo('test'); - } - - public function testFindByToFoundWithoutAtSign() - { - // when the username doesn't contain a @ the domain is removed and stored as such in the DB - // the resulting stanza then contains the full JID - $stanza1 = new Message(); - $stanza1->setFrom('jan'); - $stanza1->setTo('john'); - $stanza1->setType('test'); - $stanza1->setValue('Messageabc'); - $this->mapper->insert($stanza1); - - $stanza2 = new Message(); - $stanza2->setFrom('thomas'); - $stanza2->setTo('jan'); - $stanza2->setType('test2'); - $stanza2->setValue('Message'); - $this->mapper->insert($stanza2); - - - // check if two elements are inserted - $result = $this->fetchAll(); - $this->assertCount(2, $result); - - // check findByTo - $result = $this->mapper->findByTo('john'); - $this->assertCount(1, $result); - $this->assertEquals('<message to="john@localhost/internal" from="jan@localhost/internal" type="test" xmlns="jabber:client" id="4-msg">Messageabc</message>', $result[0]->getStanza()); - - // check if element is deleted - $result = $this->fetchAll(); - $this->assertCount(1, $result); - $this->assertEquals($stanza2->getUnSanitizedFrom(), $result[0]->getFrom()); - $this->assertEquals($stanza2->getUnSanitizedTo(), $result[0]->getTo()); - $this->assertEquals('<message to="jan" from="thomas" type="test2" xmlns="jabber:client" id="4-msg">Message</message>', $result[0]->getStanza()); // notice that the username isn't replaced by the JID since this tis the task of hte findByTo method - } - - public function testFindByToFoundWithAtSign() - { - // when the username does contain a @ the domain is removed and stored as such in the DB, but with the @ still - // in the username, the resulting stanza then contains the full JID - $stanza1 = new Message(); - $stanza1->setFrom('jan@localhost.com'); - $stanza1->setTo('john@localhost.com'); - $stanza1->setStanza('abcd1'); - $stanza1->setType('test'); - $stanza1->setValue('Messageabc'); - $this->mapper->insert($stanza1); - - $stanza2 = new Message(); - $stanza2->setFrom('thomas@localhost.com'); - $stanza2->setTo('jan@localhost.com'); - $stanza2->setStanza('abcd2'); - $stanza2->setType('test2'); - $stanza2->setValue('Message'); - $this->mapper->insert($stanza2); - - - // check if two elements are inserted - $result = $this->fetchAll(); - $this->assertCount(2, $result); - - // check findByTo - $result = $this->mapper->findByTo('john@localhost.com'); - $this->assertCount(1, $result); - $this->assertEquals('<message to="john_ojsxc_esc_at_localhost.com@localhost/internal" from="jan_ojsxc_esc_at_localhost.com@localhost/internal" type="test" xmlns="jabber:client" id="4-msg">Messageabc</message>', $result[0]->getStanza()); - - // check if element is deleted - $result = $this->fetchAll(); - $this->assertCount(1, $result); - $this->assertEquals($stanza2->getFrom(), $result[0]->getFrom()); - $this->assertEquals($stanza2->getTo(), $result[0]->getTo()); - $this->assertEquals('<message to="jan_ojsxc_esc_at_localhost.com" from="thomas_ojsxc_esc_at_localhost.com" type="test2" xmlns="jabber:client" id="4-msg">Message</message>', $result[0]->getStanza()); - } -} diff --git a/tests/integration/db/PresenceMapperTest.php b/tests/integration/db/PresenceMapperTest.php deleted file mode 100644 index 866b132..0000000 --- a/tests/integration/db/PresenceMapperTest.php +++ /dev/null @@ -1,651 +0,0 @@ -<?php - -namespace OCA\OJSXC\Db; - -use OCA\OJSXC\AppInfo\Application; -use OCA\OJSXC\Db\Presence as PresenceEntity; -use OCA\OJSXC\NewContentContainer; -use OCA\OJSXC\Utility\MapperTestUtility; -use OCA\DAV\AppInfo\Application as DavApp; - -$time = 0; - -function time() -{ - global $time; - return $time; -} - - -/** - * @group DB - */ -class PresenceMapperTest extends MapperTestUtility -{ - - /** - * @var PresenceMapper - */ - protected $mapper; - - /** - * @var NewContentContainer $newContentContainer - */ - protected $newContentContainer; - - protected function setUp(): void - { - $this->entityName = 'OCA\OJSXC\Db\Presence'; - $this->mapperName = 'PresenceMapper'; - parent::setUp(); - $this->setValueOfPrivateProperty($this->mapper, 'updatedPresence', false); - $this->setValueOfPrivateProperty($this->mapper, 'fetchedConnectedUsers', false); - $this->setValueOfPrivateProperty($this->mapper, 'connectedUsers', []); - $this->setValueOfPrivateProperty($this->mapper, 'userId', 'admin'); - $this->newContentContainer = $this->container->query('NewContentContainer'); - $this->setValueOfPrivateProperty($this->newContentContainer, 'stanzas', []); - foreach (\OC::$server->getUserManager()->search('') as $user) { - $user->delete(); - } - } - - protected function tearDown(): void - { - foreach (\OC::$server->getUserManager()->search('') as $user) { - $user->delete(); - } - } - - public function setupContactsStoreAPI() - { - foreach (\OC::$server->getUserManager()->search('') as $user) { - $user->delete(); - } - - $users[] = \OC::$server->getUserManager()->createUser('admin', 'admin'); - $users[] = \OC::$server->getUserManager()->createUser('derp', 'derp'); - $users[] = \OC::$server->getUserManager()->createUser('derpina', 'derpina'); - $users[] = \OC::$server->getUserManager()->createUser('herp', 'herp'); - $users[] = \OC::$server->getUserManager()->createUser('foo', 'foo'); - - $currentUser = \OC::$server->getUserManager()->createUser('autotest', 'autotest'); - \OC::$server->getUserSession()->setUser($currentUser); - - if (Application::contactsStoreApiSupported()) { - /** @var \OCA\DAV\CardDAV\SyncService $syncService */ - $syncService = \OC::$server->query('CardDAVSyncService'); - $syncService->getLocalSystemAddressBook(); - $syncService->updateUser($currentUser); - - foreach ($users as $user) { - $syncService->updateUser($user); - } - - $cm = \OC::$server->getContactsManager(); - $davApp = new DavApp(); - $davApp->setupSystemContactsProvider($cm); - } - \OC_User::setIncognitoMode(false); - \OC::$server->getDatabaseConnection()->executeQuery("DELETE FROM *PREFIX*ojsxc_stanzas"); - } - - /** - * @return array - */ - public function presenceIfNotExitsProvider() - { - $input1 = new PresenceEntity(); - $input1->setPresence('online'); - $input1->setUserid('admin'); - $input1->setLastActive(23434); - - $input2 = new PresenceEntity(); - $input2->setPresence('unavailable'); - $input2->setUserid('derp'); - $input2->setLastActive(23434475); - - - $input3 = new PresenceEntity(); - $input3->setPresence('chat'); - $input3->setUserid('derpina'); - $input3->setLastActive(23445645634); - - return [ - [ - [$input1, $input2, $input3], - [ - [ - 'userid' => 'admin', - 'presence' => 'online', - 'last_active' => 23434, - ], - [ - 'userid' => 'derp', - 'presence' => 'unavailable', - 'last_active' => 23434475, - ], - [ - 'userid' => 'derpina', - 'presence' => 'chat', - 'last_active' => 23445645634 - ] - ] - ] - ]; - } - - /** - * @dataProvider presenceIfNotExitsProvider - * Test setting the presence if it doesn't exits. - * @param PresenceEntity[] $inputs - * @param array $expected - */ - public function testSetPresenceIfNotExists($inputs, $expected) - { - foreach ($inputs as $input) { - $this->mapper->setPresence($input); - } - $result = $this->fetchAllAsArray(); - - $this->assertArrayDbResultsEqual($expected, $result, ['userid', 'presence', 'last_active']); - } - - /** - * @return array - */ - public function presenceIfExitsProvider() - { - $input1 = new PresenceEntity(); - $input1->setPresence('online'); - $input1->setUserid('admin'); - $input1->setLastActive(23434); - - $input2 = new PresenceEntity(); - $input2->setPresence('unavailable'); - $input2->setUserid('derp'); - $input2->setLastActive(23434475); - - $input3 = new PresenceEntity(); - $input3->setPresence('chat'); - $input3->setUserid('derpina'); - $input3->setLastActive(23445645634); - - $input4 = new PresenceEntity(); - $input4->setPresence('chat'); - $input4->setUserid('admin'); - $input4->setLastActive(3234343424); - - $input5 = new PresenceEntity(); - $input5->setPresence('online'); - $input5->setUserid('derp'); - $input5->setLastActive(23434353); - - return [ - [ - [$input1, $input2, $input3, $input4, $input5], - [ - [ - 'userid' => 'admin', - 'presence' => 'chat', - 'last_active' => 3234343424, - ], - [ - 'userid' => 'derp', - 'presence' => 'online', - 'last_active' => 23434353, - ], - [ - 'userid' => 'derpina', - 'presence' => 'chat', - 'last_active' => 23445645634, - ] - ] - ] - ]; - } - - /** - * @dataProvider presenceIfExitsProvider - * Test setting the presence if it doesn't exits. - * @param PresenceEntity[] $inputs - * @param array $expected - */ - public function testSetPresenceIfExists($inputs, $expected) - { - foreach ($inputs as $input) { - $this->mapper->setPresence($input); - } - $result = $this->fetchAllAsArray(); - - - $this->assertArrayDbResultsEqual($expected, $result, ['userid', 'presence', 'last_active']); - } - - public function getPresenceProvider() - { - $input1 = new PresenceEntity(); - $input1->setPresence('online'); - $input1->setUserid('admin'); - $input1->setLastActive(23434); - - $input2 = new PresenceEntity(); - $input2->setPresence('unavailable'); - $input2->setUserid('derp'); - $input2->setLastActive(23434475); - - - $input3 = new PresenceEntity(); - $input3->setPresence('chat'); - $input3->setUserid('derpina'); - $input3->setLastActive(23445645634); - - $input4 = new PresenceEntity(); - $input4->setPresence('chat'); - $input4->setUserid('admin'); - $input4->setLastActive(3234343424); - - $input5 = new PresenceEntity(); - $input5->setPresence('online'); - $input5->setUserid('derp'); - $input5->setLastActive(23434353); - - $expected1 = new PresenceEntity(); - $expected1->setUserid('derp'); - $expected1->setPresence('online'); - $expected1->setLastActive(23434353); - $expected1->setTo('admin', 'localhost/internal'); - $expected1->setFrom('derp', 'localhost/internal'); - - $expected2 = new PresenceEntity(); - $expected2->setUserid('derpina'); - $expected2->setPresence('chat'); - $expected2->setLastActive(23445645634); - $expected2->setTo('admin', 'localhost/internal'); - $expected2->setFrom('derpina', 'localhost/internal'); - - $expected3 = new PresenceEntity(); - $expected3->setUserid('admin'); - $expected3->setPresence('chat'); - $expected3->setLastActive(3234343424); - $expected3->setTo('admin', 'localhost/internal'); - $expected3->setFrom('admin', 'localhost/internal'); - - return [ - [ - [$input1, $input2, $input3, $input4, $input5], - [$expected1, $expected2, $expected3] - ] - ]; - } - - /** - * @dataProvider getPresenceProvider - * @param $inputs - * @param $expected - */ - public function testGetPresence($inputs, $expected) - { - foreach ($inputs as $input) { - $this->mapper->setPresence($input); - } - - $result = $this->mapper->getPresences(); - - $this->assertObjectDbResultsEqual($expected, $result, ['userid', 'presence', 'lastActive', 'to', 'from']); - } - - public function getConnectedUsersProvider() - { - $input1 = new PresenceEntity(); - $input1->setPresence('online'); - $input1->setUserid('admin'); - $input1->setLastActive(23434); - - $input2 = new PresenceEntity(); - $input2->setPresence('unavailable'); - $input2->setUserid('derp'); - $input2->setLastActive(23434475); - - $input3 = new PresenceEntity(); - $input3->setPresence('chat'); - $input3->setUserid('derpina'); - $input3->setLastActive(23445645634); - - $input4 = new PresenceEntity(); - $input4->setPresence('chat'); - $input4->setUserid('admin'); - $input4->setLastActive(3234343424); - - $input5 = new PresenceEntity(); - $input5->setPresence('online'); - $input5->setUserid('derp'); - $input5->setLastActive(23434353); - - $input6 = new PresenceEntity(); - $input6->setPresence('unavailable'); - $input6->setUserid('herp'); - $input6->setLastActive(123); - - return [ - [ - [$input1, $input2, $input3, $input4, $input5, $input6], - ['derp', 'derpina'] - ] - ]; - } - - /** - * @dataProvider getConnectedUsersProvider - */ - public function testGetConnectedUsers($inputs, $expected) - { - $this->setupContactsStoreAPI(); - foreach ($inputs as $input) { - $this->mapper->setPresence($input); - } - - $result = $this->mapper->getConnectedUsers(); - - $this->assertCount(count($expected), $result); - sort($expected); - sort($result); - $this->assertEquals($expected, $result); - } - - public function testGetConnectedUsersIfUserHasNot() - { - $this->setValueOfPrivateProperty($this->mapper, 'connectedUsers', []); - $this->setupContactsStoreAPI(); - if (!Application::contactsStoreApiSupported()) { - $this->markTestSkipped(); - } - - $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(); - $input1->setPresence('online'); - $input1->setUserid('admin'); - $input1->setLastActive(1000); - - $input2 = new PresenceEntity(); - $input2->setPresence('online'); - $input2->setUserid('foo'); - $input2->setLastActive(1000); - - $input3 = new PresenceEntity(); // will go offline - $input3->setPresence('xa'); - $input3->setUserid('derp'); - $input3->setLastActive(600); - - $input4 = new PresenceEntity(); // will go offline - $input4->setPresence('chat'); - $input4->setUserid('derpina'); - $input4->setLastActive(400); - - $expStanza1 = new PresenceEntity(); - $expStanza1->setPresence('unavailable'); - $expStanza1->setFrom('derp', 'localhost/internal'); - $expStanza1->setTo('admin', 'localhost/internal'); - - $expStanza2 = new PresenceEntity(); - $expStanza2->setPresence('unavailable'); - $expStanza2->setFrom('derpina', 'localhost/internal'); - $expStanza2->setTo('admin', 'localhost/internal'); - - return [ - [ - [$input1, $input2, $input3, $input4], - [ - [ - 'userid' => 'admin', - 'presence' => 'online', - 'last_active' => '1000' - ], - [ - 'userid' => 'foo', - 'presence' => 'online', - 'last_active' => '1000' - ], - [ - 'userid' => 'derp', - 'presence' => 'xa', - 'last_active' => '600' - ], - [ - 'userid' => 'derpina', - 'presence' => 'chat', - 'last_active' => '400' - ] - ], - ['foo', 'derp', 'derpina'], - [$expStanza1, $expStanza2], - 2, - [ - [ - "from" => 'derp', - "to" => "foo", - "stanza" => '<presence type="unavailable" from="derp" to="foo" xmlns="jabber:client"/>' - ], - [ - "from" => 'derpina', - "to" => "foo", - "stanza" => '<presence type="unavailable" from="derpina" to="foo" xmlns="jabber:client"/>' - ] - ] - ] - ]; - } - - /** - * @dataProvider updatePresenceProvider - * @param PresenceEntity[] $inputs - * @param array $expInput - * @param array $expConnectedUsers - * @param PresenceEntity[] $expNewContent - * @param int $expNewContentCount - * @param array $expStanzasToSend - */ - public function testUpdatePresence($inputs, $expInput, $expConnectedUsers, $expNewContent, $expNewContentCount, $expStanzasToSend) - { - $this->setupContactsStoreAPI(); - global $time; - $time = 1000; - foreach ($inputs as $input) { - $this->mapper->setPresence($input); - } - $this->assertArrayDbResultsEqual($expInput, $this->fetchAllAsArray(), ['userid', 'presence', 'last_active']); - - $connectedUsers = $this->mapper->getConnectedUsers(); - sort($expConnectedUsers); - sort($connectedUsers); - $this->assertEquals($expConnectedUsers, $connectedUsers); // before cleaning - - $this->mapper->updatePresence(); - - $this->assertEquals($expNewContentCount, $this->newContentContainer->getCount()); - $newContent = $this->newContentContainer->getStanzas(); - sort($expNewContent); - sort($newContent); - $this->assertObjectDbResultsEqual($expNewContent, $newContent, ['userid', 'presence', 'lastActive', 'to', 'from']); - $this->assertEquals(0, $this->newContentContainer->getCount()); // stanzas will be removed once fetched - - $stanzasToSend = $this->fetchAllAsArray('*PREFIX*ojsxc_stanzas'); - - $this->assertArrayDbResultsEqual($expStanzasToSend, $stanzasToSend, ['to', 'from', 'stanza']); - } - - public function setActiveProvider() - { - $input1 = new PresenceEntity(); - $input1->setPresence('online'); - $input1->setUserid('admin'); - $input1->setLastActive(1000); - - $input2 = new PresenceEntity(); - $input2->setPresence('unavailable'); - $input2->setUserid('foo'); - $input2->setLastActive(1000); - - $input3 = new PresenceEntity(); // will go offline - $input3->setPresence('xa'); - $input3->setUserid('derp'); - $input3->setLastActive(600); - - $input4 = new PresenceEntity(); // will go offline - $input4->setPresence('chat'); - $input4->setUserid('derpina'); - $input4->setLastActive(400); - - return [ - [ - [$input1, $input2, $input3, $input4], - [ - [ - 'userid' => 'admin', - 'presence' => 'online', - 'last_active' => '1010', - ], - [ - 'userid' => 'foo', - 'presence' => 'unavailable', - 'last_active' => '1000', - ], - [ - 'userid' => 'derp', - 'presence' => 'xa', - 'last_active' => '1020', - ], - [ - 'userid' => 'derpina', - 'presence' => 'chat', - 'last_active' => '400', - ], - ] - ] - ]; - } - - /** - * @dataProvider setActiveProvider - */ - public function testSetActive($inputs, $expected) - { - global $time; - - foreach ($inputs as $input) { - $this->mapper->setPresence($input); - } - $time = 1010; - $this->mapper->setActive('admin'); - $time = 1020; - $this->mapper->setActive('derp'); - - $result = $this->fetchAllAsArray(); - - $this->assertArrayDbResultsEqual($expected, $result, ['userid', 'last_active', 'presence']); - } - - public function deletePresenceProvider() - { - $input1 = new PresenceEntity(); - $input1->setPresence('online'); - $input1->setUserid('admin'); - $input1->setLastActive(1000); - - $input2 = new PresenceEntity(); - $input2->setPresence('unavailable'); - $input2->setUserid('foo'); - $input2->setLastActive(1000); - - $input3 = new PresenceEntity(); - $input3->setPresence('xa'); - $input3->setUserid('derp'); - $input3->setLastActive(600); - - $input4 = new PresenceEntity(); - $input4->setPresence('chat'); - $input4->setUserid('derpina'); - $input4->setLastActive(400); - - return [ - [ - [$input1, $input2, $input3, $input4], - [ - [ - 'userid' => 'foo', - 'presence' => 'unavailable', - 'last_active' => '1000', - ], - [ - 'userid' => 'derp', - 'presence' => 'xa', - 'last_active' => '600', - ], - [ - 'userid' => 'derpina', - 'presence' => 'chat', - 'last_active' => '400', - ], - ] - ] - ]; - } - - /** - * @dataProvider deletePresenceProvider - */ - - public function testDeletePresence($inputs, $expected) - { - foreach ($inputs as $input) { - $this->mapper->setPresence($input); - } - - $this->mapper->deletePresence('admin'); - - $result = $this->fetchAllAsArray(); - - $this->assertArrayDbResultsEqual($expected, $result, ['userid', 'last_active', 'presence']); - } -} diff --git a/tests/integration/db/PresenceTest.php b/tests/integration/db/PresenceTest.php deleted file mode 100644 index 55b3dd3..0000000 --- a/tests/integration/db/PresenceTest.php +++ /dev/null @@ -1,145 +0,0 @@ -<?php - -namespace OCA\OJSXC\Db; - -use Sabre\Xml\Reader; -use Sabre\Xml\Writer; -use Sabre\Xml\LibXMLException; -use Sabre\Xml\ParseException; -use OCA\OJSXC\Utility\TestCase; - -class PresenceTest extends TestCase -{ - private function generateFactoryData($xml, $from, $to, $presence, $userId) - { - $reader = new Reader(); - $reader->xml($xml); - $reader->elementMap = [ - '{jabber:client}presence' => function (Reader $reader) use ($userId) { - return Presence::createFromXml($reader, $userId); - } - ]; - - $expected = new Presence(); - $expected->setFrom($from); - $expected->setTo($to); - $expected->setPresence($presence); - $expected->setUserid($userId); - - return [ - $reader, - $userId, - $expected - ]; - } - - public function factoryProvider() - { - return [ - $this->generateFactoryData("<presence xmlns='jabber:client' type='unavailable'/>", null, null, 'unavailable', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client' type='unavailable'></presence>", null, null, 'unavailable', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client'/>", null, null, 'online', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client'><test>ack</test></presence>", null, null, 'online', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client'></presence>", null, null, 'online', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client'><show>chat</show></presence>", null, null, 'chat', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client'><show>dnd</show></presence>", null, null, 'dnd', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client'><show>ea</show></presence>", null, null, 'ea', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client'><show>away</show></presence>", null, null, 'away', 'admin'), - $this->generateFactoryData("<presence xmlns='jabber:client'><show>online</show></presence>", null, null, 'online', 'admin'), - ]; - } - - - /** - * @dataProvider factoryProvider - */ - public function testFactory($reader, $userid, Presence $expectedElement) - { - $result = $reader->parse(); - $result = $result['value']; - $this->assertEquals($expectedElement->getUnSanitizedTo(), $result->getTo()); - $this->assertEquals($expectedElement->getUnSanitizedFrom(), $result->getFrom()); - $this->assertEquals($expectedElement->getPresence(), $result->getPresence()); - $this->assertEquals($expectedElement->getUserid(), $result->getUserid()); - } - - - private function generateSerializeData($to, $from, $presence, $expected) - { - $writer = new Writer(); - $writer->openMemory(); - $writer->startElement('body'); - $writer->writeAttribute('xmlns', 'http://jabber.org/protocol/httpbind'); - - $presenceEntity = new Presence(); - $presenceEntity->setPresence($presence); - $presenceEntity->setFrom($from); - $presenceEntity->setTo($to); - - return [ - $writer, - $presenceEntity, - $expected, - $to, - $from, - $presence - ]; - } - public function serializeProvider() - { - return [ - $this->generateSerializeData( - 'admin', - 'derp', - 'chat', - '<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client"><show>chat</show></presence></body>' - ), - $this->generateSerializeData( - 'admin', - 'derp', - 'online', - '<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client" /></body>' - ), - $this->generateSerializeData( - 'admin', - 'derp', - 'away', - '<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client"><show>away</show></presence></body>' - ), - $this->generateSerializeData( - 'admin', - 'derp', - 'unavailable', - '<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client" type="unavailable"/></body>' - ), - $this->generateSerializeData( - 'admin', - 'derp', - 'ea', - '<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client"><show>ea</show></presence></body>' - ), - $this->generateSerializeData( - 'admin', - 'derp', - 'dnd', - '<body xmlns="http://jabber.org/protocol/httpbind"><presence from="derp" to="admin" xmlns="jabber:client"><show>dnd</show></presence></body>' - ), - - ]; - } - - /** - * @dataProvider serializeProvider - */ - public function testSerialize(Writer $writer, Presence $presenceEntity, $expected, $to, $from, $presence) - { - $writer->write($presenceEntity); - $writer->endElement(); - $result = $writer->outputMemory(); - - $this->assertEquals($to, $presenceEntity->getUnSanitizedTo()); - $this->assertEquals($from, $presenceEntity->getUnSanitizedFrom()); - $this->assertEquals($presence, $presenceEntity->getPresence()); - $this->assertSabreXmlEqualsXml($expected, $result); - } -} diff --git a/tests/integration/db/StanzaMapperTest.php b/tests/integration/db/StanzaMapperTest.php deleted file mode 100644 index d07e830..0000000 --- a/tests/integration/db/StanzaMapperTest.php +++ /dev/null @@ -1,145 +0,0 @@ -<?php - -namespace OCA\OJSXC\Db; - -use OCA\OJSXC\Utility\MapperTestUtility; -use OCP\AppFramework\Db\DoesNotExistException; - -/** - * @group DB - */ -class StanzaMapperTest extends MapperTestUtility -{ - - /** - * @var StanzaMapper - */ - protected $mapper; - - protected function setUp(): void - { - $this->entityName = 'OCA\OJSXC\Db\Stanza'; - $this->mapperName = 'StanzaMapper'; - parent::setUp(); - } - - public function insertProvider() - { - return [ - [ - 'john', - 'thomas', - 'abcd' - ] - ]; - } - - /** - * @dataProvider insertProvider - */ - public function testInsert($from, $to, $data) - { - $stanza = new Stanza(); - $stanza->setFrom($from); - $stanza->setTo($to); - $stanza->setStanza($data); - - $this->assertEquals($stanza->getUnSanitizedFrom(), $from); - $this->assertEquals($stanza->getUnSanitizedTo(), $to); - $this->assertEquals($stanza->getStanza(), $data); - - $this->mapper->insert($stanza); - - $result = $this->fetchAll(); - - $this->assertCount(1, $result); - $this->assertEquals($stanza->getUnSanitizedFrom(), $result[0]->getFrom()); - $this->assertEquals($stanza->getUnSanitizedTo(), $result[0]->getTo()); - $this->assertEquals($stanza->getStanza(), $result[0]->getStanza()); - } - - /** - * @expectedException \OCP\AppFramework\Db\DoesNotExistException - */ - public function testFindByToNotFound() - { - $this->mapper->findByTo('test'); - } - - /** - * @expectedException \OCP\AppFramework\Db\DoesNotExistException - */ - public function testFindByToNotFound2() - { - $stanza = new Stanza(); - $stanza->setFrom('john@localhost'); - $stanza->setTo('john@localhost'); - $stanza->setStanza('abcd'); - $this->mapper->insert($stanza); - - $this->mapper->findByTo('test'); - } - - public function testFindByToFound() - { - $stanza1 = new Stanza(); - $stanza1->setFrom('jan'); - $stanza1->setTo('john'); - $stanza1->setStanza('abcd1'); - $this->mapper->insert($stanza1); - - $stanza2 = new Stanza(); - $stanza2->setFrom('thomas'); - $stanza2->setTo('jan'); - $stanza2->setStanza('abcd2'); - $this->mapper->insert($stanza2); - - - // check if two elements are inserted - $result = $this->fetchAll(); - $this->assertCount(2, $result); - - // check findByTo - $result = $this->mapper->findByTo('john'); - $this->assertCount(1, $result); - $this->assertEquals($stanza1->getStanza(), $result[0]->getStanza()); - - // check if element is deleted - $result = $this->fetchAll(); - $this->assertCount(1, $result); - $this->assertEquals($stanza2->getUnSanitizedFrom(), $result[0]->getFrom()); - $this->assertEquals($stanza2->getUnSanitizedTo(), $result[0]->getTo()); - $this->assertEquals($stanza2->getStanza(), $result[0]->getStanza()); - } - - - public function testDeleteByTo() - { - $stanza1 = new Stanza(); - $stanza1->setFrom('jan'); - $stanza1->setTo('john'); - $stanza1->setStanza('abcd1'); - $this->mapper->insert($stanza1); - - $stanza2 = new Stanza(); - $stanza2->setFrom('thomas'); - $stanza2->setTo('jan'); - $stanza2->setStanza('abcd2'); - $this->mapper->insert($stanza2); - - // check if two elements are inserted - $result = $this->fetchAllAsArray(); - $this->assertArrayDbResultsEqual([ - ['from' => 'jan', 'to' => 'john', 'stanza' => 'abcd1'], - ['from' => 'thomas', 'to' => 'jan', 'stanza' => 'abcd2'] - ], $result, ['from', 'to', 'stanza']); - - - $this->mapper->deleteByTo('jan'); - - $result = $this->fetchAllAsArray(); - $this->assertArrayDbResultsEqual([ - ['from' => 'jan', 'to' => 'john', 'stanza' => 'abcd1'] - ], $result, ['from', 'to', 'stanza']); - } -} |