Skip to content

Commit 6384473

Browse files
author
Yuri Kovsher
committed
Merge remote-tracking branch 'tango-ce/MAGETWO-33812' into MAGETWO-33846
2 parents 9d50bb7 + 95d2271 commit 6384473

File tree

5 files changed

+47
-37
lines changed

5 files changed

+47
-37
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/ResetPassword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function execute()
3535
$resultRedirect->setPath('customer/index');
3636
return $resultRedirect;
3737
} catch (\Magento\Framework\Validator\ValidatorException $exception) {
38-
$messages = $exception->getMessages();
38+
$messages = $exception->getMessages(\Magento\Framework\Message\MessageInterface::TYPE_ERROR);
3939
if (!count($messages)) {
4040
$messages = $exception->getMessage();
4141
}

dev/tests/integration/testsuite/Magento/Tax/Model/TaxClass/RepositoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public function testSave()
6666

6767
/**
6868
* @magentoDbIsolation enabled
69-
* @expectedException \Magento\Framework\Exception\AlreadyExistsException
70-
* @expectedExceptionMessage Class name and class type already exists.
69+
* @expectedException \Magento\Framework\Exception\InputException
70+
* @expectedExceptionMessage A class with the same name already exists for ClassType PRODUCT.
7171
*/
7272
public function testSaveThrowsExceptionIfGivenTaxClassNameIsNotUnique()
7373
{

dev/tests/unit/testsuite/Magento/Customer/Controller/Adminhtml/Index/ResetPasswordTest.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ public function testResetPasswordActionCoreException()
324324
);
325325

326326
// Setup a core exception to return
327-
$error = 'Something Bad happened';
328-
$exception = new \Magento\Framework\Validator\ValidatorException($error);
329-
$exception->addError('Something Bad happened');
327+
$exception = new \Magento\Framework\Validator\ValidatorException();
328+
$error = new \Magento\Framework\Message\Error('Something Bad happened');
329+
$exception->addMessage($error);
330330

331331
$this->_customerRepositoryMock->expects(
332332
$this->once()
@@ -341,7 +341,7 @@ public function testResetPasswordActionCoreException()
341341
// Verify error message is set
342342
$this->messageManager->expects($this->once())
343343
->method('addMessage')
344-
->with($this->equalTo(new \Magento\Framework\Message\Error($error)));
344+
->with($this->equalTo($error));
345345

346346
$this->_testedObject->execute();
347347
}
@@ -351,34 +351,26 @@ public function testResetPasswordActionCoreExceptionWarn()
351351
$warningText = 'Warning';
352352
$customerId = 1;
353353

354-
$this->_request->expects(
355-
$this->once()
356-
)->method(
357-
'getParam'
358-
)->with(
359-
$this->equalTo('customer_id'),
360-
$this->equalTo(0)
361-
)->will(
362-
$this->returnValue($customerId)
363-
);
354+
$this->_request->expects($this->once())
355+
->method('getParam')
356+
->with($this->equalTo('customer_id'), $this->equalTo(0))
357+
->will($this->returnValue($customerId));
364358

365359
// Setup a core exception to return
366360
$exception = new \Magento\Framework\Validator\ValidatorException($warningText);
367-
$error = 'Something Not So Bad happened';
368-
$exception->addError($error);
361+
362+
$error = new \Magento\Framework\Message\Warning('Something Not So Bad happened');
363+
$exception->addMessage($error);
369364

370365
$this->_customerRepositoryMock->expects($this->once())
371366
->method('getById')
372367
->with($customerId)
373368
->will($this->throwException($exception));
374369

375370
// Verify Warning is converted to an Error and message text is set to exception text
376-
$this->messageManager->expects(
377-
$this->once()
378-
)->method(
379-
'addMessage'
380-
)->with(
381-
$this->equalTo(new \Magento\Framework\Message\Error($warningText))
371+
$this->messageManager->expects($this->once())
372+
->method('addMessage')
373+
->with($this->equalTo(new \Magento\Framework\Message\Error($warningText))
382374
);
383375

384376
$this->_testedObject->execute();

lib/internal/Magento/Framework/Model/AbstractModel.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,10 @@ public function validateBeforeSave()
450450
$validator = $this->_getValidatorBeforeSave();
451451
if ($validator && !$validator->isValid($this)) {
452452
$errors = $validator->getMessages();
453-
$exception = new \Magento\Framework\Validator\ValidatorException(
454-
implode(PHP_EOL, $errors), [], null, $errors
455-
);
453+
$exception = new \Magento\Framework\Validator\ValidatorException(implode(PHP_EOL, $errors));
454+
foreach ($errors as $errorMessage) {
455+
$exception->addMessage(new \Magento\Framework\Message\Error($errorMessage));
456+
}
456457
throw $exception;
457458
}
458459
return $this;

lib/internal/Magento/Framework/Validator/ValidatorException.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ValidatorException extends \Magento\Framework\Exception\InputException
1212
/**
1313
* @var array
1414
*/
15-
protected $_messages;
15+
protected $messages = [];
1616

1717
/**
1818
* Constructor
@@ -29,29 +29,46 @@ public function __construct(
2929
array $messages = []
3030
) {
3131
if (!empty($messages)) {
32-
$this->_messages = $messages;
3332
$message = '';
34-
foreach ($this->_messages as $propertyMessages) {
33+
foreach ($messages as $propertyMessages) {
3534
foreach ($propertyMessages as $propertyMessage) {
3635
if ($message) {
3736
$message .= PHP_EOL;
3837
}
3938
$message .= $propertyMessage;
39+
$this->addMessage(new \Magento\Framework\Message\Error($propertyMessage));
4040
}
4141
}
42-
} else {
43-
$this->_messages = [$message];
4442
}
4543
parent::__construct($message, $params, $cause);
4644
}
4745

4846
/**
49-
* Get validation error messages
50-
*
47+
* @param \Magento\Framework\Message\AbstractMessage $message
48+
* @return $this
49+
*/
50+
public function addMessage(\Magento\Framework\Message\AbstractMessage $message)
51+
{
52+
if (!isset($this->messages[$message->getType()])) {
53+
$this->messages[$message->getType()] = [];
54+
}
55+
$this->messages[$message->getType()][] = $message;
56+
return $this;
57+
}
58+
59+
/**
60+
* @param string $type
5161
* @return array
5262
*/
53-
public function getMessages()
63+
public function getMessages($type = '')
5464
{
55-
return $this->_messages;
65+
if ('' == $type) {
66+
$arrRes = [];
67+
foreach ($this->messages as $messages) {
68+
$arrRes = array_merge($arrRes, $messages);
69+
}
70+
return $arrRes;
71+
}
72+
return isset($this->messages[$type]) ? $this->messages[$type] : [];
5673
}
5774
}

0 commit comments

Comments
 (0)