Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[DomCrawler] FileFormField::setValue ignores file extension #4674

Closed
zapletalitstudio opened this Issue Jun 28, 2012 · 3 comments

Comments

Projects
None yet
3 participants

This function creates unique temp name, so it doesn't preserve name and extension of file passed in $value param. It's a problem if using DomCrawler to simulate file uploads (Behat + Mink). Can you fix it or is there any solution now? Thanks

Owner

fabpot commented Jul 10, 2012

This is the same behavior as PHP. The tmp_name is a random string that does not contain the original file extension, but the name entry is the name of the uploaded file with the extension included.

@fabpot fabpot closed this Jul 10, 2012

Contributor

hason commented Aug 17, 2012

@fabpot, @everzet: The problem is that DOMCrawler simulates HTTP request only on server side. But if we want create request on client side (for example for Goutte), DOMCrawler doesn't allowed to create new file with random name, but it have to use original file. I suggest this changes:

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DomCrawler/Field/FileFormField.php#L40

<?php
    /**
     * Upload the file of the field.
     *
     * @param string $value The value of the field
     *
     * @api
     */
    public function upload($value)
    {
        if (null !== $value && is_readable($value)) {
            $error = UPLOAD_ERR_OK;
            $size = filesize($value);
            $name = basename($value);

            // copy to a tmp location
            $tmp = tempnam(sys_get_temp_dir(), 'upload');
            unlink($tmp);
            copy($value, $tmp);
            $value = $tmp;
        } else {
            $error = UPLOAD_ERR_NO_FILE;
            $size = 0;
            $name = '';
            $value = '';
        }

        $this->value = array('name' => $name, 'type' => '', 'tmp_name' => $value, 'error' => $error, 'size' => $size);
    }

    /**
     * Sets the value of the field.
     *
     * @param string $value The value of the field
     */
    public function setValue($value, $raw = false)
    {
        if ($raw) {
            parent::setValue($value);
        } else {
            $this->upload($value);
        }
    }

https://github.com/fabpot/Goutte/blob/master/Goutte/Client.php#L140

<?php
    protected function addPostFiles($request, array $files, $arrayName = '')
    {
        if (!$request instanceof EntityEnclosingRequestInterface) {
            return;
        }

        foreach ($files as $name => $info) {
            if (!empty($arrayName)) {
                $name = $arrayName . '[' . $name . ']';
            }

            if (isset($info['tmp_name']) && '' !== $info['tmp_name']) {
                $request->addPostFile($name, $info['tmp_name']);
            } elseif (is_array($info)) {
                $this->addPostFiles($request, $info, $name);
+          } else {
+              $request->addPostFile($name, $info);
+          }
        }
    }

https://github.com/Behat/MinkGoutteDriver/blob/master/src/Behat/Mink/Driver/GoutteDriver.php#L71

<?php
    /**
     * Attaches file path to file field located by it's XPath query.
     *
     * @param string $xpath
     * @param string $path
     */
    public function attachFile($xpath, $path)
    {
        $this->getFormField($xpath)->setValue($path, true);
    }

What do you think? Can I prepare pull request?

@fabpot fabpot added a commit that referenced this issue Oct 14, 2012

@fabpot fabpot merged branch hason/crawler (PR #5407)
This PR was merged into the master branch.

Commits
-------

c902966 [DomCrawler] Added ability to set file as raw path to file field

Discussion
----------

[2.2][DomCrawler] Added ability to set file as raw path to file field

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
License of the code: MIT

For description see #4674 (#4674 (comment))

Related PRs:

Behat/MinkBrowserKitDriver#1
https://github.com/Behat/MinkGoutteDriver/pull/7
FriendsOfPHP/Goutte#77

---------------------------------------------------------------------------

by stof at 2012-10-13T21:53:27Z

@fabpot anything missing here ?
0fc0fb3

@hason hason referenced this issue in minkphp/MinkGoutteDriver Oct 16, 2012

Merged

Attach file as path to file #7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment