Skip to content

Commit 5cc7163

Browse files
authored
Merge pull request #6439 from magento-honey-badgers/MC-39744-graphql-store-group
Mc 39744 graphql store group
2 parents 277fd78 + 1197d73 commit 5cc7163

File tree

8 files changed

+181
-25
lines changed

8 files changed

+181
-25
lines changed

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,64 @@ public function getStoreByWebsiteId($websiteId)
5050
* @param int $websiteId
5151
* @param bool $available
5252
* @param int|null $storeGroupId
53+
* @param int|null $storeId
5354
* @return array
5455
*/
55-
public function getWebsiteStores(int $websiteId, bool $available = false, int $storeGroupId = null): array
56-
{
56+
public function getWebsiteStores(
57+
int $websiteId,
58+
bool $available = false,
59+
int $storeGroupId = null,
60+
int $storeId = null
61+
): array {
5762
$connection = $this->resource->getConnection();
5863
$storeTable = $this->resource->getTableName('store');
59-
$storeSelect = $connection->select()->from($storeTable)->where(
60-
'website_id = ?',
61-
$websiteId
62-
);
64+
$storeSelect = $connection->select()
65+
->from(['main_table' => $storeTable])
66+
->join(
67+
['group_table' => $this->resource->getTableName('store_group')],
68+
'main_table.group_id = group_table.group_id',
69+
[
70+
'store_group_code' => 'code',
71+
'store_group_name' => 'name',
72+
'default_store_id'
73+
]
74+
)
75+
->join(
76+
['website' => $this->resource->getTableName('store_website')],
77+
'main_table.website_id = website.website_id',
78+
[
79+
'website_code' => 'code',
80+
'website_name' => 'name',
81+
'website_sort_order' => 'sort_order',
82+
'default_group_id'
83+
]
84+
);
6385

6486
if ($storeGroupId) {
6587
$storeSelect = $storeSelect->where(
66-
'group_id = ?',
88+
'main_table.group_id = ?',
6789
$storeGroupId
6890
);
6991
}
7092

93+
if ($storeId) {
94+
$storeSelect = $storeSelect->where(
95+
'main_table.store_id = ?',
96+
$storeId
97+
);
98+
}
99+
71100
if ($available) {
72101
$storeSelect = $storeSelect->where(
73-
'is_active = 1'
102+
'main_table.is_active = 1'
74103
);
75104
}
76105

106+
$storeSelect = $storeSelect->where(
107+
'main_table.website_id = ?',
108+
$websiteId
109+
);
110+
77111
return $connection->fetchAll($storeSelect);
78112
}
79113
}

app/code/Magento/StoreGraphQl/Controller/HttpRequestValidator/StoreValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function validate(HttpRequestInterface $request): void
4545
$storeCode = trim($headerValue);
4646
if (!$this->isStoreActive($storeCode)) {
4747
$this->storeManager->setCurrentStore(null);
48-
throw new GraphQlInputException(__('Requested store is not found ({$storeCode})'));
48+
throw new GraphQlInputException(__('Requested store is not found (%1)', [$storeCode]));
4949
}
5050
}
5151
}

