Files
smarty/tests/UnitTests/TemplateSource/ValueTests/PHPfunctions/PhpFunctionTest.php
Simon Wisselink ff2ef3b0cb Redirect test temp dirs to system temp directory
* Redirect test temp dirs to system temp directory. Fixes #1178

Move all test-generated output (compiled templates, cache files, and
temporary template sources) from per-test-directory folders inside the
working tree to a parallel structure under sys_get_temp_dir()/smarty-tests/.

This removes 215 boilerplate .gitignore files from the repo and ensures
running the test suite leaves zero uncommitted files in the working tree.

All 2296 tests continue to pass with identical behavior.

* Isolate each test class in a unique temp directory

getTempDir() now appends a per-class uniqid token to the temp path, so
concurrent or sequential test runs never share compiled/cached output.
The token is generated lazily on first use and reset in
tearDownAfterClass(), giving every test class a fresh isolated directory.

As a result, the Bootstrap.php pre-run cleanup of smarty-tests/ is no
longer needed for correctness (stale paths are unreachable) and was
harmful to concurrent runs, so it has been removed.

* Remove individualFolders dead code and spurious assertTrue from cleanDirs()

- Remove the never-active individualFolders code path from setUpSmarty()
  (the constant was always true, making the branch unreachable)
- Remove define('individualFolders') from Config.php and the constructor
- Remove $this->assertTrue(true) from cleanDirs(): it existed solely to
  make testInit() count as a passing test; now that cleanDirs() is called
  from setUpSmarty() and from test methods directly, the assertion was
  spuriously inflating assertion counts
- Add tests/**/templates_c/, cache/, templates_tmp/ to .gitignore to
  prevent stale test output from appearing as untracked files

* Clean up each test class's unique temp dir in tearDownAfterClass()

Add a private static removeDir() helper and call it from
tearDownAfterClass() to recursively delete the per-class unique temp
directory after each test class finishes. Cleanup failures are silently
ignored (@ suppression) so they never cause test failures.

Set KEEP_SMARTY_TEST_ARTIFACTS=1 in the environment to skip cleanup and
keep the artifacts on disk for debugging.

* cleanup of unused template files, non-shared files stored in __shared folder, no longer required calls to add template folders et cetera

* fixed the unit tests

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* remove useless resetting of static properties in tearDownAfterClass

* changed an incorrect doc and formatted some code.

* add changelog

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-13 21:36:33 +02:00

220 lines
8.8 KiB
PHP

