[limb-svn] r5792 - in 3.x/trunk/limb/wact: src/components/form tests/cases/tags/form

svn at limb-project.com svn at limb-project.com
Thu May 3 12:41:35 MSD 2007


Author: pachanga
Date: 2007-05-03 12:41:35 +0400 (Thu, 03 May 2007)
New Revision: 5792
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5792

Modified:
   3.x/trunk/limb/wact/src/components/form/select.inc.php
   3.x/trunk/limb/wact/tests/cases/tags/form/WactSelectSingleComponentTest.class.php
Log:
-- WactSelectSingleComponent casts key to string before comparison since in PHP 0 == 'bar'

Modified: 3.x/trunk/limb/wact/src/components/form/select.inc.php
===================================================================
--- 3.x/trunk/limb/wact/src/components/form/select.inc.php	2007-05-03 08:00:08 UTC (rev 5791)
+++ 3.x/trunk/limb/wact/src/components/form/select.inc.php	2007-05-03 08:41:35 UTC (rev 5792)
@@ -110,10 +110,10 @@
   function renderContents()
   {
     $values = $this->getValue();
-    if (!is_object($values) && !is_array($values))
+    if(!is_object($values) && !is_array($values))
       $values = $this->default_selection;
 
-    if (empty($this->option_handler))
+    if(empty($this->option_handler))
       $this->option_handler = new WactOptionRenderer();
 
     if(!$select_field = $this->getAttribute('select_field'))
@@ -262,20 +262,22 @@
     if(is_null($value))
       $value = $this->default_selection;
 
-    if (!is_object($this->option_handler))
+    if(!is_object($this->option_handler))
       $this->option_handler = new WactOptionRenderer();
 
     if(!$select_field = $this->getAttribute('select_field'))
       $select_field = 'id';
 
+    if(!is_scalar($value))
+      $selected = $value[$select_field];
+    else
+      $selected = $value;
+
     foreach($this->choice_list as $key => $choice)
     {
-      if(!is_scalar($value))
-        $selected = $value[$select_field];
-      else
-        $selected = $value;
-
-      $this->option_handler->renderOption($key, $choice, $key == $selected);
+      //special case, since in PHP "0 == 'bar'"
+      $set = ((string)$key) == $selected;
+      $this->option_handler->renderOption($key, $choice, $set);
     }
   }
 }

Modified: 3.x/trunk/limb/wact/tests/cases/tags/form/WactSelectSingleComponentTest.class.php
===================================================================
--- 3.x/trunk/limb/wact/tests/cases/tags/form/WactSelectSingleComponentTest.class.php	2007-05-03 08:00:08 UTC (rev 5791)
+++ 3.x/trunk/limb/wact/tests/cases/tags/form/WactSelectSingleComponentTest.class.php	2007-05-03 08:41:35 UTC (rev 5792)
@@ -14,12 +14,8 @@
 
 class WactSelectSingleComponentTest extends WactTemplateTestCase
 {
-  /**
-  * @todo Should the first element of the index really be selected automatically ?
-  */
-  function testSetChoicesWithIndex()
+  function testSetChoicesDontSelectByDefault()
   {
-
     $template = '<form id="testForm" runat="server">
                       <select id="test" name="mySelect" runat="server"></select>
                   </form>';
@@ -34,12 +30,11 @@
     $output = $page->capture();
     $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
     $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="\d+"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
-    $this->assertWantedPattern('~<option\s+value="0"(?U)[^>]*selected[^>]*>[^<]*</option>~ims', $output);
+    $this->assertNoPattern('~<option\s+value="0"(?U)[^>]*selected[^>]*>[^<]*</option>~ims', $output);
   }
 
-
-  function testSetChoicesWithHash() {
-
+  function testSetChoicesWithHash()
+  {
     $template = '<form id="testForm" runat="server">
                       <select id="test" name="mySelect" runat="server"></select>
                   </form>';
@@ -56,8 +51,8 @@
     $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[a-c]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
   }
 
-  function testSetSelectionWithIndex() {
-
+  function testSetSelectionWithIndex()
+  {
     $template = '<form id="testForm" runat="server">
                       <select id="test" name="mySelect" runat="server"></select>
                   </form>';
@@ -77,8 +72,8 @@
     $this->assertWantedPattern('~<option[^>]+value="1"[^>]+selected[^>]*>green</option>~ims', $output);
   }
 
-  function testSetSelectionWithIndexByForm() {
-
+  function testSetSelectionWithIndexByForm()
+  {
     $template = '<form id="testForm" runat="server">
                       <select id="test" name="mySelect" runat="server"></select>
                   </form>';
@@ -184,6 +179,28 @@
     $this->assertWantedPattern('~<option[^>]+value="2"[^>]+selected[^>]*>green</option>~ims', $output);
   }
 
+  function testMixedOptionsWithZeroKey()
+  {
+    $template = '<form id="testForm" runat="server">
+                      <select id="test" name="mySelect" select_field="my_id" ></select>
+                  </form>';
+    $this->registerTestingTemplate('/tags/form/select_single/set_selection_with_zero.html', $template);
+
+    $page = $this->initTemplate('/tags/form/select_single/set_selection_with_zero.html');
+
+    $choices = array(0 => '--', 'red' => 'R', 'green' => 'G', 'blue' => 'B');
+    $Select = $page->getChild('test');
+    $Select->setChoices($choices);
+    $object = new WactArrayObject(array('my_id' => 'green'));
+    $Select->setSelection($object);
+
+    $output = $page->capture();
+    $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
+    $this->assertWantedPattern('~<option[^>]+value="green"[^>]+selected[^>]*>G</option>~ims', $output);
+    $this->assertNoPattern('~<option[^>]+value="0"[^>]+selected[^>]*>--</option>~ims', $output);
+  }
+
+
   /************************************************************
    Tests below use the API as it's expected to be used
    ************************************************************/
@@ -233,7 +250,8 @@
 
     $testOut = '';
 
-    foreach ($choices as $key => $choice ) {
+    foreach ($choices as $key => $choice )
+    {
       $testOut .= '<option value="'.$key.'"';
       if ( $key == $selected ) {
         $testOut .= ' selected="true"';



More information about the limb-svn mailing list