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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Setup\Test\Unit\Console\Command;
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\Input\InputOption;
use Symfony\Component\Console\Tester\CommandTester;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
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
*/
private $installerFactoryMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|AdminUserCreateCommand
*/
private $command;
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()
{
$options = [
'--' . AdminAccount::KEY_USER => 'user',
'--' . AdminAccount::KEY_PASSWORD => '123123q',
'--' . AdminAccount::KEY_EMAIL => 'test@test.com',
'--' . AdminAccount::KEY_FIRST_NAME => 'John',
'--' . AdminAccount::KEY_LAST_NAME => 'Doe',
];
$data = [
AdminAccount::KEY_USER => 'user',
AdminAccount::KEY_PASSWORD => '123123q',
AdminAccount::KEY_EMAIL => 'test@test.com',
AdminAccount::KEY_FIRST_NAME => 'John',
AdminAccount::KEY_LAST_NAME => 'Doe',
InitParamListener::BOOTSTRAP_PARAM => null,
];
$commandTester = new CommandTester($this->command);
$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, ['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()
);
}
/**
* @param int $mode
* @param string $description
* @dataProvider getOptionListDataProvider
*/
public function testGetOptionsList($mode, $description)
{
/* @var $argsList \Symfony\Component\Console\Input\InputArgument[] */
$argsList = $this->command->getOptionsList($mode);
$this->assertEquals(AdminAccount::KEY_EMAIL, $argsList[2]->getName());
$this->assertEquals($description, $argsList[2]->getDescription());
}
/**
* @return array
*/
public function getOptionListDataProvider()
{
return [
[
'mode' => InputOption::VALUE_REQUIRED,
'description' => '(Required) Admin email',
],
[
'mode' => InputOption::VALUE_OPTIONAL,
'description' => 'Admin email',
],
];
}
/**
* @dataProvider validateDataProvider
* @param bool[] $options
* @param string[] $errors
*/
public function testValidate(array $options, array $errors)
{
$inputMock = $this->getMockForAbstractClass(
\Symfony\Component\Console\Input\InputInterface::class,
[],
'',
false
);
$index = 0;
foreach ($options as $option) {
$inputMock->expects($this->at($index++))->method('getOption')->willReturn($option);
}
$this->assertEquals($errors, $this->command->validate($inputMock));
}
/**
* @return array
*/
public function validateDataProvider()
{
return [
[
[null, 'Doe', 'admin', 'test@test.com', '123123q', '123123q'],
['"First Name" is required. Enter and try again.']
],
[
['John', null, null, 'test@test.com', '123123q', '123123q'],
['"User Name" is required. Enter and try again.', '"Last Name" is required. Enter and try again.'],
],
[['John', 'Doe', 'admin', null, '123123q', '123123q'], ['Please enter a valid email.']],
[
['John', 'Doe', 'admin', 'test', '123123q', '123123q'],
["'test' is not a valid email address in the basic format local-part@hostname"]
],
[
['John', 'Doe', 'admin', 'test@test.com', '', ''],
[
'Password is required field.',
'Your password must be at least 7 characters.',
'Your password must include both numeric and alphabetic characters.'
]
],
[
['John', 'Doe', 'admin', 'test@test.com', '123123', '123123'],
[
'Your password must be at least 7 characters.',
'Your password must include both numeric and alphabetic characters.'
]
],
[
['John', 'Doe', 'admin', 'test@test.com', '1231231', '1231231'],
['Your password must include both numeric and alphabetic characters.']
],
[['John', 'Doe', 'admin', 'test@test.com', '123123q', '123123q'], []],
];
}
}