Skip to content

Commit 02eeee2

Browse files
author
Vitaliy Boyko
committed
graphQl-167: removed all TODO's, implemented deleting configs from the database
1 parent e751d14 commit 02eeee2

File tree

43 files changed

+405
-506
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+405
-506
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2019 TechDivision GmbH
4+
* All rights reserved
5+
*
6+
* This product includes proprietary software developed at TechDivision GmbH, Germany
7+
* For more information see https://www.techdivision.com/
8+
*
9+
* To obtain a valid license for using this software please contact us at
10+
* license@techdivision.com
11+
*/
12+
declare(strict_types=1);
13+
14+
namespace Magento\TestFramework\Annotation;
15+
16+
use Magento\Config\Model\Config;
17+
use Magento\Config\Model\ResourceModel\Config as ConfigResource;
18+
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
19+
use Magento\TestFramework\Helper\Bootstrap;
20+
use Magento\Store\Model\StoreManagerInterface;
21+
use PHPUnit\Framework\TestCase;
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
class ApiConfigFixture extends ConfigFixture
27+
{
28+
/**
29+
* Original values for global configuration options that need to be restored
30+
*
31+
* @var array
32+
*/
33+
private $_globalConfigValues = [];
34+
35+
/**
36+
* Original values for store-scoped configuration options that need to be restored
37+
*
38+
* @var array
39+
*/
40+
private $_storeConfigValues = [];
41+
42+
/**
43+
* Values need to be deleted form the database
44+
*
45+
* @var array
46+
*/
47+
private $_valuesToDeleteFromDatabase = [];
48+
49+
/**
50+
* Assign required config values and save original ones
51+
*
52+
* @param TestCase $test
53+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
54+
*/
55+
protected function _assignConfigData(TestCase $test)
56+
{
57+
$annotations = $test->getAnnotations();
58+
if (!isset($annotations['method'][$this->annotation])) {
59+
return;
60+
}
61+
foreach ($annotations['method'][$this->annotation] as $configPathAndValue) {
62+
if (preg_match('/^.+?(?=_store\s)/', $configPathAndValue, $matches)) {
63+
/* Store-scoped config value */
64+
$storeCode = $matches[0] != 'current' ? $matches[0] : null;
65+
$parts = preg_split('/\s+/', $configPathAndValue, 3);
66+
list($configScope, $configPath, $requiredValue) = $parts + ['', '', ''];
67+
$originalValue = $this->_getConfigValue($configPath, $storeCode);
68+
$this->_storeConfigValues[$storeCode][$configPath] = $originalValue;
69+
if ($this->checkIfValueExist($configPath, $storeCode)) {
70+
$this->_valuesToDeleteFromDatabase[$storeCode][$configPath] = $requiredValue;
71+
}
72+
$this->_setConfigValue($configPath, $requiredValue, $storeCode);
73+
} else {
74+
/* Global config value */
75+
list($configPath, $requiredValue) = preg_split('/\s+/', $configPathAndValue, 2);
76+
77+
$originalValue = $this->_getConfigValue($configPath);
78+
$this->_globalConfigValues[$configPath] = $originalValue;
79+
if ($this->checkIfValueExist($configPath)) {
80+
$this->_valuesToDeleteFromDatabase['global'][$configPath] = $requiredValue;
81+
}
82+
83+
$this->_setConfigValue($configPath, $requiredValue);
84+
}
85+
}
86+
}
87+
88+
/**
89+
* Restore original values for changed config options
90+
*/
91+
protected function _restoreConfigData()
92+
{
93+
$configResource = Bootstrap::getObjectManager()->get(ConfigResource::class);
94+
95+
/* Restore global values */
96+
foreach ($this->_globalConfigValues as $configPath => $originalValue) {
97+
if (isset($this->_valuesToDeleteFromDatabase['global'][$configPath])) {
98+
$configResource->deleteConfig($configPath);
99+
} else {
100+
$this->_setConfigValue($configPath, $originalValue);
101+
}
102+
}
103+
$this->_globalConfigValues = [];
104+
105+
/* Restore store-scoped values */
106+
foreach ($this->_storeConfigValues as $storeCode => $originalData) {
107+
foreach ($originalData as $configPath => $originalValue) {
108+
if (empty($storeCode)) {
109+
$storeCode = null;
110+
}
111+
if (isset($this->_valuesToDeleteFromDatabase[$storeCode][$configPath])) {
112+
$scopeId = $this->getStoreIdByCode($storeCode);
113+
$configResource->deleteConfig($configPath, 'stores', $scopeId);
114+
} else {
115+
$this->_setConfigValue($configPath, $originalValue, $storeCode);
116+
}
117+
}
118+
}
119+
$this->_storeConfigValues = [];
120+
}
121+
122+
/**
123+
* Load configs by path and scope
124+
*
125+
* @param string $configPath
126+
* @param string $storeCode
127+
* @return Config[]
128+
*/
129+
private function loadConfigs(string $configPath, string $storeCode = null): array
130+
{
131+
$configCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
132+
$collection = $configCollectionFactory->create();
133+
$scope = $storeCode ? 'stores' : 'default';
134+
$scopeId = $storeCode ? $this->getStoreIdByCode($storeCode) : 0;
135+
136+
$collection->addScopeFilter($scope, $scopeId, $configPath);
137+
return $collection->getItems();
138+
}
139+
140+
/**
141+
* Check if config exist in the database
142+
*
143+
* @param string $configPath
144+
* @param $originalValue
145+
* @param string|null $storeCode
146+
*/
147+
private function checkIfValueExist(string $configPath, string $storeCode = null): bool
148+
{
149+
$configs = $this->loadConfigs($configPath, $storeCode);
150+
151+
return !(bool)$configs;
152+
}
153+
154+
/**
155+
* Returns the store ID by the store code
156+
* @param string $storeCode
157+
* @return int
158+
*/
159+
private function getStoreIdByCode(string $storeCode): int
160+
{
161+
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
162+
$store = $storeManager->getStore($storeCode);
163+
return (int)$store->getId();
164+
}
165+
}

