Skip to content

Commit 1a35363

Browse files
author
Oleksii Korshenko
authored
MAGETWO-82234: Add interaction to admin:user:create command #11510
2 parents eb128a6 + be3b25b commit 1a35363

File tree

2 files changed

+170
-6
lines changed

2 files changed

+170
-6
lines changed

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

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
namespace Magento\Setup\Console\Command;
88

9-
use Magento\Setup\Model\AdminAccount;
109
use Magento\Framework\Setup\ConsoleLogger;
10+
use Magento\Setup\Model\AdminAccount;
1111
use Magento\Setup\Model\InstallerFactory;
1212
use Magento\User\Model\UserValidationRules;
13-
use Symfony\Component\Console\Input\InputOption;
1413
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Input\InputOption;
1515
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\Console\Question\Question;
1617

1718
class AdminUserCreateCommand extends AbstractSetupCommand
1819
{
@@ -50,14 +51,106 @@ protected function configure()
5051
parent::configure();
5152
}
5253

54+
/**
55+
* @param \Symfony\Component\Console\Input\InputInterface $input
56+
* @param \Symfony\Component\Console\Output\OutputInterface $output
57+
*
58+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
59+
*/
60+
protected function interact(InputInterface $input, OutputInterface $output)
61+
{
62+
/** @var \Symfony\Component\Console\Helper\QuestionHelper $questionHelper */
63+
$questionHelper = $this->getHelper('question');
64+
65+
if (!$input->getOption(AdminAccount::KEY_USER)) {
66+
$question = new Question('<question>Admin user:</question> ', '');
67+
$this->addNotEmptyValidator($question);
68+
69+
$input->setOption(
70+
AdminAccount::KEY_USER,
71+
$questionHelper->ask($input, $output, $question)
72+
);
73+
}
74+
75+
if (!$input->getOption(AdminAccount::KEY_PASSWORD)) {
76+
$question = new Question('<question>Admin password:</question> ', '');
77+
$question->setHidden(true);
78+
79+
$question->setValidator(function ($value) use ($output) {
80+
$user = new \Magento\Framework\DataObject();
81+
$user->setPassword($value);
82+
83+
$validator = new \Magento\Framework\Validator\DataObject();
84+
$this->validationRules->addPasswordRules($validator);
85+
86+
$validator->isValid($user);
87+
foreach ($validator->getMessages() as $message) {
88+
throw new \Exception($message);
89+
}
90+
91+
return $value;
92+
});
93+
94+
$input->setOption(
95+
AdminAccount::KEY_PASSWORD,
96+
$questionHelper->ask($input, $output, $question)
97+
);
98+
}
99+
100+
if (!$input->getOption(AdminAccount::KEY_EMAIL)) {
101+
$question = new Question('<question>Admin email:</question> ', '');
102+
$this->addNotEmptyValidator($question);
103+
104+
$input->setOption(
105+
AdminAccount::KEY_EMAIL,
106+
$questionHelper->ask($input, $output, $question)
107+
);
108+
}
109+
110+
if (!$input->getOption(AdminAccount::KEY_FIRST_NAME)) {
111+
$question = new Question('<question>Admin first name:</question> ', '');
112+
$this->addNotEmptyValidator($question);
113+
114+
$input->setOption(
115+
AdminAccount::KEY_FIRST_NAME,
116+
$questionHelper->ask($input, $output, $question)
117+
);
118+
}
119+
120+
if (!$input->getOption(AdminAccount::KEY_LAST_NAME)) {
121+
$question = new Question('<question>Admin last name:</question> ', '');
122+
$this->addNotEmptyValidator($question);
123+
124+
$input->setOption(
125+
AdminAccount::KEY_LAST_NAME,
126+
$questionHelper->ask($input, $output, $question)
127+
);
128+
}
129+
}
130+
131+
/**
132+
* @param \Symfony\Component\Console\Question\Question $question
133+
* @return void
134+
*/
135+
private function addNotEmptyValidator(Question $question)
136+
{
137+
$question->setValidator(function ($value) {
138+
if (trim($value) == '') {
139+
throw new \Exception('The value cannot be empty');
140+
}
141+
142+
return $value;
143+
});
144+
}
145+
53146
/**
54147
* {@inheritdoc}
55148
*/
56149
protected function execute(InputInterface $input, OutputInterface $output)
57150
{
58151
$errors = $this->validate($input);
59152
if ($errors) {
60-
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
153+
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
61154
// we must have an exit code higher than zero to indicate something was wrong
62155
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
63156
}
@@ -113,7 +206,7 @@ public function validate(InputInterface $input)
113206
? '' : $input->getOption(AdminAccount::KEY_PASSWORD)
114207
);
115208

