Files
smarty/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php
Simon Wisselink 39b69f0142 Feature/php8 support (#629)
Adds support for PHP8.0, dropping support for PHP7.0 and below.

Backwards incompatible changes:
- Dropped support for php asp tags in templates (removed from php since php7.0)
- Dropped deprecated API calls that where only accessible through SmartyBC
- Dropped support for {php} and {include_php} tags and embedded PHP in templates. Embedded PHP will now be passed through as is.
- Removed all PHP_VERSION_ID and compare_version checks and conditional code blocks that are now no longer required
- Dropped deprecated SMARTY_RESOURCE_CHAR_SET and SMARTY_RESOURCE_DATE_FORMAT constants
- Dropped deprecated Smarty::muteExpectedErrors and Smarty::unmuteExpectedErrors API methods
- Dropped deprecated $smarty->getVariable() method. Use $smarty->getTemplateVars() instead.
- $smarty->registerResource() no longer accepts an array of callback functions

See the changelog for more details.

Switched CI from Travis to Github CI.
2021-10-13 12:15:17 +02:00

311 lines
10 KiB
PHP

<?php
/**
* Smarty PHPunit tests register->block / unregister->block methods
*
* @package PHPunit
* @author Uwe Tews
*/
/**
* class for register->block / unregister->block methods tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class RegisterBlockTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(dirname(__FILE__));
$this->smarty->disableSecurity();
}
public function testInit()
{
$this->cleanDirs();
}
/**
* test registerPlugin method for block function
*/
public function testRegisterBlockFunction()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock');
$this->smarty->assign('value', 1);
$this->assertEquals('function hello world 1 1 function hello world 1 2 function hello world 1 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
}
public function testRegisterBlockFunctionModifier1()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock');
$this->smarty->assign('value', 1);
$this->assertEquals(strtoupper('function hello world 1 1 function hello world 1 2 function hello world 1 3 '), $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock|strtoupper}'));
}
public function testRegisterBlockFunctionModifier2()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock');
$this->smarty->assign('value', 1);
$this->assertEquals(strtoupper('function hello world 1 1 function hello world 1 2 function hello world 1 3 '), $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock|default:""|strtoupper}'));
}
public function testRegisterBlockFunctionWrapper()
{
$this->smarty->registerPlugin('block', 'testblock', 'myblock');
$this->smarty->assign('value', 1);
$this->assertEquals('function hello world 1 1 function hello world 1 2 function hello world 1 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
}
/**
* test registerPlugin method for block class
*/
public function testRegisterBlockClass()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', array('myblockclass', 'static_method'));
$this->smarty->assign('value', 2);
$this->assertEquals('static hello world 2 1 static hello world 2 2 static hello world 2 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
}
public function testRegisterBlockClassWrapper()
{
$this->smarty->registerPlugin('block', 'testblock', array('myblockclass', 'static_method'));
$this->smarty->assign('value', 2);
$this->assertEquals('static hello world 2 1 static hello world 2 2 static hello world 2 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
}
/**
* test registerPlugin method for block object
*/
public function testRegisterBlockObject()
{
$myblock_object = new myblockclass;
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', array($myblock_object, 'object_method'));
$this->smarty->assign('value', 3);
$this->assertEquals('object hello world 3 1 object hello world 3 2 object hello world 3 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
}
public function testRegisterBlockObjectWrapper()
{
$myblock_object = new myblockclass;
$this->smarty->registerPlugin('block', 'testblock', array($myblock_object, 'object_method'));
$this->smarty->assign('value', 3);
$this->assertEquals('object hello world 3 1 object hello world 3 2 object hello world 3 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
}
/**
* test registerPlugin method for block with caching
*/
public function testRegisterBlockCaching1()
{
$this->smarty->caching = 1;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setForceCompile(true);
$this->smarty->assign('x', 1);
$this->smarty->assign('y', 10);
$this->smarty->assign('z', 100);
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache');
$this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching2()
{
$this->smarty->caching = 1;
$this->smarty->cache_lifetime = 1000;
$this->smarty->assign('x', 2);
$this->smarty->assign('y', 20);
$this->smarty->assign('z', 200);
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache');
$this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching3()
{
$this->smarty->caching = 1;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setForceCompile(true);
$this->smarty->assign('x', 3);
$this->smarty->assign('y', 30);
$this->smarty->assign('z', 300);
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false);
$this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching4()
{
$this->smarty->caching = 1;
$this->smarty->cache_lifetime = 1000;
$this->smarty->assign('x', 4);
$this->smarty->assign('y', 40);
$this->smarty->assign('z', 400);
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false);
$this->assertEquals('3 40 300', $this->smarty->fetch('test_register_block.tpl'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching1Wrapper()
{
$this->smarty->caching = 1;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setForceCompile(true);
$this->smarty->assign('x', 1);
$this->smarty->assign('y', 10);
$this->smarty->assign('z', 100);
$this->smarty->registerPlugin('block', 'testblock', 'myblockcache');
$this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching2Wrapper()
{
$this->smarty->caching = 1;
$this->smarty->cache_lifetime = 1000;
$this->smarty->assign('x', 2);
$this->smarty->assign('y', 20);
$this->smarty->assign('z', 200);
$this->smarty->registerPlugin('block', 'testblock', 'myblockcache');
$this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching3Wrapper()
{
$this->smarty->caching = 1;
$this->smarty->cache_lifetime = 1000;
$this->smarty->setForceCompile(true);
$this->smarty->assign('x', 3);
$this->smarty->assign('y', 30);
$this->smarty->assign('z', 300);
$this->smarty->registerPlugin('block', 'testblock', 'myblockcache', false);
$this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching4Wrapper()
{
$this->smarty->caching = 1;
$this->smarty->cache_lifetime = 1000;
$this->smarty->assign('x', 4);
$this->smarty->assign('y', 40);
$this->smarty->assign('z', 400);
$this->smarty->registerPlugin('block', 'testblock', 'myblockcache', false);
$this->assertEquals('3 40 300', $this->smarty->fetch('test_register_block.tpl'));
}
/**
* test unregister->block method
*/
public function testUnregisterBlock()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock');
$this->smarty->unregisterPlugin(Smarty::PLUGIN_BLOCK, 'testblock');
$this->assertFalse(isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK]['testblock']));
}
public function testUnregisterBlockWrapper()
{
$this->smarty->registerPlugin('block', 'testblock', 'myblock');
$this->smarty->unregisterPlugin('block', 'testblock');
$this->assertFalse(isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK]['testblock']));
}
/**
* test unregister->block method not registered
*/
public function testUnregisterBlockNotRegistered()
{
$this->smarty->unregisterPlugin(Smarty::PLUGIN_BLOCK, 'testblock');
$this->assertFalse(isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK]['testblock']));
}
}
function myblock($params, $content, &$smarty_tpl, &$repeat)
{
static $loop = 0;
if ($content == null) {
$loop = 0;
return;
}
$loop ++;
if ($loop < 3) {
$repeat = true;
}
return "function $content $loop ";
}
function myblockcache($params, $content, &$smarty_tpl, &$repeat)
{
return $content;
}
class myblockclass
{
static function static_method($params, $content, &$smarty_tpl, &$repeat)
{
static $loop = 0;
if ($content == null) {
$loop = 0;
return;
}
$loop ++;
if ($loop < 3) {
$repeat = true;
}
return "static $content $loop ";
}
public function object_method($params, $content, &$smarty_tpl, &$repeat)
{
static $loop = 0;
if ($content == null) {
$loop = 0;
return;
}
$loop ++;
if ($loop < 3) {
$repeat = true;
}
return "object $content $loop ";
}
}