PageRenderTime 74ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/limb/wact/tests/cases/tags/form/selectmultiple.test.php

https://github.com/limb-php-framework/limb-app-buildman
PHP | 248 lines | 178 code | 48 blank | 22 comment | 2 complexity | 2691cac68a6fd9f6da687621aeab9125 MD5 | raw file
  1. <?php
  2. /**
  3. * Limb Web Application Framework
  4. *
  5. * @link http://limb-project.com
  6. *
  7. * @copyright Copyright &copy; 2004-2007 BIT
  8. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  9. * @version $Id: selectmultiple.test.php 5021 2007-02-12 13:04:07Z pachanga $
  10. * @package wact
  11. */
  12. require_once 'limb/wact/src/components/form/form.inc.php';
  13. class WactSelectMultipleComponentTestCase extends WactTemplateTestCase
  14. {
  15. function testGetValue()
  16. {
  17. $template = '<form id="testForm" runat="server">
  18. <select id="test" name="multiple[]" runat="server" multiple></select>
  19. </form>';
  20. $this->registerTestingTemplate('/components/form/selectmultiple/getvalue.html', $template);
  21. $page = $this->initTemplate('/components/form/selectmultiple/getvalue.html');
  22. $choices = array('red'=>'','green'=>'','blue'=>'');
  23. $select = $page->getChild('test');
  24. $select->setSelection($choices);
  25. $this->assertEqual($select->getValue(),$choices);
  26. }
  27. function testSetChoicesWithIndex()
  28. {
  29. $template = '<form id="testForm" runat="server">
  30. <select id="test" name="multiple[]" runat="server" multiple></select>
  31. </form>';
  32. $this->registerTestingTemplate('/components/form/selectmultiple/setchoiceswithindex.html', $template);
  33. $page = $this->initTemplate('/components/form/selectmultiple/setchoiceswithindex.html');
  34. $choices = array('red','green','blue');
  35. /**
  36. * Note this test must fail right now
  37. * Reflects problem with array key 0 in choices get automatically
  38. * selected
  39. */
  40. $testOut = '';
  41. foreach ( $choices as $key => $choice ) {
  42. $testOut .= '<option value="'.$key.'"';
  43. $testOut .='>'.$choice.'</option>';
  44. }
  45. $select = $page->getChild('test');
  46. $select->setChoices($choices);
  47. ob_start();
  48. $select->renderContents();
  49. $out = ob_get_contents();
  50. ob_end_clean();
  51. $this->assertEqual($out,$testOut);
  52. }
  53. function testSetChoicesWithIndexSelected()
  54. {
  55. $template = '<form id="testForm" runat="server">
  56. <select id="test" name="multiple[]" runat="server" multiple></select>
  57. </form>';
  58. $this->registerTestingTemplate('/components/form/selectmultiple/setchoiceswithindexselected.html', $template);
  59. $page = $this->initTemplate('/components/form/selectmultiple/setchoiceswithindexselected.html');
  60. $choices = array('red','green','blue');
  61. $selected = array(0,1,2);
  62. $testOut = '';
  63. foreach ( $choices as $key => $choice ) {
  64. $testOut .= '<option value="'.$key.'"';
  65. if ( in_array($key,$selected) ) {
  66. $testOut .= ' selected';
  67. }
  68. $testOut .='>'.$choice.'</option>';
  69. }
  70. $select = $page->getChild('test');
  71. $select->setChoices($choices);
  72. $select->setSelection($selected);
  73. ob_start();
  74. $select->renderContents();
  75. $out = ob_get_contents();
  76. ob_end_clean();
  77. $this->assertEqual($out,$testOut);
  78. }
  79. /**
  80. * This test is included to "simulate" a selection received via POST - remember
  81. * PHP will convert integers to strings so strict type checking in the select components
  82. * for selected values will fail
  83. */
  84. function testSetChoicesWithIndexSelectedByForm()
  85. {
  86. $template = '<form id="testForm" runat="server">
  87. <select id="test" name="multiple[]" runat="server" multiple></select>
  88. </form>';
  89. $this->registerTestingTemplate('/components/form/selectmultiple/setchoiceswithindexselectedbyform.html', $template);
  90. $page = $this->initTemplate('/components/form/selectmultiple/setchoiceswithindexselectedbyform.html');
  91. $form = $page->getChild('testForm');
  92. $choices = array('red','green','blue');
  93. $selected = array(0,1,2);
  94. // The selected options will be string values, when a form is actually submitted!
  95. $selectedToString = array('0','1','2');
  96. $testOut = '';
  97. foreach ( $choices as $key => $choice ) {
  98. $testOut .= '<option value="'.$key.'"';
  99. if ( in_array($key,$selected) ) {
  100. $testOut .= ' selected';
  101. }
  102. $testOut .='>'.$choice.'</option>';
  103. }
  104. $select = $page->getChild('test');
  105. $select->setChoices($choices);
  106. // The selected values typed as strings
  107. $data = new WactArrayObject(array('multiple' => $selectedToString));
  108. $form->registerDataSource($data);
  109. ob_start();
  110. $select->renderContents();
  111. $out = ob_get_contents();
  112. ob_end_clean();
  113. $this->assertEqual($out,$testOut);
  114. }
  115. function testSetSelectionWithFormValueAsObject()
  116. {
  117. $template = '<form id="testForm" runat="server">
  118. <select id="test" name="mySelect[]" multiple="true" runat="server"></select>
  119. </form>';
  120. $this->registerTestingTemplate('/components/form/selectmultiple/set_selection_with_form_value_as_object.html', $template);
  121. $page = $this->initTemplate('/components/form/selectmultiple/set_selection_with_form_value_as_object.html');
  122. $choices = array(1 => 'red',2 => 'green',3 => 'blue');
  123. $select = $page->getChild('test');
  124. $select->setChoices($choices);
  125. $object1 = new WactArrayObject(array('id' => 2));
  126. $object2 = new WactArrayObject(array('id' => 3));
  127. $select->setSelection(array($object1, $object2));
  128. $output = $page->capture();
  129. $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
  130. $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[1-3]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
  131. $this->assertWantedPattern('~<option[^>]+value="2"[^>]+selected[^>]*>green</option>~ims', $output);
  132. $this->assertWantedPattern('~<option[^>]+value="3"[^>]+selected[^>]*>blue</option>~ims', $output);
  133. }
  134. function testSetSelectionWithFormValueAsObjectWithSelectField()
  135. {
  136. $template = '<form id="testForm" runat="server">
  137. <select id="test" name="mySelect[]" multiple="true" select_field="my_id" ></select>
  138. </form>';
  139. $this->registerTestingTemplate('/components/form/selectmultiple/set_selection_with_form_value_as_object_with_select_field.html', $template);
  140. $page = $this->initTemplate('/components/form/selectmultiple/set_selection_with_form_value_as_object_with_select_field.html');
  141. $choices = array(1 => 'red',2 => 'green',3 => 'blue');
  142. $select = $page->getChild('test');
  143. $select->setChoices($choices);
  144. $object1 = new WactArrayObject(array('my_id' => 2));
  145. $object2 = new WactArrayObject(array('my_id' => 3));
  146. $select->setSelection(array($object1, $object2));
  147. $output = $page->capture();
  148. $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output);
  149. $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[1-3]"[^>]*>[^<]*</option>)+.*</select>~ims', $output);
  150. $this->assertWantedPattern('~<option[^>]+value="2"[^>]+selected[^>]*>green</option>~ims', $output);
  151. $this->assertWantedPattern('~<option[^>]+value="3"[^>]+selected[^>]*>blue</option>~ims', $output);
  152. }
  153. function testSelectUseOptionsListWithDefaultSelectedOption()
  154. {
  155. $template = '<form runat="server">'.
  156. '<select id="test" name="mySelect[]" runat="server" multiple>'.
  157. '<option value="1" selected>"test1"</option>'.
  158. '<option value="2">\'test2\'</option>'.
  159. '<option value="3" selected>test3</option>'.
  160. '</select>'.
  161. '</form>';
  162. $expected_template =
  163. '<form>'.
  164. '<select id="test" name="mySelect[]" multiple>'.
  165. '<option value="1" selected>&quot;test1&quot;</option>'.
  166. '<option value="2">\&#039;test2\&#039;</option>'.
  167. '<option value="3" selected>test3</option>'.
  168. '</select>'.
  169. '</form>';
  170. $this->registerTestingTemplate('/tags/form/controls/selectmultiple/select_with_options_default.html', $template);
  171. $page = $this->initTemplate('/tags/form/controls/selectmultiple/select_with_options_default.html');
  172. $output = $page->capture();
  173. $this->assertEqual($output, $expected_template);
  174. }
  175. function testSelectUseOptionsListWithSelectedOption()
  176. {
  177. $template = '<form name="my_form" runat="server">'.
  178. '<select id="test" name="mySelect[]" runat="server" multiple>'.
  179. '<option value="1">test1</option>'.
  180. '<option value="2" selected>test2</option>'.
  181. '<option value="3">test3</option>'.
  182. '</select>'.
  183. '</form>';
  184. $expected_template =
  185. '<form name="my_form">'.
  186. '<select id="test" name="mySelect[]" multiple>'.
  187. '<option value="1" selected>test1</option>'.
  188. '<option value="2">test2</option>'.
  189. '<option value="3" selected>test3</option>'.
  190. '</select>'.
  191. '</form>';
  192. $this->registerTestingTemplate('/tags/form/controls/selectmultiple/select_with_options.html', $template);
  193. $page = $this->initTemplate('/tags/form/controls/selectmultiple/select_with_options.html');
  194. $form = $page->getChild('my_form');
  195. $form->setValue('mySelect', array(1,3));
  196. $output = $page->capture();
  197. $this->assertEqual($output, $expected_template);
  198. }
  199. }
  200. ?>