Skip to content

Commit 21ddf0b

Browse files
author
mastiuhin-olexandr
committed
MC-24772: After choosing "Web Site" as default, where the "Store View" is disabled, the website is crashing
1 parent 0e77dfa commit 21ddf0b

File tree

3 files changed

+105
-23
lines changed
  • app/code/Magento/Backend
  • dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/System

3 files changed

+105
-23
lines changed

app/code/Magento/Backend/Controller/Adminhtml/System/Store/Save.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
namespace Magento\Backend\Controller\Adminhtml\System\Store;
88

99
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
10+
use Magento\Store\Model\Group as StoreGroup;
11+
use Magento\Store\Model\Store;
12+
use Magento\Framework\Exception\LocalizedException;
1013

1114
/**
1215
* Class Save
@@ -33,15 +36,14 @@ private function processWebsiteSave($postData)
3336
$websiteModel->setId(null);
3437
}
3538

36-
$groupModel = $this->_objectManager->create(\Magento\Store\Model\Group::class);
39+
$groupModel = $this->_objectManager->create(StoreGroup::class);
3740
$groupModel->load($websiteModel->getDefaultGroupId());
38-
39-
$storeModel = $this->_objectManager->create(\Magento\Store\Model\Store::class);
41+
$storeModel = $this->_objectManager->create(Store::class);
4042
$storeModel->load($groupModel->getDefaultStoreId());
4143

4244
if ($websiteModel->getIsDefault() && !$storeModel->isActive()) {
43-
throw new \Magento\Framework\Exception\LocalizedException(
44-
__("Please enable your Store View before using this Web Site as Default")
45+
throw new LocalizedException(
46+
__('Please enable your Store View before using this Web Site as Default')
4547
);
4648
}
4749

@@ -55,13 +57,13 @@ private function processWebsiteSave($postData)
5557
* Process Store model save
5658
*
5759
* @param array $postData
58-
* @throws \Magento\Framework\Exception\LocalizedException
60+
* @throws LocalizedException
5961
* @return array
6062
*/
6163
private function processStoreSave($postData)
6264
{
63-
/** @var \Magento\Store\Model\Store $storeModel */
64-
$storeModel = $this->_objectManager->create(\Magento\Store\Model\Store::class);
65+
/** @var Store $storeModel */
66+
$storeModel = $this->_objectManager->create(Store::class);
6567
$postData['store']['name'] = $this->filterManager->removeTags($postData['store']['name']);
6668
if ($postData['store']['store_id']) {
6769
$storeModel->load($postData['store']['store_id']);
@@ -71,13 +73,13 @@ private function processStoreSave($postData)
7173
$storeModel->setId(null);
7274
}
7375
$groupModel = $this->_objectManager->create(
74-
\Magento\Store\Model\Group::class
76+
StoreGroup::class
7577
)->load(
7678
$storeModel->getGroupId()
7779
);
7880
$storeModel->setWebsiteId($groupModel->getWebsiteId());
7981
if (!$storeModel->isActive() && $storeModel->isDefault()) {
80-
throw new \Magento\Framework\Exception\LocalizedException(
82+
throw new LocalizedException(
8183
__('The default store cannot be disabled')
8284
);
8385
}
@@ -91,14 +93,14 @@ private function processStoreSave($postData)
9193
* Process StoreGroup model save
9294
*
9395
* @param array $postData
94-
* @throws \Magento\Framework\Exception\LocalizedException
96+
* @throws LocalizedException
9597
* @return array
9698
*/
9799
private function processGroupSave($postData)
98100
{
99101
$postData['group']['name'] = $this->filterManager->removeTags($postData['group']['name']);
100-
/** @var \Magento\Store\Model\Group $groupModel */
101-
$groupModel = $this->_objectManager->create(\Magento\Store\Model\Group::class);
102+
/** @var StoreGroup $groupModel */
103+
$groupModel = $this->_objectManager->create(StoreGroup::class);
102104
if ($postData['group']['group_id']) {
103105
$groupModel->load($postData['group']['group_id']);
104106
}
@@ -107,7 +109,7 @@ private function processGroupSave($postData)
107109
$groupModel->setId(null);
108110
}
109111
if (!$this->isSelectedDefaultStoreActive($postData, $groupModel)) {
110-
throw new \Magento\Framework\Exception\LocalizedException(
112+
throw new LocalizedException(
111113
__('An inactive store view cannot be saved as default store view')
112114
);
113115
}
@@ -148,7 +150,7 @@ public function execute()
148150
}
149151
$redirectResult->setPath('adminhtml/*/');
150152
return $redirectResult;
151-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
153+
} catch (LocalizedException $e) {
152154
$this->messageManager->addErrorMessage($e->getMessage());
153155
$this->_getSession()->setPostData($postData);
154156
} catch (\Exception $e) {
@@ -169,10 +171,10 @@ public function execute()
169171
* Verify if selected default store is active
170172
*
171173
* @param array $postData
172-
* @param \Magento\Store\Model\Group $groupModel
174+
* @param StoreGroup $groupModel
173175
* @return bool
174176
*/
175-
private function isSelectedDefaultStoreActive(array $postData, \Magento\Store\Model\Group $groupModel)
177+
private function isSelectedDefaultStoreActive(array $postData, StoreGroup $groupModel)
176178
{
177179
if (!empty($postData['group']['default_store_id'])) {
178180
$defaultStoreId = $postData['group']['default_store_id'];

app/code/Magento/Backend/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ System,System
198198
"All Stores","All Stores"
199199
"You saved the website.","You saved the website."
200200
"The default store cannot be disabled","The default store cannot be disabled"
201+
"Please enable your Store View before using this Web Site as Default","Please enable your Store View before using this Web Site as Default"
201202
"You saved the store view.","You saved the store view."
202203
"An inactive store view cannot be saved as default store view","An inactive store view cannot be saved as default store view"
203204
"You saved the store.","You saved the store."

dev/tests/integration/testsuite/Magento/Backend/Controller/Adminhtml/System/StoreTest.php

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,56 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Backend\Controller\Adminhtml\System;
79

10+
use Magento\Store\Model\ResourceModel\Store as StoreResource;
11+
use Magento\Store\Api\Data\StoreInterfaceFactory;
12+
use Magento\Store\Api\WebsiteRepositoryInterface;
13+
use Magento\Framework\Data\Form\FormKey;
14+
use Magento\Framework\Message\MessageInterface;
15+
use Magento\Framework\Message\ManagerInterface;
16+
use Magento\Framework\App\Request\Http as HttpRequest;
17+
818
/**
919
* @magentoAppArea adminhtml
1020
*/
1121
class StoreTest extends \Magento\TestFramework\TestCase\AbstractBackendController
1222
{
23+
/**
24+
* @var FormKey
25+
*/
26+
private $formKey;
27+
28+
/**
29+
* @var StoreResource
30+
*/
31+
private $storeResource;
32+
33+
/**
34+
* @var StoreInterfaceFactory
35+
*/
36+
private $storeFactory;
37+
38+
/**
39+
* @var WebsiteRepositoryInterface
40+
*/
41+
private $websiteRepository;
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
protected function setUp(): void
47+
{
48+
parent::setUp();
49+
50+
$this->formKey = $this->_objectManager->get(FormKey::class);
51+
$this->storeResource = $this->_objectManager->get(StoreResource::class);
52+
$this->storeFactory = $this->_objectManager->get(StoreInterfaceFactory::class);
53+
$this->websiteRepository = $this->_objectManager->get(WebsiteRepositoryInterface::class);
54+
}
55+
1356
public function testIndexAction()
1457
{
1558
$this->dispatch('backend/admin/system_store/index');
@@ -67,17 +110,53 @@ public function testIndexAction()
67110
*/
68111
public function testSaveActionWithExistCode($post, $message)
69112
{
70-
/** @var $formKey \Magento\Framework\Data\Form\FormKey */
71-
$formKey = $this->_objectManager->get(\Magento\Framework\Data\Form\FormKey::class);
72-
$post['form_key'] = $formKey->getFormKey();
73-
$this->getRequest()->setMethod('POST');
113+
$post['form_key'] = $this->formKey->getFormKey();
114+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
74115
$this->getRequest()->setPostValue($post);
75116
$this->dispatch('backend/admin/system_store/save');
76117
//Check that errors was generated and set to session
77118
$this->assertSessionMessages(
78119
$this->containsEqual($message),
79-
\Magento\Framework\Message\MessageInterface::TYPE_ERROR,
80-
\Magento\Framework\Message\ManagerInterface::class
120+
MessageInterface::TYPE_ERROR,
121+
ManagerInterface::class
122+
);
123+
}
124+
125+
/**
126+
* Save action test.
127+
* Changing of a default website when a target website doesn't have a default store view.
128+
*
129+
* @return void
130+
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
131+
*/
132+
public function testSaveActionChangeDefaultWebsiteThatDoesntHaveDefaultStoreView(): void
133+
{
134+
$secondWebsite = $this->websiteRepository->get('test');
135+
// inactivate default store view of second store
136+
$secondStore = $this->storeFactory->create();
137+
$this->storeResource->load($secondStore, 'fixture_second_store', 'code');
138+
$secondStore->setIsActive(0);
139+
$this->storeResource->save($secondStore);
140+
141+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
142+
$this->getRequest()->setPostValue([
143+
'website' => [
144+
'name' => 'Test Website',
145+
'code' => 'test',
146+
'sort_order' => '0',
147+
'default_group_id' => $secondWebsite->getDefaultGroupId(),
148+
'is_default' => '1',
149+
'website_id' => $secondWebsite->getId(),
150+
],
151+
'store_type' => 'website',
152+
'store_action' => 'edit',
153+
],);
154+
$this->dispatch('backend/admin/system_store/save');
155+
//Check that errors was generated and set to session
156+
$this->assertSessionMessages(
157+
$this->containsEqual('Please enable your Store View before using this Web Site as Default'),
158+
MessageInterface::TYPE_ERROR,
159+
ManagerInterface::class
81160
);
82161
}
83162

0 commit comments

Comments
 (0)