From c38249ac286d3b04b602df564ce874acdde17eb5 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Thu, 8 Apr 2010 23:36:04 -0400 Subject: [PATCH] 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 --- cake/libs/view/helper.php | 3 +- cake/libs/view/helpers/html.php | 6 ++-- cake/libs/view/helpers/javascript.php | 2 +- cake/tests/cases/libs/view/helper.test.php | 4 +++ .../cases/libs/view/helpers/html.test.php | 29 ++++++++++++++----- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 5f8ff75c0..bb1e80429 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -245,7 +245,8 @@ class Helper extends Overloadable { 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; } diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index 840c7f3ea..6663abc72 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -358,7 +358,7 @@ class HtmlHelper extends AppHelper { $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 @@ class HtmlHelper extends AppHelper { 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 @@ class HtmlHelper extends AppHelper { if ($path[0] !== '/') { $path = IMAGES_URL . $path; } - $path = $this->webroot($this->assetTimestamp($path)); + $path = $this->assetTimestamp($this->webroot($path)); } if (!isset($options['alt'])) { diff --git a/cake/libs/view/helpers/javascript.php b/cake/libs/view/helpers/javascript.php index de42a6799..f1269367d 100644 --- a/cake/libs/view/helpers/javascript.php +++ b/cake/libs/view/helpers/javascript.php @@ -265,7 +265,7 @@ class JavascriptHelper extends AppHelper { $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); diff --git a/cake/tests/cases/libs/view/helper.test.php b/cake/tests/cases/libs/view/helper.test.php index 804f325c5..f8454739d 100644 --- a/cake/tests/cases/libs/view/helper.test.php +++ b/cake/tests/cases/libs/view/helper.test.php @@ -467,6 +467,10 @@ class HelperTest extends CakeTestCase { $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); } diff --git a/cake/tests/cases/libs/view/helpers/html.test.php b/cake/tests/cases/libs/view/helpers/html.test.php index 17011ae5c..8800e6eec 100644 --- a/cake/tests/cases/libs/view/helpers/html.test.php +++ b/cake/tests/cases/libs/view/helpers/html.test.php @@ -304,14 +304,15 @@ class HtmlHelperTest extends CakeTestCase { 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 @@ class HtmlHelperTest extends CakeTestCase { * @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 @@ class HtmlHelperTest extends CakeTestCase { * @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 @@ class HtmlHelperTest extends CakeTestCase { * @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')