diff --git a/app/code/Magento/Variable/Controller/Adminhtml/System/Variable/Validate.php b/app/code/Magento/Variable/Controller/Adminhtml/System/Variable/Validate.php index 684330532414b..86882a583a160 100644 --- a/app/code/Magento/Variable/Controller/Adminhtml/System/Variable/Validate.php +++ b/app/code/Magento/Variable/Controller/Adminhtml/System/Variable/Validate.php @@ -19,8 +19,8 @@ public function execute() $variable = $this->_initVariable(); $variable->addData($this->getRequest()->getPost('variable')); $result = $variable->validate(); - if ($result !== true && is_string($result)) { - $this->messageManager->addError($result); + if ($result instanceof \Magento\Framework\Phrase) { + $this->messageManager->addError($result->getText()); $layout = $this->layoutFactory->create(); $layout->initMessages(); $response->setError(true); diff --git a/app/code/Magento/Variable/Test/Unit/Controller/Adminhtml/System/Variable/ValidateTest.php b/app/code/Magento/Variable/Test/Unit/Controller/Adminhtml/System/Variable/ValidateTest.php new file mode 100644 index 0000000000000..4844820dcde52 --- /dev/null +++ b/app/code/Magento/Variable/Test/Unit/Controller/Adminhtml/System/Variable/ValidateTest.php @@ -0,0 +1,166 @@ +validateMock = $this->getMockBuilder('Magento\Variable\Controller\Adminhtml\System\Variable\Validate') + ->disableOriginalConstructor() + ->getMock(); + + $this->variableMock = $this->getMockBuilder('Magento\Variable\Model\Variable') + ->disableOriginalConstructor() + ->getMock(); + $this->variableMock->expects($this->any()) + ->method('addData') + ->willReturnSelf(); + + $messagesMock = $this->getMockBuilder('Magento\Framework\View\Element\Messages') + ->disableOriginalConstructor() + ->getMock(); + $this->layoutMock = $this->getMockBuilder('Magento\Framework\View\LayoutInterface') + ->setMethods(['initMessages', 'getMessagesBlock']) + ->getMockForAbstractClass(); + $this->layoutMock->expects($this->any()) + ->method('getMessagesBlock') + ->willReturn($messagesMock); + $layoutFactoryMock = $this->getMockBuilder('Magento\Framework\View\LayoutFactory') + ->disableOriginalConstructor() + ->getMock(); + $layoutFactoryMock->expects($this->any())->method('create')->willReturn($this->layoutMock); + + $this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface') + ->disableOriginalConstructor() + ->setMethods(['getPost']) + ->getMockForAbstractClass(); + $responseMock = $this->getMockBuilder('Magento\Framework\App\ResponseInterface') + ->setMethods(['setError', 'setHtmlMessage']) + ->getMockForAbstractClass(); + $this->messageManagerMock = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface') + ->getMockForAbstractClass(); + $contextMock = $this->getMockBuilder('Magento\Backend\App\Action\Context') + ->disableOriginalConstructor() + ->getMock(); + $contextMock->expects($this->any()) + ->method('getRequest')->will($this->returnValue($this->requestMock)); + $contextMock->expects($this->any()) + ->method('getResponse')->will($this->returnValue($responseMock)); + $contextMock->expects($this->any()) + ->method('getMessageManager')->will($this->returnValue($this->messageManagerMock)); + + $this->resultJsonMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Json') + ->disableOriginalConstructor() + ->getMock(); + $resultJsonFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\JsonFactory') + ->disableOriginalConstructor() + ->getMock(); + $resultJsonFactoryMock->expects($this->any())->method('create')->willReturn($this->resultJsonMock); + + $coreRegistryMock = $this->getMockBuilder('Magento\Framework\Registry') + ->disableOriginalConstructor() + ->getMock(); + + $this->validateMock = $this->getMockBuilder('Magento\Variable\Controller\Adminhtml\System\Variable\Validate') + ->setConstructorArgs( + [ + $contextMock, + $coreRegistryMock, + $this->getMock('Magento\Backend\Model\View\Result\ForwardFactory', [], [], '', false), + $resultJsonFactoryMock, + $this->getMock('Magento\Framework\View\Result\PageFactory', [], [], '', false), + $layoutFactoryMock, + ] + )->setMethods(['_initVariable']) + ->getMock(); + $this->validateMock->expects($this->any()) + ->method('_initVariable') + ->willReturn($this->variableMock); + + } + + /** + * @param mixed $result + * @param string[] $responseArray + * @dataProvider executeDataProvider + */ + public function testExecute($result, $responseArray) + { + $getParamMap = [ + ['variable_id', null, null], + ['store', 0, 0], + ]; + + $this->requestMock->expects($this->any()) + ->method('getParam')->willReturnMap($getParamMap); + $this->requestMock->expects($this->any()) + ->method('getPost')->with('variable')->will($this->returnValue([])); + + $this->variableMock->expects($this->any()) + ->method('validate') + ->willReturn($result); + + if ($result instanceof \Magento\Framework\Phrase) { + $this->messageManagerMock->expects($this->once()) + ->method('addError') + ->with($result->getText()); + $this->layoutMock->expects($this->once()) + ->method('initMessages'); + } + $this->resultJsonMock->expects($this->once()) + ->method('setData') + ->with($responseArray); + + $this->validateMock->execute(); + } + + /** + * @return array + */ + public function executeDataProvider() + { + return [ + [ false, ['error' => false]], + [ true, ['error' => false]], + [ __('Variable Code must be unique.'), ['error' => true, 'html_message' => null]], + [ __('Validation has failed.'), ['error' => true, 'html_message' => null]], + ]; + } +} diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index cadb92355d165..c712e82a26470 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -101,7 +101,7 @@ public function __construct(DirectoryList $directoryList, DriverPool $driverPool public function create(array $arguments) { $deploymentConfig = $this->createDeploymentConfig($this->directoryList, $arguments); - + $arguments = array_merge($deploymentConfig->get(), $arguments); $definitionFactory = new \Magento\Framework\ObjectManager\DefinitionFactory( $this->driverPool->getDriver(DriverPool::FILE), $this->directoryList->getPath(DirectoryList::DI),