From 10880a1bb634959d874a89dc0d42e31f3ef03bf4 Mon Sep 17 00:00:00 2001 From: Tobia De Koninck Date: Mon, 14 Aug 2017 15:45:38 +0200 Subject: [PATCH] Fix tests --- appinfo/application.php | 3 +- lib/command/refreshroster.php | 10 ++++- tests/integration/db/PresenceMapperTest.php | 41 +++++++++++++++++++++ tests/unit/HooksTest.php | 10 ++++- tests/unit/RosterPushTest.php | 20 +++++++--- tests/unit/stanzahandlers/IQTest.php | 27 +++++++++----- tests/unit/stanzahandlers/MessageTest.php | 19 ++++++++-- 7 files changed, 109 insertions(+), 21 deletions(-) diff --git a/appinfo/application.php b/appinfo/application.php index 2d1646ed..4905c8eb 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -264,7 +264,8 @@ public function __construct(array $urlParams=array()){ $container->registerService('RefreshRosterCommand', function($c) { return new RefreshRoster( $c->query('ServerContainer')->getUserManager(), - $c->query('RosterPush') + $c->query('RosterPush'), + $c->query('PresenceMapper') ); }); diff --git a/lib/command/refreshroster.php b/lib/command/refreshroster.php index e11c7836..77266705 100644 --- a/lib/command/refreshroster.php +++ b/lib/command/refreshroster.php @@ -2,6 +2,7 @@ namespace OCA\OJSXC\Command; +use OCA\OJSXC\Db\PresenceMapper; use OCA\OJSXC\RosterPush; use OCP\IUserManager; use Symfony\Component\Console\Command\Command; @@ -22,13 +23,20 @@ class RefreshRoster extends Command */ private $rosterPush; + /** + * @var PresenceMapper + */ + private $presenceMapper; + public function __construct( IUserManager $userManager, - RosterPush $rosterPush + RosterPush $rosterPush, + PresenceMapper $presenceMapper ) { parent::__construct(); $this->userManager = $userManager; $this->rosterPush = $rosterPush; + $this->presenceMapper = $presenceMapper; } protected function configure() diff --git a/tests/integration/db/PresenceMapperTest.php b/tests/integration/db/PresenceMapperTest.php index 23b1624d..681233f2 100644 --- a/tests/integration/db/PresenceMapperTest.php +++ b/tests/integration/db/PresenceMapperTest.php @@ -41,6 +41,47 @@ protected function setUp() $this->setValueOfPrivateProperty($this->mapper, 'connectedUsers', []); $this->newContentContainer = $this->container->query('NewContentContainer'); $this->setValueOfPrivateProperty($this->newContentContainer, 'stanzas', []); + foreach (\OC::$server->getUserManager()->search('') as $user) { + $user->delete(); + } + } + + protected function tearDown() { + foreach (\OC::$server->getUserManager()->search('') as $user) { + $user->delete(); + } + + } + + /** + * @before + * TODO explciclty call this function + */ + 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'); + + \OC_App::loadApp('dav'); + $currentUser = \OC::$server->getUserManager()->createUser('autotest', 'autotest'); + \OC::$server->getUserSession()->setUser($currentUser); + /** @var \OCA\DAV\CardDAV\SyncService $syncService */ + $syncService = \OC::$server->query('CardDAVSyncService'); + $syncService->getLocalSystemAddressBook(); + $syncService->updateUser($currentUser); + + foreach ($users as $user) { + $syncService->updateUser($user); + } + + \OC::$server->getDatabaseConnection()->executeQuery("DELETE FROM *PREFIX*ojsxc_stanzas"); + } /** diff --git a/tests/unit/HooksTest.php b/tests/unit/HooksTest.php index f8a6f90c..d163a162 100644 --- a/tests/unit/HooksTest.php +++ b/tests/unit/HooksTest.php @@ -44,6 +44,11 @@ class HooksTest extends PHPUnit_Framework_TestCase */ private $stanzaMapper; + /** + * @var PHPUnit_Framework_MockObject_MockObject | \OCP\IGroupManager + */ + private $groupManager; + 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(); @@ -57,12 +62,15 @@ public function setUp() $this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->disableOriginalConstructor()->getMock(); + $this->groupManager = $this->getMockBuilder('OCP\IGroupManager')->disableOriginalConstructor()->setMethods(['listen', 'isBackendUsed', 'addBackend', 'clearBackends', 'get', 'groupExists', 'createGroup', 'search', 'getUserGroups', 'getUserGroupIds', 'displayNamesInGroup', 'isAdmin', 'isInGroup'])->getMock(); + $this->hooks = new Hooks( $this->userManager, $this->userSession, $this->rosterPush, $this->presenceMapper, - $this->stanzaMapper + $this->stanzaMapper, + $this->groupManager ); } diff --git a/tests/unit/RosterPushTest.php b/tests/unit/RosterPushTest.php index 1021a9f0..611b1c8d 100644 --- a/tests/unit/RosterPushTest.php +++ b/tests/unit/RosterPushTest.php @@ -37,6 +37,11 @@ class RosterPushTest extends PHPUnit_Framework_TestCase */ private $db; + /** + * @var \PHPUnit_Framework_MockObject_MockObject | IUserProvider + */ + private $userProvider; + public function setUp() { $this->userManager = $this->getMockBuilder('OCP\IUserManager') @@ -51,12 +56,15 @@ public function setUp() $this->db = $this->getMockBuilder('OCP\IDbConnection') ->disableOriginalConstructor()->getMock(); + $this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock(); + $this->rosterPush = new RosterPush( $this->userManager, $this->userSession, 'localhost', $this->iqRosterPushMapper, - $this->db + $this->db, + $this->userProvider ); } @@ -65,7 +73,7 @@ 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]) + ->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db, $this->userProvider]) ->setMethods(['createOrUpdateRosterItem', 'removeRosterItem'])->getMock(); $user1 = $this->getMockBuilder('OCP\IUser')->getMock(); @@ -140,7 +148,7 @@ 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]) + ->setConstructorArgs([$this->userManager, $this->userSession, 'host', $this->iqRosterPushMapper, $this->db, $this->userProvider]) ->setMethods(['createOrUpdateRosterItem', 'removeRosterItem'])->getMock(); $user1 = $this->getMockBuilder('OCP\IUser')->getMock(); @@ -218,7 +226,7 @@ public function testRemoveRosterItem() public function testCreateOrUpdateRosterItem() { $user1 = $this->getMockBuilder('OCP\IUser')->getMock(); - $user1->expects($this->exactly(5)) + $user1->expects($this->exactly(6)) ->method('getUID') ->willReturn('user1'); $user2 = $this->getMockBuilder('OCP\IUser')->getMock(); @@ -230,8 +238,8 @@ public function testCreateOrUpdateRosterItem() ->method('getUID') ->willReturn('user3'); - $this->userManager->expects($this->once()) - ->method('search') + $this->userProvider->expects($this->once()) + ->method('getAllUsersForUserByUID') ->willReturn([$user1, $user2, $user3]); $stanza1 = new IQRosterPush(); diff --git a/tests/unit/stanzahandlers/IQTest.php b/tests/unit/stanzahandlers/IQTest.php index 6c15c562..db8401df 100644 --- a/tests/unit/stanzahandlers/IQTest.php +++ b/tests/unit/stanzahandlers/IQTest.php @@ -3,7 +3,10 @@ namespace OCA\OJSXC\StanzaHandlers; use OCA\OJSXC\Db\IQRoster; +use OCA\OJSXC\IUserProvider; +use OCA\OJSXC\User; use OCP\IConfig; +use OCP\IUserManager; use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_TestCase; @@ -16,7 +19,7 @@ class IQTest extends PHPUnit_Framework_TestCase private $iq; /** - * @var PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject | IUserManager */ private $userManager; @@ -35,31 +38,38 @@ class IQTest extends PHPUnit_Framework_TestCase */ private $config; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | IUserProvider + */ + private $userProvider; + public function setUp() { $this->host = 'localhost'; $this->userId = 'john'; $this->userManager = $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(); $this->config = $this->getMockBuilder('OCP\IConfig')->disableOriginalConstructor()->getMock(); - $this->iq = new IQ($this->userId, $this->host, $this->userManager, $this->config); + $this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock(); + $this->iq = new IQ($this->userId, $this->host, $this->userManager, $this->config, $this->userProvider); } public function iqRosterProvider() { - $user1 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); + $user1 = $this->getMockBuilder(User::class)->disableOriginalConstructor()->getMock(); $user1->expects($this->any()) ->method('getUID') ->will($this->returnValue('john')); $user1->expects($this->any()) - ->method('getDisplayName') + ->method('getFullName') ->will($this->returnValue('John')); - $user2 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); + $user2 = $this->getMockBuilder(User::class)->disableOriginalConstructor()->getMock(); $user2->expects($this->any()) ->method('getUID') ->will($this->returnValue('richard')); $user2->expects($this->any()) - ->method('getDisplayName') + ->method(getFullName) ->will($this->returnValue('Richard')); @@ -156,9 +166,8 @@ public function testIqRoster(array $stanza, array $users, $searchCount, $expecte ->with('debug') ->will($this->returnValue(false)); - $this->userManager->expects($searchCount) - ->method('search') - ->with('') + $this->userProvider->expects($searchCount) + ->method('getAllUsers') ->will($this->returnValue($users)); $result = $this->iq->handle($stanza); diff --git a/tests/unit/stanzahandlers/MessageTest.php b/tests/unit/stanzahandlers/MessageTest.php index 01670d01..deeb4e62 100644 --- a/tests/unit/stanzahandlers/MessageTest.php +++ b/tests/unit/stanzahandlers/MessageTest.php @@ -3,6 +3,8 @@ namespace OCA\OJSXC\StanzaHandlers; use OCA\OJSXC\Db\Message as MessageEntity; +use OCA\OJSXC\Db\MessageMapper; +use OCA\OJSXC\IUserProvider; use PHPUnit_Framework_TestCase; use PHPUnit_Framework_MockObject_MockObject; @@ -15,7 +17,7 @@ class MessageTest extends PHPUnit_Framework_TestCase private $message; /** - * @var PHPUnit_Framework_MockObject_MockObject + * @var PHPUnit_Framework_MockObject_MockObject | MessageMapper */ private $messageMapper; @@ -25,16 +27,22 @@ class MessageTest extends PHPUnit_Framework_TestCase private $userId; /** - * @var string $host ; + * @var string $host */ private $host; + /** + * @var PHPUnit_Framework_MockObject_MockObject | IUserProvider + */ + private $userProvider; + public function setUp() { $this->host = 'localhost'; $this->userId = 'john'; $this->messageMapper = $this->getMockBuilder('OCA\OJSXC\Db\MessageMapper')->disableOriginalConstructor()->getMock(); - $this->message = new Message($this->userId, $this->host, $this->messageMapper); + $this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock(); + $this->message = new Message($this->userId, $this->host, $this->messageMapper, $this->userProvider); } public function messageProvider() @@ -87,6 +95,11 @@ public function testMessage(array $stanza, $expected) ->method('insert') ->with($expected); + $this->userProvider->expects($this->once()) + ->method('hasUserByUID') + ->with('derp') + ->willReturn(true); // TODO test return false + $this->message->handle($stanza); } }