Merge pull request #21616 from owncloud/introduce-dav-application-class

Unit test contact provider registration
This commit is contained in:
Thomas Müller 2016-01-12 14:22:46 +01:00
commit 4fc0fbe8d0
5 changed files with 201 additions and 16 deletions

View file

@ -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);
});

View file

@ -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);
}
}

View file

@ -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
)
);
}
}
}

View file

@ -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);
}
}

View file

@ -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');
}
}