Skip to content
Permalink
Browse files

Adding Helper::assetTimestamp()

Refactor's repeated code from Html and Javascript Helpers.
Test cases added.
  • Loading branch information...
markstory committed Sep 24, 2009
1 parent 26d2237 commit ee514f3fc6fce7bf4a4fef76af6104b85283a208
Showing with 54 additions and 0 deletions.
  1. +19 −0 cake/libs/view/helper.php
  2. +35 −0 cake/tests/cases/libs/view/helper.test.php
@@ -224,6 +224,25 @@ function webroot($file) {
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
*
@@ -411,6 +411,41 @@ function testUrlConversion() {
$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
*

0 comments on commit ee514f3

Please sign in to comment.
You can’t perform that action at this time.