summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2016-01-21 15:51:51 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-01-27 12:10:06 +0100
commit8f4ab55b4b5b57c2f46a9313c94f9f857a0fbaf7 (patch)
tree79c681e816ac024eed0f6190dc5a1bb8aa258282 /apps
parentb2976eb72c0700fa94dbcbebf9a2afa8c5c06a86 (diff)
downloadnextcloud-server-8f4ab55b4b5b57c2f46a9313c94f9f857a0fbaf7.tar.gz
nextcloud-server-8f4ab55b4b5b57c2f46a9313c94f9f857a0fbaf7.zip
Unit testing database access to old contacts tables
Diffstat (limited to 'apps')
-rw-r--r--apps/dav/lib/migration/addressbookadapter.php6
-rw-r--r--apps/dav/tests/unit/migration/addressbookadaptertest.php113
-rw-r--r--apps/dav/tests/unit/migration/contacts_schema.xml151
3 files changed, 267 insertions, 3 deletions
diff --git a/apps/dav/lib/migration/addressbookadapter.php b/apps/dav/lib/migration/addressbookadapter.php
index 025aa07a550..a86790d1f2a 100644
--- a/apps/dav/lib/migration/addressbookadapter.php
+++ b/apps/dav/lib/migration/addressbookadapter.php
@@ -39,8 +39,8 @@ class AddressBookAdapter {
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('user', $query->createNamedParameter($user)))
+ $stmt = $query->select('*')->from($this->sourceBookTable)
+ ->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
->execute();
while($row = $stmt->fetch()) {
@@ -60,7 +60,7 @@ class AddressBookAdapter {
*/
public function foreachCard($addressBookId, \Closure $callBack) {
$query = $this->dbConnection->getQueryBuilder();
- $stmt = $query->select()->from($this->sourceCardsTable)
+ $stmt = $query->select('*')->from($this->sourceCardsTable)
->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId)))
->execute();
diff --git a/apps/dav/tests/unit/migration/addressbookadaptertest.php b/apps/dav/tests/unit/migration/addressbookadaptertest.php
new file mode 100644
index 00000000000..c011fcd13ff
--- /dev/null
+++ b/apps/dav/tests/unit/migration/addressbookadaptertest.php
@@ -0,0 +1,113 @@
+<?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();
+
+ // 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']);
+ }
+
+}
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>