diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index d0c86650401..7eeeb843898 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -142,7 +142,11 @@ function writeScripts($options = array()) { return $this->Html->scriptBlock($script, $options); } if ($options['cache'] && $options['inline']) { - //@todo cache to file and return script tag. + $filename = md5($script); + if (!file_exists(JS . $filename . '.js')) { + cache(str_replace(WWW_ROOT, '', JS) . $filename . '.js', $script, '+999 days', 'public'); + } + return $this->Html->script($filename); } $view =& ClassRegistry::getObject('view'); $view->addScript($script); diff --git a/cake/tests/cases/libs/view/helpers/js.test.php b/cake/tests/cases/libs/view/helpers/js.test.php index a6ee2a249ab..e70ef652d3c 100644 --- a/cake/tests/cases/libs/view/helpers/js.test.php +++ b/cake/tests/cases/libs/view/helpers/js.test.php @@ -159,6 +159,28 @@ function testWriteScriptsNoFile() { $view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/'))); $result = $this->Js->writeScripts(array('onDomReady' => false, 'inline' => false, 'cache' => false)); } +/** + * test that writeScripts makes files, and puts the events into them. + * + * @return void + **/ + function testWriteScriptsInFile() { + if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, script caching test has been skipped')) { + return; + } + $this->Js->JsBaseEngine = new TestJsEngineHelper(); + $this->Js->writeCache('one = 1;'); + $this->Js->writeCache('two = 2;'); + $result = $this->Js->writeScripts(array('onDomReady' => false)); + $expected = array( + 'script' => array('type' => 'text/javascript', 'src' => 'preg:/(.)*\.js/'), + ); + $this->assertTags($result, $expected); + preg_match('/src="(.*\.js)"/', $result, $filename); + $this->assertTrue(file_exists(WWW_ROOT . $filename[1])); + $contents = file_get_contents(WWW_ROOT . $filename[1]); + $this->assertPattern('/one\s=\s1;\ntwo\s=\s2;/', $contents); + } }