diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 9f37dfc..8cbcfc6 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -838,6 +838,14 @@ function drupal_get_filename($type, $name, $filename = NULL) { // drupal_static(). static $files = array(), $dirs = array(); + // We use drupal static for the bad records so we can test it. + // Drupal static fast pattern is used as this function may be called often. + static $drupal_static_fast; + if (!isset($drupal_static_fast)) { + $drupal_static_fast['bad'] = &drupal_static(__FUNCTION__ . ':bad'); + } + $bad = &$drupal_static_fast['bad']; + // Profiles are a special case: they have a fixed location and naming. if ($type == 'profile') { $profile_filename = "profiles/$name/$name.profile"; @@ -887,7 +895,18 @@ function drupal_get_filename($type, $name, $filename = NULL) { $extension = $type; } - if (!isset($dirs[$dir][$extension])) { + // See if we have a cached list of files missing from the file system. + if (is_null($bad)) { + $cache = cache_get('drupal_get_filename:bad'); + if (!empty($cache->data)) { + $bad = $cache->data; + } + else { + $bad = array(); + } + } + + if (!isset($dirs[$dir][$extension]) && !isset($bad[$type][$name])) { $dirs[$dir][$extension] = TRUE; if (!function_exists('drupal_system_listing')) { require_once DRUPAL_ROOT . '/includes/common.inc'; @@ -907,6 +926,16 @@ function drupal_get_filename($type, $name, $filename = NULL) { if (isset($files[$type][$name])) { return $files[$type][$name]; } + elseif (!isset($bad[$type][$name])) { + // Add the missing file to a temporary cache and throw a watchdog alert. + $bad[$type][$name] = TRUE; + cache_set('drupal_get_filename:bad', $bad, 'cache', CACHE_TEMPORARY); + + // We don't have the watchdog table in the tests. + if (db_table_exists('watchdog')) { + watchdog('system', 'The following @type is missing from the file system: @name.', array('@type' => $type, '@name' => $name), WATCHDOG_ALERT); + } + } } /** diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test index f723c63..016ff12 100644 --- a/modules/simpletest/tests/bootstrap.test +++ b/modules/simpletest/tests/bootstrap.test @@ -379,6 +379,18 @@ class BootstrapGetFilenameTestCase extends DrupalUnitTestCase { // automatically check there for 'script' files, just as it does for (e.g.) // 'module' files in modules. $this->assertIdentical(drupal_get_filename('script', 'test'), 'scripts/test.script', t('Retrieve test script location.')); + + // Generate a non-existing module name. + $non_existing_module = uniqid("", TRUE); + + // Searching for an item that does not exist returns NULL. + $this->assertNull(drupal_get_filename('module', $non_existing_module), 'Searching for an item that does not exist returns NULL.'); + + // Get the bad records static from drupal_get_filename. + $bad = &drupal_static('drupal_get_filename:bad'); + + // Searching for an item that does not exist creates a static record in drupal_get_filename. + $this->assertTrue($bad['module'][$non_existing_module], 'Searching for an item that does not exist creates a static record in drupal_get_filename.'); } }