[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/tests/cases/tags/form/ -> WactSelectMultipleComponentTest.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  
  10  require_once 'limb/wact/src/components/form/form.inc.php';
  11  
  12  class WactSelectMultipleComponentTest extends WactTemplateTestCase
  13  {
  14    function testGetValue()
  15    {
  16      $template = '<form id="testForm" runat="server">
  17                          <select id="test" name="multiple[]" runat="server" multiple="true"></select>
  18                      </form>';
  19      $this->registerTestingTemplate('/components/form/selectmultiple/getvalue.html', $template);
  20  
  21      $page = $this->initTemplate('/components/form/selectmultiple/getvalue.html');
  22  
  23      $choices = array('red'=>'','green'=>'','blue'=>'');
  24  
  25      $select = $page->getChild('test');
  26  
  27      $select->setSelection($choices);
  28  
  29      $this->assertEqual($select->getValue(),$choices);
  30    }
  31  
  32    function testSetChoicesWithIndex()
  33    {
  34      $template = '<form id="testForm" runat="server">
  35                        <select id="test" name="multiple[]" runat="server" multiple="true"></select>
  36                    </form>';
  37      $this->registerTestingTemplate('/components/form/selectmultiple/setchoiceswithindex.html', $template);
  38  
  39      $page = $this->initTemplate('/components/form/selectmultiple/setchoiceswithindex.html');
  40  
  41      $choices = array('red','green','blue');
  42  
  43      /**

  44      * Note this test must fail right now

  45      * Reflects problem with array key 0 in choices get automatically

  46      * selected

  47      */
  48      $testOut = '';
  49      foreach ( $choices as $key => $choice ) {
  50        $testOut .= '<option value="'.$key.'"';
  51        $testOut .='>'.$choice.'</option>';
  52      }
  53  
  54      $select = $page->getChild('test');
  55  
  56      $select->setChoices($choices);
  57  
  58      ob_start();
  59      $select->renderContents();
  60      $out = ob_get_contents();
  61      ob_end_clean();
  62  
  63      $this->assertEqual($out,$testOut);
  64    }
  65  
  66    function testSetChoicesWithIndexSelected()
  67    {
  68      $template = '<form id="testForm" runat="server">
  69                        <select id="test" name="multiple[]" runat="server" multiple="true"></select>
  70                    </form>';
  71      $this->registerTestingTemplate('/components/form/selectmultiple/setchoiceswithindexselected.html', $template);
  72  
  73      $page = $this->initTemplate('/components/form/selectmultiple/setchoiceswithindexselected.html');
  74  
  75      $choices = array('red','green','blue');
  76      $selected = array(0,1,2);
  77  
  78      $testOut = '';
  79      foreach ( $choices as $key => $choice ) {
  80        $testOut .= '<option value="'.$key.'"';
  81        if ( in_array($key,$selected) ) {
  82          $testOut .= ' selected="true"';
  83        }
  84        $testOut .='>'.$choice.'</option>';
  85      }
  86  
  87      $select = $page->getChild('test');
  88  
  89      $select->setChoices($choices);
  90      $select->setSelection($selected);
  91  
  92      ob_start();
  93      $select->renderContents();
  94      $out = ob_get_contents();
  95      ob_end_clean();
  96  
  97      $this->assertEqual($out,$testOut);
  98    }
  99  
 100    /**

 101    * This test is included to "simulate" a selection received via POST - remember

 102    * PHP will convert integers to strings so strict type checking in the select components

 103    * for selected values will fail

 104    */
 105    function testSetChoicesWithIndexSelectedByForm()
 106    {
 107      $template = '<form id="testForm" runat="server">
 108                        <select id="test" name="multiple[]" runat="server" multiple="true"></select>
 109                    </form>';
 110      $this->registerTestingTemplate('/components/form/selectmultiple/setchoiceswithindexselectedbyform.html', $template);
 111  
 112      $page = $this->initTemplate('/components/form/selectmultiple/setchoiceswithindexselectedbyform.html');
 113  
 114      $form = $page->getChild('testForm');
 115  
 116      $choices = array('red','green','blue');
 117      $selected = array(0,1,2);
 118  
 119      // The selected options will be string values, when a form is actually submitted!

 120      $selectedToString = array('0','1','2');
 121  
 122      $testOut = '';
 123      foreach ( $choices as $key => $choice ) {
 124        $testOut .= '<option value="'.$key.'"';
 125        if ( in_array($key,$selected) ) {
 126          $testOut .= ' selected="true"';
 127        }
 128        $testOut .='>'.$choice.'</option>';
 129      }
 130  
 131      $select = $page->getChild('test');
 132  
 133      $select->setChoices($choices);
 134  
 135      // The selected values typed as strings

 136      $data = new WactArrayObject(array('multiple' => $selectedToString));
 137  
 138      $form->registerDataSource($data);
 139  
 140      ob_start();
 141      $select->renderContents();
 142      $out = ob_get_contents();
 143      ob_end_clean();
 144  
 145      $this->assertEqual($out,$testOut);
 146    }
 147  
 148    function testSetSelectionWithFormValueAsObject()
 149    {
 150      $template = '<form id="testForm" runat="server">
 151                        <select id="test" name="mySelect[]" multiple="true" runat="server"></select>
 152                    </form>';
 153      $this->registerTestingTemplate('/components/form/selectmultiple/set_selection_with_form_value_as_object.html', $template);
 154  
 155      $page = $this->initTemplate('/components/form/selectmultiple/set_selection_with_form_value_as_object.html');
 156  
 157      $choices = array(1 => 'red',2 => 'green',3 => 'blue');
 158      $select = $page->getChild('test');
 159      $select->setChoices($choices);
 160      $object1 = new WactArrayObject(array('id' => 2));
 161      $object2 = new WactArrayObject(array('id' => 3));
 162      $select->setSelection(array($object1, $object2));
 163  
 164      $output = $page->capture();
 165      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
 166      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[1-3]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
 167      $this->assertWantedPattern('~<option[^>]+value="2"[^>]+selected[^>]*>green</option>~ims', $output);
 168      $this->assertWantedPattern('~<option[^>]+value="3"[^>]+selected[^>]*>blue</option>~ims', $output);
 169    }
 170  
 171    function testSetSelectionWithFormValueAsObjectWithSelectField()
 172    {
 173      $template = '<form id="testForm" runat="server">
 174                        <select id="test" name="mySelect[]" multiple="true" select_field="my_id" ></select>
 175                    </form>';
 176      $this->registerTestingTemplate('/components/form/selectmultiple/set_selection_with_form_value_as_object_with_select_field.html', $template);
 177  
 178      $page = $this->initTemplate('/components/form/selectmultiple/set_selection_with_form_value_as_object_with_select_field.html');
 179  
 180      $choices = array(1 => 'red',2 => 'green',3 => 'blue');
 181      $select = $page->getChild('test');
 182      $select->setChoices($choices);
 183      $object1 = new WactArrayObject(array('my_id' => 2));
 184      $object2 = new WactArrayObject(array('my_id' => 3));
 185      $select->setSelection(array($object1, $object2));
 186  
 187      $output = $page->capture();
 188      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
 189      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[1-3]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
 190      $this->assertWantedPattern('~<option[^>]+value="2"[^>]+selected[^>]*>green</option>~ims', $output);
 191      $this->assertWantedPattern('~<option[^>]+value="3"[^>]+selected[^>]*>blue</option>~ims', $output);
 192    }
 193  
 194    function testSelectUseOptionsListWithDefaultSelectedOption()
 195    {
 196      $template = '<form runat="server">'.
 197                    '<select id="test" name="mySelect[]" runat="server" multiple="true">'.
 198                    '<option value="foo" selected="true">"test1"</option>'.
 199                    '<option value="bar">\'test2\'</option>'.
 200                    '<option value="zoo" selected="true">test3</option>'.
 201                    '</select>'.
 202                  '</form>';
 203      $expected_template =
 204                  '<form>'.
 205                    '<select id="test" name="mySelect[]" multiple="true">'.
 206                    '<option value="foo" selected="true">&quot;test1&quot;</option>'.
 207                    '<option value="bar">\&#039;test2\&#039;</option>'.
 208                    '<option value="zoo" selected="true">test3</option>'.
 209                    '</select>'.
 210                  '</form>';
 211      $this->registerTestingTemplate('/tags/form/controls/selectmultiple/select_with_options_default.html', $template);
 212      $page = $this->initTemplate('/tags/form/controls/selectmultiple/select_with_options_default.html');
 213  
 214      $output = $page->capture();
 215      $this->assertEqual($output, $expected_template);
 216    }
 217  
 218    function testSelectUseOptionsListWithSelectedOption()
 219    {
 220      $template = '<form name="my_form" runat="server">'.
 221                    '<select id="test" name="mySelect[]" runat="server" multiple="true">'.
 222                    '<option value="1">test1</option>'.
 223                    '<option value="2" selected="true">test2</option>'.
 224                    '<option value="3">test3</option>'.
 225                    '</select>'.
 226                  '</form>';
 227      $expected_template =
 228                  '<form name="my_form">'.
 229                    '<select id="test" name="mySelect[]" multiple="true">'.
 230                    '<option value="1" selected="true">test1</option>'.
 231                    '<option value="2">test2</option>'.
 232                    '<option value="3" selected="true">test3</option>'.
 233                    '</select>'.
 234                  '</form>';
 235      $this->registerTestingTemplate('/tags/form/controls/selectmultiple/select_with_options.html', $template);
 236      $page = $this->initTemplate('/tags/form/controls/selectmultiple/select_with_options.html');
 237  
 238      $form = $page->getChild('my_form');
 239      $form->setValue('mySelect', array(1,3));
 240  
 241      $output = $page->capture();
 242      $this->assertEqual($output, $expected_template);
 243    }
 244  }
 245  ?>


Generated: Thu Jan 8 04:06:23 2009 Cross-referenced by PHPXref 0.7