summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2016-01-28 19:47:48 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-01-28 19:47:48 +0100
commit6fc326967035959847746e3831bf9a63256fc214 (patch)
tree332e8155e57b73f47368d56a24d60852732bac58
parent8b3d7d09d52ba169953d6a7d03ab570eb3ceed7a (diff)
parent128d9d7aa624cc013cb8f0dbb79693ee5d6c68dd (diff)
downloadnextcloud-server-6fc326967035959847746e3831bf9a63256fc214.tar.gz
nextcloud-server-6fc326967035959847746e3831bf9a63256fc214.zip
Merge pull request #21333 from owncloud/migrate-contacts
Migrate contacts
-rw-r--r--.gitignore3
-rw-r--r--apps/dav/appinfo/application.php27
-rw-r--r--apps/dav/appinfo/install.php1
-rw-r--r--apps/dav/appinfo/register_command.php8
-rw-r--r--apps/dav/command/createaddressbook.php2
-rw-r--r--apps/dav/command/migrateaddressbooks.php84
-rw-r--r--apps/dav/command/syncsystemaddressbook.php1
-rw-r--r--apps/dav/lib/migration/addressbookadapter.php87
-rw-r--r--apps/dav/lib/migration/migrateaddressbooks.php92
-rw-r--r--apps/dav/tests/unit/migration/addressbookadaptertest.php129
-rw-r--r--apps/dav/tests/unit/migration/contacts_schema.xml151
-rw-r--r--apps/dav/tests/unit/migration/migrateaddressbooktest.php70
12 files changed, 650 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 7d42be773b1..237f0f44e81 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,16 +11,13 @@
/apps*/*
!/apps/dav
!/apps/files
-!/apps/files_encryption
!/apps/federation
!/apps/encryption
-!/apps/encryption_dummy
!/apps/files_external
!/apps/files_sharing
!/apps/files_trashbin
!/apps/files_versions
!/apps/user_ldap
-!/apps/user_webdavauth
!/apps/provisioning_api
!/apps/systemtags
/apps/files_external/3rdparty/irodsphp/PHPUnitTest
diff --git a/apps/dav/appinfo/application.php b/apps/dav/appinfo/application.php
index d8cf2a34115..07905db7368 100644
--- a/apps/dav/appinfo/application.php
+++ b/apps/dav/appinfo/application.php
@@ -24,9 +24,12 @@ use OCA\DAV\CardDAV\ContactsManager;
use OCA\DAV\CardDAV\SyncJob;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\HookManager;
+use OCA\Dav\Migration\AddressBookAdapter;
+use OCA\Dav\Migration\MigrateAddressbooks;
use \OCP\AppFramework\App;
use OCP\AppFramework\IAppContainer;
use OCP\Contacts\IManager;
+use OCP\IUser;
class Application extends App {
@@ -73,6 +76,14 @@ class Application extends App {
return new \OCA\DAV\CardDAV\CardDavBackend($db, $principal, $logger);
});
+ $container->registerService('MigrateAddressbooks', function($c) {
+ /** @var IAppContainer $c */
+ $db = $c->getServer()->getDatabaseConnection();
+ return new MigrateAddressbooks(
+ new AddressBookAdapter($db),
+ $c->query('CardDavBackend')
+ );
+ });
}
/**
@@ -100,4 +111,20 @@ class Application extends App {
$jl->add(new SyncJob());
}
+ public function migrateAddressbooks() {
+
+ try {
+ $migration = $this->getContainer()->query('MigrateAddressbooks');
+ $migration->setup();
+ $userManager = $this->getContainer()->getServer()->getUserManager();
+
+ $userManager->callForAllUsers(function($user) use($migration) {
+ /** @var IUser $user */
+ $migration->migrateForUser($user->getUID());
+ });
+ } catch (\Exception $ex) {
+ $this->getContainer()->getServer()->getLogger()->logException($ex);
+ }
+ }
+
}
diff --git a/apps/dav/appinfo/install.php b/apps/dav/appinfo/install.php
index aaa36052cd2..f6ef533958e 100644
--- a/apps/dav/appinfo/install.php
+++ b/apps/dav/appinfo/install.php
@@ -23,3 +23,4 @@ use OCA\Dav\AppInfo\Application;
$app = new Application();
$app->setupCron();
+$app->migrateAddressbooks();
diff --git a/apps/dav/appinfo/register_command.php b/apps/dav/appinfo/register_command.php
index 8ef1979aa08..e8fea5daf23 100644
--- a/apps/dav/appinfo/register_command.php
+++ b/apps/dav/appinfo/register_command.php
@@ -22,6 +22,7 @@
use OCA\Dav\AppInfo\Application;
use OCA\DAV\Command\CreateAddressBook;
use OCA\DAV\Command\CreateCalendar;
+use OCA\Dav\Command\MigrateAddressbooks;
use OCA\DAV\Command\SyncSystemAddressBook;
$config = \OC::$server->getConfig();
@@ -37,3 +38,10 @@ $app = new Application();
$application->add(new CreateAddressBook($userManager, $groupManager, $dbConnection, $logger));
$application->add(new CreateCalendar($userManager, $dbConnection));
$application->add(new SyncSystemAddressBook($app->getSyncService()));
+
+// the occ tool is *for now* only available in debug mode for developers to test
+if ($config->getSystemValue('debug', false)){
+ $app = new \OCA\Dav\AppInfo\Application();
+ $migration = $app->getContainer()->query('MigrateAddressbooks');
+ $application->add(new MigrateAddressbooks($userManager, $migration));
+}
diff --git a/apps/dav/command/createaddressbook.php b/apps/dav/command/createaddressbook.php
index 201101d17f4..3d99afd4ba3 100644
--- a/apps/dav/command/createaddressbook.php
+++ b/apps/dav/command/createaddressbook.php
@@ -88,7 +88,7 @@ class CreateAddressBook extends Command {
);
$name = $input->getArgument('name');
- $carddav = new CardDavBackend($this->dbConnection, $principalBackend);
+ $carddav = new CardDavBackend($this->dbConnection, $principalBackend, $this->logger);
$carddav->createAddressBook("principals/users/$user", $name, []);
}
}
diff --git a/apps/dav/command/migrateaddressbooks.php b/apps/dav/command/migrateaddressbooks.php
new file mode 100644
index 00000000000..2ab7113ab1f
--- /dev/null
+++ b/apps/dav/command/migrateaddressbooks.php
@@ -0,0 +1,84 @@
+<?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\Command;
+
+use OCP\IUser;
+use OCP\IUserManager;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Helper\ProgressBar;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class MigrateAddressbooks extends Command {
+
+ /** @var IUserManager */
+ protected $userManager;
+
+ /** @var \OCA\Dav\Migration\MigrateAddressbooks */
+ private $service;
+
+ /**
+ * @param IUserManager $userManager
+ * @param \OCA\Dav\Migration\MigrateAddressbooks $service
+ */
+ function __construct(IUserManager $userManager,
+ \OCA\Dav\Migration\MigrateAddressbooks $service
+ ) {
+ parent::__construct();
+ $this->userManager = $userManager;
+ $this->service = $service;
+ }
+
+ protected function configure() {
+ $this
+ ->setName('dav:migrate-addressbooks')
+ ->setDescription('Migrate addressbooks from the contacts app to core')
+ ->addArgument('user',
+ InputArgument::OPTIONAL,
+ 'User for whom all addressbooks will be migrated');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output) {
+ $this->service->setup();
+
+ $user = $input->getArgument('user');
+ if (!is_null($user)) {
+ if (!$this->userManager->userExists($user)) {
+ throw new \InvalidArgumentException("User <$user> in unknown.");
+ }
+ $output->writeln("Start migration for $user");
+ $this->service->migrateForUser($user);
+ }
+ $output->writeln("Start migration of all known users ...");
+ $p = new ProgressBar($output);
+ $p->start();
+ $this->userManager->callForAllUsers(function($user) use ($p) {
+ $p->advance();
+ /** @var IUser $user */
+ $this->service->migrateForUser($user->getUID());
+ });
+
+ $p->finish();
+ $output->writeln('');
+ }
+}
diff --git a/apps/dav/command/syncsystemaddressbook.php b/apps/dav/command/syncsystemaddressbook.php
index 50f570ec93e..b83a37131c3 100644
--- a/apps/dav/command/syncsystemaddressbook.php
+++ b/apps/dav/command/syncsystemaddressbook.php
@@ -53,7 +53,6 @@ class SyncSystemAddressBook extends Command {
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output) {
-
$output->writeln('Syncing users ...');
$progress = new ProgressBar($output);
$progress->start();
diff --git a/apps/dav/lib/migration/addressbookadapter.php b/apps/dav/lib/migration/addressbookadapter.php
new file mode 100644
index 00000000000..ef7b00188fb
--- /dev/null
+++ b/apps/dav/lib/migration/addressbookadapter.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace OCA\Dav\Migration;
+
+use OCP\IDBConnection;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class AddressBookAdapter {
+
+ /** @var \OCP\IDBConnection */
+ protected $dbConnection;
+
+ /** @var string */
+ private $sourceBookTable;
+
+ /** @var string */
+ private $sourceCardsTable;
+
+ /**
+ * @param IDBConnection $dbConnection
+ * @param string $sourceBookTable
+ * @param string $sourceCardsTable
+ */
+ function __construct(IDBConnection $dbConnection,
+ $sourceBookTable = 'contacts_addressbooks',
+ $sourceCardsTable = 'contacts_cards') {
+ $this->dbConnection = $dbConnection;
+ $this->sourceBookTable = $sourceBookTable;
+ $this->sourceCardsTable = $sourceCardsTable;
+ }
+
+ /**
+ * @param string $user
+ * @param \Closure $callBack
+ */
+ public function foreachBook($user, \Closure $callBack) {
+ // get all addressbooks of that user
+ $query = $this->dbConnection->getQueryBuilder();
+ $stmt = $query->select('*')->from($this->sourceBookTable)
+ ->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
+ ->execute();
+
+ while($row = $stmt->fetch()) {
+ $callBack($row);
+ }
+ }
+
+ public function setup() {
+ if (!$this->dbConnection->tableExists($this->sourceBookTable)) {
+ throw new \DomainException('Contacts tables are missing. Nothing to do.');
+ }
+ }
+
+ /**
+ * @param int $addressBookId
+ * @param \Closure $callBack
+ */
+ public function foreachCard($addressBookId, \Closure $callBack) {
+ $query = $this->dbConnection->getQueryBuilder();
+ $stmt = $query->select('*')->from($this->sourceCardsTable)
+ ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
+ ->execute();
+
+ while($row = $stmt->fetch()) {
+ $callBack($row);
+ }
+ }
+
+ /**
+ * @param int $addressBookId
+ * @return array
+ */
+ public function getShares($addressBookId) {
+ $query = $this->dbConnection->getQueryBuilder();
+ $shares = $query->select('*')->from('share')
+ ->where($query->expr()->eq('item_source', $query->createNamedParameter($addressBookId)))
+ ->andWhere($query->expr()->eq('item_type', $query->expr()->literal('addressbook')))
+ ->andWhere($query->expr()->in('share_type', [ $query->expr()->literal(0), $query->expr()->literal(1)]))
+ ->execute()
+ ->fetchAll();
+
+ return $shares;
+ }
+}
diff --git a/apps/dav/lib/migration/migrateaddressbooks.php b/apps/dav/lib/migration/migrateaddressbooks.php
new file mode 100644
index 00000000000..0dad6495691
--- /dev/null
+++ b/apps/dav/lib/migration/migrateaddressbooks.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace OCA\Dav\Migration;
+
+use OCA\DAV\CardDAV\AddressBook;
+use OCA\DAV\CardDAV\CardDavBackend;
+use Sabre\CardDAV\Plugin;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class MigrateAddressbooks {
+
+ /** @var AddressBookAdapter */
+ protected $adapter;
+
+ /** @var CardDavBackend */
+ private $backend;
+
+ /**
+ * @param AddressBookAdapter $adapter
+ * @param CardDavBackend $backend
+ */
+ function __construct(AddressBookAdapter $adapter,
+ CardDavBackend $backend
+ ) {
+ $this->adapter = $adapter;
+ $this->backend = $backend;
+ }
+
+ /**
+ * @param string $user
+ */
+ public function migrateForUser($user) {
+
+ $this->adapter->foreachBook($user, function($book) use ($user) {
+ $principal = "principals/users/$user";
+ $knownBooks = $this->backend->getAddressBooksByUri($principal, $book['uri']);
+ if (!is_null($knownBooks)) {
+ return;
+ }
+
+ $newId = $this->backend->createAddressBook($principal, $book['uri'], [
+ '{DAV:}displayname' => $book['displayname'],
+ '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $book['description']
+ ]);
+
+ $this->migrateBook($book['id'], $newId);
+ $this->migrateShares($book['id'], $newId);
+ });
+ }
+
+ public function setup() {
+ $this->adapter->setup();
+ }
+
+ /**
+ * @param int $addressBookId
+ * @param int $newAddressBookId
+ */
+ private function migrateBook($addressBookId, $newAddressBookId) {
+ $this->adapter->foreachCard($addressBookId, function($card) use ($newAddressBookId) {
+ $this->backend->createCard($newAddressBookId, $card['uri'], $card['carddata']);
+ });
+ }
+
+ /**
+ * @param int $addressBookId
+ * @param int $newAddressBookId
+ */
+ private function migrateShares($addressBookId, $newAddressBookId) {
+ $shares =$this->adapter->getShares($addressBookId);
+ if (empty($shares)) {
+ return;
+ }
+
+ $add = array_map(function($s) {
+ $prefix = 'principal:principals/users/';
+ if ($s['share_type'] === 1) {
+ $prefix = 'principal:principals/groups/';
+ }
+ return [
+ 'href' => $prefix . $s['share_with']
+ ];
+ }, $shares);
+
+ $newAddressBook = $this->backend->getAddressBookById($newAddressBookId);
+ $book = new AddressBook($this->backend, $newAddressBook);
+ $this->backend->updateShares($book, $add, []);
+ }
+}
diff --git a/apps/dav/tests/unit/migration/addressbookadaptertest.php b/apps/dav/tests/unit/migration/addressbookadaptertest.php
new file mode 100644
index 00000000000..e6e57049a93
--- /dev/null
+++ b/apps/dav/tests/unit/migration/addressbookadaptertest.php
@@ -0,0 +1,129 @@
+<?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\Migration;
+
+use DomainException;
+use OCA\Dav\Migration\AddressBookAdapter;
+use OCP\IDBConnection;
+use Test\TestCase;
+
+/**
+ * Class AddressbookAdapterTest
+ *
+ * @group DB
+ *
+ * @package OCA\DAV\Tests\Unit\Migration
+ */
+class AddressbookAdapterTest extends TestCase {
+
+ /** @var IDBConnection */
+ private $db;
+ /** @var AddressBookAdapter */
+ private $adapter;
+ /** @var array */
+ private $books = [];
+ /** @var array */
+ private $cards = [];
+
+ public function setUp() {
+ parent::setUp();
+ $this->db = \OC::$server->getDatabaseConnection();
+
+ $manager = new \OC\DB\MDB2SchemaManager($this->db);
+ $manager->createDbFromStructure(__DIR__ . '/contacts_schema.xml');
+
+ $this->adapter = new AddressBookAdapter($this->db);
+ }
+
+ public function tearDown() {
+ $this->db->dropTable('contacts_addressbooks');
+ $this->db->dropTable('contacts_cards');
+ parent::tearDown();
+ }
+
+ /**
+ * @expectedException DomainException
+ */
+ public function testOldTablesDoNotExist() {
+ $adapter = new AddressBookAdapter(\OC::$server->getDatabaseConnection(), 'crazy_table_that_does_no_exist');
+ $adapter->setup();
+ }
+
+ public function test() {
+
+ // insert test data
+ $builder = $this->db->getQueryBuilder();
+ $builder->insert('contacts_addressbooks')
+ ->values([
+ 'userid' => $builder->createNamedParameter('test-user-666'),
+ 'displayname' => $builder->createNamedParameter('Display Name'),
+ 'uri' => $builder->createNamedParameter('contacts'),
+ 'description' => $builder->createNamedParameter('An address book for testing'),
+ 'ctag' => $builder->createNamedParameter('112233'),
+ 'active' => $builder->createNamedParameter('1')
+ ])
+ ->execute();
+ $builder = $this->db->getQueryBuilder();
+ $builder->insert('contacts_cards')
+ ->values([
+ 'addressbookid' => $builder->createNamedParameter(6666),
+ 'fullname' => $builder->createNamedParameter('Full Name'),
+ 'carddata' => $builder->createNamedParameter('datadatadata'),
+ 'uri' => $builder->createNamedParameter('some-card.vcf'),
+ 'lastmodified' => $builder->createNamedParameter('112233'),
+ ])
+ ->execute();
+ $builder = $this->db->getQueryBuilder();
+ $builder->insert('share')
+ ->values([
+ 'share_type' => $builder->createNamedParameter(1),
+ 'share_with' => $builder->createNamedParameter('user01'),
+ 'uid_owner' => $builder->createNamedParameter('user02'),
+ 'item_type' => $builder->createNamedParameter('addressbook'),
+ 'item_source' => $builder->createNamedParameter(6666),
+ 'item_target' => $builder->createNamedParameter('Contacts (user02)'),
+ ])
+ ->execute();
+
+ // test the adapter
+ $this->adapter->foreachBook('test-user-666', function($row) {
+ $this->books[] = $row;
+ });
+ $this->assertArrayHasKey('id', $this->books[0]);
+ $this->assertEquals('test-user-666', $this->books[0]['userid']);
+ $this->assertEquals('Display Name', $this->books[0]['displayname']);
+ $this->assertEquals('contacts', $this->books[0]['uri']);
+ $this->assertEquals('An address book for testing', $this->books[0]['description']);
+ $this->assertEquals('112233', $this->books[0]['ctag']);
+
+ $this->adapter->foreachCard(6666, function($row) {
+ $this->cards[]= $row;
+ });
+ $this->assertArrayHasKey('id', $this->cards[0]);
+ $this->assertEquals(6666, $this->cards[0]['addressbookid']);
+
+ // test getShares
+ $shares = $this->adapter->getShares(6666);
+ $this->assertEquals(1, count($shares));
+
+ }
+
+}
diff --git a/apps/dav/tests/unit/migration/contacts_schema.xml b/apps/dav/tests/unit/migration/contacts_schema.xml
new file mode 100644
index 00000000000..51836a1e0c6
--- /dev/null
+++ b/apps/dav/tests/unit/migration/contacts_schema.xml
@@ -0,0 +1,151 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<database>
+
+ <name>*dbname*</name>
+ <create>true</create>
+ <overwrite>false</overwrite>
+ <charset>utf8</charset>
+ <table>
+
+ <name>*dbprefix*contacts_addressbooks</name>
+
+ <declaration>
+
+ <field>
+ <name>id</name>
+ <type>integer</type>
+ <default>0</default>
+ <notnull>true</notnull>
+ <autoincrement>1</autoincrement>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>userid</name>
+ <type>text</type>
+ <default></default>
+ <notnull>true</notnull>
+ <length>255</length>
+ </field>
+
+ <field>
+ <name>displayname</name>
+ <type>text</type>
+ <default></default>
+ <notnull>false</notnull>
+ <length>255</length>
+ </field>
+
+ <field>
+ <name>uri</name>
+ <type>text</type>
+ <default></default>
+ <notnull>false</notnull>
+ <length>200</length>
+ </field>
+
+ <field>
+ <name>description</name>
+ <type>text</type>
+ <notnull>false</notnull>
+ <length>255</length>
+ </field>
+
+ <field>
+ <name>ctag</name>
+ <type>integer</type>
+ <default>1</default>
+ <notnull>true</notnull>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>active</name>
+ <type>integer</type>
+ <default>1</default>
+ <notnull>true</notnull>
+ <length>4</length>
+ </field>
+
+ <index>
+ <name>c_addressbook_userid_index</name>
+ <field>
+ <name>userid</name>
+ <sorting>ascending</sorting>
+ </field>
+ </index>
+ </declaration>
+
+ </table>
+
+ <table>
+
+ <name>*dbprefix*contacts_cards</name>
+
+ <declaration>
+
+ <field>
+ <name>id</name>
+ <type>integer</type>
+ <default>0</default>
+ <notnull>true</notnull>
+ <autoincrement>1</autoincrement>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>addressbookid</name>
+ <type>integer</type>
+ <default></default>
+ <notnull>true</notnull>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+ <field>
+ <name>fullname</name>
+ <type>text</type>
+ <default></default>
+ <notnull>false</notnull>
+ <length>255</length>
+ </field>
+
+ <field>
+ <name>carddata</name>
+ <type>clob</type>
+ <notnull>false</notnull>
+ </field>
+
+ <field>
+ <name>uri</name>
+ <type>text</type>
+ <default></default>
+ <notnull>false</notnull>
+ <length>200</length>
+ </field>
+
+ <field>
+ <name>lastmodified</name>
+ <type>integer</type>
+ <default></default>
+ <notnull>false</notnull>
+ <unsigned>true</unsigned>
+ <length>4</length>
+ </field>
+
+
+ <index>
+ <name>c_addressbookid_index</name>
+ <field>
+ <name>addressbookid</name>
+ <sorting>ascending</sorting>
+ </field>
+ </index>
+ </declaration>
+
+ </table>
+
+</database>
diff --git a/apps/dav/tests/unit/migration/migrateaddressbooktest.php b/apps/dav/tests/unit/migration/migrateaddressbooktest.php
new file mode 100644
index 00000000000..1b27536ce3b
--- /dev/null
+++ b/apps/dav/tests/unit/migration/migrateaddressbooktest.php
@@ -0,0 +1,70 @@
+<?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\Migration;
+
+use OCA\DAV\CardDAV\CardDavBackend;
+use OCA\Dav\Migration\AddressBookAdapter;
+use Test\TestCase;
+
+class MigrateAddressbookTest extends TestCase {
+
+ public function testMigration() {
+ /** @var AddressBookAdapter | \PHPUnit_Framework_MockObject_MockObject $adapter */
+ $adapter = $this->mockAdapter();
+
+ /** @var CardDavBackend | \PHPUnit_Framework_MockObject_MockObject $cardDav */
+ $cardDav = $this->getMockBuilder('\OCA\Dav\CardDAV\CardDAVBackend')->disableOriginalConstructor()->getMock();
+ $cardDav->method('createAddressBook')->willReturn(666);
+ $cardDav->expects($this->once())->method('createAddressBook')->with('principals/users/test01', 'test_contacts');
+ $cardDav->expects($this->once())->method('createCard')->with(666, '63f0dd6c-39d5-44be-9d34-34e7a7441fc2.vcf', 'BEGIN:VCARD');
+
+ $m = new \OCA\Dav\Migration\MigrateAddressbooks($adapter, $cardDav);
+ $m->migrateForUser('test01');
+ }
+
+ /**
+ * @return \PHPUnit_Framework_MockObject_MockObject
+ */
+ private function mockAdapter($shares = []) {
+ $adapter = $this->getMockBuilder('\OCA\Dav\Migration\AddressBookAdapter')->disableOriginalConstructor()->getMock();
+ $adapter->method('foreachBook')->willReturnCallback(function ($user, \Closure $callBack) {
+ $callBack([
+ 'id' => 0,
+ 'userid' => $user,
+ 'displayname' => 'Test Contacts',
+ 'uri' => 'test_contacts',
+ 'description' => 'Contacts to test with',
+ 'ctag' => 1234567890,
+ 'active' => 1
+ ]);
+ });
+ $adapter->method('foreachCard')->willReturnCallback(function ($addressBookId, \Closure $callBack) {
+ $callBack([
+ 'userid' => $addressBookId,
+ 'uri' => '63f0dd6c-39d5-44be-9d34-34e7a7441fc2.vcf',
+ 'carddata' => 'BEGIN:VCARD'
+ ]);
+ });
+ $adapter->method('getShares')->willReturn($shares);
+ return $adapter;
+ }
+
+}