Skip to content

Commit 318377f

Browse files
author
Korshenko, Olexii(okorshenko)
committed
Merge pull request #644 from magento-api/API-Sprint-58-Bug-Fixes
[API] Sprint58 Bug Fixes 1
2 parents cbf24e3 + 5df6651 commit 318377f

File tree

14 files changed

+408
-69
lines changed

14 files changed

+408
-69
lines changed

app/code/Magento/Backend/Service/V1/ModuleServiceInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
namespace Magento\Backend\Service\V1;
88

9+
/**
10+
* Interface for module service.
11+
*/
912
interface ModuleServiceInterface
1013
{
1114
/**

app/code/Magento/Catalog/Model/CategoryRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public function __construct(
6161
public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
6262
{
6363
$existingData = $category->toFlatArray();
64+
/** 'available_sort_by' should be set separately because fields of array type are destroyed by toFlatArray() */
65+
$existingData['available_sort_by'] = $category->getAvailableSortBy();
6466
if ($category->getId()) {
6567
$existingCategory = $this->get($category->getId());
6668
if (isset($existingData['image']) && is_array($existingData['image'])) {

app/code/Magento/Store/App/Action/Plugin/Context.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,27 @@ public function aroundDispatch(
7474
\Closure $proceed,
7575
\Magento\Framework\App\RequestInterface $request
7676
) {
77+
/** @var \Magento\Store\Model\Store $defaultStore */
7778
$defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
78-
$this->httpContext->setValue(
79-
HttpContext::CONTEXT_CURRENCY,
80-
$this->session->getCurrencyCode(),
81-
$defaultStore->getDefaultCurrency()->getCode()
79+
80+
$requestedStoreCode = $this->httpRequest->getParam(
81+
StoreResolverInterface::PARAM_NAME,
82+
$this->storeCookieManager->getStoreCodeFromCookie()
8283
);
84+
/** @var \Magento\Store\Model\Store $currentStore */
85+
$currentStore = $requestedStoreCode ? $this->storeManager->getStore($requestedStoreCode) : $defaultStore;
8386

8487
$this->httpContext->setValue(
8588
StoreManagerInterface::CONTEXT_STORE,
86-
$this->httpRequest->getParam(
87-
StoreResolverInterface::PARAM_NAME,
88-
$this->storeCookieManager->getStoreCodeFromCookie()
89-
) ?: $defaultStore->getCode(),
89+
$currentStore->getCode(),
9090
$this->storeManager->getDefaultStoreView()->getCode()
9191
);
92+
93+
$this->httpContext->setValue(
94+
HttpContext::CONTEXT_CURRENCY,
95+
$this->session->getCurrencyCode() ?: $currentStore->getDefaultCurrencyCode(),
96+
$defaultStore->getDefaultCurrencyCode()
97+
);
9298
return $proceed($request);
9399
}
94100
}

app/code/Magento/Store/Controller/Store/SwitchAction.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
use Magento\Store\Model\Store;
1515
use Magento\Store\Model\StoreIsInactiveException;
1616
use Magento\Store\Model\StoreResolver;
17+
use Magento\Store\Model\StoreManagerInterface;
1718

19+
/**
20+
* Switch current store view.
21+
*/
1822
class SwitchAction extends Action
1923
{
2024
/**
@@ -33,21 +37,31 @@ class SwitchAction extends Action
3337
protected $storeRepository;
3438

3539
/**
40+
* @var StoreManagerInterface
41+
*/
42+
protected $storeManager;
43+
44+
/**
45+
* Initialize dependencies.
46+
*
3647
* @param ActionContext $context
3748
* @param StoreCookieManagerInterface $storeCookieManager
3849
* @param HttpContext $httpContext
3950
* @param StoreRepositoryInterface $storeRepository
51+
* @param StoreManagerInterface $storeManager
4052
*/
4153
public function __construct(
4254
ActionContext $context,
4355
StoreCookieManagerInterface $storeCookieManager,
4456
HttpContext $httpContext,
45-
StoreRepositoryInterface $storeRepository
57+
StoreRepositoryInterface $storeRepository,
58+
StoreManagerInterface $storeManager
4659
) {
4760
parent::__construct($context);
4861
$this->storeCookieManager = $storeCookieManager;
4962
$this->httpContext = $httpContext;
5063
$this->storeRepository = $storeRepository;
64+
$this->storeManager = $storeManager;
5165
}
5266

5367
/**
@@ -64,7 +78,7 @@ public function execute()
6478
$store = $this->storeRepository->getActiveStoreByCode($storeCode);
6579
} catch (StoreIsInactiveException $e) {
6680
$error = __('Requested store is inactive');
67-
} catch (\InvalidArgumentException $e) { // TODO: MAGETWO-39826 Need to replace on NoSuchEntityException
81+
} catch (\InvalidArgumentException $e) {
6882
$error = __('Requested store is not found');
6983
}
7084

@@ -74,11 +88,12 @@ public function execute()
7488
return;
7589
}
7690

77-
if ($store->getWebsite()->getDefaultStore()->getId() == $store->getId()) {
91+
$defaultStoreView = $this->storeManager->getDefaultStoreView();
92+
if ($defaultStoreView->getId() == $store->getId()) {
7893
$this->storeCookieManager->deleteStoreCookie($store);
7994
} else {
95+
$this->httpContext->setValue(Store::ENTITY, $store->getCode(), $defaultStoreView->getCode());
8096
$this->storeCookieManager->setStoreCookie($store);
81-
$this->httpContext->setValue(Store::ENTITY, $store->getCode(), Store::DEFAULT_CODE);
8297
}
8398

8499
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Store\Model\Plugin;
8+
9+
use Magento\Store\Api\StoreCookieManagerInterface;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Store\Api\StoreRepositoryInterface;
12+
use Magento\Store\Model\StoreIsInactiveException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use \InvalidArgumentException;
15+
16+
/**
17+
* Class StoreCookie
18+
*/
19+
class StoreCookie
20+
{
21+
/**
22+
* @var \Magento\Store\Model\StoreManagerInterface
23+
*/
24+
protected $storeManager;
25+
26+
/**
27+
* @var StoreCookieManagerInterface
28+
*/
29+
protected $storeCookieManager;
30+
31+
/**
32+
* @var StoreRepositoryInterface
33+
*/
34+
protected $storeRepository;
35+
36+
/**
37+
* @param StoreManagerInterface $storeManager
38+
* @param StoreCookieManagerInterface $storeCookieManager
39+
* @param StoreRepositoryInterface $storeRepository
40+
*/
41+
public function __construct(
42+
StoreManagerInterface $storeManager,
43+
StoreCookieManagerInterface $storeCookieManager,
44+
StoreRepositoryInterface $storeRepository
45+
) {
46+
$this->storeManager = $storeManager;
47+
$this->storeCookieManager = $storeCookieManager;
48+
$this->storeRepository = $storeRepository;
49+
}
50+
51+
/**
52+
* Delete cookie "store" if the store (a value in the cookie) does not exist or is inactive
53+
*
54+
* @param \Magento\Framework\App\FrontController $subject
55+
* @param callable $proceed
56+
* @param \Magento\Framework\App\RequestInterface $request
57+
* @return mixed
58+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
59+
*/
60+
public function aroundDispatch(
61+
\Magento\Framework\App\FrontController $subject,
62+
\Closure $proceed,
63+
\Magento\Framework\App\RequestInterface $request
64+
) {
65+
$defaultStore = $this->storeManager->getDefaultStoreView();
66+
$storeCodeFromCookie = $this->storeCookieManager->getStoreCodeFromCookie();
67+
if ($storeCodeFromCookie) {
68+
try {
69+
$this->storeRepository->getActiveStoreByCode($storeCodeFromCookie);
70+
} catch (StoreIsInactiveException $e) {
71+
$this->storeCookieManager->deleteStoreCookie($defaultStore);
72+
} catch (NoSuchEntityException $e) {
73+
$this->storeCookieManager->deleteStoreCookie($defaultStore);
74+
} catch (InvalidArgumentException $e) {
75+
$this->storeCookieManager->deleteStoreCookie($defaultStore);
76+
}
77+
}
78+
return $proceed($request);
79+
}
80+
}

app/code/Magento/Store/Model/Store.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,8 @@ class Store extends AbstractExtensibleModel implements
9090

9191
/**#@-*/
9292

93-
/**#@+
94-
* Code constants
95-
*/
96-
const DEFAULT_CODE = 'default';
97-
9893
const ADMIN_CODE = 'admin';
9994

100-
/**#@-*/
101-
10295
/**
10396
* Cache tag
10497
*/

app/code/Magento/Store/Model/StoreResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ public function getCurrentStoreId()
8181

8282
$storeCode = $this->request->getParam(self::PARAM_NAME, $this->storeCookieManager->getStoreCodeFromCookie());
8383
if ($storeCode) {
84-
$store = $this->getRequestedStoreByCode($storeCode);
84+
try {
85+
$store = $this->getRequestedStoreByCode($storeCode);
86+
} catch (NoSuchEntityException $e) {
87+
$store = $this->getDefaultStoreById($defaultStoreId);
88+
}
8589

8690
if (!in_array($store->getId(), $stores)) {
8791
throw new NoSuchEntityException(__('Requested scope cannot be loaded'));

app/code/Magento/Store/Test/Unit/App/Action/Plugin/ContextTest.php

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
*/
2020
class ContextTest extends \PHPUnit_Framework_TestCase
2121
{
22+
const CURRENCY_SESSION = 'CNY';
23+
const CURRENCY_DEFAULT = 'USD';
24+
const CURRENCY_CURRENT_STORE = 'UAH';
25+
2226
/**
2327
* @var \Magento\Store\App\Action\Plugin\Context
2428
*/
@@ -55,9 +59,9 @@ class ContextTest extends \PHPUnit_Framework_TestCase
5559
protected $storeMock;
5660

5761
/**
58-
* @var \Magento\Directory\Model\Currency|\PHPUnit_Framework_MockObject_MockObject
62+
* @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
5963
*/
60-
protected $currencyMock;
64+
protected $currentStoreMock;
6165

6266
/**
6367
* @var \Magento\Store\Model\Website|\PHPUnit_Framework_MockObject_MockObject
@@ -114,9 +118,9 @@ public function setUp()
114118
'',
115119
false
116120
);
117-
$this->currencyMock = $this->getMock(
118-
'Magento\Directory\Model\Currency',
119-
['getCode', '__wakeup'],
121+
$this->currentStoreMock = $this->getMock(
122+
'Magento\Store\Model\Store',
123+
[],
120124
[],
121125
'',
122126
false
@@ -150,50 +154,67 @@ public function setUp()
150154
'storeCookieManager' => $this->storeCookieManager,
151155
]
152156
);
153-
}
154-
155-
/**
156-
* Test aroundDispatch
157-
*/
158-
public function testAroundDispatch()
159-
{
160157
$this->storeManager->expects($this->once())
161158
->method('getWebsite')
162159
->will($this->returnValue($this->websiteMock));
163160
$this->storeManager->method('getDefaultStoreView')
164161
->willReturn($this->storeMock);
162+
$this->storeManager->method('getStore')
163+
->willReturn($this->currentStoreMock);
165164
$this->websiteMock->expects($this->once())
166165
->method('getDefaultStore')
167166
->will($this->returnValue($this->storeMock));
168167
$this->storeMock->expects($this->once())
169-
->method('getDefaultCurrency')
170-
->will($this->returnValue($this->currencyMock));
168+
->method('getDefaultCurrencyCode')
169+
->will($this->returnValue(self::CURRENCY_DEFAULT));
171170
$this->storeMock->expects($this->once())
172171
->method('getCode')
173172
->willReturn('default');
174-
$this->currencyMock->expects($this->once())
173+
$this->currentStoreMock->expects($this->once())
175174
->method('getCode')
176-
->will($this->returnValue('UAH'));
177-
$this->sessionMock->expects($this->once())
178-
->method('getCurrencyCode')
179-
->will($this->returnValue('UAH'));
175+
->willReturn('custom_store');
180176
$this->storeCookieManager->expects($this->once())
181177
->method('getStoreCodeFromCookie')
182178
->will($this->returnValue('storeCookie'));
183179
$this->httpRequestMock->expects($this->once())
184180
->method('getParam')
185181
->with($this->equalTo('___store'))
186182
->will($this->returnValue('default'));
187-
$this->httpContextMock->expects($this->atLeastOnce())
183+
$this->currentStoreMock->expects($this->any())
184+
->method('getDefaultCurrencyCode')
185+
->will($this->returnValue(self::CURRENCY_CURRENT_STORE));
186+
}
187+
188+
public function testAroundDispatchCurrencyFromSession()
189+
{
190+
$this->sessionMock->expects($this->any())
191+
->method('getCurrencyCode')
192+
->will($this->returnValue(self::CURRENCY_SESSION));
193+
194+
$this->httpContextMock->expects($this->at(0))
195+
->method('setValue')
196+
->with(StoreManagerInterface::CONTEXT_STORE, 'custom_store', 'default');
197+
/** Make sure that current currency is taken from session if available */
198+
$this->httpContextMock->expects($this->at(1))
188199
->method('setValue')
189-
->will(
190-
$this->returnValueMap(
191-
[
192-
[Context::CONTEXT_CURRENCY, 'UAH', 'UAH', $this->httpContextMock],
193-
[StoreManagerInterface::CONTEXT_STORE, 'default', 'default', $this->httpContextMock],
194-
]
195-
)
196-
);
200+
->with(Context::CONTEXT_CURRENCY, self::CURRENCY_SESSION, self::CURRENCY_DEFAULT);
201+
202+
$this->assertEquals(
203+
'ExpectedValue',
204+
$this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock)
205+
);
206+
}
207+
208+
public function testDispatchCurrentStoreCurrency()
209+
{
210+
$this->httpContextMock->expects($this->at(0))
211+
->method('setValue')
212+
->with(StoreManagerInterface::CONTEXT_STORE, 'custom_store', 'default');
213+
/** Make sure that current currency is taken from current store if no value is provided in session */
214+
$this->httpContextMock->expects($this->at(1))
215+
->method('setValue')
216+
->with(Context::CONTEXT_CURRENCY, self::CURRENCY_CURRENT_STORE, self::CURRENCY_DEFAULT);
217+
197218
$this->assertEquals(
198219
'ExpectedValue',
199220
$this->plugin->aroundDispatch($this->subjectMock, $this->closureMock, $this->requestMock)

0 commit comments

Comments
 (0)