diff --git a/docs/programmers/api-variables/variable-compile-check.md b/docs/programmers/api-variables/variable-compile-check.md index c0582d4d4..075e7f17a 100644 --- a/docs/programmers/api-variables/variable-compile-check.md +++ b/docs/programmers/api-variables/variable-compile-check.md @@ -13,6 +13,9 @@ changing), the compile check step is no longer needed. Be sure to set change this to FALSE and a template file is changed, you will \*not\* see the change since the template will not get recompiled. +Note that up to Smarty 4.x, Smarty will check for the existence of +the source template even if `$compile_check` is disabled. + If [`$caching`](#variable.caching) is enabled and `$compile_check` is enabled, then the cache files will get regenerated if an involved template file or config file was updated. diff --git a/tests/UnitTests/SmartyMethodsTests/CompileCheck/CompileCheckTest.php b/tests/UnitTests/SmartyMethodsTests/CompileCheck/CompileCheckTest.php new file mode 100644 index 000000000..48e8730a2 --- /dev/null +++ b/tests/UnitTests/SmartyMethodsTests/CompileCheck/CompileCheckTest.php @@ -0,0 +1,123 @@ +setUpSmarty(__DIR__); + $this->smarty->addTemplateDir('./templates_tmp'); + $this->cleanDirs(); + } + + /** + * generate templates + */ + protected function makeFiles() + { + file_put_contents('./templates_tmp/t1.tpl', 'TPL1'); + file_put_contents('./templates_tmp/t2.tpl', 'TPL2'); + file_put_contents('./templates_tmp/base.tpl', '{include file="t1.tpl"}{include file="t2.tpl"}'); + } + /** + * remove generated templates + */ + protected function removeFiles() + { + unlink('./templates_tmp/t1.tpl'); + unlink('./templates_tmp/t2.tpl'); + unlink('./templates_tmp/base.tpl'); + } + + /** + * reset, but leave the files alone + * @return void + */ + private function softResetSmarty() { + $this->smarty = new Smarty(); + $this->smarty->addTemplateDir('./templates_tmp'); + } + + /** + * @group slow + */ + public function testCompileCheckOn0() + { + $this->makeFiles(); + $this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl')); + + $this->softResetSmarty(); + $this->smarty->setCompileCheck(Smarty::COMPILECHECK_ON); + + $this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl')); + } + + /** + * @group slow + */ + public function testCompileCheckOn1() + { + $this->makeFiles(); + $this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl')); + + $this->softResetSmarty(); + $this->smarty->setCompileCheck(Smarty::COMPILECHECK_ON); + + unlink('./templates_tmp/base.tpl'); + sleep(1); + + $this->expectException(Exception::class); + $this->smarty->fetch('base.tpl'); + } + + /** + * @group slow + */ + public function testCompileCheckOn2() + { + $this->makeFiles(); + $this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl')); + + $this->softResetSmarty(); + $this->smarty->setCompileCheck(Smarty::COMPILECHECK_ON); + + sleep(1); + file_put_contents('./templates_tmp/base.tpl', 'hello'); + + $this->assertEquals('hello', $this->smarty->fetch('base.tpl')); + } + + /** + * @group slow + */ + public function testCompileCheckOff0() + { + $this->makeFiles(); + $this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl')); + + $this->softResetSmarty(); + $this->smarty->setCompileCheck(Smarty::COMPILECHECK_OFF); + + $this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl')); + } + + /** + * @group slow + */ + public function testCompileCheckOff2() + { + $this->makeFiles(); + $this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl')); + + $this->softResetSmarty(); + $this->smarty->setCompileCheck(Smarty::COMPILECHECK_OFF); + + sleep(1); + file_put_contents('./templates_tmp/base.tpl', 'hello'); + + $this->assertEquals('TPL1TPL2', $this->smarty->fetch('base.tpl')); + } + +}