2020-04-13 15:30:52 +02:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Smarty PHPunit tests deault template handler
|
|
|
|
|
*
|
2023-01-05 23:06:02 +01:00
|
|
|
|
2020-04-13 15:30:52 +02:00
|
|
|
* @author Uwe Tews
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* class for block plugin tests
|
|
|
|
|
*
|
2023-01-05 23:06:02 +01:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
2020-04-13 15:30:52 +02:00
|
|
|
*/
|
|
|
|
|
class DefaultTemplateHandlerTest extends PHPUnit_Smarty
|
|
|
|
|
{
|
2021-10-13 12:15:17 +02:00
|
|
|
public function setUp(): void
|
2020-04-13 15:30:52 +02:00
|
|
|
{
|
2022-09-27 13:03:34 +03:00
|
|
|
$this->setUpSmarty(__DIR__);
|
2020-04-13 15:30:52 +02:00
|
|
|
$this->smarty->setForceCompile(true);
|
|
|
|
|
$this->smarty->disableSecurity();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function testInit()
|
|
|
|
|
{
|
|
|
|
|
$this->cleanDirs();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* test error on unknow template
|
|
|
|
|
*/
|
|
|
|
|
public function testUnknownTemplate()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->smarty->fetch('foo.tpl');
|
|
|
|
|
}
|
|
|
|
|
catch (Exception $e) {
|
2021-10-13 12:15:17 +02:00
|
|
|
$this->assertStringContainsString('Unable to load template', $e->getMessage());
|
2020-04-13 15:30:52 +02:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->fail('Exception for none existing template has not been raised.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test error on registration on none existent handler function.
|
|
|
|
|
*/
|
|
|
|
|
public function testRegisterNoneExistentHandlerFunction()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->smarty->registerDefaultTemplateHandler('foo');
|
|
|
|
|
}
|
|
|
|
|
catch (Exception $e) {
|
2021-10-13 12:15:17 +02:00
|
|
|
$this->assertStringContainsString("Default template handler", $e->getMessage());
|
|
|
|
|
$this->assertStringContainsString("not callable", $e->getMessage());
|
2020-04-13 15:30:52 +02:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->fail('Exception for none callable function has not been raised.');
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* test replacement by default template handler
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* public function testDefaultTemplateHandlerReplacement()
|
|
|
|
|
* {
|
|
|
|
|
* $this->smarty->register->defaultTemplateHandler('my_template_handler');
|
|
|
|
|
* $this->assertEquals("Recsource foo.tpl of type file not found", $this->smarty->fetch('foo.tpl'));
|
|
|
|
|
* }
|
|
|
|
|
*/
|
|
|
|
|
public function testDefaultTemplateHandlerReplacementByTemplateFile()
|
|
|
|
|
{
|
|
|
|
|
$this->smarty->registerDefaultTemplateHandler('my_template_handler_file');
|
|
|
|
|
$this->assertEquals("hello world", $this->smarty->fetch('foo.tpl'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* test default template handler returning fals
|
|
|
|
|
*/
|
|
|
|
|
public function testDefaultTemplateHandlerReturningFalse()
|
|
|
|
|
{
|
|
|
|
|
$this->smarty->registerDefaultTemplateHandler('my_false');
|
|
|
|
|
try {
|
|
|
|
|
$this->smarty->fetch('foo.tpl');
|
|
|
|
|
}
|
|
|
|
|
catch (Exception $e) {
|
2021-10-13 12:15:17 +02:00
|
|
|
$this->assertStringContainsString('Default handler: No template default content for \'file:foo.tpl\'', $e->getMessage());
|
2020-04-13 15:30:52 +02:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->fail('Exception for none existing template has not been raised.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 00:49:38 +01:00
|
|
|
function my_template_handler($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty)
|
2020-04-13 15:30:52 +02:00
|
|
|
{
|
|
|
|
|
$output = "Recsource $resource_name of type $resource_type not found";
|
|
|
|
|
$template_source = $output;
|
|
|
|
|
$template_timestamp = time();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 00:49:38 +01:00
|
|
|
function my_template_handler_file($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty)
|
2020-04-13 15:30:52 +02:00
|
|
|
{
|
|
|
|
|
return $smarty->getTemplateDir(0) . 'helloworld.tpl';
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 00:49:38 +01:00
|
|
|
function my_false($resource_type, $resource_name, &$template_source, &$template_timestamp, \Smarty\Smarty $smarty)
|
2020-04-13 15:30:52 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|