summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2016-01-12 14:22:46 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-01-12 14:22:46 +0100
commit4fc0fbe8d04b7b89ffe1c3b79621ebe12b2057be (patch)
treed290bacbc7e43f5c26d5e14edeae2f8b33f6d608
parent6ca24d53ea8fd434ef0f3fb253c720173b19cf27 (diff)
parent6f526e638c0c428604402081297fb7058cf758f2 (diff)
downloadnextcloud-server-4fc0fbe8d04b7b89ffe1c3b79621ebe12b2057be.tar.gz
nextcloud-server-4fc0fbe8d04b7b89ffe1c3b79621ebe12b2057be.zip
Merge pull request #21616 from owncloud/introduce-dav-application-class
Unit test contact provider registration
-rw-r--r--apps/dav/appinfo/app.php18
-rw-r--r--apps/dav/appinfo/application.php50
-rw-r--r--apps/dav/lib/carddav/contactsmanager.php55
-rw-r--r--apps/dav/tests/unit/appinfo/applicationtest.php51
-rw-r--r--apps/dav/tests/unit/carddav/contactsmanagertest.php43
5 files changed, 201 insertions, 16 deletions
diff --git a/apps/dav/appinfo/app.php b/apps/dav/appinfo/app.php
index 3651a5034c5..bc889176f7f 100644
--- a/apps/dav/appinfo/app.php
+++ b/apps/dav/appinfo/app.php
@@ -21,21 +21,7 @@
$cm = \OC::$server->getContactsManager();
$cm->register(function() use ($cm) {
- $db = \OC::$server->getDatabaseConnection();
$userId = \OC::$server->getUserSession()->getUser()->getUID();
- $principal = new \OCA\DAV\Connector\Sabre\Principal(
- \OC::$server->getUserManager()
- );
- $cardDav = new \OCA\DAV\CardDAV\CardDavBackend($db, $principal, \OC::$server->getLogger());
- $addressBooks = $cardDav->getAddressBooksForUser("principals/$userId");
- foreach ($addressBooks as $addressBookInfo) {
- $addressBook = new \OCA\DAV\CardDAV\AddressBook($cardDav, $addressBookInfo);
- $cm->registerAddressBook(
- new OCA\DAV\CardDAV\AddressBookImpl(
- $addressBook,
- $addressBookInfo,
- $cardDav
- )
- );
- }
+ $app = new \OCA\Dav\AppInfo\Application();
+ $app->setupContactsProvider($cm, $userId);
});
diff --git a/apps/dav/appinfo/application.php b/apps/dav/appinfo/application.php
new file mode 100644
index 00000000000..f97d43f188e
--- /dev/null
+++ b/apps/dav/appinfo/application.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace OCA\Dav\AppInfo;
+
+use OCA\DAV\CardDAV\ContactsManager;
+use \OCP\AppFramework\App;
+use OCP\AppFramework\IAppContainer;
+use OCP\Contacts\IManager;
+
+class Application extends App {
+
+ /**
+ * Application constructor.
+ *
+ * @param array $urlParams
+ */
+ public function __construct (array $urlParams=array()) {
+ parent::__construct('dav', $urlParams);
+
+ $container = $this->getContainer();
+ $container->registerService('ContactsManager', function($c) {
+ /** @var IAppContainer $c */
+ return new ContactsManager(
+ $c->query('CardDavBackend')
+ );
+ });
+
+ $container->registerService('CardDavBackend', function($c) {
+ /** @var IAppContainer $c */
+ $db = $c->getServer()->getDatabaseConnection();
+ $logger = $c->getServer()->getLogger();
+ $principal = new \OCA\DAV\Connector\Sabre\Principal(
+ $c->getServer()->getUserManager()
+ );
+ return new \OCA\DAV\CardDAV\CardDavBackend($db, $principal, $logger);
+ });
+
+ }
+
+ /**
+ * @param IManager $contactsManager
+ * @param string $userID
+ */
+ public function setupContactsProvider(IManager $contactsManager, $userID) {
+ /** @var ContactsManager $cm */
+ $cm = $this->getContainer()->query('ContactsManager');
+ $cm->setupContactsProvider($contactsManager, $userID);
+ }
+
+}
diff --git a/apps/dav/lib/carddav/contactsmanager.php b/apps/dav/lib/carddav/contactsmanager.php
new file mode 100644
index 00000000000..3fe4de4a15e
--- /dev/null
+++ b/apps/dav/lib/carddav/contactsmanager.php
@@ -0,0 +1,55 @@
+<?php
+/**
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\CardDAV;
+
+use OCP\Contacts\IManager;
+
+class ContactsManager {
+
+ /**
+ * ContactsManager constructor.
+ *
+ * @param CardDavBackend $backend
+ */
+ public function __construct(CardDavBackend $backend) {
+ $this->backend = $backend;
+ }
+
+ /**
+ * @param IManager $cm
+ * @param string $userId
+ */
+ public function setupContactsProvider(IManager $cm, $userId) {
+ $addressBooks = $this->backend->getAddressBooksForUser("principals/$userId");
+ foreach ($addressBooks as $addressBookInfo) {
+ $addressBook = new \OCA\DAV\CardDAV\AddressBook($this->backend, $addressBookInfo);
+ $cm->registerAddressBook(
+ new AddressBookImpl(
+ $addressBook,
+ $addressBookInfo,
+ $this->backend
+ )
+ );
+ }
+ }
+
+}
diff --git a/apps/dav/tests/unit/appinfo/applicationtest.php b/apps/dav/tests/unit/appinfo/applicationtest.php
new file mode 100644
index 00000000000..36a75212ff5
--- /dev/null
+++ b/apps/dav/tests/unit/appinfo/applicationtest.php
@@ -0,0 +1,51 @@
+<?php
+/**
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\Tests\Unit\AppInfo;
+
+use OCA\Dav\AppInfo\Application;
+use OCP\Contacts\IManager;
+use Test\TestCase;
+
+/**
+ * Class ApplicationTest
+ *
+ * @group DB
+ *
+ * @package OCA\DAV\Tests\Unit\AppInfo
+ */
+class ApplicationTest extends TestCase {
+ public function test() {
+ $app = new Application();
+
+ // assert service instances in the container are properly setup
+ $s = $app->getContainer()->query('ContactsManager');
+ $this->assertInstanceOf('OCA\DAV\CardDAV\ContactsManager', $s);
+ $s = $app->getContainer()->query('CardDavBackend');
+ $this->assertInstanceOf('OCA\DAV\CardDAV\CardDavBackend', $s);
+
+ // assert setupContactsProvider() is proper
+ /** @var IManager | \PHPUnit_Framework_MockObject_MockObject $cm */
+ $cm = $this->getMockBuilder('OCP\Contacts\IManager')->disableOriginalConstructor()->getMock();
+ $app->setupContactsProvider($cm, 'user01');
+ $this->assertTrue(true);
+ }
+}
diff --git a/apps/dav/tests/unit/carddav/contactsmanagertest.php b/apps/dav/tests/unit/carddav/contactsmanagertest.php
new file mode 100644
index 00000000000..c4ec4c29e39
--- /dev/null
+++ b/apps/dav/tests/unit/carddav/contactsmanagertest.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * @author Thomas Müller <thomas.mueller@tmit.eu>
+ *
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * 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, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCA\DAV\Tests\Unit\CardDAV;
+
+use OCA\DAV\CardDAV\CardDavBackend;
+use OCA\DAV\CardDAV\ContactsManager;
+use OCP\Contacts\IManager;
+use Test\TestCase;
+
+class ContactsManagerTest extends TestCase {
+ public function test() {
+ /** @var IManager | \PHPUnit_Framework_MockObject_MockObject $cm */
+ $cm = $this->getMockBuilder('OCP\Contacts\IManager')->disableOriginalConstructor()->getMock();
+ $cm->expects($this->once())->method('registerAddressBook');
+ /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $backEnd */
+ $backEnd = $this->getMockBuilder('OCA\DAV\CardDAV\CardDavBackend')->disableOriginalConstructor()->getMock();
+ $backEnd->method('getAddressBooksForUser')->willReturn([
+ []
+ ]);
+
+ $app = new ContactsManager($backEnd);
+ $app->setupContactsProvider($cm, 'user01');
+ }
+}