Index: modules/simpletest/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.info
===================================================================
RCS file: modules/simpletest/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.info
diff -N modules/simpletest/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.info	9 Oct 2010 22:31:35 -0000
@@ -0,0 +1,8 @@
+; $Id$
+name = "Drupal system listing compatible test"
+description = "Support module for testing the drupal_system_listing function."
+package = Testing
+version = VERSION
+core = 7.x
+files[] = drupal_system_listing_compatible_test.module
+hidden = TRUE
Index: modules/simpletest/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.module
===================================================================
RCS file: modules/simpletest/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.module
diff -N modules/simpletest/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.module	9 Oct 2010 22:31:35 -0000
@@ -0,0 +1,2 @@
+<?php
+// $Id$
Index: modules/simpletest/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.info
===================================================================
RCS file: modules/simpletest/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.info
diff -N modules/simpletest/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.info	9 Oct 2010 22:31:35 -0000
@@ -0,0 +1,8 @@
+; $Id$
+name = "Drupal system listing incompatible test"
+description = "Support module for testing the drupal_system_listing function."
+package = Testing
+version = VERSION
+core = 7.x
+files[] = drupal_system_listing_incompatible_test.module
+hidden = TRUE
Index: modules/simpletest/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.module
===================================================================
RCS file: modules/simpletest/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.module
diff -N modules/simpletest/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.module	9 Oct 2010 22:31:35 -0000
@@ -0,0 +1,2 @@
+<?php
+// $Id$
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.130
diff -u -p -r1.130 common.test
--- modules/simpletest/tests/common.test	8 Oct 2010 15:36:12 -0000	1.130
+++ modules/simpletest/tests/common.test	9 Oct 2010 22:31:36 -0000
@@ -1974,6 +1974,61 @@ class ParseInfoFilesTestCase extends Dru
 }
 
 /**
+ * Tests for the drupal_system_listing() function.
+ */
+class DrupalSystemListingTestCase extends DrupalUnitTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'Drupal system listing',
+      'description' => 'Tests the mechanism for scanning system directories in drupal_system_listing().',
+      'group' => 'System',
+    );
+  }
+
+  /**
+   * Test that files in different directories take precedence as expected.
+   */
+  function testDirectoryPrecedence() {
+    // Define the module files we will search for, and the directory precedence
+    // we expect.
+    $expected_directories = array(
+      // When the copy of the module in the profile directory is incompatible
+      // with Drupal core, the copy in the core modules directory takes
+      // precedence.
+      'drupal_system_listing_incompatible_test' => array(
+        'modules/simpletest/tests',
+        'profiles/standard/modules/tests',
+      ),
+      // When both copies of the module are compatible with Drupal core, the
+      // copy in the profile directory takes precedence.
+      'drupal_system_listing_compatible_test' => array(
+        'profiles/standard/modules/tests',
+        'modules/simpletest/tests',
+      ),
+    );
+
+    // This test relies on two versions of the same module existing in
+    // different places in the filesystem. Without that, the test has no
+    // meaning, so assert their presence first.
+    foreach ($expected_directories as $module => $directories) {
+      foreach ($directories as $directory) {
+        $filename = "$directory/$module/$module.module";
+        $this->assertTrue(file_exists(DRUPAL_ROOT . '/' . $filename), t('@filename exists.', array('@filename' => $filename)));
+      }
+    }
+
+    // Now scan the directories and check that the files take precedence as
+    // expected.
+    $files = drupal_system_listing('/\.module$/', 'modules', 'name', 1);
+    foreach ($expected_directories as $module => $directories) {
+      $expected_directory = array_shift($directories);
+      $expected_filename = "$expected_directory/$module/$module.module";
+      $this->assertEqual($files[$module]->uri, $expected_filename, t('Module @module was found at @filename.', array('@module' => $module, '@filename' => $expected_filename)));
+    }
+  }
+}
+
+/**
  * Tests for the format_date() function.
  */
 class FormatDateUnitTest extends DrupalWebTestCase {
Index: profiles/standard/modules/README.txt
===================================================================
RCS file: profiles/standard/modules/README.txt
diff -N profiles/standard/modules/README.txt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ profiles/standard/modules/README.txt	9 Oct 2010 22:31:36 -0000
@@ -0,0 +1,10 @@
+// $Id$
+
+This directory is used to place modules that are shipped with an installation
+profile but that are not part of Drupal core. It should be used by developers
+of the installation profile only. Modules which you download for your site
+should be placed in sites/all/modules or an equivalent location instead; see
+the modules/README.txt file for more information.
+
+The Standard profile only uses modules that are part of Drupal core, so this
+directory is empty (except for automated tests).
Index: profiles/standard/modules/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.info
===================================================================
RCS file: profiles/standard/modules/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.info
diff -N profiles/standard/modules/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ profiles/standard/modules/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.info	14 Sep 2010 03:58:29 -0000
@@ -0,0 +1,8 @@
+; $Id$
+name = "Drupal system listing compatible test"
+description = "Support module for testing the drupal_system_listing function."
+package = Testing
+version = VERSION
+core = 7.x
+files[] = drupal_system_listing_compatible_test.module
+hidden = TRUE
Index: profiles/standard/modules/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.module
===================================================================
RCS file: profiles/standard/modules/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.module
diff -N profiles/standard/modules/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ profiles/standard/modules/tests/drupal_system_listing_compatible_test/drupal_system_listing_compatible_test.module	14 Sep 2010 03:58:29 -0000
@@ -0,0 +1,2 @@
+<?php
+// $Id$
Index: profiles/standard/modules/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.info
===================================================================
RCS file: profiles/standard/modules/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.info
diff -N profiles/standard/modules/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ profiles/standard/modules/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.info	14 Sep 2010 03:58:29 -0000
@@ -0,0 +1,11 @@
+; $Id$
+name = "Drupal system listing incompatible test"
+description = "Support module for testing the drupal_system_listing function."
+package = Testing
+version = VERSION
+; This deliberately has the wrong core version, to test that it does not take
+; precedence over the version of the same module that is in the
+; modules/simpletest/tests directory.
+core = 6.x
+files[] = drupal_system_listing_incompatible_test.module
+hidden = TRUE
Index: profiles/standard/modules/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.module
===================================================================
RCS file: profiles/standard/modules/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.module
diff -N profiles/standard/modules/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ profiles/standard/modules/tests/drupal_system_listing_incompatible_test/drupal_system_listing_incompatible_test.module	14 Sep 2010 03:58:29 -0000
@@ -0,0 +1,2 @@
+<?php
+// $Id$
