Skip to content

Adding prompts to admin:user:create cli command#2690

Closed
steverobbins wants to merge 2 commits intomagento:developfrom
steverobbins:feature/admin-user-create-prompts
Closed

Adding prompts to admin:user:create cli command#2690
steverobbins wants to merge 2 commits intomagento:developfrom
steverobbins:feature/admin-user-create-prompts

Conversation

@steverobbins
Copy link
Copy Markdown
Contributor

@steverobbins steverobbins commented Dec 11, 2015

This will prompt for the admin info if not given in the options:

$ ./bin/magento admin:user:create
Admin Firstname: Foo
Admin Lastname: Bar
Admin User: foobar
Admin Email: foobar@example.com
Admin Password: 
Created Magento administrator user named foobar

@Vinai
Copy link
Copy Markdown
Contributor

Vinai commented Dec 15, 2015

Thank you for the PR! We are currently waiting for the travis builds to work again, which should happen in a few days. Once that is running and the tests are green we can continue to process your contribution.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getOption() verified that such option is supported and that it is set. Accessing it directly in the array can be risky because there is no guarantee that such key exists. I'd suggest to add some validation for all such cases. Maybe a new private method can be dedicated to it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point of execution it is guaranteed that this key exists in the array. It has gone through the optional prompting step and has been validated with addUserInfoRules().

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally against this approach in this particular case, so I'm ok, if it will be as is.
Just, in general, I'd prefer to not rely on internal logic of a method, but rather rely on the interface/signature. Even, if it is in the same class. Sometimes, it may happen that different developers touch the code and they may miss usage of data returned by a method.
Also, in this case, the method is protected, which means that it's behavior may be modified (close to 0 chance in this particular case, but I'm saying in general).

Again, it's ok to leave it as is in this case.

@steverobbins steverobbins force-pushed the feature/admin-user-create-prompts branch from 90f50b2 to fbd3313 Compare December 18, 2015 21:44
@steverobbins
Copy link
Copy Markdown
Contributor Author

@buskamuza I've addressed two of your comments in an update commit.

@buskamuza
Copy link
Copy Markdown
Contributor

@steverobbins , thanks for the update! I hope, we'll merge the PR soon.

@mazhalai
Copy link
Copy Markdown
Contributor

@steverobbins Please merge latest from develop and rerun builds.

@vancoz
Copy link
Copy Markdown

vancoz commented Jan 12, 2016

Hello @steverobbins, would be nice to add tescase into setup/src/Magento/Setup/Test/Unit/Console/Command/AdminUserCreateCommandTest.php according to your changes.

Thank you.

@steverobbins steverobbins force-pushed the feature/admin-user-create-prompts branch from 99d1c88 to 91f93d1 Compare January 12, 2016 23:07
@steverobbins
Copy link
Copy Markdown
Contributor Author

Rebased on develop.

Looks like there are some issues with the validate() method as it's parameter type changed. I will address this and @vancoz's comment shortly.

@vancoz vancoz self-assigned this Jan 14, 2016
@vancoz vancoz added Progress: accept Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development and removed Progress: needs update labels Jan 14, 2016
@vancoz
Copy link
Copy Markdown

vancoz commented Jan 14, 2016

Internal ticket - MAGETWO-49808.
@steverobbins please fix unittests.

@vancoz vancoz added improvement Progress: needs update and removed Progress: accept Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development labels Jan 14, 2016
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think we can change the signature of this method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do anything if we set our minds to it 😛

Yes, I'm having a rethink on my implementation that will also address @buskamuza's array access comment.

@steverobbins
Copy link
Copy Markdown
Contributor Author

Still working through this (see latest commit), but it's turning into a much bigger thing that I'm not sure you'll accept.

In order to keep the signature of the validate() method the same, I need to manipulate the InputInterface to set values with prompts. This requires a custom Input since $definition is protected. Otherwise validate() will always return errors even though we're getting values from prompts.

This seems to work in practice but there's still work to do due to how CommandTester instantiates the input class, which means I'll need to create our own CommandTester class, and a new Input that extends ArrayInput instead of ArgvInput.

