Development

Changeset 13322

You must first sign up to be able to contribute.

Changeset 13322

Show
Ignore:
Timestamp:
11/24/08 23:20:35 (7 months ago)
Author:
fabien
Message:

[1.2] fixed routing when a route parameter is empty (closes #5001, #4960, #5042)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/lib/routing/sfRoute.class.php

    r12976 r13322  
    151151 
    152152    // all $variables must be defined in the $tparams array 
    153     if (array_diff_key($this->variables, array_filter($tparams))) 
     153    if (array_diff_key($this->variables, $tparams)) 
    154154    { 
    155155      return false; 
     
    159159    foreach (array_keys($this->variables) as $variable) 
    160160    { 
    161       if (!is_null($tparams[$variable]) && !preg_match('#'.$this->requirements[$variable].'#', $tparams[$variable])) 
     161      if (!$tparams[$variable]) 
     162      { 
     163        continue; 
     164      } 
     165 
     166      if (!preg_match('#'.$this->requirements[$variable].'#', $tparams[$variable])) 
    162167      { 
    163168        return false; 
     
    168173    if (!$this->options['extra_parameters_as_query_string']) 
    169174    { 
    170       if (false === strpos($this->regex, '<_star>') && array_diff_key(array_filter($params), $this->variables, $defaults)) 
     175      if (false === strpos($this->regex, '<_star>') && array_diff_key($params, $this->variables, $defaults)) 
    171176      { 
    172177        return false; 
     
    175180 
    176181    // check that $params does not override a default value that is not a variable 
    177     foreach (array_filter($defaults) as $key => $value) 
     182    foreach ($defaults as $key => $value) 
    178183    { 
    179184      if (!isset($this->variables[$key]) && $tparams[$key] != $value) 
     
    207212 
    208213    // all params must be given 
    209     if ($diff = array_diff_key($this->variables, array_filter($tparams))) 
     214    if ($diff = array_diff_key($this->variables, $tparams)) 
    210215    { 
    211216      throw new InvalidArgumentException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $this->pattern, implode(', ', $diff))); 
     
    498503      { 
    499504        // a text 
    500         $this->tokens[] = array('text', $currentSeparator, $match[1]); 
     505        $this->tokens[] = array('text', $currentSeparator, $match[1], null); 
    501506 
    502507        $currentSeparator = ''; 
     
    507512      { 
    508513        // a separator 
    509         $this->tokens[] = array('separator', $currentSeparator, $match[0]); 
     514        $this->tokens[] = array('separator', $currentSeparator, $match[0], null); 
    510515 
    511516        $currentSeparator = $match[0]; 
  • branches/1.2/test/unit/routing/sfRouteTest.php

    r12969 r13322  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(43, new lime_output_color()); 
     13$t = new lime_test(49, new lime_output_color()); 
    1414 
    1515// ->matchesUrl() 
     
    3131$t->is($route->matchesUrl('/'), array('foo' => 'bar'), '->matchesUrl() matches routes with an optional parameter at the end'); 
    3232 
     33$route = new sfRoute('/:foo', array('foo' => null)); 
     34$t->is($route->matchesUrl('/'), array('foo' => null), '->matchesUrl() matches routes with an optional parameter at the end, even if it is null'); 
     35 
     36$route = new sfRoute('/:foo', array('foo' => '')); 
     37$t->is($route->matchesUrl('/'), array('foo' => ''), '->matchesUrl() matches routes with an optional parameter at the end, even if it is empty'); 
     38 
     39$route = new sfRoute('/:foo/bar', array('foo' => null)); 
     40$t->is($route->matchesUrl('//bar'), false, '->matchesUrl() does not match routes with an empty parameter not at the end'); 
     41$t->is($route->matchesUrl('/bar'), false, '->matchesUrl() does not match routes with an empty parameter not at the end'); 
     42 
    3343$route = new sfRoute('/foo/:foo/bar/:bar', array('foo' => 'bar', 'bar' => 'foo')); 
    3444$t->is($route->matchesUrl('/foo/bar/bar'), array('foo' => 'bar', 'bar' => 'foo'), '->matchesUrl() matches routes with an optional parameter at the end'); 
     
    7080 
    7181$route = new sfRoute('/:foo'); 
    72 $t->is($route->matchesParameters(array('foo' => '')), false, '->matchesParameters() checks that parameters are not empty'); 
     82$t->is($route->matchesParameters(array('foo' => '')), true, '->matchesParameters() matches if optional parameters empty'); 
     83$t->is($route->matchesParameters(array('foo' => null)), true, '->matchesParameters() matches if optional parameters empty'); 
     84 
     85/* 
     86$route = new sfRoute('/:foo/bar'); 
     87$t->is($route->matchesParameters(array('foo' => '')), false, '->matchesParameters() does not match is required parameters are empty'); 
     88$t->is($route->matchesParameters(array('foo' => null)), false, '->matchesParameters() does not match is required parameters are empty'); 
     89*/ 
    7390 
    7491$route = new sfRoute('/:foo'); 
     
    105122 
    106123$route = new sfRoute('/:foo'); 
     124$t->is($route->generate(array('foo' => '')), '/', '->generate() generates a route if a variable is empty'); 
     125$t->is($route->generate(array('foo' => null)), '/', '->generate() generates a route if a variable is empty'); 
     126/* 
     127$route = new sfRoute('/:foo/bar'); 
    107128try 
    108129{ 
    109130  $route->generate(array('foo' => '')); 
    110   $t->fail('->generate() cannot generate a route if a variable is empty'); 
     131  $t->fail('->generate() cannot generate a route if a variable is empty and mandatory'); 
    111132} 
    112133catch (Exception $e) 
    113134{ 
    114   $t->pass('->generate() cannot generate a route if a variable is empty'); 
    115 
    116  
     135  $t->pass('->generate() cannot generate a route if a variable is empty and mandatory'); 
     136
     137try 
     138
     139  $route->generate(array('foo' => null)); 
     140  $t->fail('->generate() cannot generate a route if a variable is empty and mandatory'); 
     141
     142catch (Exception $e) 
     143
     144  $t->pass('->generate() cannot generate a route if a variable is empty and mandatory'); 
     145
     146*/ 
    117147$route = new sfRoute('/:foo'); 
    118148$t->is($route->generate(array('foo' => 'bar', 'bar' => 'foo')), '/bar?bar=foo', '->generate() generates extra parameters as a query string'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.