Permalink
Browse files

Updating HtmlHelper + JavascriptHelper to call assetTimestamp() after…

… webroot(). This allows theme files in webroot/theme to be correctly timestamped.

Updating tests to reflect changes in behaviour. Fixes #244
  • Loading branch information...
1 parent 9f601ea commit c38249ac286d3b04b602df564ce874acdde17eb5 @markstory markstory committed Apr 9, 2010
@@ -245,7 +245,8 @@ function assetTimestamp($path) {
Configure::read('Asset.timestamp') === 'force'
);
if (strpos($path, '?') === false && $timestampEnabled) {
- $path .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $path));
+ $filepath = preg_replace('/^' . preg_quote($this->webroot, '/') . '/', '', $path);
+ $path .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $filepath));
}
return $path;
}
@@ -358,7 +358,7 @@ function css($path, $rel = null, $options = array()) {
$path .= '.css';
}
}
- $url = $this->webroot($this->assetTimestamp($path));
+ $url = $this->assetTimestamp($this->webroot($path));
if (Configure::read('Asset.filter.css')) {
$pos = strpos($url, CSS_URL);
@@ -434,7 +434,7 @@ function script($url, $options = array()) {
if (strpos($url, '?') === false && strpos($url, '.js') === false) {
$url .= '.js';
}
- $url = $this->webroot($this->assetTimestamp($url));
+ $url = $this->assetTimestamp($this->webroot($url));
if (Configure::read('Asset.filter.js')) {
$url = str_replace(JS_URL, 'cjs/', $url);
@@ -608,7 +608,7 @@ function image($path, $options = array()) {
if ($path[0] !== '/') {
$path = IMAGES_URL . $path;
}
- $path = $this->webroot($this->assetTimestamp($path));
+ $path = $this->assetTimestamp($this->webroot($path));
}
if (!isset($options['alt'])) {
@@ -265,7 +265,7 @@ function link($url, $inline = true) {
$url .= '.js';
}
}
- $url = $this->webroot($this->assetTimestamp($url));
+ $url = $this->assetTimestamp($this->webroot($url));
if (Configure::read('Asset.filter.js')) {
$pos = strpos($url, JS_URL);
@@ -467,6 +467,10 @@ function testAssetTimestamp() {
$result = $this->Helper->assetTimestamp(CSS_URL . 'cake.generic.css?someparam');
$this->assertEqual($result, CSS_URL . 'cake.generic.css?someparam');
+ $this->Helper->webroot = '/some/dir/';
+ $result = $this->Helper->assetTimestamp('/some/dir/' . CSS_URL . 'cake.generic.css');
+ $this->assertPattern('/' . preg_quote(CSS_URL . 'cake.generic.css?', '/') . '[0-9]+/', $result);
+
Configure::write('debug', $_debug);
Configure::write('Asset.timestamp', $_timestamp);
}
@@ -304,14 +304,15 @@ function testImageTag() {
function testImageWithTimestampping() {
Configure::write('Asset.timestamp', 'force');
+ $this->Html->webroot = '/';
$result = $this->Html->image('cake.icon.png');
- $this->assertTags($result, array('img' => array('src' => 'preg:/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
+ $this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
Configure::write('debug', 0);
Configure::write('Asset.timestamp', 'force');
$result = $this->Html->image('cake.icon.png');
- $this->assertTags($result, array('img' => array('src' => 'preg:/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
+ $this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
$webroot = $this->Html->webroot;
$this->Html->webroot = '/testing/longer/';
@@ -331,30 +332,42 @@ function testImageWithTimestampping() {
* @link https://trac.cakephp.org/ticket/6490
*/
function testImageTagWithTheme() {
+ if ($this->skipIf(!is_writable(WWW_ROOT . 'theme'), 'Cannot write to webroot/theme')) {
+ return;
+ }
+ App::import('Core', 'File');
+
+ $testfile = WWW_ROOT . 'theme' . DS . 'test_theme' . DS . 'img' . DS . '__cake_test_image.gif';
+ $file =& new File($testfile, true);
+
App::build(array(
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
));
Configure::write('Asset.timestamp', true);
Configure::write('debug', 1);
-
+
+ $this->Html->webroot = '/';
$this->Html->theme = 'test_theme';
- $result = $this->Html->image('cake.power.gif');
+ $result = $this->Html->image('__cake_test_image.gif');
$this->assertTags($result, array(
'img' => array(
- 'src' => 'preg:/theme\/test_theme\/img\/cake\.power\.gif\?\d+/',
+ 'src' => 'preg:/\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
'alt' => ''
)));
$webroot = $this->Html->webroot;
$this->Html->webroot = '/testing/';
- $result = $this->Html->image('cake.power.gif');
+ $result = $this->Html->image('__cake_test_image.gif');
$this->assertTags($result, array(
'img' => array(
- 'src' => 'preg:/\/testing\/theme\/test_theme\/img\/cake\.power\.gif\?\d+/',
+ 'src' => 'preg:/\/testing\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
'alt' => ''
)));
$this->Html->webroot = $webroot;
+
+ $dir =& new Folder(WWW_ROOT . 'theme' . DS . 'test_theme');
+ $dir->delete();
}
/**
@@ -364,6 +377,7 @@ function testImageTagWithTheme() {
* @return void
*/
function testThemeAssetsInMainWebrootPath() {
+ Configure::write('Asset.timestamp', false);
App::build(array(
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
));
@@ -542,6 +556,7 @@ function testScriptTimestamping() {
* @return void
*/
function testScript() {
+ Configure::write('Asset.timestamp', false);
$result = $this->Html->script('foo');
$expected = array(
'script' => array('type' => 'text/javascript', 'src' => 'js/foo.js')

0 comments on commit c38249a

Please sign in to comment.