Skip to content

Commit 648ce10

Browse files
committed
Merge pull request #242 from magento-api/Sprint-59-PR
[API] Sprint 59
2 parents f0645fb + f2db503 commit 648ce10

File tree

15 files changed

+1389
-55
lines changed

15 files changed

+1389
-55
lines changed
Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22
/**
3-
*
43
* Copyright © 2015 Magento. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Integration\Controller\Adminhtml\Integration;
87

98
use Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info;
109
use Magento\Framework\Exception\IntegrationException;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Integration\Model\Integration as IntegrationModel;
1112

1213
class Save extends \Magento\Integration\Controller\Adminhtml\Integration
1314
{
@@ -30,7 +31,6 @@ protected function _redirectOnSaveError()
3031
* Save integration action.
3132
*
3233
* @return void
33-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
3434
*/
3535
public function execute()
3636
{
@@ -51,40 +51,11 @@ public function execute()
5151
$this->_redirect('*/*');
5252
return;
5353
}
54-
}
55-
/** @var array $data */
56-
$data = $this->getRequest()->getPostValue();
57-
if (!empty($data)) {
58-
if (!isset($data['resource'])) {
59-
$integrationData['resource'] = [];
60-
}
61-
$integrationData = array_merge($integrationData, $data);
62-
if (!isset($integrationData[Info::DATA_ID])) {
63-
$integration = $this->_integrationService->create($integrationData);
64-
} else {
65-
$integration = $this->_integrationService->update($integrationData);
66-
}
67-
if (!$this->getRequest()->isXmlHttpRequest()) {
68-
$this->messageManager->addSuccess(
69-
__(
70-
'The integration \'%1\' has been saved.',
71-
$this->escaper->escapeHtml($integration->getName())
72-
)
73-
);
74-
}
75-
if ($this->getRequest()->isXmlHttpRequest()) {
76-
$isTokenExchange = $integration->getEndpoint() && $integration->getIdentityLinkUrl() ? '1' : '0';
77-
$this->getResponse()->representJson(
78-
$this->jsonHelper->jsonEncode(
79-
['integrationId' => $integration->getId(), 'isTokenExchange' => $isTokenExchange]
80-
)
81-
);
82-
} else {
83-
$this->_redirect('*/*/');
54+
if ($integrationData[Info::DATA_SETUP_TYPE] == IntegrationModel::TYPE_CONFIG) {
55+
throw new LocalizedException(__('Cannot edit integrations created via config file.'));
8456
}
85-
} else {
86-
$this->messageManager->addError(__('The integration was not saved.'));
8757
}
58+
$this->processData($integrationData);
8859
} catch (IntegrationException $e) {
8960
$this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
9061
$this->_getSession()->setIntegrationData($integrationData);
@@ -98,4 +69,45 @@ public function execute()
9869
$this->_redirectOnSaveError();
9970
}
10071
}
72+
73+
/**
74+
* Save integration data.
75+
*
76+
* @param array $integrationData
77+
* @return void
78+
*/
79+
private function processData($integrationData)
80+
{
81+
/** @var array $data */
82+
$data = $this->getRequest()->getPostValue();
83+
if (!empty($data)) {
84+
if (!isset($data['resource'])) {
85+
$integrationData['resource'] = [];
86+
}
87+
$integrationData = array_merge($integrationData, $data);
88+
if (!isset($integrationData[Info::DATA_ID])) {
89+
$integration = $this->_integrationService->create($integrationData);
90+
} else {
91+
$integration = $this->_integrationService->update($integrationData);
92+
}
93+
if (!$this->getRequest()->isXmlHttpRequest()) {
94+
$this->messageManager->addSuccess(
95+
__(
96+
'The integration \'%1\' has been saved.',
97+
$this->escaper->escapeHtml($integration->getName())
98+
)
99+
);
100+
$this->_redirect('*/*/');
101+
} else {
102+
$isTokenExchange = $integration->getEndpoint() && $integration->getIdentityLinkUrl() ? '1' : '0';
103+
$this->getResponse()->representJson(
104+
$this->jsonHelper->jsonEncode(
105+
['integrationId' => $integration->getId(), 'isTokenExchange' => $isTokenExchange]
106+
)
107+
);
108+
}
109+
} else {
110+
$this->messageManager->addError(__('The integration was not saved.'));
111+
}
112+
}
101113
}

