Skip to content

Commit

Permalink
Adding tests and features for script caching to files.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 20, 2009
1 parent d787ec3 commit 0bd3cc9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cake/libs/view/helpers/js.php
Expand Up @@ -142,7 +142,11 @@ function writeScripts($options = array()) {
return $this->Html->scriptBlock($script, $options); return $this->Html->scriptBlock($script, $options);
} }
if ($options['cache'] && $options['inline']) { 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 =& ClassRegistry::getObject('view');
$view->addScript($script); $view->addScript($script);
Expand Down
22 changes: 22 additions & 0 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -159,6 +159,28 @@ function testWriteScriptsNoFile() {
$view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/'))); $view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/')));
$result = $this->Js->writeScripts(array('onDomReady' => false, 'inline' => false, 'cache' => false)); $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);
}
} }




Expand Down

0 comments on commit 0bd3cc9

Please sign in to comment.