bug #22718 [Console] Fixed different behaviour of key and value user inputs in multiple choice question (borNfreee)

This PR was squashed before being merged into the 2.7 branch (closes #22718).

Discussion
----------

[Console] Fixed different behaviour of key and value user inputs in multiple choice question

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22706
| License       | MIT
| Doc PR        | -

Fixed a bug when value from multiple choice list could not be selected by user's input
while it could be selected by typing its index in the list.

Commits
-------

2861bd7b01 [Console] Fixed different behaviour of key and value user inputs in multiple choice question
This commit is contained in:
Fabien Potencier 2017-05-27 07:10:28 -07:00
commit 1d1d997b5b
2 changed files with 32 additions and 1 deletions

View File

@ -135,7 +135,7 @@ class ChoiceQuestion extends Question
if ($multiselect) {
// Check for a separated comma values
if (!preg_match('/^[a-zA-Z0-9_-]+(?:,[a-zA-Z0-9_-]+)*$/', $selectedChoices, $matches)) {
if (!preg_match('/^[^,]+(?:,[^,]+)*$/', $selectedChoices, $matches)) {
throw new \InvalidArgumentException(sprintf($errorMessage, $selected));
}
$selectedChoices = explode(',', $selectedChoices);

View File

@ -273,6 +273,37 @@ class QuestionHelperTest extends TestCase
);
}
/**
* @dataProvider specialCharacterInMultipleChoice
*/
public function testSpecialCharacterChoiceFromMultipleChoiceList($providedAnswer, $expectedValue)
{
$possibleChoices = array(
'.',
'src',
);
$dialog = new QuestionHelper();
$dialog->setInputStream($this->getInputStream($providedAnswer."\n"));
$helperSet = new HelperSet(array(new FormatterHelper()));
$dialog->setHelperSet($helperSet);
$question = new ChoiceQuestion('Please select the directory', $possibleChoices);
$question->setMaxAttempts(1);
$question->setMultiselect(true);
$answer = $dialog->ask($this->createInputInterfaceMock(), $this->createOutputInterface(), $question);
$this->assertSame($expectedValue, $answer);
}
public function specialCharacterInMultipleChoice()
{
return array(
array('.', array('.')),
array('., src', array('.', 'src')),
);
}
/**
* @dataProvider mixedKeysChoiceListAnswerProvider
*/