Skip to content

Commit

Permalink
Updating HtmlHelper + JavascriptHelper to call assetTimestamp() after…
Browse files Browse the repository at this point in the history
… webroot(). This allows theme files in webroot/theme to be correctly timestamped.

Updating tests to reflect changes in behaviour. Fixes #244
  • Loading branch information
markstory committed Apr 9, 2010
1 parent 9f601ea commit c38249a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
3 changes: 2 additions & 1 deletion cake/libs/view/helper.php
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions cake/libs/view/helpers/html.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'])) {
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/view/helpers/javascript.php
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions cake/tests/cases/libs/view/helper.test.php
Expand Up @@ -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);
}
Expand Down
29 changes: 22 additions & 7 deletions cake/tests/cases/libs/view/helpers/html.test.php
Expand Up @@ -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/';
Expand All @@ -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();
}

/**
Expand All @@ -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)
));
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit c38249a

Please sign in to comment.