|
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; |
|
10
|
|
|
|
|
11
|
|
|
use Sabre\VObject\Reader; |
|
12
|
|
|
use Test\TestCase; |
|
13
|
|
|
|
|
14
|
|
|
require_once __DIR__ . '/backend/mock.php'; |
|
15
|
|
|
|
|
16
|
|
|
class AddressBookTest extends TestCase { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $abinfo; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var \OCA\Contacts\Addressbook |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $ab; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var \OCA\Contacts\Backend\AbstractBackend |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $backend; |
|
30
|
|
|
|
|
31
|
|
|
public function setUp() { |
|
32
|
|
|
parent::setUp(); |
|
33
|
|
|
|
|
34
|
|
|
$this->backend = new Backend\Mock('foobar'); |
|
35
|
|
|
$this->abinfo = $this->backend->getAddressBook('foo'); |
|
|
|
|
|
|
36
|
|
|
$this->ab = new AddressBook($this->backend, $this->abinfo); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function tearDown() { |
|
41
|
|
|
unset($this->backend); |
|
42
|
|
|
unset($this->ab); |
|
43
|
|
|
|
|
44
|
|
|
parent::tearDown(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testGetDisplayName() { |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEquals('d-name', $this->ab->getDisplayName()); |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testGetPermissions() { |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals(\OCP\PERMISSION_ALL, $this->ab->getPermissions()); |
|
56
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testGetBackend() { |
|
60
|
|
|
|
|
61
|
|
|
$this->assertEquals($this->backend, $this->ab->getBackend()); |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testGetChild() { |
|
66
|
|
|
|
|
67
|
|
|
$contact = $this->ab->getChild('123'); |
|
68
|
|
|
$this->assertInstanceOf('OCA\\Contacts\\Contact', $contact); |
|
69
|
|
|
$this->assertEquals('Max Mustermann', $contact->getDisplayName()); |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testChildExists() { |
|
74
|
|
|
|
|
75
|
|
|
$this->assertFalse($this->ab->childExists('12')); |
|
76
|
|
|
$this->assertTrue($this->ab->childExists('123')); |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
View Code Duplication |
public function testAddChild() { |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$carddata = file_get_contents(__DIR__ . '/../data/test2.vcf'); |
|
83
|
|
|
$vcard = Reader::read($carddata); |
|
84
|
|
|
$id = $this->ab->addChild($vcard); |
|
|
|
|
|
|
85
|
|
|
$this->assertNotEquals(false, $id); |
|
86
|
|
|
|
|
87
|
|
|
return $this->ab; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testDeleteChild() { |
|
91
|
|
|
|
|
92
|
|
|
$this->assertTrue($this->ab->deleteChild('123')); |
|
93
|
|
|
$this->assertEquals(array(), $this->ab->getChildren()); |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testGetChildNotFound() { |
|
98
|
|
|
|
|
99
|
|
|
try { |
|
100
|
|
|
$contact = $this->ab->getChild('Nowhere'); |
|
|
|
|
|
|
101
|
|
|
} catch(\Exception $e) { |
|
102
|
|
|
$this->assertEquals('Contact not found', $e->getMessage()); |
|
103
|
|
|
$this->assertEquals(404, $e->getCode()); |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->fail('Expected Exception 404.'); |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @depends testAddChild |
|
113
|
|
|
* @param $ab |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testGetChildren($ab) { |
|
116
|
|
|
|
|
117
|
|
|
$contacts = $ab->getChildren(); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertCount(2, $contacts); |
|
120
|
|
|
|
|
121
|
|
|
$this->assertEquals('Max Mustermann', $contacts[0]->getDisplayName()); |
|
122
|
|
|
$this->assertEquals('John Q. Public', $contacts[1]->getDisplayName()); |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testDelete() { |
|
127
|
|
|
|
|
128
|
|
|
$this->assertTrue($this->ab->delete()); |
|
129
|
|
|
$this->assertEquals(array(), $this->backend->addressBooks); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function testGetLastModified() { |
|
134
|
|
|
|
|
135
|
|
|
$this->assertNull($this->ab->lastModified()); |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
View Code Duplication |
public function testUpdate() { |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
$this->assertTrue( |
|
142
|
|
|
$this->ab->update(array('displayname' => 'bar')) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertEquals('bar', $this->backend->addressBooks[0]['displayname']); |
|
|
|
|
|
|
146
|
|
|
$props = $this->ab->getMetaData(); |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
return $this->ab; |
|
149
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @depends testUpdate |
|
154
|
|
|
*/ |
|
155
|
|
|
public function testGetMetaData($ab) { |
|
156
|
|
|
|
|
157
|
|
|
$props = $ab->getMetaData(); |
|
158
|
|
|
$this->assertEquals('bar', $props['displayname']); |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function testArrayAccess() { |
|
163
|
|
|
|
|
164
|
|
|
$carddata = file_get_contents(__DIR__ . '/../data/test2.vcf'); |
|
165
|
|
|
$vcard = Reader::read($carddata); |
|
166
|
|
|
|
|
167
|
|
|
$contact = $this->ab['123']; |
|
168
|
|
|
|
|
169
|
|
|
// Test get |
|
170
|
|
|
$this->assertTrue(isset($this->ab['123'])); |
|
171
|
|
|
$this->assertInstanceOf('OCA\\Contacts\\Contact', $contact); |
|
172
|
|
|
$this->assertEquals('Max Mustermann', $contact->getDisplayName()); |
|
173
|
|
|
|
|
174
|
|
|
// Test unset |
|
175
|
|
|
unset($this->ab['123']); |
|
176
|
|
|
|
|
177
|
|
|
$this->assertTrue(!isset($this->ab['123'])); |
|
178
|
|
|
|
|
179
|
|
|
// Test set |
|
180
|
|
|
try { |
|
181
|
|
|
$this->ab[] = $vcard; |
|
182
|
|
|
} catch(\Exception $e) { |
|
183
|
|
|
return; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$this->fail('Expected Exception'); |
|
187
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @depends testAddChild |
|
192
|
|
|
*/ |
|
193
|
|
|
public function testIterator($ab) { |
|
194
|
|
|
|
|
195
|
|
|
$count = 0; |
|
196
|
|
|
|
|
197
|
|
|
foreach($ab as $contact) { |
|
198
|
|
|
$this->assertInstanceOf('OCA\\Contacts\\Contact', $contact); |
|
199
|
|
|
$count += 1; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$this->assertEquals(2, $count); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @depends testAddChild |
|
207
|
|
|
*/ |
|
208
|
|
|
public function testCountable($ab) { |
|
209
|
|
|
|
|
210
|
|
|
$this->assertEquals(2, count($ab)); |
|
211
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
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.