1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Config\Console\Command;
use Magento\Config\Model\Config\Backend\Admin\Custom;
use Magento\Config\Model\Config\Structure\Converter;
use Magento\Config\Model\Config\Structure\Data as StructureData;
use Magento\Directory\Model\Currency;
use Magento\Framework\App\Config\ConfigPathResolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig\FileReader;
use Magento\Framework\App\DeploymentConfig\Writer;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Console\Cli;
use Magento\Framework\Filesystem;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Store\Model\ScopeInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use PHPUnit_Framework_MockObject_MockObject as Mock;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Tests the different flows of config:set command.
*
* @inheritdoc
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @magentoDbIsolation enabled
*/
class ConfigSetCommandTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var InputInterface|Mock
*/
private $inputMock;
/**
* @var OutputInterface|Mock
*/
private $outputMock;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var FileReader
*/
private $reader;
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var ConfigFilePool
*/
private $configFilePool;
/**
* @var ArrayManager
*/
private $arrayManager;
/**
* @var array
*/
private $config;
/**
* @var ReinitableConfigInterface
*/
private $appConfig;
/**
* @inheritdoc
*/
protected function setUp()
{
Bootstrap::getInstance()->reinitialize();
$this->objectManager = Bootstrap::getObjectManager();
$this->extendSystemStructure();
$this->scopeConfig = $this->objectManager->get(ScopeConfigInterface::class);
$this->reader = $this->objectManager->get(FileReader::class);
$this->filesystem = $this->objectManager->get(Filesystem::class);
$this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
$this->arrayManager = $this->objectManager->get(ArrayManager::class);
$this->appConfig = $this->objectManager->get(ReinitableConfigInterface::class);
// Snapshot of configuration.
$this->config = $this->loadConfig();
// Mocks for objects.
$this->inputMock = $this->getMockBuilder(InputInterface::class)
->getMockForAbstractClass();
$this->outputMock = $this->getMockBuilder(OutputInterface::class)
->getMockForAbstractClass();
}
/**
* @inheritdoc
*/
protected function tearDown()
{
$this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
$this->configFilePool->getPath(ConfigFilePool::APP_ENV),
"<?php\n return array();\n"
);
/** @var Writer $writer */
$writer = $this->objectManager->get(Writer::class);
$writer->saveConfig([ConfigFilePool::APP_ENV => $this->config]);
$this->appConfig->reinit();
}
/**
* Add test system structure to main system structure
*
* @return void
*/
private function extendSystemStructure()
{
$document = new \DOMDocument();
$document->load(__DIR__ . '/../../_files/system.xml');
$converter = $this->objectManager->get(Converter::class);
$systemConfig = $converter->convert($document);
$structureData = $this->objectManager->get(StructureData::class);
$structureData->merge($systemConfig);
}
/**
* @return array
*/
private function loadConfig()
{
return $this->reader->load(ConfigFilePool::APP_ENV);
}
/**
* Tests lockable flow.
* Expects to save value and then error on saving duplicate value.
*
* @param string $path
* @param string $value
* @param string $scope
* @param string $scopeCode
* @magentoDbIsolation enabled
* @dataProvider runLockDataProvider
*/
public function testRunLockEnv($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null)
{
$this->inputMock->expects($this->any())
->method('getArgument')
->willReturnMap([
[ConfigSetCommand::ARG_PATH, $path],
[ConfigSetCommand::ARG_VALUE, $value]
]);
$this->inputMock->expects($this->any())
->method('getOption')
->willReturnMap([
[ConfigSetCommand::OPTION_LOCK_ENV, true],
[ConfigSetCommand::OPTION_SCOPE, $scope],
[ConfigSetCommand::OPTION_SCOPE_CODE, $scopeCode]
]);
$this->outputMock->expects($this->exactly(2))
->method('writeln')
->withConsecutive(
['<info>Value was saved in app/etc/env.php and locked.</info>'],
['<info>Value was saved in app/etc/env.php and locked.</info>']
);
/** @var ConfigSetCommand $command */
$command = $this->objectManager->create(ConfigSetCommand::class);
/** @var ConfigPathResolver $resolver */
$resolver = $this->objectManager->get(ConfigPathResolver::class);
$status = $command->run($this->inputMock, $this->outputMock);
$configPath = $resolver->resolve($path, $scope, $scopeCode, 'system');
$this->assertSame(Cli::RETURN_SUCCESS, $status);
$this->assertSame($value, $this->arrayManager->get($configPath, $this->loadConfig()));
$status = $command->run($this->inputMock, $this->outputMock);
$this->appConfig->reinit();
$this->assertSame(Cli::RETURN_SUCCESS, $status);
}
/**
* Retrieves variations with path, value, scope and scope code.
*
* @return array
*/
public function runLockDataProvider()
{
return [
['general/region/display_all', '1'],
['general/region/state_required', 'BR,FR', ScopeInterface::SCOPE_WEBSITE, 'base'],
['admin/security/use_form_key', '0'],
['general/group/subgroup/field', 'default_value'],
['general/group/subgroup/field', 'website_value', ScopeInterface::SCOPE_WEBSITE, 'base'],
];
}
/**
* Tests the extended flow.
*
* @param string $path
* @param string $value
* @param string $scope
* @param string $scopeCode
* @magentoDbIsolation enabled
* @dataProvider runExtendedDataProvider
*/
public function testRunExtended(
$path,
$value,
$scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
$scopeCode = null
) {
$arguments = [
[ConfigSetCommand::ARG_PATH, $path],
[ConfigSetCommand::ARG_VALUE, $value]
];
$options = [
[ConfigSetCommand::OPTION_SCOPE, $scope],
[ConfigSetCommand::OPTION_SCOPE_CODE, $scopeCode]
];
$optionsLock = array_merge($options, [[ConfigSetCommand::OPTION_LOCK_ENV, true]]);
/** @var ConfigPathResolver $resolver */
$resolver = $this->objectManager->get(ConfigPathResolver::class);
/** @var array $configPath */
$configPath = $resolver->resolve($path, $scope, $scopeCode, 'system');
$this->runCommand($arguments, $options, '<info>Value was saved.</info>');
$this->runCommand($arguments, $options, '<info>Value was saved.</info>');
$this->assertSame(
$value,
$this->scopeConfig->getValue($path, $scope, $scopeCode)
);
$this->assertSame(null, $this->arrayManager->get($configPath, $this->loadConfig()));
$this->runCommand($arguments, $optionsLock, '<info>Value was saved in app/etc/env.php and locked.</info>');
$this->runCommand($arguments, $optionsLock, '<info>Value was saved in app/etc/env.php and locked.</info>');
$this->assertSame($value, $this->arrayManager->get($configPath, $this->loadConfig()));
}
/**
* Runs pre-configured command.
*
* @param array $arguments
* @param array $options
* @param string $expectedMessage
* @param int $expectedCode
*/
private function runCommand(
array $arguments,
array $options,
$expectedMessage = '',
$expectedCode = Cli::RETURN_SUCCESS
) {
$input = clone $this->inputMock;
$output = clone $this->outputMock;
$input->expects($this->any())
->method('getArgument')
->willReturnMap($arguments);
$input->expects($this->any())
->method('getOption')
->willReturnMap($options);
$output->expects($this->once())
->method('writeln')
->with($expectedMessage);
/** @var ConfigSetCommand $command */
$command = $this->objectManager->create(ConfigSetCommand::class);
$status = $command->run($input, $output);
$this->appConfig->reinit();
$this->assertSame($expectedCode, $status);
}
/**
* Retrieves variations with path, value, scope and scope code.
*
* @return array
*/
public function runExtendedDataProvider()
{
return $this->runLockDataProvider();
}
/**
* @param string $path Config path
* @param string $value Value of config is tried to be set
* @param string $message Message command output
* @param string $scope
* @param $scopeCode string|null
* @dataProvider configSetValidationErrorDataProvider
* @magentoDbIsolation disabled
*/
public function testConfigSetValidationError(
$path,
$value,
$message,
$scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
$scopeCode = null
) {
$this->setConfigFailure($path, $value, $message, $scope, $scopeCode);
}
/**
* Data provider for testConfigSetValidationError
*
* @return array
*/
public function configSetValidationErrorDataProvider()
{
return [
//wrong value for URL - checked by backend model of URL field
[
Custom::XML_PATH_UNSECURE_BASE_URL,
'value',
'Invalid Base URL. Value must be a URL or one of placeholders: {{base_url}}'
],
//set not existed field path
[
'test/test/test',
'value',
'The "test/test/test" path doesn\'t exist. Verify and try again.'
],
//wrong scope or scope code
[
Custom::XML_PATH_GENERAL_LOCALE_CODE,
'en_UK',
'A scope is missing. Enter a scope and try again.',
''
],
[
Custom::XML_PATH_GENERAL_LOCALE_CODE,
'en_UK',
'A scope code is missing. Enter a code and try again.',
ScopeInterface::SCOPE_WEBSITE
],
[
Custom::XML_PATH_GENERAL_LOCALE_CODE,
'en_UK',
'A scope code is missing. Enter a code and try again.',
ScopeInterface::SCOPE_STORE
],
[
Custom::XML_PATH_GENERAL_LOCALE_CODE,
'en_UK',
'The "wrong_scope" value doesn\'t exist. Enter another value and try again.',
'wrong_scope',
'base'
],
[
Custom::XML_PATH_GENERAL_LOCALE_CODE,
'en_UK',
'The "wrong_website_code" value doesn\'t exist. Enter another value and try again.',
ScopeInterface::SCOPE_WEBSITE,
'wrong_website_code'
],
[
Custom::XML_PATH_GENERAL_LOCALE_CODE,
'en_UK',
'The "wrong_store_code" value doesn\'t exist. Enter another value and try again.',
ScopeInterface::SCOPE_STORE,
'wrong_store_code'
],
[
Currency::XML_PATH_CURRENCY_DEFAULT,
'GBP',
'Sorry, the default display currency you selected is not available in allowed currencies.'
],
[
Currency::XML_PATH_CURRENCY_ALLOW,
'GBP',
'Default display currency "US Dollar" is not available in allowed currencies.'
]
];
}
/**
* Saving values with successful validation
*
* @magentoDbIsolation enabled
*/
public function testConfigSetCurrency()
{
/**
* Checking saving currency as they are depend on each other.
* Default currency can not be changed to new value if this value does not exist in allowed currency
* that is why allowed currency is changed first by adding necessary value,
* then old value is removed after changing default currency
*/
$this->setConfigSuccess(Currency::XML_PATH_CURRENCY_ALLOW, 'USD,GBP');
$this->setConfigSuccess(Currency::XML_PATH_CURRENCY_DEFAULT, 'GBP');
$this->setConfigSuccess(Currency::XML_PATH_CURRENCY_ALLOW, 'GBP');
}
/**
* Saving values with successful validation
*
* @dataProvider configSetValidDataProvider
* @magentoDbIsolation enabled
*/
public function testConfigSetValid()
{
$this->setConfigSuccess(Custom::XML_PATH_UNSECURE_BASE_URL, 'http://magento2.local/');
$this->setConfigSuccess(Custom::XML_PATH_GENERAL_LOCALE_CODE, 'en_UK', ScopeInterface::SCOPE_WEBSITE, 'base');
$this->setConfigSuccess(Custom::XML_PATH_GENERAL_LOCALE_CODE, 'en_AU', ScopeInterface::SCOPE_STORE, 'default');
}
/**
* Data provider for testConfigSetValid
*
* @return array
*/
public function configSetValidDataProvider()
{
return [
[Custom::XML_PATH_UNSECURE_BASE_URL, 'http://magento2.local/'],
[Custom::XML_PATH_GENERAL_LOCALE_CODE, 'en_UK', ScopeInterface::SCOPE_WEBSITE, 'base'],
[Custom::XML_PATH_GENERAL_LOCALE_CODE, 'en_AU', ScopeInterface::SCOPE_STORE, 'default'],
[Custom::XML_PATH_ADMIN_SECURITY_USEFORMKEY, '0']
];
}
/**
* Set configuration and check this value from DB with success message this command should display
*
* @param string $path Config path
* @param string $value Value of config is tried to be set
* @param string $scope
* @param string|null $scopeCode
*/
private function setConfigSuccess(
$path,
$value,
$scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
$scopeCode = null
) {
$status = $this->setConfig($path, $value, '<info>Value was saved.</info>', $scope, $scopeCode);
$this->assertSame(Cli::RETURN_SUCCESS, $status);
$this->assertSame(
$value,
$this->scopeConfig->getValue($path, $scope, $scopeCode)
);
}
/**
* Set configuration value with some error
* and check that this value was not saved to DB and appropriate error message was displayed
*
* @param string $path Config path
* @param string $value Value of config is tried to be set
* @param string $message Message command output
* @param string $scope
* @param string|null $scopeCode
*/
private function setConfigFailure(
$path,
$value,
$message,
$scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
$scopeCode = null
) {
$status = $this->setConfig($path, $value, '<error>' . $message . '</error>', $scope, $scopeCode);
$this->assertSame(Cli::RETURN_FAILURE, $status);
$this->assertNotSame(
$value,
$this->scopeConfig->getValue($path),
"Values are the same '$value' and '{$this->scopeConfig->getValue($path)}' for $path"
);
}
/**
* @param string $path Config path
* @param string $value Value of config is tried to be set
* @param string $message Message command output
* @param string $scope
* @param string|null $scopeCode
* @return int Status that command returned
*/
private function setConfig($path, $value, $message, $scope, $scopeCode)
{
$input = clone $this->inputMock;
$output = clone $this->outputMock;
$input->expects($this->any())
->method('getArgument')
->willReturnMap([
[ConfigSetCommand::ARG_PATH, $path],
[ConfigSetCommand::ARG_VALUE, $value]
]);
$input->expects($this->any())
->method('getOption')
->willReturnMap([
[ConfigSetCommand::OPTION_SCOPE, $scope],
[ConfigSetCommand::OPTION_SCOPE_CODE, $scopeCode]
]);
$output->expects($this->once())
->method('writeln')
->with($message);
/** @var ConfigSetCommand $command */
$command = $this->objectManager->create(ConfigSetCommand::class);
$status = $command->run($input, $output);
return $status;
}
}