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 Test\TestCase; |
12
|
|
|
|
13
|
|
|
class AddressBookProviderTest extends TestCase { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $abinfo; |
19
|
|
|
/** |
20
|
|
|
* @var \OCA\Contacts\Addressbook |
21
|
|
|
*/ |
22
|
|
|
protected $ab; |
23
|
|
|
/** |
24
|
|
|
* @var \OCA\Contacts\Backend\AbstractBackend |
25
|
|
|
*/ |
26
|
|
|
protected $backend; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \OCA\Contacts\AddressbookProvider |
30
|
|
|
*/ |
31
|
|
|
private $provider; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $testUser; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $contactIds = array(); |
42
|
|
|
|
43
|
|
|
public function setUp() { |
44
|
|
|
parent::setUp(); |
45
|
|
|
|
46
|
|
|
$this->testUser = $this->getUniqueID('user_'); |
47
|
|
|
// needed because some parts of code call "getRequest()" and "getSession()" |
48
|
|
|
$session = $this->getMockBuilder('\OC\Session\Memory') |
49
|
|
|
->disableOriginalConstructor() |
50
|
|
|
->getMock(); |
51
|
|
|
$session->expects($this->any()) |
52
|
|
|
->method('get') |
53
|
|
|
->with('user_id') |
54
|
|
|
->will($this->returnValue($this->testUser)); |
55
|
|
|
$userObject = $this->getMock('\OCP\IUser'); |
56
|
|
|
$userObject->expects($this->any()) |
57
|
|
|
->method('getUId') |
58
|
|
|
->will($this->returnValue($this->testUser)); |
59
|
|
|
$userSession = $this->getMockBuilder('\OC\User\Session') |
60
|
|
|
->disableOriginalConstructor() |
61
|
|
|
->getMock(); |
62
|
|
|
$userSession->expects($this->any()) |
63
|
|
|
->method('getUser') |
64
|
|
|
->will($this->returnValue($userObject)); |
65
|
|
|
$userSession->expects($this->any()) |
66
|
|
|
->method('getSession') |
67
|
|
|
->will($this->returnValue($session)); |
68
|
|
|
\OC::$server->registerService('UserSession', function (\OCP\IServerContainer $c) use ($userSession){ |
|
|
|
|
69
|
|
|
return $userSession; |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
$this->backend = new Backend\Database($this->testUser); |
74
|
|
|
$this->abinfo = array('displayname' => uniqid('display_')); |
75
|
|
|
$this->ab = new AddressBook($this->backend, $this->abinfo); |
76
|
|
|
$this->provider = new AddressbookProvider($this->ab); |
77
|
|
|
|
78
|
|
|
$card = new \OCA\Contacts\VObject\VCard(); |
|
|
|
|
79
|
|
|
$uid = substr(md5($this->getUniqueID()), 0, 10); |
80
|
|
|
$card->add('UID', $uid); |
81
|
|
|
$card->add('FN', 'Max Mustermann'); |
82
|
|
|
$id = $this->ab->addChild($card); |
83
|
|
|
Utils\Properties::updateIndex($id, $card); |
|
|
|
|
84
|
|
|
$this->contactIds[] = $id; |
85
|
|
|
|
86
|
|
|
// Add extra contact |
87
|
|
|
$card = new \OCA\Contacts\VObject\VCard(); |
|
|
|
|
88
|
|
|
$uid = substr(md5(rand().time()), 0, 10); |
89
|
|
|
$card->add('UID', $uid); |
90
|
|
|
$card->add('FN', 'Jan Janssens'); |
91
|
|
|
$id = $this->ab->addChild($card); |
92
|
|
|
Utils\Properties::updateIndex($id, $card); |
|
|
|
|
93
|
|
|
$this->contactIds[] = $id; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function tearDown() { |
97
|
|
|
unset($this->backend); |
98
|
|
|
unset($this->ab); |
99
|
|
|
Utils\Properties::purgeIndexes($this->contactIds); |
100
|
|
|
|
101
|
|
|
parent::tearDown(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @dataProvider providesSearchData |
106
|
|
|
*/ |
107
|
|
|
public function testSearch($expected, $pattern) { |
108
|
|
|
$result = $this->provider->search($pattern, ['FN'], array()); |
109
|
|
|
|
110
|
|
|
$this->assertTrue(is_array($result)); |
111
|
|
|
$this->assertEquals(count($expected), count($result)); |
112
|
|
|
$result = array_map(function($c){ |
113
|
|
|
return $c['FN']; |
114
|
|
|
}, $result); |
115
|
|
|
$this->assertEquals($expected, $result, '', 0.0, 10, true); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function providesSearchData() { |
119
|
|
|
return [ |
120
|
|
|
'empty pattern' => [['Max Mustermann', 'Jan Janssens'], ''], |
121
|
|
|
'case insensitive' => [['Max Mustermann'], 'max'], |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.