Adding tests and features for script caching to files.

This commit is contained in:
mark_story 2009-03-19 22:02:18 -04:00
parent d787ec3720
commit 0bd3cc97c1
2 changed files with 27 additions and 1 deletions

View file

@ -142,7 +142,11 @@ class JsHelper extends AppHelper {
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);

View file

@ -159,6 +159,28 @@ class JsHelperTestCase extends CakeTestCase {
$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);
}
}