Skip to content

Commit e7b5fda

Browse files
authored
Merge pull request #3233 from magento-thunder/MAGETWO-94844
[thunder] MAGETWO-94844: Add the ability to install Magento without creating an administrator
2 parents 3dc3058 + cb0c2b5 commit e7b5fda

File tree

6 files changed

+257
-76
lines changed

6 files changed

+257
-76
lines changed

setup/src/Magento/Setup/Console/Command/AdminUserCreateCommand.php

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Symfony\Component\Console\Question\Question;
1717

18+
/**
19+
* Command to create an admin user.
20+
*/
1821
class AdminUserCreateCommand extends AbstractSetupCommand
1922
{
2023
/**
@@ -52,6 +55,8 @@ protected function configure()
5255
}
5356

5457
/**
58+
* Creation admin user in interaction mode.
59+
*
5560
* @param \Symfony\Component\Console\Input\InputInterface $input
5661
* @param \Symfony\Component\Console\Output\OutputInterface $output
5762
*
@@ -129,6 +134,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
129134
}
130135

131136
/**
137+
* Add not empty validator.
138+
*
132139
* @param \Symfony\Component\Console\Question\Question $question
133140
* @return void
134141
*/
@@ -144,7 +151,7 @@ private function addNotEmptyValidator(Question $question)
144151
}
145152

