Adding Helper::assetTimestamp()

Refactor's repeated code from Html and Javascript Helpers.
Test cases added.
This commit is contained in:
mark_story 2009-09-23 23:45:11 -04:00
parent 26d2237df0
commit ee514f3fc6
2 changed files with 54 additions and 0 deletions

View file

@ -224,6 +224,25 @@ class Helper extends Overloadable {
return $webPath;
}
/**
* Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in
* Configure. If Asset.timestamp is true and debug > 0, or Asset.timestamp == 'force'
* a timestamp will be added.
*
* @param string $path The file path to timestamp, the path must be inside WWW_ROOT
* @return string Path with a timestamp added, or not.
**/
function assetTimestamp($path) {
$timestampEnabled = (
(Configure::read('Asset.timestamp') === true && Configure::read() > 0) ||
Configure::read('Asset.timestamp') === 'force'
);
if (strpos($path, '?') === false && $timestampEnabled) {
$path .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $path));
}
return $path;
}
/**
* Used to remove harmful tags from content
*

View file

@ -411,6 +411,41 @@ class HelperTest extends CakeTestCase {
$this->assertEqual($result, "/posts/index/page:1?one=value&two=value&three=purple");
}
/**
* test assetTimestamp application
*
* @return void
**/
function testAssetTimestamp() {
$_timestamp = Configure::read('Asset.timestamp');
$_debug = Configure::read('debug');
Configure::write('Asset.timestamp', false);
$result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
$this->assertEqual($result, CSS_URL . 'cake.generic.css');
Configure::write('Asset.timestamp', true);
Configure::write('debug', 0);
$result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
$this->assertEqual($result, CSS_URL . 'cake.generic.css');
Configure::write('Asset.timestamp', true);
Configure::write('debug', 2);
$result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
$this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
Configure::write('Asset.timestamp', 'force');
Configure::write('debug', 0);
$result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css');
$this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
$result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css?someparam');
$this->assertEqual($result, CSS_URL . 'cake.generic.css?someparam');
Configure::write('debug', $_debug);
Configure::write('Asset.timestamp', $_timestamp);
}
/**
* testFieldsWithSameName method
*