|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2013 Thomas Tanghus ([email protected]) |
|
4
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
5
|
|
|
* later. |
|
6
|
|
|
* See the COPYING-README file. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OCA\Contacts\Backend; |
|
10
|
|
|
|
|
11
|
|
|
use Sabre\VObject\Reader; |
|
12
|
|
|
use Test\TestCase; |
|
13
|
|
|
|
|
14
|
|
|
require_once __DIR__ .'/mock.php'; |
|
15
|
|
|
|
|
16
|
|
|
class BackendTest extends TestCase { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $abinfo; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $permissions = array( |
|
27
|
|
|
\OCP\PERMISSION_READ, |
|
28
|
|
|
\OCP\PERMISSION_CREATE, |
|
29
|
|
|
\OCP\PERMISSION_UPDATE, |
|
30
|
|
|
\OCP\PERMISSION_DELETE |
|
31
|
|
|
); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \OCA\Contacts\Backend\AbstractBackend |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $backend; |
|
37
|
|
|
|
|
38
|
|
|
public function setUp() { |
|
39
|
|
|
parent::setUp(); |
|
40
|
|
|
$this->backend = new Mock('foobar'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function tearDown() { |
|
44
|
|
|
unset($this->backend); |
|
45
|
|
|
parent::tearDown(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testHasContactMethodFor() { |
|
49
|
|
|
|
|
50
|
|
|
foreach($this->permissions as $permission) { |
|
51
|
|
|
$this->assertTrue($this->backend->hasContactMethodFor($permission)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testHasAddressBookMethodFor() { |
|
57
|
|
|
|
|
58
|
|
|
foreach($this->permissions as $permission) { |
|
59
|
|
|
$this->assertTrue($this->backend->hasAddressBookMethodFor($permission)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testgetAddressBooksForUser() { |
|
65
|
|
|
|
|
66
|
|
|
$this->assertEquals(1, count($this->backend->getAddressBooksForUser())); |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testDeleteAddressBook() { |
|
71
|
|
|
|
|
72
|
|
|
$this->assertTrue($this->backend->deleteAddressBook('foo')); |
|
73
|
|
|
$this->assertEquals(array(), $this->backend->addressBooks); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function testCreateAddressBook() { |
|
78
|
|
|
|
|
79
|
|
|
$id = $this->backend->createAddressBook(array('displayname' => 'bar')); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertNotEquals(false, $id); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals(2, count($this->backend->getAddressBooksForUser())); |
|
84
|
|
|
|
|
85
|
|
|
$book = $this->backend->getAddressBook($id); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals('bar', $book['displayname']); |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testCreateAddressBookFail() { |
|
92
|
|
|
|
|
93
|
|
|
// displayname must be provided. |
|
94
|
|
|
$id = $this->backend->createAddressBook(array('description' => 'foo bar')); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertFalse($id); |
|
97
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
View Code Duplication |
public function testUpdateAddressBook() { |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$this->assertTrue( |
|
103
|
|
|
$this->backend->updateAddressBook('foo', array('displayname' => 'bar')) |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertEquals('bar', $this->backend->addressBooks[0]['displayname']); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
return $this->backend; |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testUpdateAddressBookFail() { |
|
113
|
|
|
|
|
114
|
|
|
$this->assertFalse( |
|
115
|
|
|
$this->backend->updateAddressBook('foo', array('description' => 'foo bar')) |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @depends testUpdateAddressBook |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testGetAddressBook($backend) { |
|
124
|
|
|
|
|
125
|
|
|
$book = $backend->getAddressBook('foo'); |
|
126
|
|
|
$this->assertEquals('bar', $book['displayname']); |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testGetAddressBookFail() { |
|
131
|
|
|
|
|
132
|
|
|
$this->assertNull($this->backend->getAddressBook('bar')); |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testGetLastModifiedAddressBook() { |
|
137
|
|
|
|
|
138
|
|
|
$this->assertNull($this->backend->lastModifiedAddressBook('foo')); |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testGetContact() { |
|
143
|
|
|
|
|
144
|
|
|
$contact = $this->backend->getContact('foo', '123'); |
|
145
|
|
|
$this->assertTrue(is_array($contact)); |
|
146
|
|
|
$this->assertEquals('Max Mustermann', $contact['displayname']); |
|
147
|
|
|
$this->assertEquals('foobar', $contact['owner']); |
|
148
|
|
|
$this->assertEquals(\OCP\PERMISSION_ALL, $contact['permissions']); |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function testGetContactFail() { |
|
153
|
|
|
|
|
154
|
|
|
$this->assertNull($this->backend->getContact('foo', '1234')); |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
View Code Duplication |
public function testCreateContact() { |
|
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
$carddata = file_get_contents(__DIR__ . '/../../data/test2.vcf'); |
|
161
|
|
|
$vcard = Reader::read($carddata); |
|
162
|
|
|
$id = $this->backend->createContact('foo', $vcard); |
|
163
|
|
|
|
|
164
|
|
|
$this->assertNotEquals(false, $id); |
|
165
|
|
|
|
|
166
|
|
|
return $this->backend; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @depends testCreateContact |
|
171
|
|
|
*/ |
|
172
|
|
|
public function testGetContacts($backend) { |
|
173
|
|
|
|
|
174
|
|
|
$contacts = $backend->getContacts('foo'); |
|
175
|
|
|
|
|
176
|
|
|
$this->assertCount(2, $contacts); |
|
177
|
|
|
|
|
178
|
|
|
$this->assertEquals('Max Mustermann', $contacts[0]['displayname']); |
|
179
|
|
|
$this->assertEquals('John Q. Public', $contacts[1]['displayname']); |
|
180
|
|
|
|
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public function testUpdateContact() { |
|
184
|
|
|
|
|
185
|
|
|
$carddata = file_get_contents(__DIR__ . '/../../data/test2.vcf'); |
|
186
|
|
|
$vcard = Reader::read($carddata); |
|
187
|
|
|
|
|
188
|
|
|
$this->assertInstanceOf('OCA\\Contacts\\VObject\\VCard', $vcard); |
|
189
|
|
|
|
|
190
|
|
|
$this->assertTrue($this->backend->updateContact('foo', '123', $vcard)); |
|
191
|
|
|
|
|
192
|
|
|
$contact = $this->backend->getContact('foo', '123'); |
|
193
|
|
|
|
|
194
|
|
|
$this->assertEquals('John Q. Public', $contact['displayname']); |
|
195
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function testDeleteContact() { |
|
199
|
|
|
|
|
200
|
|
|
$this->assertTrue($this->backend->deleteContact('foo', '123')); |
|
201
|
|
|
$this->assertEquals(array(), $this->backend->getContacts('foo')); |
|
202
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.