diff options
author | sualko <klaus@jsxc.org> | 2017-08-02 14:14:19 +0300 |
---|---|---|
committer | sualko <klaus@jsxc.org> | 2017-08-02 14:14:19 +0300 |
commit | 8a7b6e8751fb04b0aa9a5bd79f17bdbdcad97432 (patch) | |
tree | cf53a10d374d444e94995bf287ca852d58e9745c /tests/unit | |
parent | 5789cdd81afdf5a189da88e7a600aa0ae802e583 (diff) | |
parent | e3793e3099c2d6e3b6750ab4b10aea89452e4054 (diff) |
Merge branch 'master' of github.com:nextcloud/jsxc.nextcloud
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/HooksTest.php | 160 | ||||
-rw-r--r-- | tests/unit/RosterPushTest.php | 259 | ||||
-rw-r--r-- | tests/unit/stanzahandlers/IQTest.php | 14 |
3 files changed, 432 insertions, 1 deletions
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); + } +} diff --git a/tests/unit/stanzahandlers/IQTest.php b/tests/unit/stanzahandlers/IQTest.php index 8ab22be..6c15c56 100644 --- a/tests/unit/stanzahandlers/IQTest.php +++ b/tests/unit/stanzahandlers/IQTest.php @@ -3,6 +3,7 @@ namespace OCA\OJSXC\StanzaHandlers; use OCA\OJSXC\Db\IQRoster; +use OCP\IConfig; use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_TestCase; @@ -29,12 +30,18 @@ class IQTest extends PHPUnit_Framework_TestCase */ private $host; + /** + * @var PHPUnit_Framework_MockObject_MockObject | IConfig + */ + private $config; + public function setUp() { $this->host = 'localhost'; $this->userId = 'john'; $this->userManager = $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(); - $this->iq = new IQ($this->userId, $this->host, $this->userManager); + $this->config = $this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock(); + $this->iq = new IQ($this->userId, $this->host, $this->userManager, $this->config); } public function iqRosterProvider() @@ -144,6 +151,11 @@ class IQTest extends PHPUnit_Framework_TestCase */ public function testIqRoster(array $stanza, array $users, $searchCount, $expected) { + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with('debug') + ->will($this->returnValue(false)); + $this->userManager->expects($searchCount) ->method('search') ->with('') |