From 7e68ab152c319b61ff0193b2885b0f1d59269f19 Mon Sep 17 00:00:00 2001 From: nate Date: Wed, 9 Apr 2008 19:08:04 +0000 Subject: [PATCH] Correcting timestamp issues in JavascriptHelper::link(), fixes #4441 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6650 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/javascript.php | 17 +++--- .../libs/view/helpers/javascript.test.php | 56 +++++++++++-------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/cake/libs/view/helpers/javascript.php b/cake/libs/view/helpers/javascript.php index a1b77b057..b3baf17d4 100644 --- a/cake/libs/view/helpers/javascript.php +++ b/cake/libs/view/helpers/javascript.php @@ -251,19 +251,18 @@ class JavascriptHelper extends AppHelper { return; } - if (strpos($url, '?') === false) { - if (strpos($url, '.js') === false) { - $url .= '.js'; - } - if ((Configure::read('Asset.timestamp') === true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force') { - $url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $url)); - } - } - if (strpos($url, '://') === false) { if ($url{0} !== '/') { $url = JS_URL . $url; } + if (strpos($url, '?') === false) { + if (strpos($url, '.js') === false) { + $url .= '.js'; + } + if ((Configure::read('Asset.timestamp') === true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force') { + $url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $url)); + } + } $url = $this->webroot($url); if (Configure::read('Asset.filter.js')) { diff --git a/cake/tests/cases/libs/view/helpers/javascript.test.php b/cake/tests/cases/libs/view/helpers/javascript.test.php index bec5ef201..f30bc6f12 100644 --- a/cake/tests/cases/libs/view/helpers/javascript.test.php +++ b/cake/tests/cases/libs/view/helpers/javascript.test.php @@ -71,28 +71,6 @@ class JavascriptTest extends UnitTestCase { $expected = ''; $this->assertEqual($result, $expected); - Configure::write('Asset.timestamp', true); - $result = $this->Javascript->link('jquery-1.1.2'); - $this->assertPattern('/^]+src=".*js\/jquery-1\.1\.2\.js\?"[^<>]*>/', $result); - - $debug = Configure::read('debug'); - Configure::write('debug', 0); - $result = $this->Javascript->link('jquery-1.1.2'); - $expected = ''; - $this->assertEqual($result, $expected); - - Configure::write('Asset.timestamp', 'force'); - $result = $this->Javascript->link('jquery-1.1.2'); - $this->assertPattern('/^]+src=".*js\/jquery-1\.1\.2\.js\?"[^<>]*>/', $result); - - Configure::write('debug', $debug); - Configure::write('Asset.timestamp', false); - - Configure::write('Asset.filter.js', 'js.php'); - $result = $this->Javascript->link('jquery-1.1.2'); - $this->assertPattern('/^]+src=".*cjs\/jquery-1\.1\.2\.js"[^<>]*>/', $result); - Configure::write('Asset.filter.js', false); - $result = $this->Javascript->link('/plugin/js/jquery-1.1.2'); $expected = ''; $this->assertEqual($result, $expected); @@ -110,6 +88,40 @@ class JavascriptTest extends UnitTestCase { $this->assertEqual($result, $expected); } + function testFilteringAndTimestamping() { + if (!is_writable(JS)) { + echo "
JavaScript directory not writable, skipping JS asset timestamp tests
"; + return; + } + + cache(str_replace(WWW_ROOT, '', JS) . '__cake_js_test.js', 'alert("test")', '+999 days', 'public'); + $timestamp = substr(strtotime('now'), 0, 8); + + Configure::write('Asset.timestamp', true); + $result = $this->Javascript->link('__cake_js_test'); + $this->assertPattern('/^]+src=".*js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"[^<>]*>/', $result); + + $debug = Configure::read('debug'); + Configure::write('debug', 0); + $result = $this->Javascript->link('__cake_js_test'); + $expected = ''; + $this->assertEqual($result, $expected); + + Configure::write('Asset.timestamp', 'force'); + $result = $this->Javascript->link('__cake_js_test'); + $this->assertPattern('/^]+src=".*js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"[^<>]*>/', $result); + + Configure::write('debug', $debug); + Configure::write('Asset.timestamp', false); + + Configure::write('Asset.filter.js', 'js.php'); + $result = $this->Javascript->link('__cake_js_test'); + $this->assertPattern('/^]+src=".*cjs\/__cake_js_test.js"[^<>]*>/', $result); + Configure::write('Asset.filter.js', false); + + unlink(JS . '__cake_js_test.js'); + } + function testObjectGeneration() { $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8));