Skip to content

Commit

Permalink
Fetch widget's secure fields list after rendering widget.
Browse files Browse the repository at this point in the history
This allows the flexibility for widgets to populate secure fields
list based on rendering process rather than data argument.
  • Loading branch information
ADmad committed Apr 20, 2015
1 parent df4d03e commit 2e1a61d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/View/Helper/FormHelper.php
Expand Up @@ -2519,17 +2519,19 @@ public function addWidget($name, $spec)
*/
public function widget($name, array $data = [])
{
$secure = null;
if (isset($data['secure'])) {
$secure = $data['secure'];
unset($data['secure']);
}
$widget = $this->_registry->get($name);
if (isset($data['secure'], $data['name']) &&
$data['secure'] !== self::SECURE_SKIP
) {
$out = $widget->render($data, $this->context());
if (isset($data['name']) && $secure !== null && $secure !== self::SECURE_SKIP) {
foreach ($widget->secureFields($data) as $field) {
$this->_secure($data['secure'], $this->_secureFieldName($field));
$this->_secure($secure, $this->_secureFieldName($field));
}
}
unset($data['secure']);

return $widget->render($data, $this->context());
return $out;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -262,6 +262,35 @@ public function testAddWidgetAndRenderWidget()
$this->assertEquals('HTML', $result);
}

/**
* Test that secureFields() of widget is called after calling render(),
* not before.
*
* @return void
*/
public function testOrderForRenderingWidgetAndFetchingSecureFields()
{
$data = [
'val' => 1,
'name' => 'test'
];
$mock = $this->getMock('Cake\View\Widget\WidgetInterface');
$this->assertNull($this->Form->addWidget('test', $mock));

$mock->expects($this->at(0))
->method('render')
->with($data)
->will($this->returnValue('HTML'));

$mock->expects($this->at(1))
->method('secureFields')
->with($data)
->will($this->returnValue(['test']));

$result = $this->Form->widget('test', $data + ['secure' => true]);
$this->assertEquals('HTML', $result);
}

/**
* Test that empty string is not added to secure fields list when
* rendering input widget without name.
Expand Down

0 comments on commit 2e1a61d

Please sign in to comment.