116-
$validator = new \Magento\Framework\Validator\DataObject;
209+
$validator = new \Magento\Framework\Validator\DataObject();
117210
$this->validationRules->addUserInfoRules($validator);
118211
$this->validationRules->addPasswordRules($validator);
119212

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

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@
55
*/
66
namespace Magento\Setup\Test\Unit\Console\Command;
77

8-
use Magento\Setup\Model\AdminAccount;
98
use Magento\Setup\Console\Command\AdminUserCreateCommand;
9+
use Magento\Setup\Model\AdminAccount;
1010
use Magento\Setup\Mvc\Bootstrap\InitParamListener;
1111
use Magento\User\Model\UserValidationRules;
12+
use Symfony\Component\Console\Application;
13+
use Symfony\Component\Console\Helper\QuestionHelper;
1214
use Symfony\Component\Console\Tester\CommandTester;
1315

1416
class AdminUserCreateCommandTest extends \PHPUnit\Framework\TestCase
1517
{
18+
/**
19+
* @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Console\Helper\QuestionHelper
20+
*/
21+
private $questionHelperMock;
22+
1623
/**
1724
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Setup\Model\InstallerFactory
1825
*/
@@ -27,6 +34,10 @@ public function setUp()
2734
{
2835
$this->installerFactoryMock = $this->createMock(\Magento\Setup\Model\InstallerFactory::class);
2936
$this->command = new AdminUserCreateCommand($this->installerFactoryMock, new UserValidationRules());
37+
38+
$this->questionHelperMock = $this->getMockBuilder(QuestionHelper::class)
39+
->setMethods(['ask'])
40+
->getMock();
3041
}
3142

3243
public function testExecute()
@@ -50,10 +61,70 @@ public function testExecute()
5061
$installerMock = $this->createMock(\Magento\Setup\Model\Installer::class);
5162
$installerMock->expects($this->once())->method('installAdminUser')->with($data);
5263
$this->installerFactoryMock->expects($this->once())->method('create')->willReturn($installerMock);
53-
$commandTester->execute($options);
64+
$commandTester->execute($options, ['interactive' => false]);
5465
$this->assertEquals('Created Magento administrator user named user' . PHP_EOL, $commandTester->getDisplay());
5566
}
5667

68+
public function testInteraction()
69+
{
70+
$application = new Application();
71+
$application->add($this->command);
72+
73+
$this->questionHelperMock->expects($this->at(0))
74+
->method('ask')
75+
->will($this->returnValue('admin'));
76+
77+
$this->questionHelperMock->expects($this->at(1))
78+
->method('ask')
79+
->will($this->returnValue('Password123'));
80+
81+
$this->questionHelperMock->expects($this->at(2))
82+
->method('ask')
83+
->will($this->returnValue('[email protected]'));
84+
85+
$this->questionHelperMock->expects($this->at(3))
86+
->method('ask')
87+
->will($this->returnValue('John'));
88+
89+
$this->questionHelperMock->expects($this->at(4))
90+
->method('ask')
91+
->will($this->returnValue('Doe'));
92+
93+
// We override the standard helper with our mock
94+
$this->command->getHelperSet()->set($this->questionHelperMock, 'question');
95+
96+
$installerMock = $this->createMock(\Magento\Setup\Model\Installer::class);
97+
98+
$expectedData = [
99+
'admin-user' => 'admin',
100+
'admin-password' => 'Password123',
101+
'admin-email' => '[email protected]',
102+
'admin-firstname' => 'John',
103+
'admin-lastname' => 'Doe',
104+
'magento-init-params' => null,
105+
'help' => false,
106+
'quiet' => false,
107+
'verbose' => false,
108+
'version' => false,
109+
'ansi' => false,
110+
'no-ansi' => false,
111+
'no-interaction' => false,
112+
];
113+
114+
$installerMock->expects($this->once())->method('installAdminUser')->with($expectedData);
115+
$this->installerFactoryMock->expects($this->once())->method('create')->willReturn($installerMock);
116+
117+
$commandTester = new CommandTester($this->command);
118+
$commandTester->execute([
119+
'command' => $this->command->getName(),
120+
]);
121+
122+
$this->assertEquals(
123+
'Created Magento administrator user named admin' . PHP_EOL,
124+
$commandTester->getDisplay()
125+
);
126+
}
127+
57128
public function testGetOptionsList()
58129
{
59130
/* @var $argsList \Symfony\Component\Console\Input\InputArgument[] */

0 commit comments

Comments
 (0)