@vancoz
Copy link
Copy Markdown

vancoz commented Jan 21, 2016

@steverobbins you are right, this is significant change. Looks like we have no good way how to implement user interaction now. Let us discuss this internally.

@vrann
Copy link
Copy Markdown
Contributor

vrann commented Mar 22, 2017

@steverobbins can you please merge this PR with the latest develop? Going to work on accepting it.

@steverobbins steverobbins force-pushed the feature/admin-user-create-prompts branch from 0d2ee5f to 7e07366 Compare March 22, 2017 18:24
@steverobbins
Copy link
Copy Markdown
Contributor Author

@vrann Squashed and rebased... Let's see if tests still pass a year later 😟

@vrann
Copy link
Copy Markdown
Contributor

vrann commented Mar 23, 2017

@steverobbins you have couple failures. Also adding one comment to the code

@vrann vrann self-requested a review March 23, 2017 00:01
];
foreach ($keys as $key) {
if (!$input->getOption($key)) {
$input->promptForOption($dialog, $output, $key, $key === AdminAccount::KEY_PASSWORD);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check that input actually implements the interface which has promptForOption method

*/
protected function setMissingValues(InputInterface &$input, OutputInterface $output)
{
if (!method_exists($input, 'promptForOption')) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for not being more specific earlier, but better way to achieve this is to introduce new interface, i.e. InputInteractiveInterface, which extends InputInterface. Then here either check is $input an instance of this interface or even better do type hinting to InputInteractiveInterface in the method signature. The latter is not always possible, so either way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify the question please? Literally, there should be an interface with the promptForOption method and here you should either have it in protected function setMissingValues(InputInteractiveInterface &$input, OutputInterface $output) (better) or call instanceof (still ok).

Copy link
Copy Markdown
Contributor

@vrann vrann Mar 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know does this makes sense to you. I don't want to add too much work :)

@okorshenko okorshenko modified the milestones: March 2017, April 2017 Apr 2, 2017
@davidalger
Copy link
Copy Markdown
Member

@steverobbins Sorry this has been open so long. I'm attempting to resurrect this and get it merged in, but it looks like the test this PR adds is failing:

vagrant@dev-web70:05:11 PM:/var/www/sites/m2engcom-ce.dev/dev/tests/unit$ ../../../vendor/phpunit/phpunit/phpunit ../../../setup/src/Magento/Setup/Test/Unit/Console/Command
PHPUnit 4.1.0 by Sebastian Bergmann.

Configuration read from /var/www/sites/m2engcom-ce.dev/dev/tests/unit/phpunit.xml.dist

..........F....................................................  63 / 136 ( 46%)
............................................................... 126 / 136 ( 92%)
..........

Time: 1.32 seconds, Memory: 16.00MB

There was 1 failure:

1) Magento\Setup\Test\Unit\Console\Command\AdminUserCreateCommandTest::testExecuteWithPrompts
Failed asserting that 'User Name is a required field.
First Name is a required field.
Last Name is a required field.
Please enter a valid email.
Password is required field.
Your password must be at least 7 characters.
Your password must include both numeric and alphabetic characters.
' contains "Created Magento administrator user named user".

/var/www/sites/m2engcom-ce.dev/setup/src/Magento/Setup/Test/Unit/Console/Command/AdminUserCreateCommandTest.php:145
                                         
FAILURES!                                
Tests: 136, Assertions: 488, Failures: 1.

In my manual test of the command, it appears to be working, so presumably it's the test that is broken. Can you fix this?

@steverobbins
Copy link
Copy Markdown
Contributor Author

I don't know when I'll be able to get to this, someone else might want to take over.

@okorshenko okorshenko self-assigned this Jun 9, 2017
@okorshenko
Copy link
Copy Markdown
Contributor

Closing this PR.

@okorshenko okorshenko closed this Jun 9, 2017
@magento magento locked and limited conversation to collaborators Jun 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Component: Setup improvement Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Projects

None yet

Development

Successfully merging this pull request may close these issues.