Skip to content

Commit e284c7f

Browse files
authored
ENGCOM-4151: 167 magento config fixture #351
2 parents 5660eba + 10f2a6d commit e284c7f

File tree

52 files changed

+546
-578
lines changed

Some content is hidden

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

52 files changed

+546
-578
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Annotation;
9+
10+
use Magento\Config\Model\Config;
11+
use Magento\Config\Model\ResourceModel\Config as ConfigResource;
12+
use Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\Store\Model\StoreManagerInterface;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* @inheritDoc
19+
*/
20+
class ApiConfigFixture extends ConfigFixture
21+
{
22+
/**
23+
* Original values for global configuration options that need to be restored
24+
*
25+
* @var array
26+
*/
27+
private $_globalConfigValues = [];
28+
29+
/**
30+
* Original values for store-scoped configuration options that need to be restored
31+
*
32+
* @var array
33+
*/
34+
private $_storeConfigValues = [];
35+
36+
/**
37+
* Values need to be deleted form the database
38+
*
39+
* @var array
40+
*/
41+
private $_valuesToDeleteFromDatabase = [];
42+
43+
/**
44+
* Assign required config values and save original ones
45+
*
46+
* @param TestCase $test
47+
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
48+
*/
49+
protected function _assignConfigData(TestCase $test)
50+
{
51+
$annotations = $test->getAnnotations();
52+
if (!isset($annotations['method'][$this->annotation])) {
53+
return;
54+
}
55+
foreach ($annotations['method'][$this->annotation] as $configPathAndValue) {
56+
if (preg_match('/^.+?(?=_store\s)/', $configPathAndValue, $matches)) {
57+
/* Store-scoped config value */
58+
$storeCode = $matches[0] != 'current' ? $matches[0] : null;
59+
$parts = preg_split('/\s+/', $configPathAndValue, 3);
60+
list($configScope, $configPath, $requiredValue) = $parts + ['', '', ''];
61+
$originalValue = $this->_getConfigValue($configPath, $storeCode);
62+
$this->_storeConfigValues[$storeCode][$configPath] = $originalValue;
63+
if ($this->checkIfValueExist($configPath, $storeCode)) {
64+
$this->_valuesToDeleteFromDatabase[$storeCode][$configPath] = $requiredValue;
65+
}
66+
$this->_setConfigValue($configPath, $requiredValue, $storeCode);
67+
} else {
68+
/* Global config value */
69+
list($configPath, $requiredValue) = preg_split('/\s+/', $configPathAndValue, 2);
70+
71+
$originalValue = $this->_getConfigValue($configPath);
72+
$this->_globalConfigValues[$configPath] = $originalValue;
73+
if ($this->checkIfValueExist($configPath)) {
74+
$this->_valuesToDeleteFromDatabase['global'][$configPath] = $requiredValue;
75+
}
76+
77+
$this->_setConfigValue($configPath, $requiredValue);
78+
}
79+
}
80+
}
81+
82+
/**
83+
* Restore original values for changed config options
84+
*/
85+
protected function _restoreConfigData()
86+
{
87+
$configResource = Bootstrap::getObjectManager()->get(ConfigResource::class);
88+
89+
/* Restore global values */
90+
foreach ($this->_globalConfigValues as $configPath => $originalValue) {
91+
if (isset($this->_valuesToDeleteFromDatabase['global'][$configPath])) {
92+
$configResource->deleteConfig($configPath);
93+
} else {
94+
$this->_setConfigValue($configPath, $originalValue);
95+
}
96+
}
97+
$this->_globalConfigValues = [];
98+
99+
/* Restore store-scoped values */
100+
foreach ($this->_storeConfigValues as $storeCode => $originalData) {
101+
foreach ($originalData as $configPath => $originalValue) {
102+
if (empty($storeCode)) {
103+
$storeCode = null;
104+
}
105+
if (isset($this->_valuesToDeleteFromDatabase[$storeCode][$configPath])) {
106+
$scopeId = $this->getStoreIdByCode($storeCode);
107+
$configResource->deleteConfig($configPath, 'stores', $scopeId);
108+
} else {
109+
$this->_setConfigValue($configPath, $originalValue, $storeCode);
110+
}
111+
}
112+
}
113+
$this->_storeConfigValues = [];
114+
}
115+
116+
/**
117+
* Load configs by path and scope
118+
*
119+
* @param string $configPath
120+
* @param string $storeCode
121+
* @return Config[]
122+
*/
123+
private function loadConfigs(string $configPath, string $storeCode = null): array
124+
{
125+
$configCollectionFactory = Bootstrap::getObjectManager()->get(CollectionFactory::class);
126+
$collection = $configCollectionFactory->create();
127+
$scope = $storeCode ? 'stores' : 'default';
128+
$scopeId = $storeCode ? $this->getStoreIdByCode($storeCode) : 0;
129+
130+
$collection->addScopeFilter($scope, $scopeId, $configPath);
131+
return $collection->getItems();
132+
}
133+
134+
/**
135+
* Check if config exist in the database
136+
*
137+
* @param string $configPath
138+
* @param string|null $storeCode
139+
*/
140+
private function checkIfValueExist(string $configPath, string $storeCode = null): bool
141+
{
142+
$configs = $this->loadConfigs($configPath, $storeCode);
143+
144+
return !(bool)$configs;
145+
}
146+
147+
/**
148+
* Returns the store ID by the store code
149+
*
150+
* @param string $storeCode
151+
* @return int
152+
*/
153+
private function getStoreIdByCode(string $storeCode): int
154+
{
155+
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
156+
$store = $storeManager->getStore($storeCode);
157+
return (int)$store->getId();
158+
}
159+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
/**
3+
* Application configuration object. Used to access configuration when application is installed.
4+
*
5+
* Copyright © Magento, Inc. All rights reserved.
6+
* See COPYING.txt for license details.
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace Magento\TestFramework\App;
12+
13+
use Magento\Framework\App\Config\MutableScopeConfigInterface;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\TestFramework\ObjectManager;
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
class MutableScopeConfig implements MutableScopeConfigInterface
21+
{
22+
/**
23+
* @var Config
24+
*/
25+
private $testAppConfig;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function isSetFlag($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
31+
{
32+
return $this->getTestAppConfig()->isSetFlag($path, $scopeType, $scopeCode);
33+
}
34+
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function getValue($path, $scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
39+
{
40+
return $this->getTestAppConfig()->getValue($path, $scopeType, $scopeCode);
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
public function setValue(
47+
$path,
48+
$value,
49+
$scopeType = \Magento\Framework\App\Config\ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
50+
$scopeCode = null
51+
) {
52+
$this->persistConfig($path, $value, $scopeType, $scopeCode);
53+
return $this->getTestAppConfig()->setValue($path, $value, $scopeType, $scopeCode);
54+
}
55+
56+
/**
57+
* Clean app config cache
58+
*
59+
* @param string|null $type
60+
* @return void
61+
*/
62+
public function clean()
63+
{
64+
$this->getTestAppConfig()->clean();
65+
}
66+
67+
/**
68+
* Retrieve test app config instance
69+
*
70+
* @return \Magento\TestFramework\App\Config
71+
*/
72+
private function getTestAppConfig()
73+
{
74+
if (!$this->testAppConfig) {
75+
$this->testAppConfig = ObjectManager::getInstance()->get(ScopeConfigInterface::class);
76+
}
77+
78+
return $this->testAppConfig;
79+
}
80+
81+
/**
82+
* Persist config in database
83+
*
84+
* @param string $path
85+
* @param string $value
86+
* @param string $scopeType
87+
* @param string|null $scopeCode
88+
*/
89+
private function persistConfig($path, $value, $scopeType, $scopeCode): void
90+
{
91+
$pathParts = explode('/', $path);
92+
$store = '';
93+
if ($scopeType === \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
94+
if ($scopeCode !== null) {
95+
$store = ObjectManager::getInstance()
96+
->get(\Magento\Store\Api\StoreRepositoryInterface::class)
97+
->get($scopeCode)
98+
->getId();
99+
} else {
100+
$store = ObjectManager::getInstance()
101+
->get(\Magento\Store\Model\StoreManagerInterface::class)
102+
->getStore()
103+
->getId();
104+
}
105+
}
106+
$configData = [
107+
'section' => $pathParts[0],
108+
'website' => '',
109+
'store' => $store,
110+
'groups' => [
111+
$pathParts[1] => [
112+
'fields' => [
113+
$pathParts[2] => [
114+
'value' => $value
115+
]
116+
]
117+
]
118+
]
119+
];
120+
ObjectManager::getInstance()
121+
->get(\Magento\Config\Model\Config\Factory::class)
122+
->create(['data' => $configData])
123+
->save();
124+
}
125+
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,34 @@
77
*/
88
namespace Magento\TestFramework\Bootstrap;
99

10+
use Magento\TestFramework\Annotation\ApiConfigFixture;
11+
use Magento\TestFramework\Annotation\ConfigFixture;
12+
13+
/**
14+
* @inheritdoc
15+
*/
1016
class WebapiDocBlock extends \Magento\TestFramework\Bootstrap\DocBlock
1117
{
1218
/**
13-
* Get list of subscribers. In addition, register <b>magentoApiDataFixture</b> annotation processing.
19+
* Get list of subscribers.
20+
*
21+
* In addition, register magentoApiDataFixture and magentoConfigFixture
22+
* annotation processors
1423
*
1524
* @param \Magento\TestFramework\Application $application
1625
* @return array
1726
*/
1827
protected function _getSubscribers(\Magento\TestFramework\Application $application)
1928
{
2029
$subscribers = parent::_getSubscribers($application);
30+
foreach ($subscribers as $key => $subscriber) {
31+
if (get_class($subscriber) == ConfigFixture::class) {
32+
unset($subscribers[$key]);
33+
}
34+
}
2135
$subscribers[] = new \Magento\TestFramework\Annotation\ApiDataFixture($this->_fixturesBaseDir);
36+
$subscribers[] = new ApiConfigFixture();
37+
2238
return $subscribers;
2339
}
2440
}

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

Lines changed: 18 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,14 @@ 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
}

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');

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
use Magento\TestFramework\TestCase\GraphQlAbstract;
1111

12+
/**
13+
* Test for the product only x left in stock
14+
*/
1215
class ProductOnlyXLeftInStockTest extends GraphQlAbstract
1316
{
1417
/**
@@ -42,7 +45,6 @@ public function testQueryProductOnlyXLeftInStockDisabled()
4245
*/
4346
public function testQueryProductOnlyXLeftInStockEnabled()
4447
{
45-
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/167');
4648
$productSku = 'simple';
4749

4850
$query = <<<QUERY

0 commit comments

Comments
 (0)