Skip to content

Commit

Permalink
Merge forwardport of #11510 to 2.3-develop branch
Browse files Browse the repository at this point in the history
Applied pull request patch https://github.com/magento/magento2/pull/11510.patch (created by @cmuench) based on commit(s):
  1. 10b57f4
  • Loading branch information
magento-engcom-team authored Jan 23, 2018
2 parents 1ad5260 + 88096d1 commit 7b99d4f
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 6 deletions.
101 changes: 97 additions & 4 deletions setup/src/Magento/Setup/Console/Command/AdminUserCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

namespace Magento\Setup\Console\Command;

use Magento\Setup\Model\AdminAccount;
use Magento\Framework\Setup\ConsoleLogger;
use Magento\Setup\Model\AdminAccount;
use Magento\Setup\Model\InstallerFactory;
use Magento\User\Model\UserValidationRules;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class AdminUserCreateCommand extends AbstractSetupCommand
{
Expand Down Expand Up @@ -50,14 +51,106 @@ protected function configure()
parent::configure();
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
/** @var \Symfony\Component\Console\Helper\QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');

if (!$input->getOption(AdminAccount::KEY_USER)) {
$question = new Question('<question>Admin user:</question> ', '');
$this->addNotEmptyValidator($question);

$input->setOption(
AdminAccount::KEY_USER,
$questionHelper->ask($input, $output, $question)
);
}

if (!$input->getOption(AdminAccount::KEY_PASSWORD)) {
$question = new Question('<question>Admin password:</question> ', '');
$question->setHidden(true);

$question->setValidator(function ($value) use ($output) {
$user = new \Magento\Framework\DataObject();
$user->setPassword($value);

$validator = new \Magento\Framework\Validator\DataObject();
$this->validationRules->addPasswordRules($validator);

$validator->isValid($user);
foreach ($validator->getMessages() as $message) {
throw new \Exception($message);
}

return $value;
});

$input->setOption(
AdminAccount::KEY_PASSWORD,
$questionHelper->ask($input, $output, $question)
);
}

if (!$input->getOption(AdminAccount::KEY_EMAIL)) {
$question = new Question('<question>Admin email:</question> ', '');
$this->addNotEmptyValidator($question);

$input->setOption(
AdminAccount::KEY_EMAIL,
$questionHelper->ask($input, $output, $question)
);
}

if (!$input->getOption(AdminAccount::KEY_FIRST_NAME)) {
$question = new Question('<question>Admin first name:</question> ', '');
$this->addNotEmptyValidator($question);

$input->setOption(
AdminAccount::KEY_FIRST_NAME,
$questionHelper->ask($input, $output, $question)
);
}

if (!$input->getOption(AdminAccount::KEY_LAST_NAME)) {
$question = new Question('<question>Admin last name:</question> ', '');
$this->addNotEmptyValidator($question);

$input->setOption(
AdminAccount::KEY_LAST_NAME,
$questionHelper->ask($input, $output, $question)
);
}
}

/**
* @param \Symfony\Component\Console\Question\Question $question
* @return void
*/
private function addNotEmptyValidator(Question $question)
{
$question->setValidator(function ($value) {
if (trim($value) == '') {
throw new \Exception('The value cannot be empty');
}

return $value;
});
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$errors = $this->validate($input);
if ($errors) {
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
$output->writeln('<error>' . implode('</error>' . PHP_EOL . '<error>', $errors) . '</error>');
// we must have an exit code higher than zero to indicate something was wrong
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
Expand Down Expand Up @@ -113,7 +206,7 @@ public function validate(InputInterface $input)
? '' : $input->getOption(AdminAccount::KEY_PASSWORD)
);

$validator = new \Magento\Framework\Validator\DataObject;
$validator = new \Magento\Framework\Validator\DataObject();
$this->validationRules->addUserInfoRules($validator);
$this->validationRules->addPasswordRules($validator);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
*/
namespace Magento\Setup\Test\Unit\Console\Command;

use Magento\Setup\Model\AdminAccount;
use Magento\Setup\Console\Command\AdminUserCreateCommand;
use Magento\Setup\Model\AdminAccount;
use Magento\Setup\Mvc\Bootstrap\InitParamListener;
use Magento\User\Model\UserValidationRules;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Tester\CommandTester;

class AdminUserCreateCommandTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Console\Helper\QuestionHelper
*/
private $questionHelperMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Setup\Model\InstallerFactory
*/
Expand All @@ -27,6 +34,10 @@ public function setUp()
{
$this->installerFactoryMock = $this->createMock(\Magento\Setup\Model\InstallerFactory::class);
$this->command = new AdminUserCreateCommand($this->installerFactoryMock, new UserValidationRules());

$this->questionHelperMock = $this->getMockBuilder(QuestionHelper::class)
->setMethods(['ask'])
->getMock();
}

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

public function testInteraction()
{
$application = new Application();
$application->add($this->command);

$this->questionHelperMock->expects($this->at(0))
->method('ask')
->will($this->returnValue('admin'));

$this->questionHelperMock->expects($this->at(1))
->method('ask')
->will($this->returnValue('Password123'));

$this->questionHelperMock->expects($this->at(2))
->method('ask')
->will($this->returnValue('john.doe@example.com'));

$this->questionHelperMock->expects($this->at(3))
->method('ask')
->will($this->returnValue('John'));

$this->questionHelperMock->expects($this->at(4))
->method('ask')
->will($this->returnValue('Doe'));

// We override the standard helper with our mock
$this->command->getHelperSet()->set($this->questionHelperMock, 'question');

$installerMock = $this->createMock(\Magento\Setup\Model\Installer::class);

$expectedData = [
'admin-user' => 'admin',
'admin-password' => 'Password123',
'admin-email' => 'john.doe@example.com',
'admin-firstname' => 'John',
'admin-lastname' => 'Doe',
'magento-init-params' => null,
'help' => false,
'quiet' => false,
'verbose' => false,
'version' => false,
'ansi' => false,
'no-ansi' => false,
'no-interaction' => false,
];

$installerMock->expects($this->once())->method('installAdminUser')->with($expectedData);
$this->installerFactoryMock->expects($this->once())->method('create')->willReturn($installerMock);

$commandTester = new CommandTester($this->command);
$commandTester->execute([
'command' => $this->command->getName(),
]);

$this->assertEquals(
'Created Magento administrator user named admin' . PHP_EOL,
$commandTester->getDisplay()
);
}

public function testGetOptionsList()
{
/* @var $argsList \Symfony\Component\Console\Input\InputArgument[] */
Expand Down

0 comments on commit 7b99d4f

Please sign in to comment.