app/code/Magento/Integration/Test/Unit/Controller/Adminhtml/Integration/SaveTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,31 @@ public function testSaveActionExceptionDuringServiceCreation()
188188
$integrationController = $this->_createIntegrationController('Save');
189189
$integrationController->execute();
190190
}
191+
192+
public function testSaveActionExceptionOnIntegrationsCreatedFromConfigFile()
193+
{
194+
$exceptionMessage = 'Cannot edit integrations created via config file.';
195+
$intData = new \Magento\Framework\DataObject(
196+
[
197+
Info::DATA_NAME => 'nameTest',
198+
Info::DATA_ID => self::INTEGRATION_ID,
199+
'id' => self::INTEGRATION_ID,
200+
Info::DATA_EMAIL => 'test@magento.com',
201+
Info::DATA_ENDPOINT => 'http://magento.ll/endpoint',
202+
Info::DATA_SETUP_TYPE => IntegrationModel::TYPE_CONFIG,
203+
]
204+
);
205+
206+
$this->_requestMock->expects($this->any())->method('getParam')->will($this->returnValue(self::INTEGRATION_ID));
207+
$this->_integrationSvcMock
208+
->expects($this->once())
209+
->method('get')
210+
->with(self::INTEGRATION_ID)
211+
->will($this->returnValue($intData));
212+
213+
// Verify error
214+
$this->_messageManager->expects($this->once())->method('addError')->with($this->equalTo($exceptionMessage));
215+
$integrationContr = $this->_createIntegrationController('Save');
216+
$integrationContr->execute();
217+
}
191218
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Store\Test\Unit\Block\Store;
8+
9+
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
10+
11+
class SwitcherTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\Store\Block\Store\Switcher
15+
*/
16+
protected $model;
17+
18+
/**
19+
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $scopeConfigMock;
22+
23+
/**
24+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $storeManagerMock;
27+
28+
/**
29+
* @var \Magento\Store\Model\StoreFactory|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $storeFactoryMock;
32+
33+
/**
34+
* @var \Magento\Store\Model\GroupFactory|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $storeGroupFactoryMock;
37+
38+
protected function setUp()
39+
{
40+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
41+
$this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
42+
->disableOriginalConstructor()
43+
->setMethods([])
44+
->getMock();
45+
$this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface')
46+
->disableOriginalConstructor()
47+
->setMethods([])
48+
->getMock();
49+
$this->storeFactoryMock = $this->getMockBuilder('Magento\Store\Model\StoreFactory')
50+
->disableOriginalConstructor()
51+
->setMethods(['create'])
52+
->getMock();
53+
$this->storeGroupFactoryMock = $this->getMockBuilder('Magento\Store\Model\GroupFactory')
54+
->disableOriginalConstructor()
55+
->setMethods([])
56+
->getMock();
57+
$this->loadMocks();
58+
$this->model = $objectManager->getObject(
59+
'Magento\Store\Block\Store\Switcher',
60+
[
61+
'scopeConfig' => $this->scopeConfigMock,
62+
'storeManager' => $this->storeManagerMock,
63+
'storeFactory' => $this->storeFactoryMock,
64+
'storeGroupFactory' => $this->storeGroupFactoryMock,
65+
]
66+
);
67+
}
68+
69+
public function testGetStoreCount()
70+
{
71+
$this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn('en_US');
72+
$this->assertEquals(1, $this->model->getStoreCount());
73+
}
74+
75+
public function testGetStoreCountWithNotEqualLocale()
76+
{
77+
$this->scopeConfigMock->expects($this->any())->method('getValue')->willReturn('de_DE');
78+
$this->assertEquals(0, $this->model->getStoreCount());
79+
}
80+
81+
protected function loadMocks()
82+
{
83+
$storeMock = $this->getMockBuilder('Magento\Store\Model\Store')
84+
->disableOriginalConstructor()
85+
->setMethods(['getLocaleCode', 'isActive', 'getId', 'getGroupId', 'getCollection'])
86+
->getMock();
87+
$groupMock = $this->getMockBuilder('Magento\Store\Model\Group')
88+
->disableOriginalConstructor()
89+
->setMethods([])
90+
->getMock();
91+
/** @var AbstractCollection|\PHPUnit_Framework_MockObject_MockObject */
92+
$storeCollectionMock =
93+
$this->getMockBuilder('Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection')
94+
->disableOriginalConstructor()
95+
->setMethods(['addWebsiteFilter', 'load'])
96+
->getMockForAbstractClass();
97+
/** @var AbstractCollection|\PHPUnit_Framework_MockObject_MockObject */
98+
$groupCollectionMock =
99+
$this->getMockBuilder('Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection')
100+
->disableOriginalConstructor()
101+
->setMethods(['addWebsiteFilter', 'load'])
102+
->getMockForAbstractClass();
103+
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
104+
$this->storeFactoryMock->expects($this->any())->method('create')->willReturn($storeMock);
105+
$this->storeGroupFactoryMock->expects($this->any())->method('create')->willReturn($groupMock);
106+
$storeMock->expects($this->any())->method('getCollection')->willReturn($storeCollectionMock);
107+
$groupMock->expects($this->any())->method('getCollection')->willReturn($groupCollectionMock);
108+
$groupMock->expects($this->any())->method('getId')->willReturn(1);
109+
$storeMock->expects($this->any())->method('isActive')->willReturn(true);
110+
$storeMock->expects($this->atLeastOnce())->method('getLocaleCode')->willReturn('en_US');
111+
$storeMock->expects($this->any())->method('getGroupId')->willReturn(1);
112+
$storeMock->expects($this->any())->method('setLocaleCode');
113+
$storeMock->expects($this->any())->method('getId')->willReturn(1);
114+
$storeCollectionMock->expects($this->any())->method('addWebsiteFilter')->willReturn([$storeMock]);
115+
$groupCollectionMock->expects($this->any())->method('addWebsiteFilter')->willReturn([$groupMock]);
116+
}
117+
}

0 commit comments

Comments
 (0)