Implementing assettimestamps for themes and plugins. Tests added. Fixes #804, #879

This commit is contained in:
mark_story 2010-06-30 20:47:54 -04:00
parent 8b9d9b1b12
commit dc72529648
2 changed files with 45 additions and 1 deletions

View file

@ -246,7 +246,22 @@ class Helper extends Overloadable {
);
if (strpos($path, '?') === false && $timestampEnabled) {
$filepath = preg_replace('/^' . preg_quote($this->webroot, '/') . '/', '', $path);
$path .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $filepath));
$webrootPath = WWW_ROOT . str_replace('/', DS, $filepath);
if (file_exists($webrootPath)) {
return $path . '?' . @filemtime($webrootPath);
}
$segments = explode('/', ltrim($filepath, '/'));
if ($segments[0] === 'theme') {
$theme = $segments[1];
unset($segments[0], $segments[1]);
$themePath = App::themePath($theme) . 'webroot' . DS . implode(DS, $segments);
return $path . '?' . @filemtime($themePath);
} else {
$plugin = $segments[0];
unset($segments[0]);
$pluginPath = App::pluginPath($plugin) . 'webroot' . DS . implode(DS, $segments);
return $path . '?' . @filemtime($pluginPath);
}
}
return $path;
}

View file

@ -490,6 +490,35 @@ class HelperTest extends CakeTestCase {
Configure::write('Asset.timestamp', $_timestamp);
}
/**
* test assetTimestamp with plugins and themes
*
* @return void
*/
function testAssetTimestampPluginsAndThemes() {
$_timestamp = Configure::read('Asset.timestamp');
Configure::write('Asset.timestamp', 'force');
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
));
$result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
$this->assertPattern('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
$result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
$this->assertPattern('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
$result = $this->Helper->assetTimestamp('/theme/test_theme/js/theme.js');
$this->assertPattern('#/theme/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
$result = $this->Helper->assetTimestamp('/theme/test_theme/js/non_existant.js');
$this->assertPattern('#/theme/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
App::build();
Configure::write('Asset.timestamp', $_timestamp);
}
/**
* testFieldsWithSameName method
*