From a74ba9a2308d18182a83097a4d12bb8074c3f88f Mon Sep 17 00:00:00 2001 From: "github@martinvanpetten.nl" Date: Thu, 23 Jan 2014 18:42:30 +0100 Subject: [PATCH 1/8] Add autoEscape option to FlashMessenger --- src/Helper/FlashMessenger.php | 39 ++++++++++++++++++++++++++++-- test/Helper/FlashMessengerTest.php | 37 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/Helper/FlashMessenger.php b/src/Helper/FlashMessenger.php index 9c36a1fb..f86acccc 100644 --- a/src/Helper/FlashMessenger.php +++ b/src/Helper/FlashMessenger.php @@ -41,6 +41,13 @@ class FlashMessenger extends AbstractTranslatorHelper implements ServiceLocatorA protected $messageOpenFormat = '
  • '; protected $messageSeparatorString = '
  • '; + /** + * Flag whether to escape messages + * + * @var bool + */ + protected $autoEscape = true; + /** * Html escape helper * @@ -139,17 +146,23 @@ protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_D } // Flatten message array $escapeHtml = $this->getEscapeHtmlHelper(); + $autoEscape = $this->getAutoEscape(); $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 ''; @@ -161,6 +174,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 * diff --git a/test/Helper/FlashMessengerTest.php b/test/Helper/FlashMessengerTest.php index 715bb852..d5b80b21 100644 --- a/test/Helper/FlashMessengerTest.php +++ b/test/Helper/FlashMessengerTest.php @@ -443,4 +443,41 @@ 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()); + } + + public function testMessageIsEscapedByDefault() + { + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $displayAssertion = '
    • Foo<br />bar
    '; + $display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT); + $this->assertSame($displayAssertion, $display); + } + + public function testMessageIsNotEscapedWhenAutoEscapeIsFalse() + { + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->setAutoEscape(false) + ->render(PluginFlashMessenger::NAMESPACE_DEFAULT); + $this->assertSame($displayAssertion, $display); + } } From b215850e2ce87ad3033d8648b29a107c96eb14ed Mon Sep 17 00:00:00 2001 From: "github@martinvanpetten.nl" Date: Wed, 5 Mar 2014 18:47:42 +0100 Subject: [PATCH 2/8] Added tests for autoEscape via render() --- test/Helper/FlashMessengerTest.php | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/Helper/FlashMessengerTest.php b/test/Helper/FlashMessengerTest.php index d5b80b21..aebf6c83 100644 --- a/test/Helper/FlashMessengerTest.php +++ b/test/Helper/FlashMessengerTest.php @@ -480,4 +480,42 @@ public function testMessageIsNotEscapedWhenAutoEscapeIsFalse() ->render(PluginFlashMessenger::NAMESPACE_DEFAULT); $this->assertSame($displayAssertion, $display); } + + /** + * @covers Zend\View\Helper\FlashMessenger::render + */ + public function testCanSetAutoEscapeOnRender() + { + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT, array(), true); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\View\Helper\FlashMessenger::render + */ + public function testRenderUsesCurrentAutoEscapeByDefault() + { + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $this->helper->setAutoEscape(false); + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT); + $this->assertSame($displayAssertion, $display); + + $helper = new FlashMessenger; + $helper->addMessage('Foo
    bar'); + unset($helper); + + $this->helper->setAutoEscape(true); + $displayAssertion = '
    • Foo<br />bar
    '; + $display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT); + $this->assertSame($displayAssertion, $display); + } } From 47bcdaa4954ff543a4981bcda557b853af1c15e8 Mon Sep 17 00:00:00 2001 From: "github@martinvanpetten.nl" Date: Wed, 5 Mar 2014 18:54:46 +0100 Subject: [PATCH 3/8] Fixed error in test --- test/Helper/FlashMessengerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Helper/FlashMessengerTest.php b/test/Helper/FlashMessengerTest.php index aebf6c83..844a596e 100644 --- a/test/Helper/FlashMessengerTest.php +++ b/test/Helper/FlashMessengerTest.php @@ -491,7 +491,7 @@ public function testCanSetAutoEscapeOnRender() unset($helper); $displayAssertion = '
    • Foo
      bar
    '; - $display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT, array(), true); + $display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT, array(), false); $this->assertSame($displayAssertion, $display); } From 988d63157313d5bda77b8a5347ce54057e52b3ed Mon Sep 17 00:00:00 2001 From: "github@martinvanpetten.nl" Date: Wed, 5 Mar 2014 18:57:11 +0100 Subject: [PATCH 4/8] Added third parameter to render() whether to use autoEscape --- src/Helper/FlashMessenger.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Helper/FlashMessenger.php b/src/Helper/FlashMessenger.php index f86acccc..b75890ba 100644 --- a/src/Helper/FlashMessenger.php +++ b/src/Helper/FlashMessenger.php @@ -105,7 +105,7 @@ public function __call($method, $argv) * @param array $classes * @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); @@ -144,9 +144,12 @@ protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_D } $classes = array($classes); } + if (is_null($autoEscape)) { + $autoEscape = $this->getAutoEscape(); + } + // Flatten message array $escapeHtml = $this->getEscapeHtmlHelper(); - $autoEscape = $this->getAutoEscape(); $messagesToPrint = array(); $translator = $this->getTranslator(); $translatorTextDomain = $this->getTranslatorTextDomain(); From 13523e30da4336459ebb2f920c272348c1943a75 Mon Sep 17 00:00:00 2001 From: "github@martinvanpetten.nl" Date: Wed, 5 Mar 2014 21:30:31 +0100 Subject: [PATCH 5/8] Updated render() docblock with the third parameter --- src/Helper/FlashMessenger.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Helper/FlashMessenger.php b/src/Helper/FlashMessenger.php index b75890ba..e3a5ba3e 100644 --- a/src/Helper/FlashMessenger.php +++ b/src/Helper/FlashMessenger.php @@ -101,8 +101,9 @@ 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(), $autoEscape = null) From 7aa531ea7c9c8186d720e5c0a11d3e6120deca72 Mon Sep 17 00:00:00 2001 From: Martin-P Date: Fri, 4 Apr 2014 04:14:19 +0200 Subject: [PATCH 6/8] Fixed rebase conflicts, added same functionality to renderCurrent and added tests --- src/Helper/FlashMessenger.php | 8 ++-- test/Helper/FlashMessengerTest.php | 63 +++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/Helper/FlashMessenger.php b/src/Helper/FlashMessenger.php index e3a5ba3e..f3cc2f27 100644 --- a/src/Helper/FlashMessenger.php +++ b/src/Helper/FlashMessenger.php @@ -110,7 +110,7 @@ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, arr { $flashMessenger = $this->getPluginFlashMessenger(); $messages = $flashMessenger->getMessagesFromNamespace($namespace); - return $this->renderMessages($namespace, $messages, $classes); + return $this->renderMessages($namespace, $messages, $classes, $autoEscape); } /** @@ -120,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); } /** @@ -134,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)) { diff --git a/test/Helper/FlashMessengerTest.php b/test/Helper/FlashMessengerTest.php index 844a596e..6be7d88e 100644 --- a/test/Helper/FlashMessengerTest.php +++ b/test/Helper/FlashMessengerTest.php @@ -454,10 +454,13 @@ public function testCanSetAutoEscape() $this->helper->setAutoEscape(false); $this->assertFalse($this->helper->getAutoEscape()); - $this->helper->SetAutoEscape(true); + $this->helper->setAutoEscape(true); $this->assertTrue($this->helper->getAutoEscape()); } + /** + * @covers Zend\View\Helper\FlashMessenger::render + */ public function testMessageIsEscapedByDefault() { $helper = new FlashMessenger; @@ -469,6 +472,9 @@ public function testMessageIsEscapedByDefault() $this->assertSame($displayAssertion, $display); } + /** + * @covers Zend\View\Helper\FlashMessenger::render + */ public function testMessageIsNotEscapedWhenAutoEscapeIsFalse() { $helper = new FlashMessenger; @@ -518,4 +524,59 @@ public function testRenderUsesCurrentAutoEscapeByDefault() $display = $this->helper->render(PluginFlashMessenger::NAMESPACE_DEFAULT); $this->assertSame($displayAssertion, $display); } + + /** + * @covers Zend\View\Helper\FlashMessenger::renderCurrent + */ + public function testCurrentMessageIsEscapedByDefault() + { + $this->helper->addMessage('Foo
    bar'); + + $displayAssertion = '
    • Foo<br />bar
    '; + $display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT); + $this->assertSame($displayAssertion, $display); + } + + /** + * @covers Zend\View\Helper\FlashMessenger::renderCurrent + */ + public function testCurrentMessageIsNotEscapedWhenAutoEscapeIsFalse() + { + $this->helper->addMessage('Foo
    bar'); + + $displayAssertion = '
    • Foo
      bar
    '; + $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
    bar'); + + $displayAssertion = '
    • Foo
      bar
    '; + $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
    bar'); + + $this->helper->setAutoEscape(false); + $displayAssertion = '
    • Foo
      bar
    '; + $display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT); + $this->assertSame($displayAssertion, $display); + + $this->helper->setAutoEscape(true); + $displayAssertion = '
    • Foo<br />bar
    '; + $display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT); + $this->assertSame($displayAssertion, $display); + } } From 94bcadc87b41f5fcf98b142e29de7b3d5d33ddf6 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 11 Apr 2014 11:10:45 -0500 Subject: [PATCH 7/8] [zendframework/zf2#5743] CS fix - use `null ===` instead of `is_null` (consistency) --- src/Helper/FlashMessenger.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Helper/FlashMessenger.php b/src/Helper/FlashMessenger.php index f3cc2f27..aa316eb3 100644 --- a/src/Helper/FlashMessenger.php +++ b/src/Helper/FlashMessenger.php @@ -145,7 +145,8 @@ protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_D } $classes = array($classes); } - if (is_null($autoEscape)) { + + if (null === $autoEscape) { $autoEscape = $this->getAutoEscape(); } From 59d5caaccbdbba78c3087eec6868a21586010679 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 11 Apr 2014 11:11:40 -0500 Subject: [PATCH 8/8] [zendframework/zf2#5743] object calisthenics - Return from conditional, to remove an else clause --- src/Helper/FlashMessenger.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Helper/FlashMessenger.php b/src/Helper/FlashMessenger.php index aa316eb3..75caf81e 100644 --- a/src/Helper/FlashMessenger.php +++ b/src/Helper/FlashMessenger.php @@ -165,9 +165,10 @@ protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_D if ($autoEscape) { $messagesToPrint[] = $escapeHtml($item); - } else { - $messagesToPrint[] = $item; + return; } + + $messagesToPrint[] = $item; }); if (empty($messagesToPrint)) { return '';