Skip to content

Commit 79e92f9

Browse files
committed
bug symfony#10444 [DomCrawler] Fixed incorrect value name conversion in getPhpValues() and getPhpFiles() (romainneutron)
This PR was merged into the 2.3 branch. Discussion ---------- [DomCrawler] Fixed incorrect value name conversion in getPhpValues() and getPhpFiles() | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#6908 | License | MIT This PR replaces symfony#10193 Commits ------- 89c599e [DomCrawler] Add tests for recursive cases of getPhpValues() and getPhpFiles() e961f57 [DomCrawler] Fixed incorrect value name conversion in getPhpValues() and getPhpFiles()
2 parents 9213a82 + 89c599e commit 79e92f9

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/Symfony/Component/DomCrawler/Form.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,13 @@ public function getFiles()
143143
*/
144144
public function getPhpValues()
145145
{
146-
$qs = http_build_query($this->getValues(), '', '&');
147-
parse_str($qs, $values);
146+
$values = array();
147+
foreach ($this->getValues() as $name => $value) {
148+
$qs = http_build_query(array($name => $value), '', '&');
149+
parse_str($qs, $expandedValue);
150+
$varName = substr($name, 0, strlen(key($expandedValue)));
151+
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
152+
}
148153

149154
return $values;
150155
}
@@ -161,8 +166,13 @@ public function getPhpValues()
161166
*/
162167
public function getPhpFiles()
163168
{
164-
$qs = http_build_query($this->getFiles(), '', '&');
165-
parse_str($qs, $values);
169+
$values = array();
170+
foreach ($this->getFiles() as $name => $value) {
171+
$qs = http_build_query(array($name => $value), '', '&');
172+
parse_str($qs, $expandedValue);
173+
$varName = substr($name, 0, strlen(key($expandedValue)));
174+
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
175+
}
166176

167177
return $values;
168178
}
@@ -398,8 +408,7 @@ private function initialize()
398408

399409
// restore the original name of the input node
400410
$this->button->setAttribute('name', $name);
401-
}
402-
else {
411+
} else {
403412
$this->set(new Field\InputFormField($document->importNode($this->button, true)));
404413
}
405414
}

src/Symfony/Component/DomCrawler/Tests/FormTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@ public function testGetPhpValues()
391391
{
392392
$form = $this->createForm('<form><input type="text" name="foo[bar]" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
393393
$this->assertEquals(array('foo' => array('bar' => 'foo'), 'bar' => 'bar'), $form->getPhpValues(), '->getPhpValues() converts keys with [] to arrays');
394+
395+
$form = $this->createForm('<form><input type="text" name="fo.o[ba.r]" value="foo" /><input type="text" name="ba r" value="bar" /><input type="submit" /></form>');
396+
$this->assertEquals(array('fo.o' => array('ba.r' => 'foo'), 'ba r' => 'bar'), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names');
397+
398+
$form = $this->createForm('<form><input type="text" name="fo.o[ba.r][]" value="foo" /><input type="text" name="fo.o[ba.r][ba.z]" value="bar" /><input type="submit" /></form>');
399+
$this->assertEquals(array('fo.o' => array('ba.r' => array('foo', 'ba.z' => 'bar'))), $form->getPhpValues(), '->getPhpValues() preserves periods and spaces in names recursively');
394400
}
395401

396402
public function testGetFiles()
@@ -418,6 +424,12 @@ public function testGetPhpFiles()
418424
{
419425
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
420426
$this->assertEquals(array('foo' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() converts keys with [] to arrays');
427+
428+
$form = $this->createForm('<form method="post"><input type="file" name="f.o o[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
429+
$this->assertEquals(array('f.o o' => array('bar' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() preserves periods and spaces in names');
430+
431+
$form = $this->createForm('<form method="post"><input type="file" name="f.o o[bar][ba.z]" /><input type="file" name="f.o o[bar][]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
432+
$this->assertEquals(array('f.o o' => array('bar' => array('ba.z' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0), array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)))), $form->getPhpFiles(), '->getPhpFiles() preserves periods and spaces in names recursively');
421433
}
422434

423435
/**

0 commit comments

Comments
 (0)