Index: modules/locale/locale.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/locale/locale.test,v
retrieving revision 1.79
diff -u -p -r1.79 locale.test
--- modules/locale/locale.test	1 Sep 2010 20:08:17 -0000	1.79
+++ modules/locale/locale.test	1 Oct 2010 01:39:38 -0000
@@ -1811,6 +1811,47 @@ class LocaleUILanguageNegotiationTest ex
     $this->drupalGet($test['path'], array(), $test['http_header']);
     $this->assertText($test['expect'], $test['message']);
   }
+
+  /**
+   * Test that URL rewriting works as expected with non-enabled languages.
+   */
+  function testUrlRewritingEdgeCases() {
+    // Check that disabled or non-installed languages are not considered for URL
+    // rewriting. To test this we need to enable URL language negotiation and
+    // have at least two languages enabled and one disabled.
+    // We need to use API functions instead of using the administration UI
+    // because url() will act in the current execution context.
+    require_once DRUPAL_ROOT . '/includes/language.inc';
+    require_once DRUPAL_ROOT . '/includes/locale.inc';
+    locale_add_language('fr');
+    locale_add_language('it', 'Italian', 'Italiano', LANGUAGE_LTR, '', 'it', 0);
+    language_negotiation_set(LANGUAGE_TYPE_INTERFACE, array(LOCALE_LANGUAGE_NEGOTIATION_URL => 0));
+    drupal_language_initialize();
+
+    // Check URL rewriting with a disabled language.
+    drupal_static_reset('language_list');
+    $languages = language_list();
+    $this->checkUrl($languages['it'], t('Path language is ignored if language is disabled.'), t('URL language negotiation does not work with disabled languages'));
+
+    // Check URL rewriting with a non-installed language.
+    $non_existing = language_default();
+    $non_existing->language = $this->randomName();
+    $non_existing->prefix = $this->randomName();
+    $this->checkUrl($non_existing, t('Path language is ignored if language is not installed.'), t('URL language negotiation does not work with non-installed languages'));
+  }
+
+  private function checkUrl($language, $message1, $message2) {
+    $options = array('language' => $language);
+    $rewritten_path = trim(str_replace('?q=', '', url('node', $options)), '/');
+    $segments = explode('/', $rewritten_path, 2);
+    $prefix = $segments[0];
+    $path = isset($segments[1]) ? $segments[1] : $prefix;
+    $prefixed = !$this->assertNotEqual($language->prefix, $prefix, $message1);
+    // If the rewritten URL has a language prefix we get the unprefixed system
+    // path to avoid having it prefixed two times.
+    $this->drupalGet($prefixed ? $path : "$prefix/$path", $options);
+    $this->assertResponse(404, $message2);
+  }
 }
 
 /**