146153
/**
147-
* {@inheritdoc}
154+
* @inheritdoc
148155
*/
149156
protected function execute(InputInterface $input, OutputInterface $output)
150157
{
@@ -165,25 +172,43 @@ protected function execute(InputInterface $input, OutputInterface $output)
165172
/**
166173
* Get list of arguments for the command
167174
*
175+
* @param int $mode The mode of options.
168176
* @return InputOption[]
169177
*/
170-
public function getOptionsList()
178+
public function getOptionsList($mode = InputOption::VALUE_REQUIRED)
171179
{
180+
$requiredStr = ($mode === InputOption::VALUE_REQUIRED ? '(Required) ' : '');
181+
172182
return [
173-
new InputOption(AdminAccount::KEY_USER, null, InputOption::VALUE_REQUIRED, '(Required) Admin user'),
174-
new InputOption(AdminAccount::KEY_PASSWORD, null, InputOption::VALUE_REQUIRED, '(Required) Admin password'),
175-
new InputOption(AdminAccount::KEY_EMAIL, null, InputOption::VALUE_REQUIRED, '(Required) Admin email'),
183+
new InputOption(
184+
AdminAccount::KEY_USER,
185+
null,
186+
$mode,
187+
$requiredStr . 'Admin user'
188+
),
189+
new InputOption(
190+
AdminAccount::KEY_PASSWORD,
191+
null,
192+
$mode,
193+
$requiredStr . 'Admin password'
194+
),
195+
new InputOption(
196+
AdminAccount::KEY_EMAIL,
197+
null,
198+
$mode,
199+
$requiredStr . 'Admin email'
200+
),
176201
new InputOption(
177202
AdminAccount::KEY_FIRST_NAME,
178203
null,
179-
InputOption::VALUE_REQUIRED,
180-
'(Required) Admin first name'
204+
$mode,
205+
$requiredStr . 'Admin first name'
181206
),
182207
new InputOption(
183208
AdminAccount::KEY_LAST_NAME,
184209
null,
185-
InputOption::VALUE_REQUIRED,
186-
'(Required) Admin last name'
210+
$mode,
211+
$requiredStr . 'Admin last name'
187212
),
188213
];
189214
}

setup/src/Magento/Setup/Console/Command/InstallCommand.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Deploy\Console\Command\App\ConfigImportCommand;
1111
use Magento\Framework\Setup\Declaration\Schema\DryRunLogger;
1212
use Magento\Framework\Setup\Declaration\Schema\OperationsExecutor;
13+
use Magento\Framework\Setup\Declaration\Schema\Request;
14+
use Magento\Setup\Model\AdminAccount;
1315
use Magento\Setup\Model\ConfigModel;
1416
use Magento\Setup\Model\InstallerFactory;
1517
use Magento\Framework\Setup\ConsoleLogger;
@@ -136,7 +138,7 @@ protected function configure()
136138
{
137139
$inputOptions = $this->configModel->getAvailableOptions();
138140
$inputOptions = array_merge($inputOptions, $this->userConfig->getOptionsList());
139-
$inputOptions = array_merge($inputOptions, $this->adminUser->getOptionsList());
141+
$inputOptions = array_merge($inputOptions, $this->adminUser->getOptionsList(InputOption::VALUE_OPTIONAL));
140142
$inputOptions = array_merge($inputOptions, [
141143
new InputOption(
142144
self::INPUT_KEY_CLEANUP_DB,
@@ -251,7 +253,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
251253
}
252254

253255
$errors = $this->configModel->validate($configOptionsToValidate);
254-
$errors = array_merge($errors, $this->adminUser->validate($input));
256+
$errors = array_merge($errors, $this->validateAdmin($input));
255257
$errors = array_merge($errors, $this->validate($input));
256258
$errors = array_merge($errors, $this->userConfig->validate($input));
257259

@@ -320,7 +322,7 @@ private function interactiveQuestions(InputInterface $input, OutputInterface $ou
320322

321323
$output->writeln("");
322324

323-
foreach ($this->adminUser->getOptionsList() as $option) {
325+
foreach ($this->adminUser->getOptionsList(InputOption::VALUE_OPTIONAL) as $option) {
324326
$configOptionsToValidate[$option->getName()] = $this->askQuestion(
325327
$input,
326328
$output,
@@ -411,4 +413,24 @@ private function askQuestion(
411413

412414
return $value;
413415
}
416+
417+
/**
418+
* Performs validation of admin options if at least one of them was set.
419+
*
420+
* @param InputInterface $input
421+
* @return array
422+
*/
423+
private function validateAdmin(InputInterface $input): array
424+
{
425+
if ($input->getOption(AdminAccount::KEY_FIRST_NAME)
426+
|| $input->getOption(AdminAccount::KEY_LAST_NAME)
427+
|| $input->getOption(AdminAccount::KEY_EMAIL)
428+
|| $input->getOption(AdminAccount::KEY_USER)
429+
|| $input->getOption(AdminAccount::KEY_PASSWORD)
430+
) {
431+
return $this->adminUser->validate($input);
432+
}
433+
434+
return [];
435+
}
414436
}

setup/src/Magento/Setup/Model/Installer.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ class Installer
267267
* @param \Magento\Framework\Setup\SampleData\State $sampleDataState
268268
* @param ComponentRegistrar $componentRegistrar
269269
* @param PhpReadinessCheck $phpReadinessCheck
270-
*
270+
* @throws \Magento\Setup\Exception
271271
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
272272
*/
273273
public function __construct(
@@ -345,7 +345,9 @@ public function install($request)
345345
[$request[InstallCommand::INPUT_KEY_SALES_ORDER_INCREMENT_PREFIX]],
346346
];
347347
}
348-
$script[] = ['Installing admin user...', 'installAdminUser', [$request]];
348+
if ($this->isAdminDataSet($request)) {
349+
$script[] = ['Installing admin user...', 'installAdminUser', [$request]];
350+
}
349351

350352
if (!$this->isDryRun($request)) {
351353
$script[] = ['Caches clearing:', 'cleanCaches', [$request]];
@@ -909,6 +911,7 @@ private function throwExceptionForNotWritablePaths(array $paths)
909911
* @param string $type
910912
* @param array $request
911913
* @return void
914+
* @throws \Magento\Framework\Setup\Exception
912915
* @throws \Magento\Setup\Exception
913916
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
914917
* @SuppressWarnings(PHPMD.NPathComplexity)
@@ -1473,4 +1476,28 @@ private function cleanupGeneratedFiles()
14731476
$this->log->log($message);
14741477
}
14751478
}
1479+
1480+
/**
1481+
* Checks that admin data is not empty in request array
1482+
*
1483+
* @param \ArrayObject|array $request
1484+
* @return bool
1485+
*/
1486+
private function isAdminDataSet($request)
1487+
{
1488+
$adminData = array_filter($request, function ($value, $key) {
1489+
return in_array(
1490+
$key,
1491+
[
1492+
AdminAccount::KEY_EMAIL,
1493+
AdminAccount::KEY_FIRST_NAME,
1494+
AdminAccount::KEY_LAST_NAME,
1495+
AdminAccount::KEY_USER,
1496+
AdminAccount::KEY_PASSWORD,
1497+
]
1498+
) && $value !== null;
1499+
}, ARRAY_FILTER_USE_BOTH);
1500+
1501+
return !empty($adminData);
1502+
}
14761503
}

setup/src/Magento/Setup/Test/Unit/Console/Command/AdminUserCreateCommandTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
use Magento\User\Model\UserValidationRules;
1212
use Symfony\Component\Console\Application;
1313
use Symfony\Component\Console\Helper\QuestionHelper;
14+
use Symfony\Component\Console\Input\InputOption;
1415
use Symfony\Component\Console\Tester\CommandTester;
1516

17+
/**
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19+
*/
1620
class AdminUserCreateCommandTest extends \PHPUnit\Framework\TestCase
1721
{
1822
/**
@@ -125,11 +129,34 @@ public function testInteraction()
125129
);
126130
}
127131

128-
public function testGetOptionsList()
132+
/**
133+
* @param int $mode
134+
* @param string $description
135+
* @dataProvider getOptionListDataProvider
136+
*/
137+
public function testGetOptionsList($mode, $description)
129138
{
130139
/* @var $argsList \Symfony\Component\Console\Input\InputArgument[] */
131-
$argsList = $this->command->getOptionsList();
140+
$argsList = $this->command->getOptionsList($mode);
132141
$this->assertEquals(AdminAccount::KEY_EMAIL, $argsList[2]->getName());
142+
$this->assertEquals($description, $argsList[2]->getDescription());
143+
}
144+
145+
/**
146+
* @return array
147+
*/
148+
public function getOptionListDataProvider()
149+
{
150+
return [
151+
[
152+
'mode' => InputOption::VALUE_REQUIRED,
153+
'description' => '(Required) Admin email',
154+
],
155+
[
156+
'mode' => InputOption::VALUE_OPTIONAL,
157+
'description' => 'Admin email',
158+
],
159+
];
133160
}
134161

135162
/**

setup/src/Magento/Setup/Test/Unit/Console/Command/InstallCommandTest.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Backend\Setup\ConfigOptionsList as BackendConfigOptionsList;
1717
use Magento\Framework\Config\ConfigOptionsListConstants as SetupConfigOptionsList;
1818
use Magento\Setup\Model\StoreConfigurationDataMapper;
19+
use Magento\Setup\Console\Command\AdminUserCreateCommand;
1920

2021
/**
2122
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -62,6 +63,11 @@ class InstallCommandTest extends \PHPUnit\Framework\TestCase
6263
*/
6364
private $configImportMock;
6465

66+
/**
67+
* @var AdminUserCreateCommand|\PHPUnit_Framework_MockObject_MockObject
68+
*/
69+
private $adminUserMock;
70+
6571
public function setUp()
6672
{
6773
$this->input = [
@@ -73,11 +79,6 @@ public function setUp()
7379
'--' . StoreConfigurationDataMapper::KEY_LANGUAGE => 'en_US',
7480
'--' . StoreConfigurationDataMapper::KEY_TIMEZONE => 'America/Chicago',
7581
'--' . StoreConfigurationDataMapper::KEY_CURRENCY => 'USD',
76-
'--' . AdminAccount::KEY_USER => 'user',
77-
'--' . AdminAccount::KEY_PASSWORD => '123123q',
78-
'--' . AdminAccount::KEY_EMAIL => '[email protected]',
79-
'--' . AdminAccount::KEY_FIRST_NAME => 'John',
80-
'--' . AdminAccount::KEY_LAST_NAME => 'Doe',
8182
];
8283

8384
$configModel = $this->createMock(\Magento\Setup\Model\ConfigModel::class);
@@ -100,15 +101,11 @@ public function setUp()
100101
->method('validate')
101102
->will($this->returnValue([]));
102103

103-
$adminUser = $this->createMock(\Magento\Setup\Console\Command\AdminUserCreateCommand::class);
104-
$adminUser
104+
$this->adminUserMock = $this->createMock(AdminUserCreateCommand::class);
105+
$this->adminUserMock
105106
->expects($this->once())
106107
->method('getOptionsList')
107108
->will($this->returnValue($this->getOptionsListAdminUser()));
108-
$adminUser
109-
->expects($this->once())
110-
->method('validate')
111-
->will($this->returnValue([]));
112109

113110
$this->installerFactory = $this->createMock(\Magento\Setup\Model\InstallerFactory::class);
114111
$this->installer = $this->createMock(\Magento\Setup\Model\Installer::class);
@@ -143,7 +140,7 @@ public function setUp()
143140
$this->installerFactory,
144141
$configModel,
145142
$userConfig,
146-
$adminUser
143+
$this->adminUserMock
147144
);
148145
$this->command->setApplication(
149146
$this->applicationMock
@@ -152,6 +149,16 @@ public function setUp()
152149

153150
public function testExecute()
154151
{
152+
$this->input['--' . AdminAccount::KEY_USER] = 'user';
153+
$this->input['--' . AdminAccount::KEY_PASSWORD] = '123123q';
154+
$this->input['--' . AdminAccount::KEY_EMAIL] = '[email protected]';
155+
$this->input['--' . AdminAccount::KEY_FIRST_NAME] = 'John';
156+
$this->input['--' . AdminAccount::KEY_LAST_NAME] = 'Doe';
157+
158+
$this->adminUserMock
159+
->expects($this->once())
160+
->method('validate')
161+
->willReturn([]);
155162
$this->installerFactory->expects($this->once())
156163
->method('create')
157164
->will($this->returnValue($this->installer));
@@ -269,6 +276,9 @@ private function getOptionsListAdminUser()
269276
*/
270277
public function testValidate($prefixValue)
271278
{
279+
$this->adminUserMock
280+
->expects($this->never())
281+
->method('validate');
272282
$this->installerFactory->expects($this->once())
273283
->method('create')
274284
->will($this->returnValue($this->installer));
@@ -288,6 +298,9 @@ public function testValidate($prefixValue)
288298
*/
289299
public function testValidateWithException($prefixValue)
290300
{
301+
$this->adminUserMock
302+
->expects($this->never())
303+
->method('validate');
291304
$this->installerFactory->expects($this->never())
292305
->method('create')
293306
->will($this->returnValue($this->installer));

0 commit comments

Comments
 (0)