diff options
author | Tobia De Koninck <tobia@ledfan.be> | 2017-07-30 15:20:17 +0300 |
---|---|---|
committer | Tobia De Koninck <tobia@ledfan.be> | 2017-07-30 15:22:34 +0300 |
commit | ef2703dad6f26b1590384f724ba35e319b0a9104 (patch) | |
tree | ab34ad11d2e1db621c0b7794f452b692cbb92cda /tests | |
parent | bcad36c33adbade5dd84ba6ef2a38969e36fe53e (diff) |
Small fixes + test hooks, rosterpush and rosterrefresh
Diffstat (limited to 'tests')
-rw-r--r-- | tests/integration/db/IqRosterPushTest.php | 36 | ||||
-rw-r--r-- | tests/integration/db/PresenceMapperTest.php | 63 | ||||
-rw-r--r-- | tests/integration/db/StanzaMapperTest.php | 31 | ||||
-rw-r--r-- | tests/unit/HooksTest.php | 160 | ||||
-rw-r--r-- | tests/unit/RosterPushTest.php | 259 |
5 files changed, 549 insertions, 0 deletions
diff --git a/tests/integration/db/IqRosterPushTest.php b/tests/integration/db/IqRosterPushTest.php new file mode 100644 index 0000000..e6e8c0d --- /dev/null +++ b/tests/integration/db/IqRosterPushTest.php @@ -0,0 +1,36 @@ +<?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($iqRosterPush->getJid(), 'john@localhost'); + $this->assertEquals($iqRosterPush->getTo(), 'jan@localhost'); + $this->assertEquals($iqRosterPush->getName(), 'john'); + $this->assertEquals($iqRosterPush->getSubscription(), 'both'); + + $writer->write($iqRosterPush); // needed to test the xmlSerialize function + + $writer->endElement(); + $result = $writer->outputMemory(); + + $this->assertEquals($expected, $result); + } +} diff --git a/tests/integration/db/PresenceMapperTest.php b/tests/integration/db/PresenceMapperTest.php index 11968c1..23b1624 100644 --- a/tests/integration/db/PresenceMapperTest.php +++ b/tests/integration/db/PresenceMapperTest.php @@ -480,4 +480,67 @@ class PresenceMapperTest extends MapperTestUtility $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/StanzaMapperTest.php b/tests/integration/db/StanzaMapperTest.php index a2c09b0..279b2a1 100644 --- a/tests/integration/db/StanzaMapperTest.php +++ b/tests/integration/db/StanzaMapperTest.php @@ -111,4 +111,35 @@ class StanzaMapperTest extends MapperTestUtility $this->assertEquals($stanza2->getTo(), $result[0]->getTo()); $this->assertEquals($stanza2->getStanza(), $result[0]->getStanza()); } + + + public function testDeleteByTo() + { + $stanza1 = new Stanza(); + $stanza1->setFrom('jan@localhost'); + $stanza1->setTo('john@localhost'); + $stanza1->setStanza('abcd1'); + $this->mapper->insert($stanza1); + + $stanza2 = new Stanza(); + $stanza2->setFrom('thomas@localhost'); + $stanza2->setTo('jan@localhost'); + $stanza2->setStanza('abcd2'); + $this->mapper->insert($stanza2); + + // check if two elements are inserted + $result = $this->fetchAllAsArray(); + $this->assertArrayDbResultsEqual([ + ['from' => 'jan@localhost', 'to' => 'john@localhost', 'stanza' => 'abcd1'], + ['from' => 'thomas@localhost', 'to' => 'jan@localhost', 'stanza' => 'abcd2'] + ], $result, ['from', 'to', 'stanza']); + + + $this->mapper->deleteByTo('jan@localhost'); + + $result = $this->fetchAllAsArray(); + $this->assertArrayDbResultsEqual([ + ['from' => 'jan@localhost', 'to' => 'john@localhost', 'stanza' => 'abcd1'] + ], $result, ['from', 'to', 'stanza']); + } } diff --git a/tests/unit/HooksTest.php b/tests/unit/HooksTest.php new file mode 100644 index 0000000..f8a6f90 --- /dev/null +++ b/tests/unit/HooksTest.php @@ -0,0 +1,160 @@ +<?php + + +namespace OCA\OJSXC; + +use OCA\OJSXC\Db\Presence; +use OCA\OJSXC\Db\PresenceMapper; +use OCA\OJSXC\Db\StanzaMapper; +use OCP\IUserManager; +use OCP\IUserSession; +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + +class HooksTest extends PHPUnit_Framework_TestCase +{ + + /** + * @var Hooks $hooks + */ + private $hooks; + + /** + * @var PHPUnit_Framework_MockObject_MockObject | IUserManager + */ + private $userManager; + + /** + * @var PHPUnit_Framework_MockObject_MockObject | IuserSession + */ + private $userSession; + + /** + * @var PHPUnit_Framework_MockObject_MockObject | RosterPush + */ + private $rosterPush; + + /** + * @var PHPUnit_Framework_MockObject_MockObject | PresenceMapper + */ + private $presenceMapper; + + /** + * @var PHPUnit_Framework_MockObject_MockObject | StanzaMapper + */ + private $stanzaMapper; + + public function setUp() + { + $this->userManager = $this->getMockBuilder('OCP\IUserManager')->setMethods(['listen', 'registerBackend', 'getBackends', 'removeBackend', 'clearBackends', 'get', 'userExists', 'checkPassword', 'search', 'searchDisplayName', 'createUser', 'createUserFromBackend', 'countUsers', 'callForAllUsers', 'countDisabledUsers', 'countSeenUsers', 'callForSeenUsers', 'getByEmail'])->getMock(); + + $this->userSession = $this->getMockBuilder('OCP\IUserSession')->setMethods(['listen', 'login', 'logout', 'setUser', 'getUser', 'isLoggedIn'])->getMock(); + + + $this->rosterPush = $this->getMockBuilder('OCA\OJSXC\RosterPush')->disableOriginalConstructor()->getMock(); + + $this->presenceMapper = $this->getMockBuilder('OCA\OJSXC\Db\PresenceMapper')->disableOriginalConstructor()->getMock(); + + $this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->disableOriginalConstructor()->getMock(); + + $this->hooks = new Hooks( + $this->userManager, + $this->userSession, + $this->rosterPush, + $this->presenceMapper, + $this->stanzaMapper + ); + } + + + public function testRegister() + { + $this->userManager->expects($this->at(0)) + ->method('listen') + ->with('\OC\User', 'postCreateUser', [$this->hooks, 'onCreateUser']); + + $this->userManager->expects($this->at(1)) + ->method('listen') + ->with('\OC\User', 'postDelete', [$this->hooks, 'onDeleteUser']); + + $this->userSession->expects($this->once()) + ->method('listen') + ->with('\OC\User', 'changeUser', [$this->hooks, 'onChangeUser']); + + $this->hooks->register(); + } + + public function testOnCreateUser() + { + $user = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); + + $this->rosterPush->expects($this->once()) + ->method('createOrUpdateRosterItem') + ->with($user); + + $this->hooks->onCreateUser($user, 'abc'); + } + + public function testOnDeleteUser() + { + $user = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); + + $user->expects($this->exactly(3)) + ->method('getUID') + ->willReturn('test'); + + $this->rosterPush->expects($this->once()) + ->method('removeRosterItem') + ->with('test'); + + $this->presenceMapper->expects($this->once()) + ->method('deletePresence') + ->with('test'); + + $this->stanzaMapper->expects($this->once()) + ->method('deleteByTo') + ->with('test'); + + $this->hooks->onDeleteUser($user); + } + + + public function testOnChangeUserEnabled() + { + $user = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); + + $hooks = $this->getMockBuilder('OCA\OJSXC\Hooks')->disableOriginalConstructor()->setMethods(['onCreateUser'])->getMock(); + + $hooks->expects($this->once()) + ->method('onCreateUser') + ->with($user, ''); + + $hooks->onChangeUser($user, 'enabled', 'true'); + } + + public function testOnChangeUserDisabled() + { + $user = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); + + $hooks = $this->getMockBuilder('OCA\OJSXC\Hooks')->disableOriginalConstructor()->setMethods(['onDeleteUser'])->getMock(); + + $hooks->expects($this->once()) + ->method('onDeleteUser') + ->with($user); + + $hooks->onChangeUser($user, 'enabled', 'false'); + } + + public function testOnChangeUserDisplayName() + { + $user = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); + + $hooks = $this->getMockBuilder('OCA\OJSXC\Hooks')->disableOriginalConstructor()->setMethods(['onCreateUser'])->getMock(); + + $hooks->expects($this->once()) + ->method('onCreateUser') + ->with($user); + + $hooks->onChangeUser($user, 'displayName', 'abc'); + } +} diff --git a/tests/unit/RosterPushTest.php b/tests/unit/RosterPushTest.php new file mode 100644 index 0000000..1021a9f --- /dev/null +++ b/tests/unit/RosterPushTest.php @@ -0,0 +1,259 @@ +<?php + +namespace OCA\OJSXC; + +use OCA\OJSXC\Db\IQRosterPush; +use OCA\OJSXC\Db\IQRosterPushMapper; +use OCP\IDBConnection; +use OCP\IUserManager; +use OCP\IUserSession; +use PHPUnit_Framework_TestCase; + +class RosterPushTest extends PHPUnit_Framework_TestCase +{ + + /** + * @var RosterPush + */ + private $rosterPush; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | IUserManager + */ + private $userManager; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | IUserSession + */ + private $userSession; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | IQRosterPushMapper + */ + private $iqRosterPushMapper; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | IDBConnection + */ + private $db; + + public function setUp() + { + $this->userManager = $this->getMockBuilder('OCP\IUserManager') + ->disableOriginalConstructor()->getMock(); + + $this->userSession = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor()->getMock(); + + $this->iqRosterPushMapper = $this->getMockBuilder('OCA\OJSXC\Db\IQRosterPushMapper') + ->disableOriginalConstructor()->getMock(); + + $this->db = $this->getMockBuilder('OCP\IDbConnection') + ->disableOriginalConstructor()->getMock(); + + $this->rosterPush = new RosterPush( + $this->userManager, + $this->userSession, + 'localhost', + $this->iqRosterPushMapper, + $this->db + ); + } + + public function testRefreshRoster() + { + + /** @var \PHPUnit_Framework_MockObject_MockObject | RosterPush $rosterPush */ + $rosterPush = $this->getMockBuilder('OCA\OJSXC\RosterPush') + ->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db]) + ->setMethods(['createOrUpdateRosterItem', 'removeRosterItem'])->getMock(); + + $user1 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user2 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user3 = $this->getMockBuilder('OCP\IUser')->getMock(); + + $this->userManager->expects($this->once()) + ->method('search') + ->willReturn([$user1, $user2, $user3]); + + $rosterPush->expects($this->at(0)) + ->method('createOrUpdateRosterItem') + ->with($user1); + + $rosterPush->expects($this->at(1)) + ->method('createOrUpdateRosterItem') + ->with($user2); + + $rosterPush->expects($this->at(2)) + ->method('createOrUpdateRosterItem') + ->with($user3); + + $resultStatement = $this->getMockBuilder('Doctrine\DBAL\Driver\ResultStatement')->getMock(); + + $resultStatement->expects($this->at(0)) + ->method('fetchAll') + ->willReturn([["id" => 10]]); + + $resultStatement->expects($this->at(1)) + ->method('fetchAll') + ->willReturn([ + ["uri" => 'Database:user1.vcf'], + ["uri" => 'Database:user2.vcf'], + ["uri" => 'Database:user3.vcf'], + ["uri" => 'Database:user4.vcf'] + ]); + + $this->db->expects($this->at(0)) + ->method('executeQuery') + ->with('SELECT `id` FROM `*PREFIX*addressbooks` WHERE `principaluri`=\'principals/system/system\' LIMIT 1') + ->willReturn($resultStatement); + + $this->db->expects($this->at(1)) + ->method('executeQuery') + ->with('SELECT `uri` FROM `*PREFIX*addressbookchanges` AS ac1 WHERE `addressbookid` = ? AND `operation` = 3 AND `id`=(SELECT MAX(id) FROM `*PREFIX*addressbookchanges` AS ac2 WHERE `uri`=ac1.uri)', [10]) + ->willReturn($resultStatement); + + + $rosterPush->expects($this->at(3)) + ->method('removeRosterItem') + ->with('user1'); + + $rosterPush->expects($this->at(4)) + ->method('removeRosterItem') + ->with('user2'); + + $rosterPush->expects($this->at(5)) + ->method('removeRosterItem') + ->with('user3'); + + $rosterPush->expects($this->at(6)) + ->method('removeRosterItem') + ->with('user4'); + + $stats = $rosterPush->refreshRoster(); + + $this->assertEquals($stats, ["removed" => 4, "updated" => 3]); + } + + public function testRefreshRosterThrowsDuringRemove() + { + + /** @var \PHPUnit_Framework_MockObject_MockObject | RosterPush $rosterPush */ + $rosterPush = $this->getMockBuilder('OCA\OJSXC\RosterPush') + ->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db]) + ->setMethods(['createOrUpdateRosterItem', 'removeRosterItem'])->getMock(); + + $user1 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user2 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user3 = $this->getMockBuilder('OCP\IUser')->getMock(); + + $this->userManager->expects($this->once()) + ->method('search') + ->willReturn([$user1, $user2, $user3]); + + $rosterPush->expects($this->at(0)) + ->method('createOrUpdateRosterItem') + ->with($user1); + + $rosterPush->expects($this->at(1)) + ->method('createOrUpdateRosterItem') + ->with($user2); + + $rosterPush->expects($this->at(2)) + ->method('createOrUpdateRosterItem') + ->with($user3); + + $this->db->expects($this->at(0)) + ->method('executeQuery') + ->with('SELECT `id` FROM `*PREFIX*addressbooks` WHERE `principaluri`=\'principals/system/system\' LIMIT 1') + ->willThrowException(new \Exception("A random exception")); + + $stats = $rosterPush->refreshRoster(); + + $this->assertEquals($stats, ["removed" => 0, "updated" => 3]); + } + + public function testRemoveRosterItem() + { + $user1 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user1->expects($this->once()) + ->method('getUID') + ->willReturn('user1'); + $user2 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user2->expects($this->exactly(2)) + ->method('getUID') + ->willReturn('user2'); + $user3 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user3->expects($this->exactly(2)) + ->method('getUID') + ->willReturn('user3'); + + $this->userManager->expects($this->once()) + ->method('search') + ->willReturn([$user1, $user2, $user3]); + + $stanza1 = new IQRosterPush(); + $stanza1->setJid('user1'); + $stanza1->setSubscription('remove'); + $stanza1->setFrom(''); + $stanza1->setTo('user2'); + + $stanza2 = new IQRosterPush(); + $stanza2->setJid('user1'); + $stanza2->setSubscription('remove'); + $stanza2->setFrom(''); + $stanza2->setTo('user3'); + + $this->iqRosterPushMapper->expects($this->at(0)) + ->method('insert') + ->with($stanza1); + + $this->iqRosterPushMapper->expects($this->at(1)) + ->method('insert') + ->with($stanza2); + + $this->rosterPush->removeRosterItem('user1'); + } + + public function testCreateOrUpdateRosterItem() + { + $user1 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user1->expects($this->exactly(5)) + ->method('getUID') + ->willReturn('user1'); + $user2 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user2->expects($this->exactly(2)) + ->method('getUID') + ->willReturn('user2'); + $user3 = $this->getMockBuilder('OCP\IUser')->getMock(); + $user3->expects($this->exactly(2)) + ->method('getUID') + ->willReturn('user3'); + + $this->userManager->expects($this->once()) + ->method('search') + ->willReturn([$user1, $user2, $user3]); + + $stanza1 = new IQRosterPush(); + $stanza1->setJid('user1'); + $stanza1->setSubscription('both'); + $stanza1->setFrom(''); + $stanza1->setTo('user2'); + + $stanza2 = new IQRosterPush(); + $stanza2->setJid('user1'); + $stanza2->setSubscription('both'); + $stanza2->setFrom(''); + $stanza2->setTo('user3'); + + $this->iqRosterPushMapper->expects($this->at(0)) + ->method('insert') + ->with($stanza1); + + $this->iqRosterPushMapper->expects($this->at(1)) + ->method('insert') + ->with($stanza2); + + $this->rosterPush->createOrUpdateRosterItem($user1); + } +} |