1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-12-01 13:24:10 +01:00
OwncloudContactsOfficial/tests/contacts.php

128 lines
4.3 KiB
PHP
Raw Normal View History

2013-03-16 15:53:46 +01:00
<?php
/**
* Copyright (c) 2013 Thomas Tanghus (thomas@tanghus.net)
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
OC_App::loadApp('contacts');
class Test_Contacts_Backend_Datebase extends PHPUnit_Framework_TestCase {
2013-03-16 15:53:46 +01:00
protected static $schema_file = 'static://test_db_scheme';
2013-03-17 21:30:10 +01:00
protected static $test_prefix;
protected static $backend;
protected static $user;
protected static $addressBooksTableName;
protected static $cardsTableName;
2013-03-16 15:53:46 +01:00
2013-03-17 21:30:10 +01:00
public static function setUpBeforeClass() {
2013-03-16 15:53:46 +01:00
$dbfile = __DIR__.'/../appinfo/database.xml';
2013-03-17 21:30:10 +01:00
self::$test_prefix = '_'.OC_Util::generate_random_bytes('4').'_';
2013-03-16 15:53:46 +01:00
$content = file_get_contents($dbfile);
2013-03-17 21:30:10 +01:00
$content = str_replace( '*dbprefix*', '*dbprefix*'.self::$test_prefix, $content );
2013-03-16 15:53:46 +01:00
file_put_contents( self::$schema_file, $content );
OC_DB::createDbFromStructure(self::$schema_file);
2013-03-17 21:30:10 +01:00
self::$addressBooksTableName = '*PREFIX*'.self::$test_prefix.'contacts_addressbooks';
self::$cardsTableName = '*PREFIX*'.self::$test_prefix.'contacts_cards';
2013-03-16 15:53:46 +01:00
OC_User::clearBackends();
OC_User::useBackend('dummy');
2013-03-17 21:30:10 +01:00
self::$user = uniqid('user_');
OC_User::createUser(self::$user, 'pass');
OC_User::setUserId(self::$user);
self::$backend = new OCA\Contacts\Backend\Database(
self::$user,
self::$addressBooksTableName,
self::$cardsTableName
);
2013-05-19 00:53:33 +02:00
\Sabre\VObject\Property::$classMap['CATEGORIES'] = 'OCA\Contacts\VObject\GroupProperty';
2013-03-16 15:53:46 +01:00
}
2013-03-17 21:30:10 +01:00
public static function tearDownAfterClass() {
2013-03-16 15:53:46 +01:00
OC_DB::removeDBStructure(self::$schema_file);
unlink(self::$schema_file);
}
public function testDatabaseBackend() {
2013-03-17 21:30:10 +01:00
$this->assertEquals(array(), self::$backend->getAddressBooksForUser());
2013-03-16 15:53:46 +01:00
2013-03-17 21:30:10 +01:00
$aid = self::$backend->createAddressBook(
2013-03-16 15:53:46 +01:00
array(
'displayname' => 'Contacts',
'description' => 'My Contacts',
)
);
// Test address books
2013-03-17 21:30:10 +01:00
$this->assertEquals(1, count(self::$backend->getAddressBooksForUser()));
$this->assertTrue(self::$backend->hasAddressBook($aid));
$addressBook = self::$backend->getAddressBook($aid);
$this->assertEquals('Contacts', $addressBook['displayname']);
$this->assertEquals('My Contacts', $addressBook['description']);
2013-03-17 21:30:10 +01:00
self::$backend->updateAddressBook($aid, array('description' => 'All my contacts'));
$addressBook = self::$backend->getAddressBook($aid);
$this->assertEquals('All my contacts', $addressBook['description']);
// Test contacts
2013-03-17 21:30:10 +01:00
$this->assertEquals(array(), self::$backend->getContacts($aid));
2013-05-15 22:56:52 +02:00
$carddata = file_get_contents(__DIR__ . '/data/test1.vcf');
2013-03-17 21:30:10 +01:00
$id = self::$backend->createContact($aid, $carddata);
$this->assertNotEquals(false, $id); // Isn't there an assertNotFalse() ?
2013-03-17 21:30:10 +01:00
$this->assertEquals(1, count(self::$backend->getContacts($aid)));
$this->assertTrue(self::$backend->hasContact($aid, $id));
$contact = self::$backend->getContact($aid, $id);
$this->assertEquals('Max Mustermann', $contact['displayname']);
$carddata = file_get_contents(__DIR__ . '/data/test2.vcf');
2013-03-17 21:30:10 +01:00
$this->assertTrue(self::$backend->updateContact($aid, $id, $carddata));
$contact = self::$backend->getContact($aid, $id);
$this->assertEquals('John Q. Public', $contact['displayname']);
2013-03-17 21:30:10 +01:00
$this->assertTrue(self::$backend->deleteContact($aid, $id));
$this->assertTrue(self::$backend->deleteAddressBook($aid));
}
2013-03-17 21:30:10 +01:00
public function testAddressBook() {
$addressBook = new OCA\Contacts\AddressBook(
self::$backend,
array(
'displayname' => 'Contacts',
'description' => 'My Contacts',
)
);
$this->assertEquals(0, count($addressBook));
$id = $addressBook->addChild(
array(
'displayname' => 'John Q. Public'
)
);
$this->assertNotEquals(false, $id);
$this->assertEquals(1, count($addressBook));
$contact = $addressBook->getChild($id);
2013-03-25 17:07:49 +01:00
$this->assertEquals('John Q. Public', (string)$contact->FN);
$contact->FN = 'Max Mustermann';
$contact->save();
$contact = $addressBook[$id];
2013-03-25 17:07:49 +01:00
$metadata = $contact->getMetaData();
$this->assertEquals('Max Mustermann', $metadata['displayname']);
2013-04-27 02:19:44 +02:00
$this->assertEquals($contact->getPermissions(), $addressBook->getPermissions());
// Array access
$this->assertEquals($contact, $addressBook[$id]);
$this->assertTrue(isset($addressBook[$id]));
// Magic accessors
2013-03-25 17:07:49 +01:00
//$this->assertEquals($contact, $addressBook->{$id});
$this->assertTrue($addressBook->deleteChild($id));
$this->assertEquals(0, count($addressBook));
2013-08-20 01:14:32 +02:00
$this->assertEquals(0, 1);
2013-03-16 15:53:46 +01:00
}
2013-05-15 22:56:52 +02:00
2013-03-16 15:53:46 +01:00
}