1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoL\LdapBundle\Tests\Ldap; |
4
|
|
|
|
5
|
|
|
use DoL\LdapBundle\Event\SwitchParameterSetEvent; |
6
|
|
|
use DoL\LdapBundle\Hydrator\HydratorInterface; |
7
|
|
|
use DoL\LdapBundle\Ldap\LdapManager; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \DoL\LdapBundle\Ldap\LdapManager |
13
|
|
|
*/ |
14
|
|
|
class LdapManagerTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
/** @var array */ |
17
|
|
|
protected $paramSets; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \DoL\LdapBundle\Driver\LdapDriverInterface|\PHPUnit_Framework_MockObject_MockObject |
21
|
|
|
*/ |
22
|
|
|
protected $driver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var HydratorInterface|\PHPUnit_Framework_MockObject_MockObject |
26
|
|
|
*/ |
27
|
|
|
protected $hydrator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject |
31
|
|
|
*/ |
32
|
|
|
protected $eventDispatcher; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var LdapManager |
36
|
|
|
*/ |
37
|
|
|
protected $ldapManager; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets up the fixture, for example, opens a network connection. |
41
|
|
|
* This method is called before a test is executed. |
42
|
|
|
*/ |
43
|
|
|
protected function setUp() |
44
|
|
|
{ |
45
|
|
|
$this->paramSets = [ |
46
|
|
|
'server1' => [ |
47
|
|
|
'driver' => [ |
48
|
|
|
// SOME ATTRIBUTES |
49
|
|
|
], |
50
|
|
|
'user' => [ |
51
|
|
|
'baseDn' => 'ou=Groups,dc=example,dc=com', |
52
|
|
|
'filter' => '(attr0=value0)', |
53
|
|
|
'attributes' => [ |
54
|
|
|
[ |
55
|
|
|
'ldap_attr' => 'uid', |
56
|
|
|
'user_method' => 'setUsername', |
57
|
|
|
], |
58
|
|
|
], |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
$this->driver = $this->getMock('DoL\LdapBundle\Driver\LdapDriverInterface'); |
64
|
|
|
|
65
|
|
|
$this->hydrator = $this->getMock('DoL\LdapBundle\Hydrator\HydratorInterface'); |
66
|
|
|
|
67
|
|
|
$this->eventDispatcher = $this->getMock(EventDispatcherInterface::class); |
68
|
|
|
|
69
|
|
|
$this->ldapManager = new LdapManager($this->driver, $this->hydrator, $this->eventDispatcher, $this->paramSets); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @covers \DoL\LdapBundle\Ldap\LdapManager::findUserByUsername |
74
|
|
|
*/ |
75
|
|
|
public function testFindUserByUsername() |
76
|
|
|
{ |
77
|
|
|
$username = 'test_username'; |
78
|
|
|
|
79
|
|
|
$ldapResponse = $this->ldapResponse($username); |
80
|
|
|
|
81
|
|
|
$this->driver |
82
|
|
|
->expects($this->once()) |
83
|
|
|
->method('search') |
84
|
|
|
->with($this->equalTo('ou=Groups,dc=example,dc=com'), |
85
|
|
|
$this->equalTo('(&(attr0=value0)(uid=test_username))') |
86
|
|
|
) |
87
|
|
|
->will($this->returnValue($ldapResponse)) |
88
|
|
|
; |
89
|
|
|
|
90
|
|
|
$resultUser = $this->ldapManager->findUserByUsername($username); |
91
|
|
|
|
92
|
|
|
self::assertEquals($username, $resultUser->getUsername()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @covers \DoL\LdapBundle\Ldap\LdapManager::findUserBy |
97
|
|
|
*/ |
98
|
|
|
public function testFindUserBy() |
99
|
|
|
{ |
100
|
|
|
$username = 'test_username'; |
101
|
|
|
|
102
|
|
|
$ldapResponse = $this->ldapResponse($username); |
103
|
|
|
|
104
|
|
|
$this->driver |
105
|
|
|
->expects($this->once()) |
106
|
|
|
->method('search') |
107
|
|
|
->with($this->equalTo('ou=Groups,dc=example,dc=com'), |
108
|
|
|
$this->equalTo('(&(attr0=value0)(uid=test_username))') |
109
|
|
|
) |
110
|
|
|
->will($this->returnValue($ldapResponse)) |
111
|
|
|
; |
112
|
|
|
|
113
|
|
|
$criteria = ['uid' => 'test_username']; |
114
|
|
|
$resultUser = $this->ldapManager->findUserBy($criteria); |
115
|
|
|
|
116
|
|
|
self::assertEquals($username, $resultUser->getUsername()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @covers \DoL\LdapBundle\Ldap\LdapManager::buildFilter |
121
|
|
|
*/ |
122
|
|
|
public function testBuildFilter() |
123
|
|
|
{ |
124
|
|
|
$reflectionClass = new \ReflectionClass('DoL\LdapBundle\Ldap\LdapManager'); |
125
|
|
|
$method = $reflectionClass->getMethod('buildFilter'); |
126
|
|
|
$method->setAccessible(true); |
127
|
|
|
$params = $reflectionClass->getProperty('params'); |
128
|
|
|
$params->setAccessible(true); |
129
|
|
|
$params->setValue($this->ldapManager, $this->paramSets['server1']['user']); |
130
|
|
|
|
131
|
|
|
$criteria = array( |
132
|
|
|
'attr1' => 'value1', |
133
|
|
|
'attr2' => 'value2', |
134
|
|
|
); |
135
|
|
|
$expected = '(&(attr0=value0)(attr1=value1)(attr2=value2))'; |
136
|
|
|
|
137
|
|
|
self::assertEquals($expected, $method->invoke($this->ldapManager, $criteria)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @covers \DoL\LdapBundle\Ldap\LdapManager::bind |
142
|
|
|
*/ |
143
|
|
|
public function testBind() |
144
|
|
|
{ |
145
|
|
|
$password = 'password'; |
146
|
|
|
|
147
|
|
|
/** @var UserInterface $user */ |
148
|
|
|
$user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); |
149
|
|
|
|
150
|
|
|
$this->driver->expects($this->once()) |
151
|
|
|
->method('bind') |
152
|
|
|
->with($user, $this->equalTo($password)) |
153
|
|
|
->will($this->returnValue(true)); |
154
|
|
|
$this->eventDispatcher->expects($this->once()) |
155
|
|
|
->method('dispatch') |
156
|
|
|
->with( |
157
|
|
|
$this->equalTo('dol_ldap.manager.switch_parameter_set'), |
158
|
|
|
$this->callback(function(SwitchParameterSetEvent $event){ |
159
|
|
|
$parameterSet = $event->getParameterSet(); |
160
|
|
|
return (is_array($parameterSet) AND isset($parameterSet['driver'])); |
161
|
|
|
}) |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
self::assertTrue($this->ldapManager->bind($user, $password)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param $username |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
protected function ldapResponse($username) |
173
|
|
|
{ |
174
|
|
|
$entry = [ |
175
|
|
|
'dn' => 'ou=group, dc=host, dc=foo', |
176
|
|
|
'uid' => [ |
177
|
|
|
'count' => 1, |
178
|
|
|
0 => $username, |
179
|
|
|
], |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
$entries = [ |
183
|
|
|
'count' => 1, |
184
|
|
|
$entry, |
185
|
|
|
]; |
186
|
|
|
|
187
|
|
|
$user = $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserInterface'); |
188
|
|
|
$user->expects($this->any()) |
189
|
|
|
->method('getUsername') |
190
|
|
|
->willReturn($username) |
191
|
|
|
; |
192
|
|
|
|
193
|
|
|
$this->hydrator->expects($this->once()) |
194
|
|
|
->method('hydrate') |
195
|
|
|
->with($entry) |
196
|
|
|
->willReturn($user) |
197
|
|
|
; |
198
|
|
|
|
199
|
|
|
return $entries; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|