Development

Changeset 13023

You must first sign up to be able to contribute.

Changeset 13023

Show
Ignore:
Timestamp:
11/16/08 01:25:26 (8 months ago)
Author:
FabianLange
Message:

[1.2] added basic implementation of select()/deselect() support to test browser for changing selection in check and radiobuttons. Closes #4673

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/lib/test/sfTestFunctionalBase.class.php

    r12996 r13023  
    244244 
    245245    $this->browser->call($uri, $method, $parameters, $changeStack); 
     246 
     247    return $this; 
     248  } 
     249 
     250  /** 
     251   * Simulates deselecting a checkbox or radiobutton. 
     252   * 
     253   * @param string  $name       The checkbox or radiobutton id, name or text 
     254   * 
     255   * @return sfBrowser 
     256   */ 
     257  public function deselect($name) 
     258  { 
     259    $this->browser->doSelect($name, false); 
     260 
     261    return $this; 
     262  } 
     263 
     264  /** 
     265   * Simulates selecting a checkbox or radiobutton. 
     266   * 
     267   * @param string  $name       The checkbox or radiobutton id, name or text 
     268   * 
     269   * @return sfBrowser 
     270   */ 
     271  public function select($name) 
     272  { 
     273    $this->browser->doSelect($name, true); 
    246274 
    247275    return $this; 
  • branches/1.2/lib/util/sfBrowserBase.class.php

    r12853 r13023  
    540540 
    541541  /** 
     542   * Simulates deselecting a checkbox or radiobutton. 
     543   * 
     544   * @param string  $name       The checkbox or radiobutton id, name or text 
     545   * 
     546   * @return sfBrowser 
     547   * 
     548   * @see    doSelect() 
     549   */ 
     550  public function deselect($name) 
     551  { 
     552    $this->doSelect($name, false); 
     553 
     554    return $this; 
     555  } 
     556 
     557  /** 
     558   * Simulates selecting a checkbox or radiobutton. 
     559   * 
     560   * @param string  $name       The checkbox or radiobutton id, name or text 
     561   * 
     562   * @return sfBrowser 
     563   * 
     564   * @see    doSelect() 
     565   */ 
     566  public function select($name) 
     567  { 
     568    $this->doSelect($name, true); 
     569 
     570    return $this; 
     571  } 
     572 
     573  /** 
     574   * Simulates selecting a checkbox or radiobutton. 
     575   * 
     576   * This method is called internally by the select() and deselect() methods. 
     577   * 
     578   * @param string  $name       The checkbox or radiobutton id, name or text 
     579   * @param boolean $selected   If true the item will be selected 
     580   * 
     581   */ 
     582  public function doSelect($name, $selected) 
     583  { 
     584    $position = 0; 
     585    $dom = $this->getResponseDom(); 
     586 
     587    if (!$dom) 
     588    { 
     589      throw new LogicException('Cannot select because there is no current page in the browser.'); 
     590    } 
     591 
     592    $xpath = new DomXpath($dom); 
     593 
     594    if ($element = $xpath->query(sprintf('//input[(@type="radio" or @type="checkbox") and (.="%s" or @id="%s" or @name="%s")]', $name, $name, $name))->item($position)) 
     595    { 
     596      if ($selected) 
     597      { 
     598        if ($element->getAttribute('type') == 'radio') 
     599        { 
     600          //we need to deselect all other radio buttons with the same name 
     601          foreach ($xpath->query(sprintf('//input[@type="radio" and @name="%s"]', $element->getAttribute('name'))) as $radio) 
     602          { 
     603            $radio->removeAttribute('checked'); 
     604          } 
     605        } 
     606        $element->setAttribute('checked', 'checked'); 
     607      } 
     608      else 
     609      { 
     610        if ($element->getAttribute('type') == 'radio') 
     611        { 
     612          throw new InvalidArgumentException('Radiobutton cannot be deselected - Select another radiobutton to deselect the current.'); 
     613        } 
     614        $element->removeAttribute('checked'); 
     615      } 
     616    } 
     617    else 
     618    { 
     619      throw new InvalidArgumentException(sprintf('Cannot find the "%s" checkbox or radiobutton.', $name)); 
     620    } 
     621  } 
     622 
     623  /** 
    542624   * Simulates a click on a link or button. 
    543625   * 
  • branches/1.2/test/unit/util/sfBrowserTest.php

    r11497 r13023  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(54, new lime_output_color()); 
     13$t = new lime_test(59, new lime_output_color()); 
    1414 
    1515// ->click() 
     
    7979      <input type="checkbox" name="checkbox1" value="checkboxvalue" checked="checked" /> 
    8080      <input type="checkbox" name="checkbox2" checked="checked" /> 
     81      <input type="checkbox" name="checkbox3" /> 
     82      <input type="radio" name="radio1" value="a" id="a-radio" /> 
     83      <input type="radio" name="radio1" value="b" id="b-radio" /> 
    8184      <input type="button" name="mybutton" value="mybuttonvalue" /> 
    8285      <input type="submit" name="submit" value="submit" /> 
     
    234237$t->is($parameters['text'], 'yourothervalue', '->setField() is overriden by parameters from click call'); 
    235238 
     239// ->deselect()/select() 
     240$t->diag('->deselect()/select()'); 
     241list($method, $uri, $parameters) = $b-> 
     242  deselect('checkbox1')-> 
     243  select('checkbox3')-> 
     244  select('b-radio')-> 
     245  click('submit') 
     246; 
     247$t->is(isset($parameters['checkbox1']), false, '->deselect() unckecks a checkbox'); 
     248$t->is(isset($parameters['checkbox3']), true, '->select() ckecks a checkbox'); 
     249$t->is($parameters['radio1'], 'b', '->select() selects a radiobutton'); 
     250list($method, $uri, $parameters) = $b-> 
     251  select('a-radio')-> 
     252  click('submit') 
     253; 
     254$t->is($parameters['radio1'], 'a', '->select() toggles radiobuttons'); 
     255 
     256try 
     257{ 
     258  $b->deselect('b-radio'); 
     259  $t->fail('->deselect() cannot deselect radiobuttons'); 
     260} 
     261catch(Exception $e) 
     262{ 
     263  $t->pass('->deselect() cannot deselect radiobuttons'); 
     264} 
     265 
    236266// ->call() 
    237267$t->diag('->call()'); 

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.