Skip to content
Snippets Groups Projects
Commit c5019e4f authored by Sergey Semenov's avatar Sergey Semenov
Browse files

MAGETWO-36353: False VAT number verification message if soap extension is not enabled

parent 9d313761
Branches
Tags
No related merge requests found
<?php <?php
/** /**
* * * Copyright © 2015 Magento. All rights reserved.
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details. * See COPYING.txt for license details.
*/ */
namespace Magento\Customer\Controller\Adminhtml\System\Config\Validatevat; namespace Magento\Customer\Controller\Adminhtml\System\Config\Validatevat;
use Magento\Framework\Controller\Result\JsonFactory;
class Validate extends \Magento\Customer\Controller\Adminhtml\System\Config\Validatevat class Validate extends \Magento\Customer\Controller\Adminhtml\System\Config\Validatevat
{ {
/** /**
* @var \Magento\Framework\Controller\Result\RawFactory * @var JsonFactory
*/ */
protected $resultRawFactory; protected $resultJsonFactory;
/** /**
* @param \Magento\Backend\App\Action\Context $context * @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory * @param JsonFactory $resultJsonFactory
*/ */
public function __construct( public function __construct(
\Magento\Backend\App\Action\Context $context, \Magento\Backend\App\Action\Context $context,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory JsonFactory $resultJsonFactory
) { ) {
parent::__construct($context); parent::__construct($context);
$this->resultRawFactory = $resultRawFactory; $this->resultJsonFactory = $resultJsonFactory;
} }
/** /**
* Check whether vat is valid * Check whether vat is valid
* *
* @return \Magento\Framework\Controller\Result\Raw * @return \Magento\Framework\Controller\Result\Json
*/ */
public function execute() public function execute()
{ {
$result = $this->_validate(); $result = $this->_validate();
/** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
$resultRaw = $this->resultRawFactory->create(); /** @var \Magento\Framework\Controller\Result\Json $resultJson */
return $resultRaw->setContents((int)$result->getIsValid()); $resultJson = $this->resultJsonFactory->create();
return $resultJson->setData([
'valid' => (int)$result->getIsValid(),
'message' => $result->getRequestMessage(),
]);
} }
} }
...@@ -166,7 +166,8 @@ class Vat ...@@ -166,7 +166,8 @@ class Vat
'is_valid' => false, 'is_valid' => false,
'request_date' => '', 'request_date' => '',
'request_identifier' => '', 'request_identifier' => '',
'request_success' => false 'request_success' => false,
'request_message' => __('Error during VAT Number verification.'),
]); ]);
if (!extension_loaded('soap')) { if (!extension_loaded('soap')) {
...@@ -194,6 +195,12 @@ class Vat ...@@ -194,6 +195,12 @@ class Vat
$gatewayResponse->setRequestDate((string)$result->requestDate); $gatewayResponse->setRequestDate((string)$result->requestDate);
$gatewayResponse->setRequestIdentifier((string)$result->requestIdentifier); $gatewayResponse->setRequestIdentifier((string)$result->requestIdentifier);
$gatewayResponse->setRequestSuccess(true); $gatewayResponse->setRequestSuccess(true);
if ($gatewayResponse->getIsValid()) {
$gatewayResponse->setRequestMessage(__('VAT Number is valid.'));
} else {
$gatewayResponse->setRequestMessage(__('Please enter a valid VAT number.'));
}
} catch (\Exception $exception) { } catch (\Exception $exception) {
$gatewayResponse->setIsValid(false); $gatewayResponse->setIsValid(false);
$gatewayResponse->setRequestDate(''); $gatewayResponse->setRequestDate('');
......
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Test\Unit\Controller\Adminhtml\System\Config\Validatevat;
class ValidateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Customer\Controller\Adminhtml\System\Config\Validatevat\Validate
*/
protected $controller;
/**
* @var \Magento\Backend\App\Action\Context
*/
protected $context;
/**
* @var \Magento\Framework\Controller\Result\Json | \PHPUnit_Framework_MockObject_MockObject
*/
protected $resultJson;
/**
* @var \Magento\Framework\ObjectManagerInterface | \PHPUnit_Framework_MockObject_MockObject
*/
protected $objectManager;
/**
* @var \Magento\Framework\App\Request\Http | \PHPUnit_Framework_MockObject_MockObject
*/
protected $request;
protected function setUp()
{
$resultJsonFactory = $this->mockResultJson();
$this->request = $this->getMockBuilder('Magento\Framework\App\Request\Http')
->disableOriginalConstructor()
->getMock();
$this->objectManager = $this->getMockBuilder('Magento\Framework\ObjectManagerInterface')
->getMockForAbstractClass();
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->context = $objectManager->getObject(
'Magento\Backend\App\Action\Context',
[
'request' => $this->request,
'objectManager' => $this->objectManager,
]
);
$this->controller = $objectManager->getObject(
'Magento\Customer\Controller\Adminhtml\System\Config\Validatevat\Validate',
[
'context' => $this->context,
'resultJsonFactory' => $resultJsonFactory,
]
);
}
public function testExecute()
{
$country = 'US';
$vat = '123456789';
$isValid = true;
$requestMessage = 'test';
$json = '{"valid":' . (int)$isValid . ',"message":"' . $requestMessage . '"}';
$gatewayResponse = new \Magento\Framework\DataObject([
'is_valid' => $isValid,
'request_message' => $requestMessage,
]);
$this->request->expects($this->any())
->method('getParam')
->willReturnMap([
['country', null, $country],
['vat', null, $vat],
]);
$vatMock = $this->getMockBuilder('Magento\Customer\Model\Vat')
->disableOriginalConstructor()
->getMock();
$vatMock->expects($this->once())
->method('checkVatNumber')
->with($country, $vat)
->willReturn($gatewayResponse);
$this->objectManager->expects($this->once())
->method('get')
->with('Magento\Customer\Model\Vat')
->willReturn($vatMock);
$this->resultJson->expects($this->once())
->method('setData')
->with([
'valid' => $gatewayResponse->getIsValid(),
'message' => $gatewayResponse->getRequestMessage()
])
->willReturn($json);
$this->assertEquals($json, $this->controller->execute());
}
/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
protected function mockResultJson()
{
$this->resultJson = $this->getMockBuilder('Magento\Framework\Controller\Result\Json')
->disableOriginalConstructor()
->getMock();
$resultJsonFactory = $this->getMockBuilder('Magento\Framework\Controller\Result\JsonFactory')
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();
$resultJsonFactory->expects($this->any())
->method('create')
->willReturn($this->resultJson);
return $resultJsonFactory;
}
}
...@@ -27,11 +27,13 @@ require(['prototype'], function(){ ...@@ -27,11 +27,13 @@ require(['prototype'], function(){
new Ajax.Request('<?php /* @escapeNotVerified */ echo $block->getAjaxUrl() ?>', { new Ajax.Request('<?php /* @escapeNotVerified */ echo $block->getAjaxUrl() ?>', {
parameters: params, parameters: params,
onSuccess: function(response) { onSuccess: function(response) {
result = '<?php /* @escapeNotVerified */ echo __('Please enter a valid VAT number.') ?>'; var result = '<?php /* @escapeNotVerified */ echo __('Error during VAT Number verification.') ?>';
try { try {
response = response.responseText; if (response.responseText.isJSON()) {
if (response == 1) { response = response.responseText.evalJSON();
result = '<?php /* @escapeNotVerified */ echo __('VAT Number is valid.') ?>'; result = response.message;
}
if (response.valid == 1) {
validationMessage.removeClassName('hidden').addClassName('success') validationMessage.removeClassName('hidden').addClassName('success')
} else { } else {
validationMessage.removeClassName('hidden').addClassName('error') validationMessage.removeClassName('hidden').addClassName('error')
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment