[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/wact/tests/cases/tags/form/ -> WactSelectSingleComponentTest.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 WactSelectSingleComponentTest extends WactTemplateTestCase
  13  {
  14    function testSetChoicesDontSelectByDefault()
  15    {
  16      $template = '<form id="testForm" runat="server">
  17                        <select id="test" name="mySelect" runat="server"></select>
  18                    </form>';
  19      $this->registerTestingTemplate('/tags/form/select_single/setchoiceswithindex.html', $template);
  20  
  21      $page = $this->initTemplate('/tags/form/select_single/setchoiceswithindex.html');
  22  
  23      $choices = array('red','green','blue');
  24      $Select = $page->getChild('test');
  25      $Select->setChoices($choices);
  26  
  27      $output = $page->capture();
  28      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
  29      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="\d+"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
  30      $this->assertNoPattern('~<option\s+value="0"(?U)[^>]*selected[^>]*>[^<]*</option>~ims', $output);
  31    }
  32  
  33    function testSetChoicesWithHash()
  34    {
  35      $template = '<form id="testForm" runat="server">
  36                        <select id="test" name="mySelect" runat="server"></select>
  37                    </form>';
  38      $this->registerTestingTemplate('/tags/form/select_single/setchoiceswithhash.html', $template);
  39  
  40      $page = $this->initTemplate('/tags/form/select_single/setchoiceswithhash.html');
  41  
  42      $choices = array('a'=>'red', 'b'=>'green', 'c'=>'blue');
  43      $Select = $page->getChild('test');
  44      $Select->setChoices($choices);
  45  
  46      $output = $page->capture();
  47      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
  48      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[a-c]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
  49    }
  50  
  51    function testSetSelectionWithIndex()
  52    {
  53      $template = '<form id="testForm" runat="server">
  54                        <select id="test" name="mySelect" runat="server"></select>
  55                    </form>';
  56      $this->registerTestingTemplate('/tags/form/select_single/setselectionwithindex.html', $template);
  57  
  58      $page = $this->initTemplate('/tags/form/select_single/setselectionwithindex.html');
  59  
  60      $choices = array('red','green','blue');
  61      $selectedKey = '1';
  62      $Select = $page->getChild('test');
  63      $Select->setChoices($choices);
  64      $Select->setSelection($selectedKey);
  65  
  66      $output = $page->capture();
  67      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
  68      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="\d+"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
  69      $this->assertWantedPattern('~<option[^>]+value="1"[^>]+selected[^>]*>green</option>~ims', $output);
  70    }
  71  
  72    function testSetSelectionWithIndexByForm()
  73    {
  74      $template = '<form id="testForm" runat="server">
  75                        <select id="test" name="mySelect" runat="server"></select>
  76                    </form>';
  77      $this->registerTestingTemplate('/tags/form/select_single/setselectionwithindexbyform.html', $template);
  78  
  79      $page = $this->initTemplate('/tags/form/select_single/setselectionwithindexbyform.html');
  80  
  81      $Form = $page->getChild('testForm');
  82  
  83      $choices = array('red','green','blue');
  84      $selectedKey = '1';
  85      $Select = $page->getChild('test');
  86      $Select->setChoices($choices);
  87  
  88      $data = new WactArrayObject(array('mySelect' => $selectedKey));
  89  
  90      $Form->registerDataSource($data);
  91  
  92      $output = $page->capture();
  93      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
  94      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="\d+"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
  95      $this->assertWantedPattern('~<option[^>]+value="1"[^>]+selected[^>]*>green</option>~ims', $output);
  96    }
  97  
  98    function testSetSelectionWithGivenValue()
  99    {
 100      $template = '<select id="mySelect" given_value="{$#bar}" runat="server"></select>';;
 101      $this->registerTestingTemplate('/tags/form/select_single/set_selection_with_given_value.html', $template);
 102  
 103      $page = $this->initTemplate('/tags/form/select_single/set_selection_with_given_value.html');
 104  
 105      $choices = array('red','green','blue');
 106      $Select = $page->getChild('mySelect');
 107      $selectedKey = 1;
 108      $page->set('bar', $selectedKey);
 109      $Select->setChoices($choices);
 110  
 111      $output = $page->capture();
 112      $this->assertWantedPattern('~<select[^>]+id="mySelect"[^>]*>(\s*<option\svalue="\d+"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
 113      $this->assertWantedPattern('~<option[^>]+value="1"[^>]+selected[^>]*>green</option>~ims', $output);
 114    }
 115  
 116    function testSetSelectionWithHash()
 117    {
 118      $template = '<form id="testForm" runat="server">
 119                        <select id="test" name="mySelect" runat="server"></select>
 120                    </form>';
 121      $this->registerTestingTemplate('/tags/form/select_single/setselectionwithhash.html', $template);
 122  
 123      $page = $this->initTemplate('/tags/form/select_single/setselectionwithhash.html');
 124  
 125      $choices = array('a'=>'red','b'=>'green','c'=>'blue');
 126      $selectedKey = 'b';
 127      $Select = $page->getChild('test');
 128      $Select->setChoices($choices);
 129      $Select->setSelection('b');
 130  
 131      $output = $page->capture();
 132      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
 133      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[a-c]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
 134      $this->assertWantedPattern('~<option[^>]+value="b"[^>]+selected[^>]*>green</option>~ims', $output);
 135    }
 136  
 137    function testSetSelectionWithFormValueAsObject()
 138    {
 139      $template = '<form id="testForm" runat="server">
 140                        <select id="test" name="mySelect" runat="server"></select>
 141                    </form>';
 142      $this->registerTestingTemplate('/tags/form/select_single/set_selection_with_form_value_as_object.html', $template);
 143  
 144      $page = $this->initTemplate('/tags/form/select_single/set_selection_with_form_value_as_object.html');
 145  
 146      $choices = array(1 => 'red',2 => 'green',3 => 'blue');
 147      $Select = $page->getChild('test');
 148      $Select->setChoices($choices);
 149      $object = new WactArrayObject(array('id' => 2));
 150      $Select->setSelection($object);
 151  
 152      $output = $page->capture();
 153      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
 154      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[1-3]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
 155      $this->assertWantedPattern('~<option[^>]+value="2"[^>]+selected[^>]*>green</option>~ims', $output);
 156    }
 157  
 158    function testSetSelectionWithFormValueAsObjectWithSelectField()
 159    {
 160      $template = '<form id="testForm" runat="server">
 161                        <select id="test" name="mySelect" select_field="my_id" ></select>
 162                    </form>';
 163      $this->registerTestingTemplate('/tags/form/select_single/set_selection_with_form_value_as_object_with_select_field.html', $template);
 164  
 165      $page = $this->initTemplate('/tags/form/select_single/set_selection_with_form_value_as_object_with_select_field.html');
 166  
 167      $choices = array(1 => 'red',2 => 'green',3 => 'blue');
 168      $Select = $page->getChild('test');
 169      $Select->setChoices($choices);
 170      $object = new WactArrayObject(array('my_id' => 2));
 171      $Select->setSelection($object);
 172  
 173      $output = $page->capture();
 174      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
 175      $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[1-3]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
 176      $this->assertWantedPattern('~<option[^>]+value="2"[^>]+selected[^>]*>green</option>~ims', $output);
 177    }
 178  
 179    function testMixedOptionsWithZeroKey()
 180    {
 181      $template = '<form id="testForm" runat="server">
 182                        <select id="test" name="mySelect" select_field="my_id" ></select>
 183                    </form>';
 184      $this->registerTestingTemplate('/tags/form/select_single/set_selection_with_zero.html', $template);
 185  
 186      $page = $this->initTemplate('/tags/form/select_single/set_selection_with_zero.html');
 187  
 188      $choices = array(0 => '--', 'red' => 'R', 'green' => 'G', 'blue' => 'B');
 189      $Select = $page->getChild('test');
 190      $Select->setChoices($choices);
 191      $object = new WactArrayObject(array('my_id' => 'green'));
 192      $Select->setSelection($object);
 193  
 194      $output = $page->capture();
 195      $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
 196      $this->assertWantedPattern('~<option[^>]+value="green"[^>]+selected[^>]*>G</option>~ims', $output);
 197      $this->assertNoPattern('~<option[^>]+value="0"[^>]+selected[^>]*>--</option>~ims', $output);
 198    }
 199  
 200  
 201    /************************************************************

 202     Tests below use the API as it's expected to be used

 203     ************************************************************/
 204  
 205    function testSetChoicesWithKeys()
 206    {
 207      $template = '<form id="testForm" runat="server">
 208                        <select id="test" name="mySelect" runat="server"></select>
 209                    </form>';
 210      $this->registerTestingTemplate('/tags/form/select_single/setchoiceswithkeys.html', $template);
 211  
 212      $page = $this->initTemplate('/tags/form/select_single/setchoiceswithkeys.html');
 213  
 214      $choices = array('red'=>'','green'=>'','blue'=>'');
 215  
 216      $testOut = '';
 217      foreach ( $choices as $key => $choice ) {
 218        $testOut .= '<option value="'.$key.'"';
 219        $testOut .='>'.$key.'</option>';
 220      }
 221  
 222      $Select = $page->getChild('test');
 223  
 224      // Array flip?

 225      $Select->setChoices($choices);
 226  
 227      ob_start();
 228      $Select->renderContents();
 229      $out = ob_get_contents();
 230      ob_end_clean();
 231  
 232      $this->assertEqual($out,$testOut);
 233  
 234    }
 235  
 236    function testSetChoicesWithKeysSelection()
 237    {
 238      $template = '<form id="testForm" runat="server">
 239                        <select id="test" name="mySelect" runat="server"></select>
 240                    </form>';
 241      $this->registerTestingTemplate('/tags/form/select_single/setchoiceswithkeysselection.html', $template);
 242  
 243      $page = $this->initTemplate('/tags/form/select_single/setchoiceswithkeysselection.html');
 244  
 245      $choices = array('red'=>'','green'=>'','blue'=>'');
 246      $selected = 'green';
 247  
 248      $testOut = '';
 249  
 250      foreach ($choices as $key => $choice )
 251      {
 252        $testOut .= '<option value="'.$key.'"';
 253        if ( $key == $selected ) {
 254          $testOut .= ' selected="true"';
 255        }
 256        $testOut .='>'.$key.'</option>';
 257      }
 258  
 259      $Select = $page->getChild('test');
 260  
 261      $Select->setChoices($choices);
 262  
 263      $Select->setSelection($selected);
 264  
 265      ob_start();
 266      $Select->renderContents();
 267      $out = ob_get_contents();
 268      ob_end_clean();
 269  
 270      $this->assertEqual($out,$testOut);
 271    }
 272  
 273    function testSelectUseOptionsListWithDefaultSelectedOption()
 274    {
 275      $template = '<form runat="server">'.
 276                    '<select id="test" name="mySelect" runat="server">'.
 277                    '<option value="1">"test1"</option>'.
 278                    '<option value="2" selected="true">\'test2\'</option>'.
 279                    '</select>'.
 280                  '</form>';
 281      $expected_template =
 282                  '<form>'.
 283                    '<select id="test" name="mySelect">'.
 284                    '<option value="1">&quot;test1&quot;</option>'.
 285                    '<option value="2" selected="true">\&#039;test2\&#039;</option>'.
 286                    '</select>'.
 287                  '</form>';
 288      $this->registerTestingTemplate('/tags/form/controls/select/select_with_options_default.html', $template);
 289      $page = $this->initTemplate('/tags/form/controls/select/select_with_options_default.html');
 290  
 291      $output = $page->capture();
 292      $this->assertEqual($output, $expected_template);
 293    }
 294  
 295    function testMergeOptionsListFromTagContentWithOtherOptions()
 296    {
 297      $template = '<form runat="server">'.
 298                    '<select id="test" name="mySelect" runat="server">'.
 299                    '<option value="1" prepend="true">"test1"</option>'.
 300                    '<option value="4" selected="true">\'test4\'</option>'.
 301                    '</select>'.
 302                  '</form>';
 303      $expected_template =
 304                  '<form>'.
 305                    '<select id="test" name="mySelect">'.
 306                    '<option value="1">&quot;test1&quot;</option>'.
 307                    '<option value="2">test2</option>'.
 308                    '<option value="3">test3</option>'.
 309                    '<option value="4" selected="true">\&#039;test4\&#039;</option>'.
 310                    '</select>'.
 311                  '</form>';
 312      $this->registerTestingTemplate('/tags/form/controls/select/merge_select_options.html', $template);
 313      $page = $this->initTemplate('/tags/form/controls/select/merge_select_options.html');
 314  
 315      $select = $page->getChild('test');
 316      $select->setChoices(array('2' => 'test2', '3' => 'test3'));
 317  
 318      $output = $page->capture();
 319      $this->assertEqual($output, $expected_template);
 320    }
 321  
 322    function testSelectUseOptionsListWithSelectedOption()
 323    {
 324      $template = '<form name="my_form" runat="server">'.
 325                    '<select id="test" name="mySelect" runat="server">'.
 326                    '<option value="1">test1</option>'.
 327                    '<option value="2">test2</option>'.
 328                    '</select>'.
 329                  '</form>';
 330      $expected_template =
 331                  '<form name="my_form">'.
 332                    '<select id="test" name="mySelect">'.
 333                    '<option value="1">test1</option>'.
 334                    '<option value="2" selected="true">test2</option>'.
 335                    '</select>'.
 336                  '</form>';
 337      $this->registerTestingTemplate('/tags/form/controls/select/select_with_options.html', $template);
 338      $page = $this->initTemplate('/tags/form/controls/select/select_with_options.html');
 339  
 340      $form = $page->getChild('my_form');
 341      $form->setValue('mySelect', 2);
 342  
 343      $output = $page->capture();
 344      $this->assertEqual($output, $expected_template);
 345    }
 346  }
 347  ?>


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