Blame view
sources/apps/contacts/tests/lib/backend/mock.php
4.26 KB
|
d1bafeea1
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
<?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.
*/
namespace OCA\Contacts\Backend;
class Mock extends AbstractBackend {
public $name = 'mock';
public $addressBooks;
public $contacts;
public $userid;
function __construct($userid = null, $addressBooks = null, $contacts = null) {
$this->userid = $userid ? $userid : \OCP\User::getUser();
$this->addressBooks = $addressBooks;
$this->contacts = $contacts;
if (is_null($this->addressBooks)) {
$this->addressBooks = array(
array(
'id' => 'foo',
'owner' => $userid,
'displayname' => 'd-name',
'permissions' => \OCP\PERMISSION_ALL,
),
);
$this->contacts = array(
'foo' => array(
'123' =>
array(
'id' => '123',
'displayname' => 'Max Mustermann',
'carddata' => file_get_contents(__DIR__ . '/../../data/test1.vcf')
)
),
);
}
}
function getAddressBooksForUser(array $options = array()) {
$books = array();
foreach($this->addressBooks as $book) {
if ($book['owner'] === $this->userid) {
$books[] = $book;
}
}
return $books;
}
function getAddressBook($addressBookId, array $options = array()) {
foreach($this->addressBooks as &$book) {
if ($book['id'] === $addressBookId) {
return $book;
}
}
}
function updateAddressBook($addressBookId, array $changes, array $options = array()) {
if(count($changes) === 0 || !isset($changes['displayname'])) {
return false;
}
foreach($this->addressBooks as &$book) {
if ($book['id'] === $addressBookId) {
foreach($changes as $key => $value) {
$book[$key] = $value;
return true;
}
}
}
return false;
}
function createAddressBook(array $properties, array $options = array()) {
if(count($properties) === 0 || !isset($properties['displayname'])) {
return false;
}
$id = \OC_Util::generateRandomBytes('4');
$this->addressBooks[] = array_merge($properties, array(
'id' => $id,
'permissions' => \OCP\PERMISSION_ALL,
'owner' => $this->userid,
));
return $id;
}
function deleteAddressBook($addressBookId, array $options = array()) {
foreach($this->addressBooks as $key => $value) {
if ($value['id'] === $addressBookId) {
unset($this->addressBooks[$key]);
}
}
if(isset($this->contacts[$addressBookId])) {
unset($this->contacts[$addressBookId]);
return true;
}
return false;
}
function getContacts($addressBookId, array $options = array()) {
$contacts = array();
$omitdata = isset($options['omitdata']) ? $options['omitdata'] : false;
$book = $this->getAddressBook($addressBookId);
if(!$book) {
return $contacts;
}
foreach($this->contacts[$addressBookId] as $contact) {
$contact['permissions'] = $book['permissions'];
$contact['owner'] = $book['owner'];
if($omitdata) {
unset($contact['carddata']);
}
$contacts[] = $contact;
}
return $contacts;
}
function getContact($addressBookId, $id, array $options = array()) {
$book = $this->getAddressBook($addressBookId);
if(!$book) {
return null;
}
if (!isset($this->contacts[$addressBookId][$id])) {
return null;
}
$contact = $this->contacts[$addressBookId][$id];
$contact['permissions'] = $book['permissions'];
$contact['owner'] = $book['owner'];
return $contact;
}
function createContact($addressBookId, $contact, array $options = array()) {
$id = \OC_Util::generateRandomBytes('4');
$this->contacts[$addressBookId][$id] = array(
'id' => $id,
'displayname' => $contact->FN,
'carddata' => $contact->serialize()
);
return $id;
}
function updateContact($addressBookId, $id, $contact, array $options = array()) {
//echo __METHOD__ . $addressBookId .', ' . $id . ', ' . print_r($contact, true);
$this->contacts[$addressBookId][$id] = array(
'displayname' => $contact->FN,
'carddata' => $contact->serialize()
);
return true;
}
function deleteContact($addressBookId, $id, array $options = array()) {
if(isset($this->contacts[$addressBookId][$id])) {
unset($this->contacts[$addressBookId][$id]);
return true;
}
return false;
}
|
|
6d9380f96
|
192 193 194 |
function numContacts($addressBookId) {
return count($this->contacts[$addressBookId]);
}
|
|
d1bafeea1
|
195 |
} |