Development

Changeset 6612

You must first sign up to be able to contribute.

Changeset 6612

Show
Ignore:
Timestamp:
12/20/07 15:22:05 (1 year ago)
Author:
hartym
Message:

sfBrowser: allow file uploads in sfBrowser/functional tests

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/util/sfBrowser.class.php

    r6551 r6612  
    2828    $cookieJar          = array(), 
    2929    $fields             = array(), 
     30    $files              = array(), 
    3031    $vars               = array(), 
    3132    $defaultServerArray = array(), 
     
    137138      $_GET  = $parameters; 
    138139    } 
     140 
     141    // handle input type="file" fields 
     142    if (count($this->files)) 
     143    { 
     144      $_FILES = $this->files; 
     145    } 
     146    $this->files = array(); 
     147 
    139148    parse_str($query_string, $qs); 
    140149    if (is_array($qs)) 
     
    351360    // merge form default values and arguments 
    352361    $defaults = array(); 
     362    $arguments = sfToolkit::arrayDeepMerge($this->fields, $arguments); 
     363 
    353364    foreach ($xpath->query('descendant::input | descendant::textarea | descendant::select', $form) as $element) 
    354365    { 
     
    356367      $nodeName    = $element->nodeName; 
    357368      $value       = null; 
     369 
    358370      if ($nodeName == 'input' && ($element->getAttribute('type') == 'checkbox' || $element->getAttribute('type') == 'radio')) 
    359371      { 
     
    362374          $value = $element->getAttribute('value'); 
    363375        } 
     376      } 
     377      else if ($nodeName == 'input' && $element->getAttribute('type') == 'file') 
     378      { 
     379        $filename = isset($arguments[$elementName]) ? $arguments[$elementName] : ''; 
     380 
     381        if (is_readable($filename)) 
     382        { 
     383          $fileError = UPLOAD_ERR_OK; 
     384          $fileSize = filesize($filename); 
     385        } 
     386        else 
     387        { 
     388          $fileError = UPLOAD_ERR_NO_FILE; 
     389          $fileSize = 0; 
     390        } 
     391 
     392        $this->files[$elementName] = array('name' => basename($filename), 'type' => '', 'tmp_name' => $filename, 'error' => $fileError, 'size' => $fileSize); 
    364393      } 
    365394      else if ( 
     
    424453 
    425454    // create request parameters 
    426     $arguments = sfToolkit::arrayDeepMerge($defaults, $this->fields, $arguments); 
     455    $arguments = sfToolkit::arrayDeepMerge($defaults, $arguments); 
    427456    if ('post' == $method) 
    428457    { 
  • branches/1.1/test/unit/util/sfBrowserTest.php

    r6130 r6612  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(43, new lime_output_color()); 
     13$t = new lime_test(50, new lime_output_color()); 
    1414 
    1515// ->click() 
     
    2222    $this->dom->validateOnParse = true; 
    2323    $this->dom->loadHTML($html); 
     24  } 
     25 
     26  public function getFiles() 
     27  { 
     28    $f = $this->files; 
     29    $this->files = array(); 
     30 
     31    return $f; 
    2432  } 
    2533 
     
    6876      <input name="myarray[]" value="value2" /> 
    6977      <input name="myarray[]" value="value3" /> 
     78      <input type="file" name="myfile" /> 
    7079      <input type="button" name="mybutton" value="mybuttonvalue" /> 
    7180      <input type="submit" name="submit" value="submit" /> 
     
    100109      </span></div> 
    101110    </form> 
     111 
    102112  </body> 
    103113</html> 
     
    218228$b->call('http://app-test/index.phpmain/index'); 
    219229$t->is($b->getDefaultServerArray('HTTPS'), null, '->call() preserve non-secure requests'); 
     230 
     231// file uploads 
     232$t->diag('file uploads'); 
     233 
     234$unexistentFilename = sfConfig::get('sf_test_cache_dir') . DIRECTORY_SEPARATOR . 'unexistent-file-'.md5(getmypid().'-'.microtime()); 
     235$existentFilename =  sfConfig::get('sf_test_cache_dir') . DIRECTORY_SEPARATOR . 'existent-file-'.md5(getmypid().'-'.microtime()); 
     236file_put_contents($existentFilename, 'test'); 
     237 
     238list($method, $uri, $parameters) = $b->click('submit', array('myfile'=>$unexistentFilename)); 
     239$files = $b->getFiles(); 
     240$t->is($method, 'post', 'file upload is using right method'); 
     241$t->is($parameters['myfile'], $unexistentFilename, 'file upload kept _REQUEST variable available @todo check'); 
     242$t->is(isset($files['myfile'])&&is_array($files['myfile']), true, 'file upload set up a _FILE entry for our test file'); 
     243$t->is(array_keys($files['myfile']), array('name','type','tmp_name','error','size'), 'file upload returns correctly formatted array'); 
     244$t->is($files['myfile']['error'], UPLOAD_ERR_NO_FILE, 'unexistent file does not exists (UPLOAD_ERR_NO_FILE)'); 
     245 
     246list($method, $uri, $parameters) = $b->click('submit', array('myfile'=>$existentFilename)); 
     247$files = $b->getFiles(); 
     248 
     249$t->is($files['myfile']['error'], UPLOAD_ERR_OK, 'existent file exists (UPLOAD_ERR_OK)'); 
     250$t->is($files['myfile']['name'], basename($existentFilename), 'name key ok'); 
     251