From c04ac78d3db49c43dee57b0e16055c1c70b40753 Mon Sep 17 00:00:00 2001
From: Franck Deroche <defr@ows.fr>
Date: Wed, 18 Jan 2012 19:30:46 +0100
Subject: [PATCH 2/2] bug #1408190 by DeFr: Fix port check for url with
 credentials

The regexp introduced in d222449 doesn't take into account urls that
embed credentials, of the form http://login:pass@host:port/path.
---
 apachesolr.admin.inc |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/apachesolr.admin.inc b/apachesolr.admin.inc
index 9fd810a..d204c12 100644
--- a/apachesolr.admin.inc
+++ b/apachesolr.admin.inc
@@ -200,7 +200,8 @@ function apachesolr_environment_edit_validate($form, &$form_state) {
   if (isset($parts['port'])) {
     // parse_url() should always give an integer for port. Since drupal_http_request()
     // also uses parse_url(), we don't need to validate anything except the range.
-    preg_match('@://[^:]+:([^/]+)@', $form_state['values']['url'], $m);
+    $pattern = empty($parts['user']) ? '@://[^:]+:([^/]+)@' : '#://[^@]+@[^:]+:([^/]+)#';
+    preg_match($pattern, $form_state['values']['url'], $m);
     if (empty($m[1]) || !ctype_digit($m[1]) || $m[1] < 1 || $m[1] > 65535) {
       form_set_error('port', t('The port has to be an integer between 1 and 65535.'));
     }
diff --git a/tests/apachesolr_base.test b/tests/apachesolr_base.test
index ce11f3c..aaabfca 100644
--- a/tests/apachesolr_base.test
+++ b/tests/apachesolr_base.test
@@ -110,6 +110,32 @@ class DrupalSolrOfflineEnvironmentWebTestCase extends DrupalWebTestCase {
   }
 
   /**
+   *	Asserts that we can use various url forms for the search environment
+   */
+  function testEditSearchEnvironmentURLs() {
+    // Set the various url schemes that will be tested
+    $urls = array(
+      'http://user@localhost:8983/solr/core_does_not_exists',
+      'http://user:pass@localhost:8983/solr/core_does_not_exists',
+      'http://user:pass@localhost/solr/core_does_not_exists',
+      'https://localhost:8983/solr/core_does_not_exists'
+    );
+    $this->drupalLogin($this->admin_user);
+    foreach ($urls as $url) {
+      $this->drupalGet('admin/config/search/apachesolr/settings');
+      $this->clickLink(t('Edit'));
+      $this->assertText(t('Example: http://localhost:8983/solr'), t('Edit page was succesfully loaded'));
+      $edit = array('url' => $url);
+      $this->drupalPost($this->getUrl(), $edit, t('Save'));
+      $this->assertResponse(200);
+      drupal_static_reset('apachesolr_load_all_environments');
+      drupal_static_reset('apachesolr_get_solr');
+      $this->drupalGet('admin/config/search/apachesolr/settings');
+      $this->assertText($url, t('Search environment url was succesfully set to !url', array('!url' => $url)));
+    }
+  }
+
+  /**
    *	Asserts that we can edit a search environment
    */
   function testCloneSearchEnvironment() {
