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

Update FlashMessenger.php #5759

Closed
wants to merge 10 commits into from
Closed
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
31 changes: 25 additions & 6 deletions library/Zend/View/Helper/FlashMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,32 @@ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, arr
{
$flashMessenger = $this->getPluginFlashMessenger();
$messages = $flashMessenger->getMessagesFromNamespace($namespace);
return $this->renderMessages($namespace, $messages, $classes);
}

/**
* Render Current Messages
*
* @param string $namespace
* @param array $classes
* @return string
*/
public function renderCurrent($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $classes = array())
{
$flashMessenger = $this->getPluginFlashMessenger();
$messages = $flashMessenger->getCurrentMessagesFromNamespace($namespace);
return $this->renderMessages($namespace, $messages, $classes);
}

/**
* Render Messages
*
* @param array $messages
* @param array $classes
* @return string
*/
protected function renderMessages($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, array $messages = array(), array $classes = array())
{
// Prepare classes for opening tag
if (empty($classes)) {
if (isset($this->classMessages[$namespace])) {
Expand All @@ -111,14 +136,11 @@ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, arr
}
$classes = array($classes);
}

// 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) {
if ($translator !== null) {
$item = $translator->translate(
Expand All @@ -128,16 +150,13 @@ public function render($namespace = PluginFlashMessenger::NAMESPACE_DEFAULT, arr
}
$messagesToPrint[] = $escapeHtml($item);
});

if (empty($messagesToPrint)) {
return '';
}

// Generate markup
$markup = sprintf($this->getMessageOpenFormat(), ' class="' . implode(' ', $classes) . '"');
$markup .= implode(sprintf($this->getMessageSeparatorString(), ' class="' . implode(' ', $classes) . '"'), $messagesToPrint);
$markup .= $this->getMessageCloseString();

return $markup;
}

Expand Down
193 changes: 193 additions & 0 deletions tests/ZendTest/View/Helper/FlashMessengerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ public function seedMessages()
unset($helper);
}

public function seedCurrentMessages()
{
$helper = new FlashMessenger();
$helper->setSessionManager($this->session);
$helper->addMessage('foo');
$helper->addMessage('bar');
$helper->addInfoMessage('bar-info');
$helper->addSuccessMessage('bar-success');
$helper->addErrorMessage('bar-error');
}

