summaryrefslogtreecommitdiffstats
path: root/apps/dav/tests
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 /apps/dav/tests
parent8b3d7d09d52ba169953d6a7d03ab570eb3ceed7a (diff)
parent128d9d7aa624cc013cb8f0dbb79693ee5d6c68dd (diff)
downloadnextcloud-server-6fc326967035959847746e3831bf9a63256fc214.tar.gz
nextcloud-server-6fc326967035959847746e3831bf9a63256fc214.zip
Merge pull request #21333 from owncloud/migrate-contacts
Migrate contacts
Diffstat (limited to 'apps/dav/tests')
-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
3 files changed, 350 insertions, 0 deletions
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;
+ }
+
+}