dev/tests/api-functional/framework/Magento/TestFramework/Bootstrap/WebapiDocBlock.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
namespace Magento\TestFramework\Bootstrap;
99

10+
use Magento\TestFramework\Annotation\ApiConfigFixture;
1011
use Magento\TestFramework\Annotation\ConfigFixture;
1112

1213
/**
@@ -17,7 +18,7 @@ class WebapiDocBlock extends \Magento\TestFramework\Bootstrap\DocBlock
1718
/**
1819
* Get list of subscribers.
1920
*
20-
* In addition, register magentoApiDataFixture and magentoApiConfigFixture
21+
* In addition, register magentoApiDataFixture and magentoConfigFixture
2122
* annotation processors
2223
*
2324
* @param \Magento\TestFramework\Application $application
@@ -26,8 +27,14 @@ class WebapiDocBlock extends \Magento\TestFramework\Bootstrap\DocBlock
2627
protected function _getSubscribers(\Magento\TestFramework\Application $application)
2728
{
2829
$subscribers = parent::_getSubscribers($application);
30+
foreach ($subscribers as $key => $subscriber) {
31+
if (get_class($subscriber) == ConfigFixture::class) {
32+
unset($subscribers[$key]);
33+
}
34+
}
2935
$subscribers[] = new \Magento\TestFramework\Annotation\ApiDataFixture($this->_fixturesBaseDir);
30-
$subscribers[] = new ConfigFixture();
36+
$subscribers[] = new ApiConfigFixture();
37+
3138
return $subscribers;
3239
}
3340
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/StoreConfigTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
*/
1515
class StoreConfigTest extends GraphQlAbstract
1616
{
17-
protected function setUp()
18-
{
19-
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/167');
20-
}
21-
2217
/**
2318
* @magentoApiDataFixture Magento/Store/_files/store.php
19+
* @magentoConfigFixture default_store catalog/seo/product_url_suffix test_product_suffix
20+
* @magentoConfigFixture default_store catalog/seo/category_url_suffix test_category_suffix
21+
* @magentoConfigFixture default_store catalog/seo/title_separator ___
22+
* @magentoConfigFixture default_store catalog/frontend/list_mode 2
23+
* @magentoConfigFixture default_store catalog/frontend/grid_per_page_values 16
24+
* @magentoConfigFixture default_store catalog/frontend/list_per_page_values 8
25+
* @magentoConfigFixture default_store catalog/frontend/grid_per_page 16
26+
* @magentoConfigFixture default_store catalog/frontend/list_per_page 8
27+
* @magentoConfigFixture default_store catalog/frontend/default_sort_by asc
2428
*/
2529
public function testGetStoreConfig()
2630
{
@@ -43,6 +47,15 @@ public function testGetStoreConfig()
4347
$response = $this->graphQlQuery($query);
4448
$this->assertArrayHasKey('storeConfig', $response);
4549

46-
//TODO: provide assertions after unmarking test as incomplete
50+
$this->assertEquals('test_product_suffix', $response['storeConfig']['product_url_suffix']);
51+
$this->assertEquals('test_category_suffix', $response['storeConfig']['category_url_suffix']);
52+
$this->assertEquals('___', $response['storeConfig']['title_separator']);
53+
$this->assertEquals('2', $response['storeConfig']['list_mode']);
54+
$this->assertEquals('16', $response['storeConfig']['grid_per_page_values']);
55+
$this->assertEquals(16, $response['storeConfig']['grid_per_page']);
56+
$this->assertEquals('8', $response['storeConfig']['list_per_page_values']);
57+
$this->assertEquals(8, $response['storeConfig']['list_per_page']);
58+
$this->assertEquals('asc', $response['storeConfig']['catalog_default_sort_by']);
4759
}
4860
}
61+

dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/AddProductToCartTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,12 @@ public function testAddProductIfQuantityIsNotAvailable()
4949
/**
5050
* @magentoApiDataFixture Magento/Catalog/_files/products.php
5151
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
52-
* @magentoConfigFixture default cataloginventory/item_options/max_sale_qty 5
52+
* @magentoConfigFixture default_store cataloginventory/item_options/max_sale_qty 5
5353
* @expectedException \Exception
5454
* @expectedExceptionMessage The most you may purchase is 5.
5555
*/
5656
public function testAddMoreProductsThatAllowed()
5757
{
58-
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/167');
59-
6058
$sku = 'custom-design-simple-product';
6159
$quantity = 7;
6260
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');

0 commit comments

Comments
 (0)