/tests/suites/unit/joomla/form/fields/JFormFieldCheckboxesTest.php

https://github.com/Hackwar/joomla-platform · PHP · 366 lines · 232 code · 49 blank · 85 comment · 0 complexity · d08382d8d90e4c04aabd25e88ab9ad4f MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.UnitTest
  4. * @subpackage Form
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. /**
  10. * Test class for JForm.
  11. *
  12. * @package Joomla.UnitTest
  13. * @subpackage Form
  14. * @since 11.3
  15. */
  16. class JFormFieldCheckboxesTest extends TestCase
  17. {
  18. /**
  19. * Sets up dependencies for the test.
  20. *
  21. * @since 11.3
  22. *
  23. * @return void
  24. */
  25. protected function setUp()
  26. {
  27. require_once JPATH_PLATFORM . '/joomla/form/fields/checkboxes.php';
  28. }
  29. /**
  30. * Test the getInput method with no value and no checked attribute.
  31. *
  32. * @since 12.2
  33. *
  34. * @return void
  35. */
  36. public function testGetInputNoValueNoChecked()
  37. {
  38. $formFieldCheckboxes = $this->getMock('JFormFieldCheckboxes', array('getOptions'));
  39. $option1 = new JObject;
  40. $option1->set('value', 'red');
  41. $option1->set('text', 'red');
  42. $option2 = new JObject;
  43. $option2->set('value', 'blue');
  44. $option2->set('text', 'blue');
  45. $optionsReturn = array($option1, $option2);
  46. $formFieldCheckboxes->expects($this->any())
  47. ->method('getOptions')
  48. ->will($this->returnValue($optionsReturn));
  49. // Test with no value, no checked element
  50. $element = simplexml_load_string(
  51. '<field name="color" type="checkboxes">
  52. <option value="red">red</option>
  53. <option value="blue">blue</option>
  54. </field>');
  55. TestReflection::setValue($formFieldCheckboxes, 'element', $element);
  56. TestReflection::setValue($formFieldCheckboxes, 'id', 'myTestId');
  57. TestReflection::setValue($formFieldCheckboxes, 'name', 'myTestName');
  58. $this->assertEquals(
  59. '<fieldset id="myTestId" class="checkboxes"><ul><li><input type="checkbox" id="myTestId0" name="myTestName" value="red"/><label for="myTestId0">red</label></li><li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label></li></ul></fieldset>',
  60. TestReflection::invoke($formFieldCheckboxes, 'getInput'),
  61. 'The field with no value and no checked values did not produce the right html'
  62. );
  63. }
  64. /**
  65. * Test the getInput method with one value selected and no checked attribute.
  66. *
  67. * @since 12.2
  68. *
  69. * @return void
  70. */
  71. public function testGetInputValueNoChecked()
  72. {
  73. $formFieldCheckboxes = $this->getMock('JFormFieldCheckboxes', array('getOptions'));
  74. $option1 = new JObject;
  75. $option1->set('value', 'red');
  76. $option1->set('text', 'red');
  77. $option2 = new JObject;
  78. $option2->set('value', 'blue');
  79. $option2->set('text', 'blue');
  80. $optionsReturn = array($option1, $option2);
  81. $formFieldCheckboxes->expects($this->any())
  82. ->method('getOptions')
  83. ->will($this->returnValue($optionsReturn));
  84. // Test with one value checked, no checked element
  85. $element = simplexml_load_string(
  86. '<field name="color" type="checkboxes">
  87. <option value="red">red</option>
  88. <option value="blue">blue</option>
  89. </field>');
  90. TestReflection::setValue($formFieldCheckboxes, 'element', $element);
  91. TestReflection::setValue($formFieldCheckboxes, 'id', 'myTestId');
  92. TestReflection::setValue($formFieldCheckboxes, 'value', 'red');
  93. TestReflection::setValue($formFieldCheckboxes, 'name', 'myTestName');
  94. $this->assertEquals(
  95. '<fieldset id="myTestId" class="checkboxes"><ul><li><input type="checkbox" id="myTestId0" name="myTestName" value="red" checked="checked"/><label for="myTestId0">red</label></li><li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label></li></ul></fieldset>',
  96. TestReflection::invoke($formFieldCheckboxes, 'getInput'),
  97. 'The field with one value did not produce the right html'
  98. );
  99. }
  100. /**
  101. * Test the getInput method with one value that is an array and no checked attribute.
  102. *
  103. * @since 12.2
  104. *
  105. * @return void
  106. */
  107. public function testGetInputValueArrayNoChecked()
  108. {
  109. $formFieldCheckboxes = $this->getMock('JFormFieldCheckboxes', array('getOptions'));
  110. $option1 = new JObject;
  111. $option1->set('value', 'red');
  112. $option1->set('text', 'red');
  113. $option2 = new JObject;
  114. $option2->set('value', 'blue');
  115. $option2->set('text', 'blue');
  116. $optionsReturn = array($option1, $option2);
  117. $formFieldCheckboxes->expects($this->any())
  118. ->method('getOptions')
  119. ->will($this->returnValue($optionsReturn));
  120. // Test with one value checked, no checked element
  121. $element = simplexml_load_string(
  122. '<field name="color" type="checkboxes">
  123. <option value="red">red</option>
  124. <option value="blue">blue</option>
  125. </field>');
  126. $valuearray = array('red');
  127. TestReflection::setValue($formFieldCheckboxes, 'element', $element);
  128. TestReflection::setValue($formFieldCheckboxes, 'id', 'myTestId');
  129. TestReflection::setValue($formFieldCheckboxes, 'value', $valuearray);
  130. TestReflection::setValue($formFieldCheckboxes, 'name', 'myTestName');
  131. $this->assertEquals(
  132. '<fieldset id="myTestId" class="checkboxes"><ul><li><input type="checkbox" id="myTestId0" name="myTestName" value="red" checked="checked"/><label for="myTestId0">red</label></li><li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label></li></ul></fieldset>',
  133. TestReflection::invoke($formFieldCheckboxes, 'getInput'),
  134. 'The field with one value did not produce the right html'
  135. );
  136. }
  137. /**
  138. * Test the getInput method with no value and one value in checked.
  139. *
  140. * @since 12.2
  141. *
  142. * @return void
  143. */
  144. public function testGetInputNoValueOneChecked()
  145. {
  146. $formFieldCheckboxes = $this->getMock('JFormFieldCheckboxes', array('getOptions'));
  147. $option1 = new JObject;
  148. $option1->set('value', 'red');
  149. $option1->set('text', 'red');
  150. $option2 = new JObject;
  151. $option2->set('value', 'blue');
  152. $option2->set('text', 'blue');
  153. $optionsReturn = array($option1, $option2);
  154. $formFieldCheckboxes->expects($this->any())
  155. ->method('getOptions')
  156. ->will($this->returnValue($optionsReturn));
  157. // Test with nothing checked, one value in checked element
  158. $element = simplexml_load_string(
  159. '<field name="color" type="checkboxes" checked="blue">
  160. <option value="red">red</option>
  161. <option value="blue">blue</option>
  162. </field>');
  163. TestReflection::setValue($formFieldCheckboxes, 'element', $element);
  164. TestReflection::setValue($formFieldCheckboxes, 'id', 'myTestId');
  165. TestReflection::setValue($formFieldCheckboxes, 'name', 'myTestName');
  166. $this->assertEquals(
  167. '<fieldset id="myTestId" class="checkboxes"><ul><li><input type="checkbox" id="myTestId0" name="myTestName" value="red"/><label for="myTestId0">red</label></li><li><input type="checkbox" id="myTestId1" name="myTestName" value="blue" checked="checked"/><label for="myTestId1">blue</label></li></ul></fieldset>',
  168. TestReflection::invoke($formFieldCheckboxes, 'getInput'),
  169. 'The field with no values and one value in the checked element did not produce the right html'
  170. );
  171. }
  172. /**
  173. * Test the getInput method with no value and two values in the checked element.
  174. *
  175. * @since 12.2
  176. *
  177. * @return void
  178. */
  179. public function testGetInputNoValueTwoChecked()
  180. {
  181. $formFieldCheckboxes = $this->getMock('JFormFieldCheckboxes', array('getOptions'));
  182. $option1 = new JObject;
  183. $option1->set('value', 'red');
  184. $option1->set('text', 'red');
  185. $option2 = new JObject;
  186. $option2->set('value', 'blue');
  187. $option2->set('text', 'blue');
  188. $optionsReturn = array($option1, $option2);
  189. $formFieldCheckboxes->expects($this->any())
  190. ->method('getOptions')
  191. ->will($this->returnValue($optionsReturn));
  192. // Test with nothing checked, two values in checked element
  193. $element = simplexml_load_string(
  194. '<field name="color" type="checkboxes" checked="red,blue">
  195. <option value="red">red</option>
  196. <option value="blue">blue</option>
  197. </field>');
  198. TestReflection::setValue($formFieldCheckboxes, 'element', $element);
  199. TestReflection::setValue($formFieldCheckboxes, 'id', 'myTestId');
  200. TestReflection::setValue($formFieldCheckboxes, 'name', 'myTestName');
  201. TestReflection::setValue($formFieldCheckboxes, 'value', '""');
  202. $this->assertEquals(
  203. '<fieldset id="myTestId" class="checkboxes"><ul><li><input type="checkbox" id="myTestId0" name="myTestName" value="red"/><label for="myTestId0">red</label></li><li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label></li></ul></fieldset>',
  204. TestReflection::invoke($formFieldCheckboxes, 'getInput'),
  205. 'The field with no values and two items in the checked element did not produce the right html'
  206. );
  207. }
  208. /**
  209. * Test the getInput method with one value and a different checked value.
  210. *
  211. * @since 12.2
  212. *
  213. * @return void
  214. */
  215. public function testGetInputValueChecked()
  216. {
  217. $formFieldCheckboxes = $this->getMock('JFormFieldCheckboxes', array('getOptions'));
  218. $option1 = new JObject;
  219. $option1->set('value', 'red');
  220. $option1->set('text', 'red');
  221. $option2 = new JObject;
  222. $option2->set('value', 'blue');
  223. $option2->set('text', 'blue');
  224. $optionsReturn = array($option1, $option2);
  225. $formFieldCheckboxes->expects($this->any())
  226. ->method('getOptions')
  227. ->will($this->returnValue($optionsReturn));
  228. // Test with one item checked, a different value in checked element
  229. $element = simplexml_load_string(
  230. '<field name="color" type="checkboxes" checked="blue">
  231. <option value="red">red</option>
  232. <option value="blue">blue</option>
  233. </field>');
  234. TestReflection::setValue($formFieldCheckboxes, 'element', $element);
  235. TestReflection::setValue($formFieldCheckboxes, 'id', 'myTestId');
  236. TestReflection::setValue($formFieldCheckboxes, 'value', 'red');
  237. TestReflection::setValue($formFieldCheckboxes, 'name', 'myTestName');
  238. $this->assertEquals(
  239. '<fieldset id="myTestId" class="checkboxes"><ul><li><input type="checkbox" id="myTestId0" name="myTestName" value="red" checked="checked"/><label for="myTestId0">red</label></li><li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label></li></ul></fieldset>',
  240. TestReflection::invoke($formFieldCheckboxes, 'getInput'),
  241. 'The field with one value and a different value in the checked element did not produce the right html'
  242. );
  243. }
  244. /**
  245. * Test the getInput method with multiple values, no checked.
  246. *
  247. * @since 12.2
  248. *
  249. * @return void
  250. */
  251. public function testGetInputValuesNoChecked()
  252. {
  253. $formFieldCheckboxes = $this->getMock('JFormFieldCheckboxes', array('getOptions'));
  254. $option1 = new JObject;
  255. $option1->set('value', 'red');
  256. $option1->set('text', 'red');
  257. $option2 = new JObject;
  258. $option2->set('value', 'blue');
  259. $option2->set('text', 'blue');
  260. $optionsReturn = array($option1, $option2);
  261. $formFieldCheckboxes->expects($this->any())
  262. ->method('getOptions')
  263. ->will($this->returnValue($optionsReturn));
  264. // Test with two values checked, no checked element
  265. $element = simplexml_load_string(
  266. '<field name="color" type="checkboxes">
  267. <option value="red">red</option>
  268. <option value="blue">blue</option>
  269. </field>');
  270. TestReflection::setValue($formFieldCheckboxes, 'element', $element);
  271. TestReflection::setValue($formFieldCheckboxes, 'id', 'myTestId');
  272. TestReflection::setValue($formFieldCheckboxes, 'value', 'yellow,green');
  273. TestReflection::setValue($formFieldCheckboxes, 'name', 'myTestName');
  274. $this->assertEquals(
  275. '<fieldset id="myTestId" class="checkboxes"><ul><li><input type="checkbox" id="myTestId0" name="myTestName" value="red"/><label for="myTestId0">red</label></li><li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label></li></ul></fieldset>',
  276. TestReflection::invoke($formFieldCheckboxes, 'getInput'),
  277. 'The field with two values did not produce the right html'
  278. );
  279. }
  280. /**
  281. * Test the getOptions method.
  282. *
  283. * @since 12.2
  284. *
  285. * @return void
  286. */
  287. public function testGetOptions()
  288. {
  289. $formFieldCheckboxes = new JFormFieldCheckboxes;
  290. $option1 = new stdClass;
  291. $option1->value = 'yellow';
  292. $option1->text = 'yellow';
  293. $option1->disable = false;
  294. $option1->class = '';
  295. $option1->onclick = '';
  296. $option2 = new stdClass;
  297. $option2->value = 'green';
  298. $option2->text = 'green';
  299. $option2->disable = false;
  300. $option2->class = '';
  301. $option2->onclick = '';
  302. $optionsExpected = array($option1, $option2);
  303. // Test with two values checked, no checked element
  304. TestReflection::setValue(
  305. $formFieldCheckboxes, 'element', simplexml_load_string(
  306. '<field name="color" type="checkboxes">
  307. <option value="yellow">yellow</option>
  308. <option value="green">green</option>
  309. </field>')
  310. );
  311. $this->assertEquals(
  312. $optionsExpected,
  313. TestReflection::invoke($formFieldCheckboxes, 'getOptions'),
  314. 'The field with two values did not produce the right options'
  315. );
  316. }
  317. }