Skip to content

Commit bcfe152

Browse files
committed
bug #24141 [DomCrawler] Fix conversion to int on GetPhpFiles (MaraBlaga)
This PR was merged into the 2.7 branch. Discussion ---------- [DomCrawler] Fix conversion to int on GetPhpFiles | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | We've encountered that when using the DomCrawler with the UploadedFile, everything gets converted into strings. We've addressed the issue, and made sure that the attributes in the UploadedFile respects their type. The code below demonstrates the problem. ```<?php require 'vendor/autoload.php'; require 'app/AppKernel.php'; $crawler = new \Symfony\Component\DomCrawler\Crawler( '<form method="post"><input type="file" name="image"/></form>', 'http://www.example.com' ); $form = $crawler->filter('form')->form(); $form['image'] = new \Symfony\Component\HttpFoundation\File\UploadedFile( 'path/to/file', 'foo', 'text/plain', 100 ); $client = new \Symfony\Bundle\FrameworkBundle\Client(new AppKernel('test', true)); $crawler = $client->submit($form); var_dump($client->getRequest()->files->get('image')->getClientSize()); //returns string, not int echo 'Done.' . PHP_EOL; Commits ------- 122da5a [DomCrawler] Fix conversion to int on GetPhpFiles
2 parents 47871c9 + 122da5a commit bcfe152

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Symfony/Component/DomCrawler/Form.php

+12
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ public function getPhpFiles()
172172
if (!empty($qs)) {
173173
parse_str($qs, $expandedValue);
174174
$varName = substr($name, 0, strlen(key($expandedValue)));
175+
176+
array_walk_recursive(
177+
$expandedValue,
178+
function (&$value, $key) {
179+
if (ctype_digit($value) && ('size' === $key || 'error' === $key)) {
180+
$value = (int) $value;
181+
}
182+
}
183+
);
184+
185+
reset($expandedValue);
186+
175187
$values = array_replace_recursive($values, array($varName => current($expandedValue)));
176188
}
177189
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,15 @@ public function testGetPhpFiles()
456456

457457
$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>');
458458
$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');
459+
460+
$form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
461+
$files = $form->getPhpFiles();
462+
463+
$this->assertSame(0, $files['foo']['bar']['size'], '->getPhpFiles() converts size to int');
464+
$this->assertSame(4, $files['foo']['bar']['error'], '->getPhpFiles() converts error to int');
465+
466+
$form = $this->createForm('<form method="post"><input type="file" name="size[error]" /><input type="text" name="error" value="error" /><input type="submit" /></form>');
467+
$this->assertEquals(array('size' => array('error' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0))), $form->getPhpFiles(), '->getPhpFiles() int conversion does not collide with file names');
459468
}
460469

461470
/**

0 commit comments

Comments
 (0)