2020-04-13 15:30:52 +02:00
< ? 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
{
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 -> disableSecurity ();
}
public function testInit ()
{
$this -> cleanDirs ();
}
/**
* test registerPlugin method for block function
*/
public function testRegisterBlockFunction ()
{
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , 'myblock' );
2020-04-13 15:30:52 +02:00
$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 ()
{
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , 'myblock' );
2020-04-13 15:30:52 +02:00
$this -> smarty -> assign ( 'value' , 1 );
2022-09-23 00:09:00 +02:00
$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|upper}' ));
2020-04-13 15:30:52 +02:00
}
public function testRegisterBlockFunctionModifier2 ()
{
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , 'myblock' );
2020-04-13 15:30:52 +02:00
$this -> smarty -> assign ( 'value' , 1 );
2022-09-23 00:09:00 +02:00
$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:""|upper}' ));
2020-04-13 15:30:52 +02:00
}
public function testRegisterBlockFunctionWrapper ()
{
2021-10-13 12:15:17 +02:00
$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}' ));
2020-04-13 15:30:52 +02:00
}
/**
* test registerPlugin method for block class
*/
public function testRegisterBlockClass ()
{
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , array ( 'myblockclass' , 'static_method' ));
2020-04-13 15:30:52 +02:00
$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 ()
{
2021-10-13 12:15:17 +02:00
$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}' ));
2020-04-13 15:30:52 +02:00
}
/**
* test registerPlugin method for block object
*/
public function testRegisterBlockObject ()
{
$myblock_object = new myblockclass ;
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , array ( $myblock_object , 'object_method' ));
2020-04-13 15:30:52 +02:00
$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 ;
2021-10-13 12:15:17 +02:00
$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}' ));
2020-04-13 15:30:52 +02:00
}
/**
* 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 );
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , 'myblockcache' );
2020-04-13 15:30:52 +02:00
$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 );
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , 'myblockcache' );
2020-04-13 15:30:52 +02:00
$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 );
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , 'myblockcache' , false );
2020-04-13 15:30:52 +02:00
$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 );
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , 'myblockcache' , false );
2020-04-13 15:30:52 +02:00
$this -> assertEquals ( '3 40 300' , $this -> smarty -> fetch ( 'test_register_block.tpl' ));
}
/**
* @ runInSeparateProcess
* @ preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching1Wrapper ()
{
2021-10-13 12:15:17 +02:00
$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' ));
2020-04-13 15:30:52 +02:00
}
/**
* @ runInSeparateProcess
* @ preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching2Wrapper ()
{
2021-10-13 12:15:17 +02:00
$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' ));
2020-04-13 15:30:52 +02:00
}
/**
* @ runInSeparateProcess
* @ preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching3Wrapper ()
{
2021-10-13 12:15:17 +02:00
$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' ));
2020-04-13 15:30:52 +02:00
}
/**
* @ runInSeparateProcess
* @ preserveGlobalState disabled
*
*/
public function testRegisterBlockCaching4Wrapper ()
{
2021-10-13 12:15:17 +02:00
$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' ));
2020-04-13 15:30:52 +02:00
}
/**
* test unregister -> block method
*/
public function testUnregisterBlock ()
{
2022-11-30 00:25:27 +01:00
$this -> smarty -> registerPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' , 'myblock' );
$this -> smarty -> unregisterPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' );
$this -> assertFalse ( isset ( $this -> smarty -> registered_plugins [ \Smarty\Smarty :: PLUGIN_BLOCK ][ 'testblock' ]));
2020-04-13 15:30:52 +02:00
}
public function testUnregisterBlockWrapper ()
{
2021-10-13 12:15:17 +02:00
$this -> smarty -> registerPlugin ( 'block' , 'testblock' , 'myblock' );
$this -> smarty -> unregisterPlugin ( 'block' , 'testblock' );
2022-11-30 00:25:27 +01:00
$this -> assertFalse ( isset ( $this -> smarty -> registered_plugins [ \Smarty\Smarty :: PLUGIN_BLOCK ][ 'testblock' ]));
2020-04-13 15:30:52 +02:00
}
/**
* test unregister -> block method not registered
*/
public function testUnregisterBlockNotRegistered ()
{
2022-11-30 00:25:27 +01:00
$this -> smarty -> unregisterPlugin ( \Smarty\Smarty :: PLUGIN_BLOCK , 'testblock' );
$this -> assertFalse ( isset ( $this -> smarty -> registered_plugins [ \Smarty\Smarty :: PLUGIN_BLOCK ][ 'testblock' ]));
2020-04-13 15:30:52 +02:00
}
}
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 " ;
}
}