Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request zendframework/zendframework#5743 from Martin-P/iss…
Browse files Browse the repository at this point in the history
…ue-5600

Add auto escape method to Zend\View\Helper\FlashMessenger
  • Loading branch information
weierophinney committed Apr 11, 2014
3 parents 5bae244 + a883a8d + 7aa531e commit 73431a3
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 9 deletions.
57 changes: 48 additions & 9 deletions src/Helper/FlashMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class FlashMessenger extends AbstractTranslatorHelper implements ServiceLocatorA
protected $messageOpenFormat = '<ul%s><li>';
protected $messageSeparatorString = '</li><li>';

/**
* Flag whether to escape messages
*
* @var bool
*/
protected $autoEscape = true;

/**
* Html escape helper
*
Expand Down Expand Up @@ -94,15 +101,16 @@ public function __call($method, $argv)
/**
* Render Messages
*
* @param string $namespace
* @param array $classes
* @param string $namespace
* @param array $classes
* @param null|bool $autoEscape
* @return string
*/
public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array())
public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array(), $autoEscape = null)
{
$flashMessenger = $this->getPluginFlashMessenger();
$messages = $flashMessenger->getMessagesFromNamespace($namespace);
return $this->renderMessages($namespace, $messages, $classes);
return $this->renderMessages($namespace, $messages, $classes, $autoEscape);
}

/**
Expand All @@ -112,11 +120,11 @@ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, arr
* @param array $classes
* @return string
*/
public function renderCurrent($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array())
public function renderCurrent($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array(), $autoEscape = null)
{
$flashMessenger = $this->getPluginFlashMessenger();
$messages = $flashMessenger->getCurrentMessagesFromNamespace($namespace);
return $this->renderMessages($namespace, $messages, $classes);
return $this->renderMessages($namespace, $messages, $classes, $autoEscape);
}

/**
Expand All @@ -126,7 +134,7 @@ public function renderCurrent($namespace = PluginFlashMessenger::NAMESPACE_DEFAU
* @param array $classes
* @return string
*/
protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $messages = array(), array $classes = array())
protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $messages = array(), array $classes = array(), $autoEscape = null)
{
// Prepare classes for opening tag
if (empty($classes)) {
Expand All @@ -137,19 +145,28 @@ protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_D
}
$classes = array($classes);
}
if (is_null($autoEscape)) {
$autoEscape = $this->getAutoEscape();
}

// Flatten message array
$escapeHtml = $this->getEscapeHtmlHelper();
$messagesToPrint = array();
$translator = $this->getTranslator();
$translatorTextDomain = $this->getTranslatorTextDomain();
array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml, $translator, $translatorTextDomain) {
array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml, $autoEscape, $translator, $translatorTextDomain) {
if ($translator !== null) {
$item = $translator->translate(
$item,
$translatorTextDomain
);
}
$messagesToPrint[] = $escapeHtml($item);

if ($autoEscape) {
$messagesToPrint[] = $escapeHtml($item);
} else {
$messagesToPrint[] = $item;
}
});
if (empty($messagesToPrint)) {
return '';
Expand All @@ -161,6 +178,28 @@ protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_D
return $markup;
}

/**
* Set whether or not auto escaping should be used
*
* @param bool $autoEscape
* @return self
*/
public function setAutoEscape($autoEscape = true)
{
$this->autoEscape = (bool) $autoEscape;
return $this;
}

/**
* Return whether auto escaping is enabled or disabled
*
* return bool
*/
public function getAutoEscape()
{
return $this->autoEscape;
}

/**
* Set the string used to close message representation
*
Expand Down
136 changes: 136 additions & 0 deletions test/Helper/FlashMessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,140 @@ public function testCanTranslateCurrentMessages()
$display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayAssertion, $display);
}

public function testAutoEscapeDefaultsToTrue()
{
$this->assertTrue($this->helper->getAutoEscape());
}

public function testCanSetAutoEscape()
{
$this->helper->setAutoEscape(false);
$this->assertFalse($this->helper->getAutoEscape());

$this->helper->setAutoEscape(true);
$this->assertTrue($this->helper->getAutoEscape());
}

/**
* @covers Zend\View\Helper\FlashMessenger::render
*/
public function testMessageIsEscapedByDefault()
{
$helper = new FlashMessenger;
$helper->addMessage('Foo<br />bar');
unset($helper);

$displayAssertion = '<ul class="default"><li>Foo&lt;br /&gt;bar</li></ul>';
$display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT);
$this->assertSame($displayAssertion, $display);
}

