Skip to content

Commit

Permalink
MAGETWO-46014: delayed error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
slavvka committed May 17, 2016
1 parent af2a832 commit d97ebe5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
Expand Up @@ -10,6 +10,7 @@
namespace Magento\TestFramework\TestCase;

use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;
use Magento\Theme\Controller\Result\MessagePlugin;

/**
Expand Down Expand Up @@ -181,11 +182,11 @@ public function assertRedirect(\PHPUnit_Framework_Constraint $urlConstraint = nu
}
}

/**
/**ishlist/Controller/IndexTest
* Assert that actual session messages meet expectations:
* Usage examples:
* $this->assertSessionMessages($this->isEmpty(), \Magento\Framework\Message\MessageInterface::TYPE_ERROR);
* $this->assertSessionMessages($this->equalTo(array('Entity has been saved.')),
* $this->assertSessionMessages($this->equalTo(['Entity has been saved.'],
* \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS);
*
* @param \PHPUnit_Framework_Constraint $constraint Constraint to compare actual messages against
Expand All @@ -199,6 +200,44 @@ public function assertSessionMessages(
$messageManagerClass = 'Magento\Framework\Message\Manager'
) {
$this->_assertSessionErrors = false;

$messages = $this->getMessages($messageType, $messageManagerClass);

$this->assertThat(
$messages,
$constraint,
'Session messages do not meet expectations ' . var_export($messages, true)
);
}

/**
* Return all stored messages
*
* @param string|null $messageType
* @param string $messageManagerClass
* @return array
*/
protected function getMessages(
$messageType = null,
$messageManagerClass = 'Magento\Framework\Message\Manager'
) {
return array_merge(
$this->getSessionMessages($messageType, $messageManagerClass),
$this->getCookieMessages($messageType)
);
}

/**
* Return messages stored in session
*
* @param string|null $messageType
* @param string $messageManagerClass
* @return array
*/
protected function getSessionMessages(
$messageType = null,
$messageManagerClass = 'Magento\Framework\Message\Manager'
) {
/** @var $messageManager \Magento\Framework\Message\ManagerInterface */
$messageManager = $this->_objectManager->get($messageManagerClass);
/** @var $messages \Magento\Framework\Message\AbstractMessage[] */
Expand All @@ -208,18 +247,15 @@ public function assertSessionMessages(
$messages = $messageManager->getMessages()->getItemsByType($messageType);
}

/** @var $messageManager InterpretationStrategyInterface */
$interpretationStrategy = $this->_objectManager->get(InterpretationStrategyInterface::class);

$actualMessages = [];
foreach ($messages as $message) {
$actualMessages[] = $message->getText();
$actualMessages[] = $interpretationStrategy->interpret($message);
}

$actualMessages = array_merge($actualMessages, $this->getCookieMessages($messageType));

$this->assertThat(
$actualMessages,
$constraint,
'Session messages do not meet expectations ' . var_export($actualMessages, true)
);
return $actualMessages;
}

/**
Expand All @@ -228,7 +264,7 @@ public function assertSessionMessages(
* @param string|null $messageType
* @return array
*/
private function getCookieMessages($messageType = null)
protected function getCookieMessages($messageType = null)
{
/** @var $cookieManager CookieManagerInterface */
$cookieManager = $this->_objectManager->get(CookieManagerInterface::class);
Expand Down
Expand Up @@ -4,7 +4,9 @@
* See COPYING.txt for license details.
*/
namespace Magento\Test\TestCase;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand All @@ -16,6 +18,9 @@ class ControllerAbstractTest extends \Magento\TestFramework\TestCase\AbstractCon
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Message\Manager */
private $messageManager;

/** @var \PHPUnit_Framework_MockObject_MockObject | InterpretationStrategyInterface */
private $interpretationStrategyMock;

/** @var \PHPUnit_Framework_MockObject_MockObject | CookieManagerInterface */
private $cookieManagerMock;

Expand All @@ -25,6 +30,15 @@ protected function setUp()

$this->messageManager = $this->getMock('\Magento\Framework\Message\Manager', [], [], '', false);
$this->cookieManagerMock = $this->getMock(CookieManagerInterface::class, [], [], '', false);
$this->interpretationStrategyMock = $this->getMock(InterpretationStrategyInterface::class, [], [], '', false);
$this->interpretationStrategyMock->expects($this->any())
->method('interpret')
->willReturnCallback(
function (MessageInterface $message) {
return $message->getText();
}
);

$request = $testObjectManager->getObject('Magento\TestFramework\Request');
$response = $testObjectManager->getObject('Magento\TestFramework\Response');
$this->_objectManager = $this->getMock(
Expand All @@ -43,6 +57,7 @@ protected function setUp()
['Magento\Framework\App\ResponseInterface', $response],
['Magento\Framework\Message\Manager', $this->messageManager],
[CookieManagerInterface::class, $this->cookieManagerMock],
[InterpretationStrategyInterface::class, $this->interpretationStrategyMock],
]
)
);
Expand Down Expand Up @@ -147,19 +162,19 @@ public function assertSessionMessagesDataProvider()
return [
'message warning type filtering' => [
['some_warning', 'warning_cookie'],
\Magento\Framework\Message\MessageInterface::TYPE_WARNING,
MessageInterface::TYPE_WARNING,
],
'message error type filtering' => [
['error_one', 'error_two', 'error_cookie'],
\Magento\Framework\Message\MessageInterface::TYPE_ERROR,
MessageInterface::TYPE_ERROR,
],
'message notice type filtering' => [
['some_notice', 'notice_cookie'],
\Magento\Framework\Message\MessageInterface::TYPE_NOTICE,
MessageInterface::TYPE_NOTICE,
],
'message success type filtering' => [
['success!', 'success_cookie'],
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS,
MessageInterface::TYPE_SUCCESS,
],
];
}
Expand Down
Expand Up @@ -5,8 +5,6 @@
*/
namespace Magento\Wishlist\Controller;

use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;

class IndexTest extends \Magento\TestFramework\TestCase\AbstractController
{
/**
Expand Down Expand Up @@ -99,7 +97,10 @@ public function testAddActionProductNameXss()

$this->assertSessionMessages(
$this->equalTo(
['You removed product <script>alert("xss");</script> from the comparison list.']
[
"\n<script>alert("xss");</script> has been added to your Wish List. "
. 'Click <a href="http://localhost/index.php/">here</a> to continue shopping.',
]
),
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
);
Expand Down

0 comments on commit d97ebe5

Please sign in to comment.