Browse files

Adding tests and features for script caching to files.

  • Loading branch information...
1 parent d787ec3 commit 0bd3cc97c105e0f5d94398da7fe5b03e604c3f68 @markstory markstory committed Mar 20, 2009
Showing with 27 additions and 1 deletion.
  1. +5 −1 cake/libs/view/helpers/js.php
  2. +22 −0 cake/tests/cases/libs/view/helpers/js.test.php
View
6 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);
View
22 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);
+ }
}

0 comments on commit 0bd3cc9

Please sign in to comment.