<?php
/**
* Smarty PHPunit tests of modifier
*
* @author Rodney Rehm
*/
/**
* class for modifier tests
*
*
*
*
*
*
*/
class PhpFunctionTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* test PHP empty() on variables true
*/
public function testEmpty1()
{
$this->smarty->disableSecurity();
$this->smarty->assign('var', array(null,
false,
(int) 0,
(float) 0.0,
'',
array()));
$expected = ' true , true , true , true , true , true , true , true ';
$this->assertEquals($expected, $this->smarty->fetch('string:{strip}{if empty($var[0])} true {else} false {/IF}
,{if empty($var[1])} true {else} false {/IF}
,{if empty($var[2])} true {else} false {/IF}
,{if empty($var[3])} true {else} false {/IF}
,{if empty($var[4])} true {else} false {/IF}
,{if empty($var[5])} true {else} false {/IF}
,{if empty($var[6])} true {else} false {/IF}
,{if empty($varr)} true {else} false {/IF}
'));
}
/**
* test PHP empty() on function result true
*/
public function testEmpty2()
{
$this->smarty->disableSecurity();
$this->getSmarty()->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'pass', function ($v) { return $v; });
$this->smarty->assign('var', array(null,
false,
(int) 0,
(float) 0.0,
'',
array()));
$expected = ' true , true , true , true , true , true ';
$this->assertEquals($expected, $this->smarty->fetch('string:{strip}{if empty(pass($var[0]))} true {else} false {/IF}
,{if empty(pass($var[1]))} true {else} false {/IF}
,{if empty(pass($var[2]))} true {else} false {/IF}
,{if empty(pass($var[3]))} true {else} false {/IF}
,{if empty(pass($var[4]))} true {else} false {/IF}
,{if empty(pass($var[5]))} true {else} false {/IF}
'));
}
/**
* test PHP empty() on function result false
*/
public function testEmpty3()
{
$this->smarty->disableSecurity();
$this->getSmarty()->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'pass', function ($v) { return $v; });
$this->smarty->assign('var', array(true,
(int) 1,
(float) 0.1,
' ',
array(1)));
$expected = ' false , false , false , false , false ';
$this->assertEquals($expected, $this->smarty->fetch('string:{strip}{if empty(pass($var[0]))} true {else} false {/IF}
,{if empty(pass($var[1]))} true {else} false {/IF}
,{if empty(pass($var[2]))} true {else} false {/IF}
,{if empty(pass($var[3]))} true {else} false {/IF}
,{if empty(pass($var[4]))} true {else} false {/IF}
'));
}
/**
* test PHP empty() on object
*/
public function testEmpty4()
{
$this->smarty->disableSecurity();
$this->getSmarty()->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'pass', function ($v) { return $v; });
$this->smarty->assign('var', new TestIsset());
$expected = ' true , false , false , true , true , true , false ';
$this->assertEquals($expected, $this->smarty->fetch('string:{strip}{if empty($var->isNull)} true {else} false {/IF}
,{if empty($var->isSet)} true {else} false {/IF}
,{if empty($var->arr[\'isSet\'])} true {else} false {/IF}
,{if empty($var->arr[\'isNull\'])} true {else} false {/IF}
,{if empty($var->arr[\'foo\'])} true {else} false {/IF}
,{if empty($var->pass(null))} true {else} false {/IF}
,{if empty($var->pass(1))} true {else} false {/IF}
'));
}
/**
* test PHP isset() on variables and functions
*/
public function testIsset1()
{
$this->smarty->disableSecurity();
$this->getSmarty()->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'pass', function ($v) { return $v; });
$this->smarty->assign('isNull', null);
$this->smarty->assign('isSet', 1);
$this->smarty->assign('arr', array('isNull' => null, 'isSet' => 1));
$expected = ' false , true , false , true , false , false , false , true ';
$this->assertEquals($expected, $this->smarty->fetch('string:{strip}{if isset($isNull)} true {else} false {/IF}
,{if isset($isSet)} true {else} false {/IF}
,{if isset($foo)} true {else} false {/IF}
,{if isset($arr[\'isSet\'])} true {else} false {/IF}
,{if isset($arr[\'isNull\'])} true {else} false {/IF}
,{if isset($arr[\'foo\'])} true {else} false {/IF}
,{if isset(pass(null))} true {else} false {/IF}
,{if isset(pass(1))} true {else} false {/IF}
'));
}
/**
* test PHP isset() on object
*/
public function testIsset2()
{
$this->smarty->disableSecurity();
$this->smarty->assign('var', new TestIsset());
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'pass', function ($v) { return $v; });
$expected = ' false , true , true , false , false , false , true ';
$this->assertEquals($expected, $this->smarty->fetch('string:{strip}{if isset($var->isNull)} true {else} false {/IF}
,{if isset($var->isSet)} true {else} false {/IF}
,{if isset($var->arr[\'isSet\'])} true {else} false {/IF}
,{if isset($var->arr[\'isNull\'])} true {else} false {/IF}
,{if isset($var->arr[\'foo\'])} true {else} false {/IF}
,{if isset($var->pass(null))} true {else} false {/IF}
,{if isset($var->pass(1))} true {else} false {/IF}
'));
}
/**
* test PHP isset() on (non-)variables
* @dataProvider dataTestIsset3
* @param string $strTemplate template to test
* @param string $result expected result
*/
public function testIsset3($strTemplate, $result)
{
$this->smarty->disableSecurity();
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'intval', 'intval');
$this->smarty->assign('varobject', new TestIsset());
$this->smarty->assign('vararray', $vararray = array(
'keythatexists' => false,
'keywitharray' => array(1 => 1),
'keywithobject' => new TestIsset()
));
$this->smarty->assign('key', 'A');
$this->smarty->assign('_varsimpleA', 1);
$this->smarty->assign('varsimpleB', 0);
$this->smarty->assign('varsimpleC', null);
$this->assertEquals($result, $this->smarty->fetch('string:' . $strTemplate));
}
/**
* Data provider for testIsset3
*/
public function dataTestIsset3()
{
return array(
array('{if isset($varobject->arr)}true{else}false{/if}', 'true'),
array('{if isset($vararray["keywitharray"])}true{else}false{/if}', 'true'),
array('{if isset($vararray["keythatexists"])}true{else}false{/if}', 'true'),
array('{if isset($vararray["nonexistingkey"])}true{else}false{/if}', 'false'),
array('{if isset($_GET["sscr6hr6cz34j6"])}true{else}false{/if}', 'false'),
array('{if isset(count([\'hi\']))}true{else}false{/if}', 'true'),
array('{if isset($vararray[\'keywitharray\'][intval(\'1\')])}true{else}false{/if}', 'true'),
array('{if isset($vararray[\'keywithobject\']->arr[\'isSet\'])}true{else}false{/if}', 'true'),
array('{if isset($vararray[\'keywithobject\']->arr[\'isNull\'])}true{else}false{/if}', 'false'),
array('{if isset($varobject->arr[\'isSet\'])}true{else}false{/if}', 'true'),
array('{if isset($varobject->arr[\'isNull\'])}true{else}false{/if}', 'false'),
array('{if isset($_varsimpleA)}true{else}false{/if}', 'true'),
array('{if isset($varsimpleB)}true{else}false{/if}', 'true'),
array('{if isset($varsimpleC)}true{else}false{/if}', 'false'),
array('{if isset($_varsimpleA && varsimpleB)}true{else}false{/if}', 'true'),
array('{if isset($_varsimpleA && varsimpleC)}true{else}false{/if}', 'true'),
array('{if isset($_varsimple{$key})}true{else}false{/if}', 'true'),
);
}
}
/**
* Class TestIsset
*/
class TestIsset {
public $isNull = null;
public $isSet = 1;
public $arr = array('isNull' => null, 'isSet' => 1);
/**
* @param mixed $v
*
* @return mixed
*/
public function pass($v) {
return $v;
}
}