/**
* @covers Zend\View\Helper\FlashMessenger::render
*/
public function testMessageIsNotEscapedWhenAutoEscapeIsFalse()
{
$helper = new FlashMessenger;
$helper->addMessage('Foo<br />bar');
unset($helper);

$displayAssertion = '<ul class="default"><li>Foo<br />bar</li></ul>';
$display = $this->helper->setAutoEscape(false)
->render(PluginFlashMessenger::NAMESPACE_DEFAULT);
$this->assertSame($displayAssertion, $display);
}

/**
* @covers Zend\View\Helper\FlashMessenger::render
*/
public function testCanSetAutoEscapeOnRender()
{
$helper = new FlashMessenger;
$helper->addMessage('Foo<br />bar');
unset($helper);

$displayAssertion = '<ul class="default"><li>Foo<br />bar</li></ul>';
$display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT, array(), false);
$this->assertSame($displayAssertion, $display);
}

/**
* @covers Zend\View\Helper\FlashMessenger::render
*/
public function testRenderUsesCurrentAutoEscapeByDefault()
{
$helper = new FlashMessenger;
$helper->addMessage('Foo<br />bar');
unset($helper);

$this->helper->setAutoEscape(false);
$displayAssertion = '<ul class="default"><li>Foo<br />bar</li></ul>';
$display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT);
$this->assertSame($displayAssertion, $display);

$helper = new FlashMessenger;
$helper->addMessage('Foo<br />bar');
unset($helper);

$this->helper->setAutoEscape(true);
$displayAssertion = '<ul class="default"><li>Foo&lt;br /&gt;bar</li></ul>';
$display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT);
$this->assertSame($displayAssertion, $display);
}

/**
* @covers Zend\View\Helper\FlashMessenger::renderCurrent
*/
public function testCurrentMessageIsEscapedByDefault()
{
$this->helper->addMessage('Foo<br />bar');

$displayAssertion = '<ul class="default"><li>Foo&lt;br /&gt;bar</li></ul>';
$display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT);
$this->assertSame($displayAssertion, $display);
}

/**
* @covers Zend\View\Helper\FlashMessenger::renderCurrent
*/
public function testCurrentMessageIsNotEscapedWhenAutoEscapeIsFalse()
{
$this->helper->addMessage('Foo<br />bar');

$displayAssertion = '<ul class="default"><li>Foo<br />bar</li></ul>';
$display = $this->helper->setAutoEscape(false)
->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT);
$this->assertSame($displayAssertion, $display);
}

/**
* @covers Zend\View\Helper\FlashMessenger::renderCurrent
*/
public function testCanSetAutoEscapeOnRenderCurrent()
{
$this->helper->addMessage('Foo<br />bar');

$displayAssertion = '<ul class="default"><li>Foo<br />bar</li></ul>';
$display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT, array(), false);
$this->assertSame($displayAssertion, $display);
}

/**
* @covers Zend\View\Helper\FlashMessenger::renderCurrent
*/
public function testRenderCurrentUsesCurrentAutoEscapeByDefault()
{
$this->helper->addMessage('Foo<br />bar');

$this->helper->setAutoEscape(false);
$displayAssertion = '<ul class="default"><li>Foo<br />bar</li></ul>';
$display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT);
$this->assertSame($displayAssertion, $display);

$this->helper->setAutoEscape(true);
$displayAssertion = '<ul class="default"><li>Foo&lt;br /&gt;bar</li></ul>';
$display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT);
$this->assertSame($displayAssertion, $display);
}
}

0 comments on commit 73431a3

Please sign in to comment.