app/code/Magento/StoreGraphQl/Model/Resolver/Store/StoreConfigDataProvider.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,19 @@ public function __construct(
6565
*/
6666
public function getStoreConfigData(StoreInterface $store): array
6767
{
68-
$defaultStoreConfig = $this->storeConfigManager->getStoreConfigs([$store->getCode()]);
69-
return $this->prepareStoreConfigData(current($defaultStoreConfig), $store->getName());
68+
$defaultWebsiteStore = $this->storeWebsiteRelation->getWebsiteStores(
69+
(int) $store->getWebsiteId(),
70+
true,
71+
(int) $store->getStoreGroupId(),
72+
(int) $store->getStoreId()
73+
);
74+
if (empty($defaultWebsiteStore)) {
75+
return [];
76+
}
77+
78+
$storeConfigs = $this->storeConfigManager->getStoreConfigs([$store->getCode()]);
79+
80+
return $this->prepareStoreConfigData(current($storeConfigs), current($defaultWebsiteStore));
7081
}
7182

7283
/**
@@ -86,7 +97,7 @@ public function getAvailableStoreConfig(int $websiteId, int $storeGroupId = null
8697

8798
foreach ($storeConfigs as $storeConfig) {
8899
$key = array_search($storeConfig->getCode(), array_column($websiteStores, 'code'), true);
89-
$storesConfigData[] = $this->prepareStoreConfigData($storeConfig, $websiteStores[$key]['name']);
100+
$storesConfigData[] = $this->prepareStoreConfigData($storeConfig, $websiteStores[$key]);
90101
}
91102

92103
return $storesConfigData;
@@ -96,15 +107,25 @@ public function getAvailableStoreConfig(int $websiteId, int $storeGroupId = null
96107
* Prepare store config data
97108
*
98109
* @param StoreConfigInterface $storeConfig
99-
* @param string $storeName
110+
* @param array $storeData
100111
* @return array
101112
*/
102-
private function prepareStoreConfigData(StoreConfigInterface $storeConfig, string $storeName): array
113+
private function prepareStoreConfigData(StoreConfigInterface $storeConfig, array $storeData): array
103114
{
104115
return array_merge([
105116
'id' => $storeConfig->getId(),
106117
'code' => $storeConfig->getCode(),
118+
'store_code' => $storeConfig->getCode(),
119+
'store_name' => $storeData['name'] ?? null,
120+
'store_sort_order' => $storeData['sort_order'] ?? null,
121+
'is_default_store' => $storeData['default_store_id'] == $storeConfig->getId() ?? null,
122+
'store_group_code' => $storeData['store_group_code'] ?? null,
123+
'store_group_name' => $storeData['store_group_name'] ?? null,
124+
'is_default_store_group' => $storeData['default_group_id'] == $storeData['group_id'] ?? null,
125+
'store_group_default_store_code' => $storeData['store_group_default_store_code'] ?? null,
107126
'website_id' => $storeConfig->getWebsiteId(),
127+
'website_code' => $storeData['website_code'] ?? null,
128+
'website_name' => $storeData['website_name'] ?? null,
108129
'locale' => $storeConfig->getLocale(),
109130
'base_currency_code' => $storeConfig->getBaseCurrencyCode(),
110131
'default_display_currency_code' => $storeConfig->getDefaultDisplayCurrencyCode(),
@@ -118,7 +139,6 @@ private function prepareStoreConfigData(StoreConfigInterface $storeConfig, strin
118139
'secure_base_link_url' => $storeConfig->getSecureBaseLinkUrl(),
119140
'secure_base_static_url' => $storeConfig->getSecureBaseStaticUrl(),
120141
'secure_base_media_url' => $storeConfig->getSecureBaseMediaUrl(),
121-
'store_name' => $storeName,
122142
], $this->getExtendedConfigData((int)$storeConfig->getId()));
123143
}
124144

app/code/Magento/StoreGraphQl/Test/Unit/StoreValidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function testValidate(array $config): void
9595
->method('setCurrentStore')
9696
->with(null)
9797
->willReturnSelf();
98-
$this->expectExceptionMessage('Requested store is not found ({$storeCode})');
98+
$this->expectExceptionMessage('Requested store is not found (sv1)');
9999
$this->storeValidator->validate($this->requestMock);
100100
}
101101

app/code/Magento/StoreGraphQl/etc/schema.graphqls

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ type Website @doc(description: "Website is deprecated because it is should not b
1717
}
1818

1919
type StoreConfig @doc(description: "The type contains information about a store config") {
20-
id : Int @doc(description: "The ID number assigned to the store")
21-
code : String @doc(description: "A code assigned to the store to identify it")
22-
website_id : Int @doc(description: "The ID number assigned to the website store belongs")
20+
id : Int @deprecated(reason: "Use `store_code` instead.") @doc(description: "The ID number assigned to the store")
21+
code : String @deprecated(reason: "Use `store_code` instead.") @doc(description: "A code assigned to the store to identify it")
22+
store_code: ID @doc(description: "The unique ID of the store view. In the Admin, this is called the Store View Code. When making a GraphQL call, assign this value to the `Store` header to provide the scope")
23+
store_name : String @doc(description: "The label assigned to the store view")
24+
store_sort_order : Int @doc(description: "The store view sort order")
25+
is_default_store : Boolean @doc(description: "Indicates whether the store view has been designated as the default within the store group")
26+
store_group_code : ID @doc(description: "The unique ID assigned to the store group. In the Admin, this is called the Store Name")
27+
store_group_name : String @doc(description: "The label assigned to the store group")
28+
is_default_store_group : Boolean @doc(description: "Indicates whether the store group has been designated as the default within the website")
29+
website_id : Int @deprecated(reason: "The field should not be used on the storefront") @doc(description: "The ID number assigned to the website store")
30+
website_code : ID @doc(description: "The unique ID for the website")
31+
website_name : ID @doc(description: "The label assigned to the website")
2332
locale : String @doc(description: "Store locale")
2433
base_currency_code : String @doc(description: "Base currency code")
2534
default_display_currency_code : String @doc(description: "Default display currency code")
@@ -33,6 +42,5 @@ type StoreConfig @doc(description: "The type contains information about a store
3342
secure_base_link_url : String @doc(description: "Secure base link URL for the store")
3443
secure_base_static_url : String @doc(description: "Secure base static URL for the store")
3544
secure_base_media_url : String @doc(description: "Secure base media URL for the store")
36-
store_name : String @doc(description: "Name of the store")
3745
use_store_in_url: Boolean @doc(description: "The configuration determines if the store code should be used in the URL")
3846
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Store/AvailableStoreConfigTest.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,16 @@ public function testDefaultWebsiteAvailableStoreConfigs(): void
7272
availableStores {
7373
id,
7474
code,
75+
store_code,
76+
store_name,
77+
store_sort_order,
78+
is_default_store,
79+
store_group_code,
80+
store_group_name,
81+
is_default_store_group,
7582
website_id,
83+
website_code,
84+
website_name,
7685
locale,
7786
base_currency_code,
7887
default_display_currency_code,
@@ -112,7 +121,16 @@ public function testNonDefaultWebsiteAvailableStoreConfigs(): void
112121
availableStores {
113122
id,
114123
code,
124+
store_code,
125+
store_name,
126+
store_sort_order,
127+
is_default_store,
128+
store_group_code,
129+
store_group_name,
130+
is_default_store_group,
115131
website_id,
132+
website_code,
133+
website_name,
116134
locale,
117135
base_currency_code,
118136
default_display_currency_code,
@@ -148,10 +166,26 @@ public function testNonDefaultWebsiteAvailableStoreConfigs(): void
148166
*/
149167
private function validateStoreConfig(StoreConfigInterface $storeConfig, array $responseConfig): void
150168
{
169+
/** @var Store $store */
151170
$store = $this->objectManager->get(Store::class);
152171
$this->storeResource->load($store, $storeConfig->getCode(), 'code');
153172
$this->assertEquals($storeConfig->getId(), $responseConfig['id']);
154173
$this->assertEquals($storeConfig->getCode(), $responseConfig['code']);
174+
$this->assertEquals($store->getName(), $responseConfig['store_name']);
175+
$this->assertEquals($store->getSortOrder(), $responseConfig['store_sort_order']);
176+
$this->assertEquals(
177+
$store->getGroup()->getDefaultStoreId() == $store->getId(),
178+
$responseConfig['is_default_store']
179+
);
180+
$this->assertEquals($store->getGroup()->getCode(), $responseConfig['store_group_code']);
181+
$this->assertEquals($store->getGroup()->getName(), $responseConfig['store_group_name']);
182+
$this->assertEquals(
183+
$store->getWebsite()->getDefaultGroupId() === $store->getGroupId(),
184+
$responseConfig['is_default_store_group']
185+
);
186+
$this->assertEquals($store->getWebsite()->getCode(), $responseConfig['website_code']);
187+
$this->assertEquals($store->getWebsite()->getName(), $responseConfig['website_name']);
188+
$this->assertEquals($storeConfig->getCode(), $responseConfig['store_code']);
155189
$this->assertEquals($storeConfig->getLocale(), $responseConfig['locale']);
156190
$this->assertEquals($storeConfig->getBaseCurrencyCode(), $responseConfig['base_currency_code']);
157191
$this->assertEquals(
@@ -168,7 +202,6 @@ private function validateStoreConfig(StoreConfigInterface $storeConfig, array $r
168202
$this->assertEquals($storeConfig->getSecureBaseLinkUrl(), $responseConfig['secure_base_link_url']);
169203
$this->assertEquals($storeConfig->getSecureBaseStaticUrl(), $responseConfig['secure_base_static_url']);
170204
$this->assertEquals($storeConfig->getSecureBaseMediaUrl(), $responseConfig['secure_base_media_url']);
171-
$this->assertEquals($store->getName(), $responseConfig['store_name']);
172205
$this->assertEquals($store->isUseStoreInUrl(), $responseConfig['use_store_in_url']);
173206
}
174207

@@ -193,7 +226,16 @@ public function testAllStoreConfigsWithCodeInUrlEnabled(): void
193226
availableStores(useCurrentGroup:false) {
194227
id,
195228
code,
229+
store_code,
230+
store_name,
231+
store_sort_order,
232+
is_default_store,
233+
store_group_code,
234+
store_group_name,
235+
is_default_store_group,
196236
website_id,
237+
website_code,
238+
website_name,
197239
locale,
198240
base_currency_code,
199241
default_display_currency_code,
@@ -236,7 +278,16 @@ public function testCurrentGroupStoreConfigs(): void
236278
availableStores(useCurrentGroup:true) {
237279
id,
238280
code,
281+
store_code,
282+
store_name,
283+
store_sort_order,
284+
is_default_store,
285+
store_group_code,
286+
store_group_name,
287+
is_default_store_group,
239288
website_id,
289+
website_code,
290+
website_name,,
240291
locale,
241292
base_currency_code,
242293
default_display_currency_code,

dev/tests/api-functional/testsuite/Magento/GraphQl/Store/StoreConfigResolverTest.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Store\Api\StoreConfigManagerInterface;
1313
use Magento\Store\Api\StoreRepositoryInterface;
1414
use Magento\Store\Api\StoreResolverInterface;
15+
use Magento\Store\Model\Store;
1516
use Magento\TestFramework\Helper\Bootstrap;
1617
use Magento\TestFramework\ObjectManager;
1718
use Magento\TestFramework\TestCase\GraphQlAbstract;
@@ -55,7 +56,16 @@ public function testGetStoreConfig(): void
5556
storeConfig {
5657
id,
5758
code,
59+
store_code,
60+
store_name,
61+
store_sort_order,
62+
is_default_store,
63+
store_group_code,
64+
store_group_name,
65+
is_default_store_group,
5866
website_id,
67+
website_code,
68+
website_name,
5969
locale,
6070
base_currency_code,
6171
default_display_currency_code,
@@ -75,23 +85,40 @@ public function testGetStoreConfig(): void
7585
QUERY;
7686
$response = $this->graphQlQuery($query);
7787
$this->assertArrayHasKey('storeConfig', $response);
78-
$this->validateStoreConfig($defaultStoreConfig, $response['storeConfig'], $store->getName());
88+
$this->validateStoreConfig($defaultStoreConfig, $response['storeConfig'], $store);
7989
}
8090

8191
/**
8292
* Validate Store Config Data
8393
*
8494
* @param StoreConfigInterface $storeConfig
8595
* @param array $responseConfig
86-
* @param string $storeName
96+
* @param Store $store
8797
*/
8898
private function validateStoreConfig(
8999
StoreConfigInterface $storeConfig,
90100
array $responseConfig,
91-
string $storeName
101+
Store $store
92102
): void {
93103
$this->assertEquals($storeConfig->getId(), $responseConfig['id']);
94104
$this->assertEquals($storeConfig->getCode(), $responseConfig['code']);
105+
106+
$this->assertEquals($store->getName(), $responseConfig['store_name']);
107+
$this->assertEquals($store->getSortOrder(), $responseConfig['store_sort_order']);
108+
$this->assertEquals(
109+
$store->getGroup()->getDefaultStoreId() == $store->getId(),
110+
$responseConfig['is_default_store']
111+
);
112+
$this->assertEquals($store->getGroup()->getCode(), $responseConfig['store_group_code']);
113+
$this->assertEquals($store->getGroup()->getName(), $responseConfig['store_group_name']);
114+
$this->assertEquals(
115+
$store->getWebsite()->getDefaultGroupId() === $store->getGroupId(),
116+
$responseConfig['is_default_store_group']
117+
);
118+
$this->assertEquals($store->getWebsite()->getCode(), $responseConfig['website_code']);
119+
$this->assertEquals($store->getWebsite()->getName(), $responseConfig['website_name']);
120+
$this->assertEquals($storeConfig->getCode(), $responseConfig['store_code']);
121+
95122
$this->assertEquals($storeConfig->getLocale(), $responseConfig['locale']);
96123
$this->assertEquals($storeConfig->getBaseCurrencyCode(), $responseConfig['base_currency_code']);
97124
$this->assertEquals(
@@ -108,6 +135,6 @@ private function validateStoreConfig(
108135
$this->assertEquals($storeConfig->getSecureBaseLinkUrl(), $responseConfig['secure_base_link_url']);
109136
$this->assertEquals($storeConfig->getSecureBaseStaticUrl(), $responseConfig['secure_base_static_url']);
110137
$this->assertEquals($storeConfig->getSecureBaseMediaUrl(), $responseConfig['secure_base_media_url']);
111-
$this->assertEquals($storeName, $responseConfig['store_name']);
138+
$this->assertEquals($store->getName(), $responseConfig['store_name']);
112139
}
113140
}

dev/tests/integration/testsuite/Magento/Store/_files/second_website_with_four_stores_divided_in_groups.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@
3737
/* Refresh stores memory cache */
3838
$storeManager->reinitStores();
3939

40+
$store = $objectManager->create(Store::class);
41+
if (!$store->load('fixture_second_store', 'code')->getId()) {
42+
$store->setGroupId(
43+
$storeGroup->getId()
44+
);
45+
$store->save();
46+
}
47+
48+
$store = $objectManager->create(Store::class);
49+
if (!$store->load('fixture_third_store', 'code')->getId()) {
50+
$store->setGroupId(
51+
$storeGroup->getId()
52+
);
53+
$store->save();
54+
}
55+
4056
$store = $objectManager->create(Store::class);
4157
if (!$store->load('fixture_fourth_store', 'code')->getId()) {
4258
$store->setCode(

0 commit comments

Comments
 (0)