From 3a880879ce373e490e0964b0db503c7d098cb43e Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Thu, 17 Nov 2016 10:27:46 +0100 Subject: [PATCH] update unit tests Signed-off-by: Bjoern Schiessle --- apps/dav/tests/unit/CardDAV/ConverterTest.php | 138 ++++++++++++------ .../tests/unit/CardDAV/SyncServiceTest.php | 50 ++++++- apps/dav/tests/unit/DAV/HookManagerTest.php | 13 +- 3 files changed, 145 insertions(+), 56 deletions(-) diff --git a/apps/dav/tests/unit/CardDAV/ConverterTest.php b/apps/dav/tests/unit/CardDAV/ConverterTest.php index 7737b999507..737bbd96aaa 100644 --- a/apps/dav/tests/unit/CardDAV/ConverterTest.php +++ b/apps/dav/tests/unit/CardDAV/ConverterTest.php @@ -24,79 +24,121 @@ namespace OCA\DAV\Tests\unit\CardDAV; +use OC\Accounts\AccountManager; use OCA\DAV\CardDAV\Converter; +use OCP\IDBConnection; use OCP\IImage; use OCP\IUser; +use OpenCloud\ObjectStore\Resource\Account; use PHPUnit_Framework_MockObject_MockObject; +use Symfony\Component\EventDispatcher\EventDispatcher; use Test\TestCase; class ConverterTest extends TestCase { - /** - * @dataProvider providesNewUsers - */ - public function testCreation($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null) { - $user = $this->getUserMock($displayName, $eMailAddress, $cloudId); + /** @var AccountManager | PHPUnit_Framework_MockObject_MockObject */ + private $accountManager; - $converter = new Converter(); - $vCard = $converter->createCardFromUser($user); - $cardData = $vCard->serialize(); + /** @var EventDispatcher | PHPUnit_Framework_MockObject_MockObject */ + private $eventDispatcher; - $this->assertEquals($expectedVCard, $cardData); - } + /** @var IDBConnection | PHPUnit_Framework_MockObject_MockObject */ + private $databaseConnection; - public function providesNewUsers() { - return [ - ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU2Nzg5\r\nEND:VCARD\r\n"], - ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU2Nzg5\r\nEND:VCARD\r\n", "Dr. Foo Bar"], - ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nEMAIL;TYPE=OTHER:foo@bar.net\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU2Nzg5\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"], - ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nCLOUD:foo@bar.net\r\nPHOTO;ENCODING=b;TYPE=JPEG:MTIzNDU2Nzg5\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"], - ]; + public function setUp() { + parent::setUp(); + $this->databaseConnection = $this->getMockBuilder('OCP\IDBConnection')->getMock(); + $this->eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') + ->disableOriginalConstructor()->getMock(); + $this->accountManager = $this->getMockBuilder('OC\Accounts\AccountManager') + ->disableOriginalConstructor()->getMock(); } - /** - * @dataProvider providesNewUsers - */ - public function testUpdateOfUnchangedUser($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null) { - $user = $this->getUserMock($displayName, $eMailAddress, $cloudId); - - $converter = new Converter(); - $vCard = $converter->createCardFromUser($user); - $updated = $converter->updateCard($vCard, $user); - $this->assertFalse($updated); - $cardData = $vCard->serialize(); - - $this->assertEquals($expectedVCard, $cardData); + public function getAccountManager(IUser $user) { + $accountManager = $this->getMockBuilder('OC\Accounts\AccountManager') + ->disableOriginalConstructor()->getMock(); + $accountManager->expects($this->any())->method('getUser')->willReturn( + [ + AccountManager::PROPERTY_DISPLAYNAME => + [ + 'value' => $user->getDisplayName(), + 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY, + ], + AccountManager::PROPERTY_ADDRESS => + [ + 'value' => '', + 'scope' => AccountManager::VISIBILITY_PRIVATE, + ], + AccountManager::PROPERTY_WEBSITE => + [ + 'value' => '', + 'scope' => AccountManager::VISIBILITY_PRIVATE, + ], + AccountManager::PROPERTY_EMAIL => + [ + 'value' => $user->getEMailAddress(), + 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY, + ], + AccountManager::PROPERTY_AVATAR => + [ + 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY + ], + AccountManager::PROPERTY_PHONE => + [ + 'value' => '', + 'scope' => AccountManager::VISIBILITY_PRIVATE, + ], + AccountManager::PROPERTY_TWITTER => + [ + 'value' => '', + 'scope' => AccountManager::VISIBILITY_PRIVATE, + ], + ] + ); + + return $accountManager; } /** - * @dataProvider providesUsersForUpdateOfRemovedElement + * @dataProvider providesNewUsers */ - public function testUpdateOfRemovedElement($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null) { + public function testCreation($expectedVCard, $displayName = null, $eMailAddress = null, $cloudId = null) { $user = $this->getUserMock($displayName, $eMailAddress, $cloudId); + $accountManager = $this->getAccountManager($user); - $converter = new Converter(); + $converter = new Converter($accountManager); $vCard = $converter->createCardFromUser($user); + if ($expectedVCard !== null) { + $this->assertInstanceOf('Sabre\VObject\Component\VCard', $vCard); + $cardData = $vCard->jsonSerialize(); + $this->compareData($expectedVCard, $cardData); - $user1 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); - $user1->method('getUID')->willReturn('12345'); - $user1->method('getDisplayName')->willReturn(null); - $user1->method('getEMailAddress')->willReturn(null); - $user1->method('getCloudId')->willReturn(null); - $user1->method('getAvatarImage')->willReturn(null); + } else { + $this->assertSame($expectedVCard, $vCard); + } - $updated = $converter->updateCard($vCard, $user1); - $this->assertTrue($updated); - $cardData = $vCard->serialize(); + } - $this->assertEquals($expectedVCard, $cardData); + protected function compareData($expected, $data) { + foreach ($expected as $key => $value) { + $found = false; + foreach ($data[1] as $d) { + if($d[0] === $key && $d[3] === $value) { + $found = true; + break; + } + } + if (!$found) $this->assertTrue(false, 'Expected data: ' . $key . ' not found.'); + } } - public function providesUsersForUpdateOfRemovedElement() { + public function providesNewUsers() { return [ - ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar"], - ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"], - ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"], + [null], + [null, null, 'foo@bar.net'], + [['cloud' => 'foo@cloud.net', 'email' => 'foo@bar.net'], null, 'foo@bar.net', 'foo@cloud.net'], + [['cloud' => 'foo@cloud.net', 'email' => 'foo@bar.net', 'fn' => 'Dr. Foo Bar'], "Dr. Foo Bar", "foo@bar.net", 'foo@cloud.net'], + [['cloud' => 'foo@cloud.net', 'fn' => 'Dr. Foo Bar'], "Dr. Foo Bar", null, "foo@cloud.net"], ]; } @@ -107,7 +149,7 @@ class ConverterTest extends TestCase { */ public function testNameSplitter($expected, $fullName) { - $converter = new Converter(); + $converter = new Converter($this->accountManager); $r = $converter->splitFullName($fullName); $r = implode(';', $r); $this->assertEquals($expected, $r); diff --git a/apps/dav/tests/unit/CardDAV/SyncServiceTest.php b/apps/dav/tests/unit/CardDAV/SyncServiceTest.php index e6a5ac1f16a..68345def66b 100644 --- a/apps/dav/tests/unit/CardDAV/SyncServiceTest.php +++ b/apps/dav/tests/unit/CardDAV/SyncServiceTest.php @@ -25,6 +25,7 @@ namespace OCA\DAV\Tests\unit\CardDAV; +use OC\Accounts\AccountManager; use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\SyncService; use OCP\IUser; @@ -76,7 +77,8 @@ class SyncServiceTest extends TestCase { /** @var IUserManager $userManager */ $userManager = $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(); $logger = $this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock(); - $ss = new SyncService($backend, $userManager, $logger); + $accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock(); + $ss = new SyncService($backend, $userManager, $logger, $accountManager); $book = $ss->ensureSystemAddressBookExists('principals/users/adam', 'contacts', []); } @@ -100,8 +102,47 @@ class SyncServiceTest extends TestCase { $user = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock(); $user->method('getBackendClassName')->willReturn('unittest'); $user->method('getUID')->willReturn('test-user'); - - $ss = new SyncService($backend, $userManager, $logger); + $user->method('getCloudId')->willReturn('cloudId'); + $accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock(); + $accountManager->expects($this->any())->method('getUser') + ->willReturn([ + AccountManager::PROPERTY_DISPLAYNAME => + [ + 'value' => $user->getDisplayName(), + 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY, + ], + AccountManager::PROPERTY_ADDRESS => + [ + 'value' => '', + 'scope' => AccountManager::VISIBILITY_PRIVATE, + ], + AccountManager::PROPERTY_WEBSITE => + [ + 'value' => '', + 'scope' => AccountManager::VISIBILITY_PRIVATE, + ], + AccountManager::PROPERTY_EMAIL => + [ + 'value' => $user->getEMailAddress(), + 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY, + ], + AccountManager::PROPERTY_AVATAR => + [ + 'scope' => AccountManager::VISIBILITY_CONTACTS_ONLY + ], + AccountManager::PROPERTY_PHONE => + [ + 'value' => '', + 'scope' => AccountManager::VISIBILITY_PRIVATE, + ], + AccountManager::PROPERTY_TWITTER => + [ + 'value' => '', + 'scope' => AccountManager::VISIBILITY_PRIVATE, + ], + ]); + + $ss = new SyncService($backend, $userManager, $logger, $accountManager); $ss->updateUser($user); $user->method('getDisplayName')->willReturn('A test user for unit testing'); @@ -135,10 +176,11 @@ class SyncServiceTest extends TestCase { private function getSyncServiceMock($backend, $response) { $userManager = $this->getMockBuilder('OCP\IUserManager')->disableOriginalConstructor()->getMock(); $logger = $this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock(); + $accountManager = $this->getMockBuilder('OC\Accounts\AccountManager')->disableOriginalConstructor()->getMock(); /** @var SyncService | \PHPUnit_Framework_MockObject_MockObject $ss */ $ss = $this->getMockBuilder(SyncService::class) ->setMethods(['ensureSystemAddressBookExists', 'requestSyncReport', 'download']) - ->setConstructorArgs([$backend, $userManager, $logger]) + ->setConstructorArgs([$backend, $userManager, $logger, $accountManager]) ->getMock(); $ss->method('requestSyncReport')->withAnyParameters()->willReturn(['response' => $response, 'token' => 'sync-token-1']); $ss->method('ensureSystemAddressBookExists')->willReturn(['id' => 1]); diff --git a/apps/dav/tests/unit/DAV/HookManagerTest.php b/apps/dav/tests/unit/DAV/HookManagerTest.php index f980e595bf9..9579ce1c6d3 100644 --- a/apps/dav/tests/unit/DAV/HookManagerTest.php +++ b/apps/dav/tests/unit/DAV/HookManagerTest.php @@ -31,14 +31,19 @@ use OCA\DAV\HookManager; use OCP\IL10N; use OCP\IUser; use OCP\IUserManager; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; class HookManagerTest extends TestCase { /** @var IL10N */ private $l10n; + /** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */ + private $eventDispatcher; + public function setUp() { parent::setUp(); + $this->eventDispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock(); $this->l10n = $this->createMock(IL10N::class); $this->l10n ->expects($this->any()) @@ -82,7 +87,7 @@ class HookManagerTest extends TestCase { 'principals/users/newUser', 'contacts', ['{DAV:}displayname' => 'Contacts']); - $hm = new HookManager($userManager, $syncService, $cal, $card); + $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher); $hm->firstLogin($user); } @@ -116,7 +121,7 @@ class HookManagerTest extends TestCase { $card->expects($this->once())->method('getAddressBooksForUserCount')->willReturn(1); $card->expects($this->never())->method('createAddressBook'); - $hm = new HookManager($userManager, $syncService, $cal, $card); + $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher); $hm->firstLogin($user); } @@ -154,7 +159,7 @@ class HookManagerTest extends TestCase { 'principals/users/newUser', 'contacts', ['{DAV:}displayname' => 'Contacts']); - $hm = new HookManager($userManager, $syncService, $cal, $card); + $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher); $hm->firstLogin($user); } @@ -195,7 +200,7 @@ class HookManagerTest extends TestCase { ]); $card->expects($this->once())->method('deleteAddressBook'); - $hm = new HookManager($userManager, $syncService, $cal, $card, $this->l10n); + $hm = new HookManager($userManager, $syncService, $cal, $card, $this->eventDispatcher); $hm->preDeleteUser(['uid' => 'newUser']); $hm->postDeleteUser(['uid' => 'newUser']); } -- 2.39.5