Skip to content
Browse files

Add "preset" attribute to form fields and adapt list/checkboxes multi…

…ple default/preset values
  • Loading branch information...
1 parent d3432ab commit 35c7b784c7ead1b8a962791a122daa33e0dd489e @chdemko committed Jun 6, 2012
View
50 libraries/joomla/form/fields/checkboxes.php
@@ -58,13 +58,55 @@ protected function getInput()
// Get the field options.
$options = $this->getOptions();
+ // Get the value(s)
+ if (empty($this->value))
+ {
+ // Filter the option using the preset flag
+ $filter = array_filter(
+ $options,
+ function($option)
+ {
+ return $option->preset;
+ }
+ );
+
+ // If the data exists or there is no preset
+ if ($this->form->existValue($this->fieldname, $this->group) || empty($filter))
+ {
+ $filter = array_filter(
+ $options,
+ function($option)
+ {
+ return $option->default;
+ }
+ );
+ }
+
+ // Extract the values from the filter
+ $values = array_map(
+ function ($option)
+ {
+ return $option->value;
+ },
+ $filter
+ );
+ }
+ elseif (is_array($this->value))
+ {
+ $values = $this->value;
+ }
+ else
+ {
+ $values = array($this->value);
+ }
+
// Build the checkbox field output.
$html[] = '<ul>';
foreach ($options as $i => $option)
{
// Initialize some option attributes.
- $checked = (in_array((string) $option->value, (array) $this->value) ? ' checked="checked"' : '');
+ $checked = (in_array((string) $option->value, $values) ? ' checked="checked"' : '');
$class = !empty($option->class) ? ' class="' . $option->class . '"' : '';
$disabled = !empty($option->disable) ? ' disabled="disabled"' : '';
@@ -119,6 +161,12 @@ protected function getOptions()
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
+ // Set the default attribute.
+ $tmp->default = (string) $option['default'] == 'true';
+
+ // Set the preset attribute.
+ $tmp->preset = (string) $option['preset'] == 'true';
+
// Add the option object to the result set.
$options[] = $tmp;
}
View
64 libraries/joomla/form/fields/list.php
@@ -59,16 +59,68 @@ protected function getInput()
// Get the field options.
$options = (array) $this->getOptions();
+ // Get the value(s)
+ if ($this->multiple)
+ {
+ if (empty($this->value))
+ {
+ // Filter the option using the preset flag
+ $filter = array_filter(
+ $options,
+ function($option)
+ {
+ return $option->preset;
+ }
+ );
+
+ // If the data exists or there is no preset
+ if ($this->form->existValue($this->fieldname, $this->group) || empty($filter))
+ {
+ $filter = array_filter(
+ $options,
+ function($option)
+ {
+ return $option->default;
+ }
+ );
+ }
+
+ // Extract the values from the filter
+ $values = array_map(
+ function ($option)
+ {
+ return $option->value;
+ },
+ $filter
+ );
+ }
+ elseif (is_array($this->value))
+ {
+ $values = $this->value;
+ }
+ else
+ {
+ $values = array($this->value);
+ }
+ }
+ else
+ {
+ $values = array($this->value);
+ }
+
// Create a read-only list (no name) with a hidden input to store the value.
if ((string) $this->element['readonly'] == 'true')
{
- $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $this->value, $this->id);
- $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
+ $html[] = JHtml::_('select.genericlist', $options, '', trim($attr), 'value', 'text', $values, $this->id);
+ foreach ($values as $value)
+ {
+ $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $value . '"/>';
+ }
}
// Create a regular list.
else
{
- $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $this->value, $this->id);
+ $html[] = JHtml::_('select.genericlist', $options, $this->name, trim($attr), 'value', 'text', $values, $this->id);
}
return implode($html);
@@ -108,6 +160,12 @@ protected function getOptions()
// Set some JavaScript option attributes.
$tmp->onclick = (string) $option['onclick'];
+ // Set the default attribute.
+ $tmp->default = (string) $option['default'] == 'true';
+
+ // Set the preset attribute.
+ $tmp->preset = (string) $option['preset'] == 'true';
+
// Add the option object to the result set.
$options[] = $tmp;
}
View
56 libraries/joomla/form/form.php
@@ -622,6 +622,31 @@ public function getValue($name, $group = null, $default = null)
}
/**
+ * Method to test if the value of a field exists.
+ *
+ * @param string $name The name of the field for which to get the value.
+ * @param string $group The optional dot-separated form group path on which to get the value.
+ *
+ * @return bool TRUE on success, FALSE on failure.
+ *
+ * @since 12.2
+ */
+ public function existValue($name, $group = null)
+ {
+ // If a group is set use it.
+ if ($group)
+ {
+ $return = $this->data->exists($group . '.' . $name);
+ }
+ else
+ {
+ $return = $this->data->exists($name);
+ }
+
+ return $return;
+ }
+
+ /**
* Method to load the form description from an XML string or object.
*
* The replace option works per field. If a field being loaded already exists in the current
@@ -1667,6 +1692,9 @@ protected function loadField($element, $group = null, $value = null)
return false;
}
+ // Get the field name.
+ $name = (string) $element['name'];
+
// Get the field type.
$type = $element['type'] ? (string) $element['type'] : 'text';
@@ -1687,6 +1715,7 @@ protected function loadField($element, $group = null, $value = null)
*/
if ($value === null)
{
+ // Get the default value
$default = (string) $element['default'];
if (($translate = $element['translate_default']) && ((string) $translate == 'true' || (string) $translate == '1'))
{
@@ -1702,7 +1731,32 @@ protected function loadField($element, $group = null, $value = null)
$default = JText::_($default);
}
}
- $value = $this->getValue((string) $element['name'], $group, $default);
+
+ // Get the value
+ if ($this->existValue($name, $group) || !isset($element['preset']))
+ {
+ $value = $this->getValue($name, $group, $default);
+ }
+ else
+ {
+ // Get the preset value
+ $preset = (string) $element['preset'];
+ if (($translate = $element['translate_preset']) && ((string) $translate == 'true' || (string) $translate == '1'))
+ {
+ $lang = JFactory::getLanguage();
+ if ($lang->hasKey($default))
+ {
+ $debug = $lang->setDebug(false);
+ $preset = JText::_($preset);
+ $lang->setDebug($debug);
+ }
+ else
+ {
+ $preset = JText::_($preset);
+ }
+ }
+ $value = $this->getValue((string) $element['name'], $group, $preset);
+ }
}
// Setup the JFormField object.
View
5 tests/suites/unit/joomla/form/JFormDataHelper.php
@@ -249,6 +249,11 @@ class JFormDataHelper
name="title"
type="text"
description="The title." />
+ <field
+ name="alias"
+ type="text"
+ description="The alias."
+ preset="preset-alias" />
<fields
name="params">
<field
View
49 tests/suites/unit/joomla/form/JFormTest.php
@@ -814,10 +814,17 @@ public function testGetField()
'Line:'.__LINE__.' Prior to binding data, the defaults in the field should be used.'
);
+ $this->assertThat(
+ $form->getField('alias')->value,
+ $this->equalTo('preset-alias'),
+ 'Line:'.__LINE__.' Prior to binding data, the presets in the field should be used.'
+ );
+
// Check values after binding.
$data = array(
'title' => 'The title',
+ 'alias' => '',
'show_title' => 3,
'params' => array(
'show_title' => 2,
@@ -837,6 +844,12 @@ public function testGetField()
);
$this->assertThat(
+ $form->getField('alias')->value,
+ $this->equalTo(''),
+ 'Line:'.__LINE__.' Check the field value bound correctly.'
+ );
+
+ $this->assertThat(
$form->getField('show_title', 'params')->value,
$this->equalTo(2),
'Line:'.__LINE__.' Check the field value bound correctly.'
@@ -1138,6 +1151,42 @@ public function testGetValue()
}
/**
+ * Test for JForm::existValue method.
+ */
+ public function testExistValue()
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load(JFormDataHelper::$loadFieldDocument),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->existValue('title'),
+ $this->equalTo(false),
+ 'Line:'.__LINE__.' The value should not exist.'
+ );
+
+ $data = array(
+ 'title' => 'Avatar',
+ );
+
+ $this->assertThat(
+ $form->bind($data),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->existValue('title'),
+ $this->equalTo(true),
+ 'Line:'.__LINE__.' The value should exist.'
+ );
+ }
+
+ /**
* Test for JForm::getFieldset method.
*/
public function testGetFieldset()
View
123 tests/suites/unit/joomla/form/fields/JFormFieldCheckboxesTest.php
@@ -48,4 +48,127 @@ public function testGetInput()
// TODO: Should check all the attributes have come in properly.
}
+
+ /**
+ * Data provider for testGetInputDefaultPreset
+ */
+ public function caseGetInputDefaultPreset()
+ {
+ return array(
+ // no default, no preset, existing value
+ array(
+ '',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '1',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // one default, no preset, existing value
+ array(
+ 'default="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // one default, no preset, empty value
+ array(
+ 'default="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // no default, one preset, existing value
+ array(
+ 'preset="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // one default, one preset, empty value
+ array(
+ 'preset="1" default="2"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // one default, one preset, existing value
+ array(
+ 'preset="1" default="2"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '3',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3" checked="checked"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // many default, many preset, empty value
+ array(
+ '',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ '',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3" checked="checked"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // many default, many preset, existing value
+ array(
+ '',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ '2',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+
+ // many default, many preset, existing value
+ array(
+ '',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ array('2', '3'),
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1" checked="checked"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ '<fieldset id="checkboxes" class="checkboxes"><ul><li><input type="checkbox" id="checkboxes0" name="checkboxes[]" value="1"/><label for="checkboxes0">1</label></li><li><input type="checkbox" id="checkboxes1" name="checkboxes[]" value="2" checked="checked"/><label for="checkboxes1">2</label></li><li><input type="checkbox" id="checkboxes2" name="checkboxes[]" value="3" checked="checked"/><label for="checkboxes2">3</label></li></ul></fieldset>',
+ ),
+ );
+ }
+
+ /**
+ * Test the getInput method.
+ *
+ * @dataProvider caseGetInputDefaultPreset
+ */
+ public function testGetInputDefaultPreset($attributes, $options, $bind, $before, $after)
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load('<form><field name="checkboxes" type="checkboxes" ' . $attributes . ' >' . $options . '</field></form>'),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('checkboxes')->input,
+ $this->equalTo($before),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+
+ $this->assertThat(
+ $form->bind(array('checkboxes' => $bind)),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('checkboxes')->input,
+ $this->equalTo($after),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+ }
}
View
289 tests/suites/unit/joomla/form/fields/JFormFieldListTest.php
@@ -53,4 +53,293 @@ public function testGetInput()
// TODO: Should check all the attributes have come in properly.
}
+
+ /**
+ * Data provider for testGetInputDefaultPreset
+ */
+ public function caseGetInputDefaultPreset()
+ {
+ return array(
+ // no multiple, no default, no preset, existing value
+ array(
+ '',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '1',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // multiple, no default, no preset, existing value
+ array(
+ 'multiple="true"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '1',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, one default, no preset, existing value
+ array(
+ 'default="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, one default, no preset, empty value
+ array(
+ 'default="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, no default, one preset, existing value
+ array(
+ 'preset="1"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, no default, no preset, existing value, readonly
+ array(
+ 'readonly="true"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+'<select id="list" name="" disabled="disabled">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+<input type="hidden" name="list" value=""/>',
+'<select id="list" name="" disabled="disabled">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+<input type="hidden" name="list" value="2"/>'
+ ),
+
+ // multiple, no default, one preset, existing value, readonly
+ array(
+ 'multiple="true" readonly="true"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '2',
+'<select id="list" name="" disabled="disabled" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="" disabled="disabled" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+<input type="hidden" name="list[]" value="2"/>'
+ ),
+
+ // multiple, no default, one preset, existing value, readonly
+ array(
+ 'multiple="true" readonly="true"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ array('1', '2'),
+'<select id="list" name="" disabled="disabled" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="" disabled="disabled" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+<input type="hidden" name="list[]" value="1"/><input type="hidden" name="list[]" value="2"/>'
+ ),
+
+ // no multiple, one default, one preset, empty value
+ array(
+ 'preset="1" default="2"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // no multiple, one default, one preset, existing value
+ array(
+ 'preset="1" default="2"',
+ '<option value="1">1</option><option value="2">2</option><option value="3">3</option>',
+ '3',
+'<select id="list" name="list">
+ <option value="1" selected="selected">1</option>
+ <option value="2">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3" selected="selected">3</option>
+</select>
+'
+ ),
+
+ // multiple, many default, many preset, empty value
+ array(
+ 'multiple="true"',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ '',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2">2</option>
+ <option value="3" selected="selected">3</option>
+</select>
+'
+ ),
+
+ // multiple, many default, many preset, existing value
+ array(
+ 'multiple="true"',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ '2',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+'
+ ),
+
+ // multiple, many default, many preset, existing value
+ array(
+ 'multiple="true"',
+ '<option value="1" preset="true">1</option><option value="2" preset="true">2</option><option value="3" default="true">3</option>',
+ array('2', '3'),
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1" selected="selected">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3">3</option>
+</select>
+',
+'<select id="list" name="list[]" multiple="multiple">
+ <option value="1">1</option>
+ <option value="2" selected="selected">2</option>
+ <option value="3" selected="selected">3</option>
+</select>
+'
+ ),
+ );
+ }
+
+ /**
+ * Test the getInput method.
+ *
+ * @dataProvider caseGetInputDefaultPreset
+ */
+ public function testGetInputDefaultPreset($attributes, $options, $bind, $before, $after)
+ {
+ $form = new JFormInspector('form1');
+
+ $this->assertThat(
+ $form->load('<form><field name="list" type="list" ' . $attributes . ' >' . $options . '</field></form>'),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' XML string should load successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('list')->input,
+ $this->equalTo($before),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+
+ $this->assertThat(
+ $form->bind(array('list' => $bind)),
+ $this->isTrue(),
+ 'Line:'.__LINE__.' The data should bind successfully.'
+ );
+
+ $this->assertThat(
+ $form->getField('list')->input,
+ $this->equalTo($after),
+ 'Line:'.__LINE__.' The getInput method should return correct input.'
+ );
+ }
}

0 comments on commit 35c7b78

Please sign in to comment.
Something went wrong with that request. Please try again.