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

99 lines
3.4 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-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));
$carddata = file_get_contents(__DIR__ . '/data/test.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',
'permissions' => OCP\PERMISSION_ALL,
'owner' => self::$user,
)
);
2013-03-16 15:53:46 +01:00
}
}