public function testCanAssertPluginClass()
{
$this->assertEquals(
Expand Down Expand Up @@ -84,6 +95,28 @@ public function testCanRetrieveMessages()
$this->assertTrue($this->plugin->hasErrorMessages());
}

public function testCanRetrieveCurrentMessages()
{
$helper = $this->helper;

$this->assertFalse($helper()->hasCurrentMessages());
$this->assertFalse($helper()->hasCurrentInfoMessages());
$this->assertFalse($helper()->hasCurrentSuccessMessages());
$this->assertFalse($helper()->hasCurrentErrorMessages());

$this->seedCurrentMessages();

$this->assertTrue(count($helper('default')) > 0);
$this->assertTrue(count($helper('info')) > 0);
$this->assertTrue(count($helper('success')) > 0);
$this->assertTrue(count($helper('error')) > 0);

$this->assertFalse($this->plugin->hasCurrentMessages());
$this->assertFalse($this->plugin->hasCurrentInfoMessages());
$this->assertFalse($this->plugin->hasCurrentSuccessMessages());
$this->assertFalse($this->plugin->hasCurrentErrorMessages());
}

public function testCanProxyAndRetrieveMessagesFromPluginController()
{
$this->assertFalse($this->helper->hasMessages());
Expand All @@ -99,6 +132,21 @@ public function testCanProxyAndRetrieveMessagesFromPluginController()
$this->assertTrue($this->helper->hasErrorMessages());
}

public function testCanProxyAndRetrieveCurrentMessagesFromPluginController()
{
$this->assertFalse($this->helper->hasCurrentMessages());
$this->assertFalse($this->helper->hasCurrentInfoMessages());
$this->assertFalse($this->helper->hasCurrentSuccessMessages());
$this->assertFalse($this->helper->hasCurrentErrorMessages());

$this->seedCurrentMessages();

$this->assertTrue($this->helper->hasCurrentMessages());
$this->assertTrue($this->helper->hasCurrentInfoMessages());
$this->assertTrue($this->helper->hasCurrentSuccessMessages());
$this->assertTrue($this->helper->hasCurrentErrorMessages());
}

public function testCanDisplayListOfMessages()
{
$displayInfoAssertion = '';
Expand All @@ -112,6 +160,19 @@ public function testCanDisplayListOfMessages()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessages()
{
$displayInfoAssertion = '';
$displayInfo = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayInfoAssertion, $displayInfo);

$this->seedCurrentMessages();

$displayInfoAssertion = '<ul class="info"><li>bar-info</li></ul>';
$displayInfo = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesByDefaultParameters()
{
$helper = $this->helper;
Expand All @@ -122,6 +183,16 @@ public function testCanDisplayListOfMessagesByDefaultParameters()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesByDefaultCurrentParameters()
{
$helper = $this->helper;
$this->seedCurrentMessages();

$displayInfoAssertion = '<ul class="default"><li>foo</li><li>bar</li></ul>';
$displayInfo = $helper()->renderCurrent();
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesByInvoke()
{
$helper = $this->helper;
Expand All @@ -132,6 +203,16 @@ public function testCanDisplayListOfMessagesByInvoke()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesByInvoke()
{
$helper = $this->helper;
$this->seedCurrentMessages();

$displayInfoAssertion = '<ul class="info"><li>bar-info</li></ul>';
$displayInfo = $helper()->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesCustomised()
{
$this->seedMessages();
Expand All @@ -145,6 +226,19 @@ public function testCanDisplayListOfMessagesCustomised()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesCustomised()
{
$this->seedCurrentMessages();

$displayInfoAssertion = '<div class="foo-baz foo-bar"><p>bar-info</p></div>';
$displayInfo = $this->helper
->setMessageOpenFormat('<div%s><p>')
->setMessageSeparatorString('</p><p>')
->setMessageCloseString('</p></div>')
->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO, array('foo-baz', 'foo-bar'));
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesCustomisedSeparator()
{
$this->seedMessages();
Expand All @@ -158,6 +252,19 @@ public function testCanDisplayListOfMessagesCustomisedSeparator()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesCustomisedSeparator()
{
$this->seedCurrentMessages();

$displayInfoAssertion = '<div><p class="foo-baz foo-bar">foo</p><p class="foo-baz foo-bar">bar</p></div>';
$displayInfo = $this->helper
->setMessageOpenFormat('<div><p%s>')
->setMessageSeparatorString('</p><p%s>')
->setMessageCloseString('</p></div>')
->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT, array('foo-baz', 'foo-bar'));
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesCustomisedByConfig()
{
$this->seedMessages();
Expand Down Expand Up @@ -193,6 +300,40 @@ public function testCanDisplayListOfMessagesCustomisedByConfig()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesCustomisedByConfig()
{
$this->seedCurrentMessages();
$config = array(
'view_helper_config' => array(
'flashmessenger' => array(
'message_open_format' => '<div%s><ul><li>',
'message_separator_string' => '</li><li>',
'message_close_string' => '</li></ul></div>',
),
),
);
$sm = new ServiceManager();
$sm->setService('Config', $config);
$helperPluginManager = new HelperPluginManager(new Config(array(
'factories' => array(
'flashmessenger' => 'Zend\View\Helper\Service\FlashMessengerFactory',
),
)));
$controllerPluginManager = new PluginManager(new Config(array(
'invokables' => array(
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
),
)));
$helperPluginManager->setServiceLocator($sm);
$controllerPluginManager->setServiceLocator($sm);
$sm->setService('ControllerPluginManager', $controllerPluginManager);
$helper = $helperPluginManager->get('flashmessenger');

$displayInfoAssertion = '<div class="info"><ul><li>bar-info</li></ul></div>';
$displayInfo = $helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfMessagesCustomisedByConfigSeparator()
{
$this->seedMessages();
Expand Down Expand Up @@ -228,6 +369,41 @@ public function testCanDisplayListOfMessagesCustomisedByConfigSeparator()
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanDisplayListOfCurrentMessagesCustomisedByConfigSeparator()
{
$this->seedCurrentMessages();

$config = array(
'view_helper_config' => array(
'flashmessenger' => array(
'message_open_format' => '<div><ul><li%s>',
'message_separator_string' => '</li><li%s>',
'message_close_string' => '</li></ul></div>',
),
),
);
$sm = new ServiceManager();
$sm->setService('Config', $config);
$helperPluginManager = new HelperPluginManager(new Config(array(
'factories' => array(
'flashmessenger' => 'Zend\View\Helper\Service\FlashMessengerFactory',
),
)));
$controllerPluginManager = new PluginManager(new Config(array(
'invokables' => array(
'flashmessenger' => 'Zend\Mvc\Controller\Plugin\FlashMessenger',
),
)));
$helperPluginManager->setServiceLocator($sm);
$controllerPluginManager->setServiceLocator($sm);
$sm->setService('ControllerPluginManager', $controllerPluginManager);
$helper = $helperPluginManager->get('flashmessenger');

$displayInfoAssertion = '<div><ul><li class="foo-baz foo-bar">foo</li><li class="foo-baz foo-bar">bar</li></ul></div>';
$displayInfo = $helper->renderCurrent(PluginFlashMessenger::NAMESPACE_DEFAULT, array('foo-baz', 'foo-bar'));
$this->assertEquals($displayInfoAssertion, $displayInfo);
}

public function testCanTranslateMessages()
{
$mockTranslator = $this->getMock('Zend\I18n\Translator\Translator');
Expand All @@ -244,4 +420,21 @@ public function testCanTranslateMessages()
$display = $this->helper->render(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayAssertion, $display);
}

public function testCanTranslateCurrentMessages()
{
$mockTranslator = $this->getMock('Zend\I18n\Translator\Translator');
$mockTranslator->expects($this->exactly(1))
->method('translate')
->will($this->returnValue('translated message'));

$this->helper->setTranslator($mockTranslator);
$this->assertTrue($this->helper->hasTranslator());

$this->seedCurrentMessages();

$displayAssertion = '<ul class="info"><li>translated message</li></ul>';
$display = $this->helper->renderCurrent(PluginFlashMessenger::NAMESPACE_INFO);
$this->assertEquals($displayAssertion, $display);
}
}