Skip to content

Commit cd68ed3

Browse files
committed
fix(user_ldap): Fix tests using wrong types
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 87bf78c commit cd68ed3

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

apps/user_ldap/lib/Connection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,11 @@ private function doConnect($host, $port): bool {
650650
return false;
651651
}
652652

653-
$this->ldapConnectionRes = $this->ldap->connect($host, $port);
653+
$this->ldapConnectionRes = $this->ldap->connect($host, $port) ?: null;
654+
655+
if ($this->ldapConnectionRes === null) {
656+
throw new ServerNotAvailableException('Connection failed');
657+
}
654658

655659
if (!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
656660
throw new ServerNotAvailableException('Could not set required LDAP Protocol version.');

apps/user_ldap/tests/AccessTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ private function getConnectorAndLdapMock() {
114114
$connector = $this->getMockBuilder(Connection::class)
115115
->setConstructorArgs([$lw, '', null])
116116
->getMock();
117+
$connector->expects($this->any())
118+
->method('getConnectionResource')
119+
->willReturn(ldap_connect('ldap://example.com'));
117120
$um = $this->getMockBuilder(Manager::class)
118121
->setConstructorArgs([
119122
$this->createMock(IConfig::class),
@@ -287,6 +290,11 @@ public function testBatchApplyUserAttributes() {
287290
->method('isResource')
288291
->willReturn(true);
289292

293+
$this->connection
294+
->expects($this->any())
295+
->method('getConnectionResource')
296+
->willReturn(ldap_connect('ldap://example.com'));
297+
290298
$this->ldap->expects($this->any())
291299
->method('getAttributes')
292300
->willReturn(['displayname' => ['bar', 'count' => 1]]);
@@ -469,7 +477,7 @@ public function testSetPasswordWithLdapNotAvailable() {
469477
$this->connection
470478
->method('__get')
471479
->willReturn(true);
472-
$connection = $this->createMock(LDAP::class);
480+
$connection = ldap_connect('ldap://example.com');
473481
$this->connection
474482
->expects($this->once())
475483
->method('getConnectionResource')
@@ -492,7 +500,7 @@ public function testSetPasswordWithRejectedChange() {
492500
$this->connection
493501
->method('__get')
494502
->willReturn(true);
495-
$connection = $this->createMock(LDAP::class);
503+
$connection = ldap_connect('ldap://example.com');
496504
$this->connection
497505
->expects($this->any())
498506
->method('getConnectionResource')
@@ -516,7 +524,7 @@ public function testSetPassword() {
516524
$this->connection
517525
->method('__get')
518526
->willReturn(true);
519-
$connection = $this->createMock(LDAP::class);
527+
$connection = ldap_connect('ldap://example.com');
520528
$this->connection
521529
->expects($this->any())
522530
->method('getConnectionResource')

apps/user_ldap/tests/ConnectionTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function testUseBackupServer() {
114114

115115
$this->ldap->expects($this->exactly(3))
116116
->method('connect')
117-
->willReturn('ldapResource');
117+
->willReturn(ldap_connect('ldap://example.com'));
118118

119119
$this->ldap->expects($this->any())
120120
->method('errno')
@@ -173,7 +173,7 @@ public function testDontUseBackupServerOnFailedAuth() {
173173

174174
$this->ldap->expects($this->once())
175175
->method('connect')
176-
->willReturn('ldapResource');
176+
->willReturn(ldap_connect('ldap://example.com'));
177177

178178
$this->ldap->expects($this->any())
179179
->method('errno')
@@ -221,7 +221,7 @@ public function testBindWithInvalidCredentials() {
221221

222222
$this->ldap->expects($this->any())
223223
->method('connect')
224-
->willReturn('ldapResource');
224+
->willReturn(ldap_connect('ldap://example.com'));
225225

226226
$this->ldap->expects($this->once())
227227
->method('bind')
@@ -264,7 +264,7 @@ public function testStartTlsNegotiationFailure() {
264264

265265
$this->ldap->expects($this->any())
266266
->method('connect')
267-
->willReturn('ldapResource');
267+
->willReturn(ldap_connect('ldap://example.com'));
268268

269269
$this->ldap->expects($this->any())
270270
->method('setOption')

apps/user_ldap/tests/LDAPProviderTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,15 @@ public function testGetLDAPConnection() {
271271
$userBackend->expects($this->any())
272272
->method('userExists')
273273
->willReturn(true);
274+
$ldapConnection = ldap_connect('ldap://example.com');
274275
$userBackend->expects($this->any())
275276
->method('getNewLDAPConnection')
276-
->willReturn(true);
277+
->willReturn($ldapConnection);
277278

278279
$server = $this->getServerMock($userBackend, $this->getDefaultGroupBackendMock());
279280

280281
$ldapProvider = $this->getLDAPProvider($server);
281-
$this->assertTrue($ldapProvider->getLDAPConnection('existing_user'));
282+
$this->assertEquals($ldapConnection, $ldapProvider->getLDAPConnection('existing_user'));
282283
}
283284

284285

@@ -317,14 +318,15 @@ public function testGetGroupLDAPConnection() {
317318
->method('groupExists')
318319
->willReturn(true);
319320

321+
$ldapConnection = ldap_connect('ldap://example.com');
320322
$groupBackend->expects($this->any())
321323
->method('getNewLDAPConnection')
322-
->willReturn(true);
324+
->willReturn($ldapConnection);
323325

324326
$server = $this->getServerMock($userBackend, $groupBackend);
325327

326328
$ldapProvider = $this->getLDAPProvider($server);
327-
$this->assertTrue($ldapProvider->getGroupLDAPConnection('existing_group'));
329+
$this->assertEquals($ldapConnection, $ldapProvider->getGroupLDAPConnection('existing_group'));
328330
}
329331

330332

apps/user_ldap/tests/User_LDAPTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ public function testDeleteUserSuccess() {
320320
->willReturn($mapping);
321321
$this->connection->expects($this->any())
322322
->method('getConnectionResource')
323-
->willReturn('this is an ldap link');
323+
->willReturn(ldap_connect('ldap://example.com'));
324324

325325
$this->deletedUsersIndex->expects($this->once())
326326
->method('isUserMarked')

apps/user_ldap/tests/WizardTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ private function getWizardAndMocks() {
8484
private function prepareLdapWrapperForConnections(MockObject &$ldap) {
8585
$ldap->expects($this->once())
8686
->method('connect')
87-
//dummy value, usually invalid
88-
->willReturn(true);
87+
//dummy value
88+
->willReturn(ldap_connect('ldap://example.com'));
8989

9090
$ldap->expects($this->exactly(3))
9191
->method('setOption')
@@ -173,7 +173,7 @@ public function testCumulativeSearchOnAttributeUnlimited() {
173173
$ldap->expects($this->any())
174174
->method('isResource')
175175
->willReturnCallback(function ($r) {
176-
if ($r === true) {
176+
if ($r instanceof \LDAP\Connection) {
177177
return true;
178178
}
179179
if ($r % 24 === 0) {

0 commit comments

Comments
 (0)