Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix change in signature of getTemplateVars #995

Merged
merged 2 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/994.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix that getTemplateVars would return an array of objects instead of the assigned variables values [#994](https://github.com/smarty-php/smarty/issues/994)
5 changes: 4 additions & 1 deletion src/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ public function getTemplateVars($varName = null, $searchParents = true)
return $this->getValue($varName, $searchParents);
}

return array_merge($this->parent && $searchParents ? $this->parent->getTemplateVars() : [], $this->tpl_vars);
return array_merge(
$this->parent && $searchParents ? $this->parent->getTemplateVars() : [],
array_map(function(Variable $var) { return $var->getValue(); }, $this->tpl_vars)
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,112 +1,126 @@
<?php
/**
* Smarty PHPunit tests getTemplateVars method
*

* @author Uwe Tews
*/

/**
* class for getTemplateVars method test
*
*
*
*
*/
class GetTemplateVarsTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}

public function testInit()
{
$this->cleanDirs();
}
/**
* test root getTemplateVars single value
*/
public function testGetSingleTemplateVarScopeRoot()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->assign('blar', 'buh');
$this->assertEquals("bar", $this->smarty->getTemplateVars('foo'));
}

/**
* test root getTemplateVars all values
*/
public function testGetAllTemplateVarsScopeRoot()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->assign('blar', 'buh');
$vars = $this->smarty->getTemplateVars();
$this->assertTrue(is_array($vars));
$this->assertEquals("bar", $vars['foo']);
$this->assertEquals("buh", $vars['blar']);
}

/**
* test single variable with data object chain
*/
public function testGetSingleTemplateVarScopeAll()
{
$data1 = $this->smarty->createData($this->smarty);
$data2 = $this->smarty->createData($data1);
$this->smarty->assign('foo', 'bar');
$this->smarty->assign('blar', 'buh');
$this->assertEquals("bar", $data2->getTemplateVars('foo'));
}

/**
* test get all variables with data object chain
*/
public function testGetAllTemplateVarsScopeAll()
{
$data1 = $this->smarty->createData($this->smarty);
$data2 = $this->smarty->createData($data1);
$this->smarty->assign('foo', 'bar');
$data1->assign('blar', 'buh');
$data2->assign('foo2', 'bar2');
$vars = $data2->getTemplateVars(null);
$this->assertTrue(is_array($vars));
$this->assertEquals("bar", $vars['foo']);
$this->assertEquals("bar2", $vars['foo2']);
$this->assertEquals("buh", $vars['blar']);
}

/**
* test get all variables with data object chain search parents disabled
*/
public function testGetAllTemplateVarsScopeAllNoParents()
{
$data1 = $this->smarty->createData($this->smarty);
$data2 = $this->smarty->createData($data1);
$this->smarty->assign('foo', 'bar');
$data1->assign('blar', 'buh');
$data2->assign('foo2', 'bar2');
$vars = $data2->getTemplateVars(null, false);
$this->assertTrue(is_array($vars));
$this->assertFalse(isset($vars['foo']));
$this->assertEquals("bar2", $vars['foo2']);
$this->assertFalse(isset($vars['blar']));
}

public function testInit()
{
$this->cleanDirs();
}
/**
* test root getTemplateVars single value
*/
public function testGetSingleTemplateVarScopeRoot()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->assign('blar', 'buh');
$this->assertEquals("bar", $this->smarty->getTemplateVars('foo'));
}
/**
* test get single variables with data object chain search parents disabled
*/
public function testGetSingleTemplateVarsScopeAllNoParents()
{
error_reporting(error_reporting() & ~(E_NOTICE | E_USER_NOTICE));
$data1 = $this->smarty->createData($this->smarty);
$data2 = $this->smarty->createData($data1);
$this->smarty->assign('foo', 'bar');
$data1->assign('blar', 'buh');
$data2->assign('foo2', 'bar2');
$this->assertEquals("", $data2->getTemplateVars('foo', false));
$this->assertEquals("bar2", $data2->getTemplateVars('foo2', false));
$this->assertEquals("", $data2->getTemplateVars('blar', false));
}

/**
* test root getTemplateVars all values
*/
public function testGetAllTemplateVarsScopeRoot()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->assign('blar', 'buh');
$vars = $this->smarty->getTemplateVars();
$this->assertTrue(is_array($vars));
$this->assertEquals("bar", $vars['foo']);
$this->assertEquals("buh", $vars['blar']);
}
/**
* test that variable assigned by global assign in template is included in getTemplateVars
*/
public function testAssignedInTemplate()
{
$this->smarty->fetch('string:{assign var="b" value="x" scope="global"}');
$this->assertEquals('x', $this->smarty->getTemplateVars('b'));
}

/**
* test single variable with data object chain
*/
public function testGetSingleTemplateVarScopeAll()
{
$data1 = $this->smarty->createData($this->smarty);
$data2 = $this->smarty->createData($data1);
$this->smarty->assign('foo', 'bar');
$this->smarty->assign('blar', 'buh');
$this->assertEquals("bar", $data2->getTemplateVars('foo'));
}
/**
* test that getTemplateVars returns simple array of values
*/
public function testSimpleCallReturnsArrayWithAllValues()
{
$this->smarty->assign('foo', 'bar');
$this->smarty->assign('i', 3);

/**
* test get all variables with data object chain
*/
public function testGetAllTemplateVarsScopeAll()
{
$data1 = $this->smarty->createData($this->smarty);
$data2 = $this->smarty->createData($data1);
$this->smarty->assign('foo', 'bar');
$data1->assign('blar', 'buh');
$data2->assign('foo2', 'bar2');
$vars = $data2->getTemplateVars(null);
$this->assertTrue(is_array($vars));
$this->assertEquals("bar", $vars['foo']);
$this->assertEquals("bar2", $vars['foo2']);
$this->assertEquals("buh", $vars['blar']);
}
$vars = $this->smarty->getTemplateVars();

/**
* test get all variables with data object chain search parents disabled
*/
public function testGetAllTemplateVarsScopeAllNoParents()
{
$data1 = $this->smarty->createData($this->smarty);
$data2 = $this->smarty->createData($data1);
$this->smarty->assign('foo', 'bar');
$data1->assign('blar', 'buh');
$data2->assign('foo2', 'bar2');
$vars = $data2->getTemplateVars(null, false);
$this->assertTrue(is_array($vars));
$this->assertFalse(isset($vars['foo']));
$this->assertEquals("bar2", $vars['foo2']);
$this->assertFalse(isset($vars['blar']));
}
$this->assertArrayHasKey('foo', $vars);
$this->assertArrayHasKey('i', $vars);
$this->assertEquals('bar', $vars['foo']);
$this->assertEquals(3,$vars['i']);
}

/**
* test get single variables with data object chain search parents disabled
*/
public function testGetSingleTemplateVarsScopeAllNoParents()
{
error_reporting(error_reporting() & ~(E_NOTICE | E_USER_NOTICE));
$data1 = $this->smarty->createData($this->smarty);
$data2 = $this->smarty->createData($data1);
$this->smarty->assign('foo', 'bar');
$data1->assign('blar', 'buh');
$data2->assign('foo2', 'bar2');
$this->assertEquals("", $data2->getTemplateVars('foo', false));
$this->assertEquals("bar2", $data2->getTemplateVars('foo2', false));
$this->assertEquals("", $data2->getTemplateVars('blar', false));
}
}
Loading