Skip to content

Commit

Permalink
fix(user_ldap): Fix tests using wrong types
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Apr 4, 2024
1 parent c13e2ef commit 5ba403a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
6 changes: 5 additions & 1 deletion apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,11 @@ private function doConnect($host, $port): bool {
return false;
}

$this->ldapConnectionRes = $this->ldap->connect($host, $port);
$this->ldapConnectionRes = $this->ldap->connect($host, $port) ?: null;

if ($this->ldapConnectionRes === null) {
throw new ServerNotAvailableException('Connection failed');
}

if (!$this->ldap->setOption($this->ldapConnectionRes, LDAP_OPT_PROTOCOL_VERSION, 3)) {
throw new ServerNotAvailableException('Could not set required LDAP Protocol version.');
Expand Down
14 changes: 11 additions & 3 deletions apps/user_ldap/tests/AccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ private function getConnectorAndLdapMock() {
$connector = $this->getMockBuilder(Connection::class)
->setConstructorArgs([$lw, '', null])
->getMock();
$connector->expects($this->any())
->method('getConnectionResource')
->willReturn(ldap_connect('ldap://example.com'));
$um = $this->getMockBuilder(Manager::class)
->setConstructorArgs([
$this->createMock(IConfig::class),
Expand Down Expand Up @@ -287,6 +290,11 @@ public function testBatchApplyUserAttributes() {
->method('isResource')
->willReturn(true);

$this->connection
->expects($this->any())
->method('getConnectionResource')
->willReturn(ldap_connect('ldap://example.com'));

$this->ldap->expects($this->any())
->method('getAttributes')
->willReturn(['displayname' => ['bar', 'count' => 1]]);
Expand Down Expand Up @@ -469,7 +477,7 @@ public function testSetPasswordWithLdapNotAvailable() {
$this->connection
->method('__get')
->willReturn(true);
$connection = $this->createMock(LDAP::class);
$connection = ldap_connect('ldap://example.com');
$this->connection
->expects($this->once())
->method('getConnectionResource')
Expand All @@ -492,7 +500,7 @@ public function testSetPasswordWithRejectedChange() {
$this->connection
->method('__get')
->willReturn(true);
$connection = $this->createMock(LDAP::class);
$connection = ldap_connect('ldap://example.com');
$this->connection
->expects($this->any())
->method('getConnectionResource')
Expand All @@ -516,7 +524,7 @@ public function testSetPassword() {
$this->connection
->method('__get')
->willReturn(true);
$connection = $this->createMock(LDAP::class);
$connection = ldap_connect('ldap://example.com');
$this->connection
->expects($this->any())
->method('getConnectionResource')
Expand Down
8 changes: 4 additions & 4 deletions apps/user_ldap/tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function testUseBackupServer() {

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

$this->ldap->expects($this->any())
->method('errno')
Expand Down Expand Up @@ -173,7 +173,7 @@ public function testDontUseBackupServerOnFailedAuth() {

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

$this->ldap->expects($this->any())
->method('errno')
Expand Down Expand Up @@ -221,7 +221,7 @@ public function testBindWithInvalidCredentials() {

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

$this->ldap->expects($this->once())
->method('bind')
Expand Down Expand Up @@ -264,7 +264,7 @@ public function testStartTlsNegotiationFailure() {

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

$this->ldap->expects($this->any())
->method('setOption')
Expand Down
10 changes: 6 additions & 4 deletions apps/user_ldap/tests/LDAPProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,14 +271,15 @@ public function testGetLDAPConnection() {
$userBackend->expects($this->any())
->method('userExists')
->willReturn(true);
$ldapConnection = ldap_connect('ldap://example.com');
$userBackend->expects($this->any())
->method('getNewLDAPConnection')
->willReturn(true);
->willReturn($ldapConnection);

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

$ldapProvider = $this->getLDAPProvider($server);
$this->assertTrue($ldapProvider->getLDAPConnection('existing_user'));
$this->assertEquals($ldapConnection, $ldapProvider->getLDAPConnection('existing_user'));
}


Expand Down Expand Up @@ -317,14 +318,15 @@ public function testGetGroupLDAPConnection() {
->method('groupExists')
->willReturn(true);

$ldapConnection = ldap_connect('ldap://example.com');
$groupBackend->expects($this->any())
->method('getNewLDAPConnection')
->willReturn(true);
->willReturn($ldapConnection);

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

$ldapProvider = $this->getLDAPProvider($server);
$this->assertTrue($ldapProvider->getGroupLDAPConnection('existing_group'));
$this->assertEquals($ldapConnection, $ldapProvider->getGroupLDAPConnection('existing_group'));
}


Expand Down
2 changes: 1 addition & 1 deletion apps/user_ldap/tests/User_LDAPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public function testDeleteUserSuccess() {
->willReturn($mapping);
$this->connection->expects($this->any())
->method('getConnectionResource')
->willReturn('this is an ldap link');
->willReturn(ldap_connect('ldap://example.com'));

$this->deletedUsersIndex->expects($this->once())
->method('isUserMarked')
Expand Down
6 changes: 3 additions & 3 deletions apps/user_ldap/tests/WizardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ private function getWizardAndMocks() {
private function prepareLdapWrapperForConnections(MockObject &$ldap) {
$ldap->expects($this->once())
->method('connect')
//dummy value, usually invalid
->willReturn(true);
//dummy value
->willReturn(ldap_connect('ldap://example.com'));

$ldap->expects($this->exactly(3))
->method('setOption')
Expand Down Expand Up @@ -173,7 +173,7 @@ public function testCumulativeSearchOnAttributeUnlimited() {
$ldap->expects($this->any())
->method('isResource')
->willReturnCallback(function ($r) {
if ($r === true) {
if ($r instanceof \LDAP\Connection) {
return true;
}
if ($r % 24 === 0) {
Expand Down

0 comments on commit 5ba403a

Please sign in to comment.