diff options
Diffstat (limited to 'tests/lib')
7 files changed, 642 insertions, 0 deletions
diff --git a/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php b/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php new file mode 100644 index 00000000000..d1273c2b9ad --- /dev/null +++ b/tests/lib/Contacts/ContactsMenu/ActionFactoryTest.php @@ -0,0 +1,67 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Contacts\ContactsMenu; + +use OC\Contacts\ContactsMenu\ActionFactory; +use OCP\Contacts\ContactsMenu\IAction; +use Test\TestCase; + +class ActionFactoryTest extends TestCase { + + /** @var ActionFactory */ + private $actionFactory; + + protected function setUp() { + parent::setUp(); + + $this->actionFactory = new ActionFactory(); + } + + public function testNewLinkAction() { + $icon = 'icon-test'; + $name = 'Test'; + $href = 'some/url'; + + $action = $this->actionFactory->newLinkAction($icon, $name, $href); + + $this->assertInstanceOf(IAction::class, $action); + $this->assertEquals($name, $action->getName()); + $this->assertEquals(10, $action->getPriority()); + } + + public function testNewEMailAction() { + $icon = 'icon-test'; + $name = 'Test'; + $href = 'user@example.com'; + + $action = $this->actionFactory->newEMailAction($icon, $name, $href); + + $this->assertInstanceOf(IAction::class, $action); + $this->assertEquals($name, $action->getName()); + $this->assertEquals(10, $action->getPriority()); + $this->assertEquals('mailto:user%40example.com', $action->getHref()); + } + +} diff --git a/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php b/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php new file mode 100644 index 00000000000..208ce94c6b0 --- /dev/null +++ b/tests/lib/Contacts/ContactsMenu/ActionProviderStoreTest.php @@ -0,0 +1,82 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Contacts\ContactsMenu; + +use Exception; +use OC\Contacts\ContactsMenu\ActionProviderStore; +use OC\Contacts\ContactsMenu\Providers\EMailProvider; +use OCP\AppFramework\QueryException; +use OCP\ILogger; +use OCP\IServerContainer; +use PHPUnit_Framework_MockObject_MockObject; +use Test\TestCase; + +class ActionProviderStoreTest extends TestCase { + + /** @var IServerContainer|PHPUnit_Framework_MockObject_MockObject */ + private $serverContainer; + + /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var ActionProviderStore */ + private $actionProviderStore; + + protected function setUp() { + parent::setUp(); + + $this->serverContainer = $this->createMock(IServerContainer::class); + $this->logger = $this->createMock(ILogger::class); + $this->actionProviderStore = new ActionProviderStore($this->serverContainer, $this->logger); + } + + public function testGetProviders() { + $emailProvider = $this->createMock(EMailProvider::class); + $this->serverContainer->expects($this->exactly(2)) + ->method('query') + ->will($this->returnValueMap([ + [EMailProvider::class, $emailProvider], + ])); + + $providers = $this->actionProviderStore->getProviders(); + + $this->assertCount(1, $providers); + $this->assertInstanceOf(EMailProvider::class, $providers[0]); + } + + /** + * @expectedException Exception + */ + public function testGetProvidersWithQueryException() { + $emailProvider = $this->createMock(EMailProvider::class); + $detailsProvider = $this->createMock(DetailsProvider::class); + $this->serverContainer->expects($this->once()) + ->method('query') + ->willThrowException(new QueryException()); + + $providers = $this->actionProviderStore->getProviders(); + } + +} diff --git a/tests/lib/Contacts/ContactsMenu/Actions/LinkActionTest.php b/tests/lib/Contacts/ContactsMenu/Actions/LinkActionTest.php new file mode 100644 index 00000000000..31654b40918 --- /dev/null +++ b/tests/lib/Contacts/ContactsMenu/Actions/LinkActionTest.php @@ -0,0 +1,90 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Contacts\ContactsMenu\Actions; + +use OC\Contacts\ContactsMenu\Actions\LinkAction; +use Test\TestCase; + +class LinkActionTest extends TestCase { + + private $action; + + protected function setUp() { + parent::setUp(); + + $this->action = new LinkAction(); + } + + public function testSetIcon() { + $icon = 'icon-test'; + + $this->action->setIcon($icon); + $json = $this->action->jsonSerialize(); + + $this->assertArrayHasKey('icon', $json); + $this->assertEquals($json['icon'], $icon); + } + + public function testGetSetName() { + $name = 'Jane Doe'; + + $this->assertNull($this->action->getName()); + $this->action->setName($name); + $this->assertEquals($name, $this->action->getName()); + } + + public function testGetSetPriority() { + $prio = 50; + + $this->assertEquals(10, $this->action->getPriority()); + $this->action->setPriority($prio); + $this->assertEquals($prio, $this->action->getPriority()); + } + + public function testSetHref() { + $this->action->setHref('/some/url'); + + $json = $this->action->jsonSerialize(); + $this->assertArrayHasKey('hyperlink', $json); + $this->assertEquals($json['hyperlink'], '/some/url'); + } + + public function testJsonSerialize() { + $this->action->setIcon('icon-contacts'); + $this->action->setName('Nickie Works'); + $this->action->setPriority(33); + $this->action->setHref('example.com'); + $expected = [ + 'title' => 'Nickie Works', + 'icon' => 'icon-contacts', + 'hyperlink' => 'example.com', + ]; + + $json = $this->action->jsonSerialize(); + + $this->assertEquals($expected, $json); + } + +} diff --git a/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php b/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php new file mode 100644 index 00000000000..fa7c57bb409 --- /dev/null +++ b/tests/lib/Contacts/ContactsMenu/ContactsStoreTest.php @@ -0,0 +1,121 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Contacts\ContactsMenu; + +use OC\Contacts\ContactsMenu\ContactsStore; +use OCP\Contacts\IManager; +use PHPUnit_Framework_MockObject_MockObject; +use Test\TestCase; + +class ContactsStoreTest extends TestCase { + + /** @var ContactsStore */ + private $contactsStore; + + /** @var IManager|PHPUnit_Framework_MockObject_MockObject */ + private $contactsManager; + + protected function setUp() { + parent::setUp(); + + $this->contactsManager = $this->createMock(IManager::class); + + $this->contactsStore = new ContactsStore($this->contactsManager); + } + + public function testGetContactsWithoutFilter() { + $this->contactsManager->expects($this->once()) + ->method('search') + ->with($this->equalTo(''), $this->equalTo(['FN'])) + ->willReturn([ + [ + 'id' => 123, + ], + [ + 'id' => 567, + 'FN' => 'Darren Roner', + 'EMAIL' => [ + 'darren@roner.au' + ], + ], + ]); + + $entries = $this->contactsStore->getContacts(''); + + $this->assertCount(2, $entries); + $this->assertEquals([ + 'darren@roner.au' + ], $entries[1]->getEMailAddresses()); + } + + public function testGetContactsWithoutBinaryImage() { + $this->contactsManager->expects($this->once()) + ->method('search') + ->with($this->equalTo(''), $this->equalTo(['FN'])) + ->willReturn([ + [ + 'id' => 123, + ], + [ + 'id' => 567, + 'FN' => 'Darren Roner', + 'EMAIL' => [ + 'darren@roner.au' + ], + 'PHOTO' => base64_encode('photophotophoto'), + ], + ]); + + $entries = $this->contactsStore->getContacts(''); + + $this->assertCount(2, $entries); + $this->assertNull($entries[1]->getAvatar()); + } + + public function testGetContactsWithoutAvatarURI() { + $this->contactsManager->expects($this->once()) + ->method('search') + ->with($this->equalTo(''), $this->equalTo(['FN'])) + ->willReturn([ + [ + 'id' => 123, + ], + [ + 'id' => 567, + 'FN' => 'Darren Roner', + 'EMAIL' => [ + 'darren@roner.au' + ], + 'PHOTO' => 'VALUE=uri:https://photo', + ], + ]); + + $entries = $this->contactsStore->getContacts(''); + + $this->assertCount(2, $entries); + $this->assertEquals('https://photo', $entries[1]->getAvatar()); + } + +} diff --git a/tests/lib/Contacts/ContactsMenu/EntryTest.php b/tests/lib/Contacts/ContactsMenu/EntryTest.php new file mode 100644 index 00000000000..fa34916f47f --- /dev/null +++ b/tests/lib/Contacts/ContactsMenu/EntryTest.php @@ -0,0 +1,113 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Contacts\ContactsMenu; + +use OC\Contacts\ContactsMenu\Actions\LinkAction; +use OC\Contacts\ContactsMenu\Entry; +use OCP\Contacts\ContactsMenu\IAction; +use Test\TestCase; + +class EntryTest extends \PHPUnit_Framework_TestCase { + + /** @var Entry */ + private $entry; + + protected function setUp() { + parent::setUp(); + + $this->entry = new Entry(); + } + + public function testSetId() { + $this->entry->setId(123); + } + + public function testSetGetFullName() { + $fn = 'Danette Chaille'; + $this->assertEquals('', $this->entry->getFullName()); + $this->entry->setFullName($fn); + $this->assertEquals($fn, $this->entry->getFullName()); + } + + public function testAddGetEMailAddresses() { + $this->assertEmpty($this->entry->getEMailAddresses()); + $this->entry->addEMailAddress('user@example.com'); + $this->assertEquals(['user@example.com'], $this->entry->getEMailAddresses()); + } + + public function testAddAndSortAction() { + // Three actions, two with equal priority + $action1 = new LinkAction(); + $action2 = new LinkAction(); + $action3 = new LinkAction(); + $action1->setPriority(10); + $action1->setName('Bravo'); + + $action2->setPriority(0); + $action2->setName('Batman'); + + $action3->setPriority(10); + $action3->setName('Alfa'); + + $this->entry->addAction($action1); + $this->entry->addAction($action2); + $this->entry->addAction($action3); + $sorted = $this->entry->getActions(); + + $this->assertSame($action3, $sorted[0]); + $this->assertSame($action1, $sorted[1]); + $this->assertSame($action2, $sorted[2]); + } + + public function testSetGetProperties() { + $props = [ + 'prop1' => 123, + 'prop2' => 'string', + ]; + + $this->entry->setProperties($props); + + $this->assertNull($this->entry->getProperty('doesntexist')); + $this->assertEquals(123, $this->entry->getProperty('prop1')); + $this->assertEquals('string', $this->entry->getProperty('prop2')); + } + + public function testJsonSerialize() { + $expectedJson = [ + 'id' => 123, + 'fullName' => 'Guadalupe Frisbey', + 'topAction' => null, + 'actions' => [], + 'lastMessage' => '', + ]; + + $this->entry->setId(123); + $this->entry->setFullName('Guadalupe Frisbey'); + $json = $this->entry->jsonSerialize(); + + $this->assertEquals($expectedJson, $json); + } + +} diff --git a/tests/lib/Contacts/ContactsMenu/ManagerTest.php b/tests/lib/Contacts/ContactsMenu/ManagerTest.php new file mode 100644 index 00000000000..bcbcec96817 --- /dev/null +++ b/tests/lib/Contacts/ContactsMenu/ManagerTest.php @@ -0,0 +1,100 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Contacts\ContactsMenu; + +use OC\Contacts\ContactsMenu\ActionProviderStore; +use OC\Contacts\ContactsMenu\ContactsStore; +use OC\Contacts\ContactsMenu\Manager; +use OCP\App\IAppManager; +use OCP\Contacts\ContactsMenu\IEntry; +use OCP\Contacts\ContactsMenu\IProvider; +use PHPUnit_Framework_MockObject_MockObject; +use Test\TestCase; + +class ManagerTest extends TestCase { + + /** @var ContactsStore|PHPUnit_Framework_MockObject_MockObject */ + private $contactsStore; + + /** @var IAppManager|PHPUnit_Framework_MockObject_MockObject */ + private $appManager; + + /** @var ActionProviderStore|PHPUnit_Framework_MockObject_MockObject */ + private $actionProviderStore; + + /** @var Manager */ + private $manager; + + protected function setUp() { + parent::setUp(); + + $this->contactsStore = $this->createMock(ContactsStore::class); + $this->actionProviderStore = $this->createMock(ActionProviderStore::class); + $this->appManager = $this->createMock(IAppManager::class); + + $this->manager = new Manager($this->contactsStore, $this->actionProviderStore, $this->appManager); + } + + private function generateTestEntries() { + $entries = []; + foreach (range('Z', 'A') as $char) { + $entry = $this->createMock(IEntry::class); + $entry->expects($this->any()) + ->method('getFullName') + ->willReturn('Contact ' . $char); + $entries[] = $entry; + } + return $entries; + } + + public function testGetFilteredEntries() { + $filter = 'con'; + $user = 'user849'; + $entries = $this->generateTestEntries(); + $provider = $this->createMock(IProvider::class); + $this->contactsStore->expects($this->once()) + ->method('getContacts') + ->with($filter) + ->willReturn($entries); + $this->actionProviderStore->expects($this->once()) + ->method('getProviders') + ->willReturn([$provider]); + $provider->expects($this->exactly(25)) + ->method('process'); + $this->appManager->expects($this->once()) + ->method('isEnabledForUser') + ->with($this->equalTo('contacts'), $user) + ->willReturn(false); + $expected = [ + 'contacts' => array_slice($entries, 0, 25), + 'contactsAppEnabled' => false, + ]; + + $data = $this->manager->getEntries($user, $filter); + + $this->assertEquals($expected, $data); + } + +} diff --git a/tests/lib/Contacts/ContactsMenu/Providers/EMailproviderTest.php b/tests/lib/Contacts/ContactsMenu/Providers/EMailproviderTest.php new file mode 100644 index 00000000000..af7b21a6485 --- /dev/null +++ b/tests/lib/Contacts/ContactsMenu/Providers/EMailproviderTest.php @@ -0,0 +1,69 @@ +<?php + +/** + * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2017 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Tests\Contacts\ContactsMenu\Providers; + +use OC\Contacts\ContactsMenu\Providers\EMailProvider; +use OCP\Contacts\ContactsMenu\IActionFactory; +use OCP\Contacts\ContactsMenu\IEntry; +use OCP\Contacts\ContactsMenu\ILinkAction; +use PHPUnit_Framework_MockObject_MockObject; +use Test\TestCase; + +class EMailproviderTest extends TestCase { + + /** @var IActionFactory|PHPUnit_Framework_MockObject_MockObject */ + private $actionFactory; + + /** @var EMailProvider */ + private $provider; + + protected function setUp() { + parent::setUp(); + + $this->actionFactory = $this->createMock(IActionFactory::class); + $this->provider = new EMailProvider($this->actionFactory); + } + + public function testProcess() { + $entry = $this->createMock(IEntry::class); + $action = $this->createMock(ILinkAction::class); + + $entry->expects($this->once()) + ->method('getEMailAddresses') + ->willReturn([ + 'user@example.com', + ]); + $this->actionFactory->expects($this->once()) + ->method('newEMailAction') + ->with($this->equalTo('icon-mail'), $this->equalTo('Mail'), $this->equalTo('user@example.com')) + ->willReturn($action); + $entry->expects($this->once()) + ->method('addAction') + ->with($action); + + $this->provider->process($entry); + } + +} |