@@ -98,6 +98,19 @@ public function testNotValidIfCannotFindUser()
9898 $ this ->assertTrue ($ this ->fakeLdap ->_AuthenticateCalled );
9999 }
100100
101+ public function testValidateRethrowsRuntimeExceptionFromConnect ()
102+ {
103+ $ exception = new RuntimeException ("LDAP settings 'host' and 'port' have been removed. Use only the 'uri' setting. " );
104+ $ this ->fakeLdap ->_ConnectException = $ exception ;
105+
106+ $ auth = new Ldap ($ this ->fakeAuth , $ this ->fakeLdap , $ this ->fakeLdapOptions );
107+
108+ $ this ->expectException (RuntimeException::class);
109+ $ this ->expectExceptionMessage ("LDAP settings 'host' and 'port' have been removed " );
110+
111+ $ auth ->Validate ($ this ->username , $ this ->password );
112+ }
113+
101114 public function testDoesNotTryToLoadUserDetailsIfNotValid ()
102115 {
103116 $ this ->fakeLdap ->_ExpectedAuthenticate = false ;
@@ -186,17 +199,15 @@ public function testDoesNotSyncIfUserWasNotFoundInLdap()
186199
187200 public function testConstructsOptionsCorrectly ()
188201 {
189- $ hosts = 'localhost, localhost.2 ' ;
190- $ port = '389 ' ;
202+ $ uris = 'ldap://localhost:389 ldap://localhost.2:389 ' ;
191203 $ binddn = 'cn=admin,ou=users,dc=example,dc=org ' ;
192204 $ password = 'pw ' ;
193205 $ base = 'dc=example,dc=org ' ;
194206 $ starttls = 'false ' ;
195207 $ version = '3 ' ;
196208
197209 $ configFile = new FakeConfigFile ();
198- $ configFile ->SetKey (LdapConfigKeys::HOST , $ hosts );
199- $ configFile ->SetKey (LdapConfigKeys::PORT , $ port );
210+ $ configFile ->SetKey (LdapConfigKeys::URI , $ uris );
200211 $ configFile ->SetKey (LdapConfigKeys::BINDDN , $ binddn );
201212 $ configFile ->SetKey (LdapConfigKeys::BINDPW , $ password );
202213 $ configFile ->SetKey (LdapConfigKeys::BASEDN , $ base );
@@ -209,8 +220,7 @@ public function testConstructsOptionsCorrectly()
209220 $ options = $ ldapOptions ->Ldap2Config ();
210221
211222 $ this ->assertNotNull ($ this ->fakeConfig ->_RegisteredFiles [LdapConfigKeys::CONFIG_ID ]);
212- $ this ->assertEquals ('localhost ' , $ options ['host ' ][0 ], 'domain_controllers must be an array ' );
213- $ this ->assertEquals (intval ($ port ), $ options ['port ' ], 'port should be int ' );
223+ $ this ->assertEquals ('ldap://localhost:389 ' , $ options ['host ' ][0 ], 'controllers must be an array of URIs ' );
214224 $ this ->assertEquals ($ binddn , $ options ['binddn ' ]);
215225 $ this ->assertEquals ($ password , $ options ['bindpw ' ]);
216226 $ this ->assertEquals ($ base , $ options ['basedn ' ]);
@@ -220,15 +230,51 @@ public function testConstructsOptionsCorrectly()
220230
221231 public function testGetAllHosts ()
222232 {
223- $ controllers = 'localhost, localhost.2 ' ;
233+ $ controllers = 'ldap:// localhost:389 ldap:// localhost.2:389 ' ;
224234
225235 $ configFile = new FakeConfigFile ();
226- $ configFile ->SetKey (LdapConfigKeys::HOST , $ controllers );
236+ $ configFile ->SetKey (LdapConfigKeys::URI , $ controllers );
227237 $ this ->fakeConfig ->SetFile (LdapConfigKeys::CONFIG_ID , $ configFile );
228238
229239 $ options = new LdapOptions ();
230240
231- $ this ->assertEquals (['localhost ' , 'localhost.2 ' ], $ options ->Controllers (), 'comma separated values should become array ' );
241+ $ this ->assertEquals (['ldap://localhost:389 ' , 'ldap://localhost.2:389 ' ], $ options ->Controllers (), 'space separated uris should become array ' );
242+ }
243+
244+ public function testThrowsIfLegacyHostIsConfigured ()
245+ {
246+ $ configFile = new FakeConfigFile ();
247+ $ configFile ->SetKey ([
248+ 'key ' => 'host ' ,
249+ 'section ' => 'ldap ' ,
250+ 'type ' => 'string ' ,
251+ ], 'ldap://localhost ' );
252+ $ this ->fakeConfig ->SetFile (LdapConfigKeys::CONFIG_ID , $ configFile );
253+
254+ $ options = new LdapOptions ();
255+
256+ $ this ->expectException (RuntimeException::class);
257+ $ this ->expectExceptionMessage ("LDAP settings 'host' and 'port' have been removed " );
258+
259+ $ options ->Ldap2Config ();
260+ }
261+
262+ public function testThrowsIfLegacyPortIsConfigured ()
263+ {
264+ $ configFile = new FakeConfigFile ();
265+ $ configFile ->SetKey ([
266+ 'key ' => 'port ' ,
267+ 'section ' => 'ldap ' ,
268+ 'type ' => 'string ' ,
269+ ], '389 ' );
270+ $ this ->fakeConfig ->SetFile (LdapConfigKeys::CONFIG_ID , $ configFile );
271+
272+ $ options = new LdapOptions ();
273+
274+ $ this ->expectException (RuntimeException::class);
275+ $ this ->expectExceptionMessage ("LDAP settings 'host' and 'port' have been removed " );
276+
277+ $ options ->Ldap2Config ();
232278 }
233279
234280 public function testUserHandlesArraysAsAttribute ()
@@ -345,6 +391,10 @@ public function testAuthRealLdap()
345391
346392class FakeLdapOptions extends LdapOptions
347393{
394+ public function __construct ()
395+ {
396+ }
397+
348398 public $ _RetryAgainstDatabase = false ;
349399
350400 public function RetryAgainstDatabase ()
@@ -391,9 +441,14 @@ public function __construct()
391441
392442 public $ _ExpectedLdapUser ;
393443
444+ public ?\RuntimeException $ _ConnectException = null ;
445+
394446 public function Connect ()
395447 {
396448 $ this ->_ConnectCalled = true ;
449+ if ($ this ->_ConnectException !== null ) {
450+ throw $ this ->_ConnectException ;
451+ }
397452 return $ this ->_ExpectedConnect ;
398453 }
399454
0 commit comments