Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Commit

Permalink
Merge pull request #1270 from elinw/checkbox
Browse files Browse the repository at this point in the history
Add tests for JFormFieldCheckbox and fix an issue in the field.
  • Loading branch information
pasamio committed Jun 23, 2012
2 parents 9ef09b3 + 699f641 commit 9869873
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 22 deletions.
9 changes: 8 additions & 1 deletion libraries/joomla/form/fields/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ protected function getInput()
$class = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
$disabled = ((string) $this->element['disabled'] == 'true') ? ' disabled="disabled"' : '';
$value = $this->element['value'] ? (string) $this->element['value'] : '1';
$checked = ($value == $this->value) ? ' checked="checked"' : '';
if (empty($this->value))
{
$checked = (isset($this->element['checked'] )) ? ' checked="checked"' : '';
}
else
{
$checked = ' checked="checked"';
}

// Initialize JavaScript field attributes.
$onclick = $this->element['onclick'] ? ' onclick="' . (string) $this->element['onclick'] . '"' : '';
Expand Down
107 changes: 86 additions & 21 deletions tests/suites/unit/joomla/form/fields/JFormFieldCheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,105 @@ protected function setUp()
}

/**
* Test the getInput method.
* Test the getInput method where there is no value from the element
* and no checked attribute.
*
* @return void
*
* @since 11.1
* @since 12.2
*/
public function testGetInput()
public function testGetInputNoValueNoChecked()
{
$form = new JFormInspector('form1');
$formField = new JFormFieldCheckbox;

// Test with no checked element
$element = simplexml_load_string(
'<field name="color" type="checkbox" value="red" />');
TestReflection::setValue($formField, 'element', $element);
TestReflection::setValue($formField, 'id', 'myTestId');
TestReflection::setValue($formField, 'name', 'myTestName');

$this->assertThat(
$form->load('<form><field name="checkbox" type="checkbox" /></form>'),
$this->isTrue(),
'Line:'.__LINE__.' XML string should load successfully.'
$this->assertEquals(
'<input type="checkbox" name="myTestName" id="myTestId" value="red" />',
TestReflection::invoke($formField, 'getInput'),
'The field with no value and no checked attribute did not produce the right html'
);
}

/**
* Test the getInput method where there is a value from the element
* and no checked attribute.
*
* @return void
*
* @since 12.2
*/
public function testGetInputValueNoChecked()
{
$formField = new JFormFieldCheckbox;

$field = new JFormFieldCheckbox($form);
// Test with no checked element
$element = simplexml_load_string(
'<field name="color" type="checkbox" value="red" />');
TestReflection::setValue($formField, 'element', $element);
TestReflection::setValue($formField, 'id', 'myTestId');
TestReflection::setValue($formField, 'name', 'myTestName');
TestReflection::setValue($formField, 'value', 'red');

$this->assertThat(
$field->setup($form->getXml()->field, 'value'),
$this->isTrue(),
'Line:'.__LINE__.' The setup method should return true.'
$this->assertEquals(
'<input type="checkbox" name="myTestName" id="myTestId" value="red" checked="checked" />',
TestReflection::invoke($formField, 'getInput'),
'The field with a value and no checked attribute did not produce the right html'
);
}

/**
* Test the getInput method where there is a checked attribute
*
* @return void
*
* @since 12.2
*/
public function testGetInputNoValueChecked()
{
$formField = new JFormFieldCheckbox;

$this->assertThat(
strlen($field->input),
$this->greaterThan(0),
'Line:'.__LINE__.' The getInput method should return something without error.'
// Test with checked element
$element = simplexml_load_string(
'<field name="color" type="checkbox" value="red" checked="checked" />');
TestReflection::setValue($formField, 'element', $element);
TestReflection::setValue($formField, 'id', 'myTestId');
TestReflection::setValue($formField, 'name', 'myTestName');

$this->assertEquals(
'<input type="checkbox" name="myTestName" id="myTestId" value="red" checked="checked" />',
TestReflection::invoke($formField, 'getInput'),
'The field with no value and the checked attribute did not produce the right html'
);
}

/**
* Test the getInput method where the field is disabled
*
* @return void
*
* @since 12.2
*/
public function testGetInputDisabled()
{
$formField = new JFormFieldCheckbox;

// Test with checked element
$element = simplexml_load_string(
'<field name="color" type="checkbox" value="red" disabled="true" />');
TestReflection::setValue($formField, 'element', $element);
TestReflection::setValue($formField, 'id', 'myTestId');
TestReflection::setValue($formField, 'name', 'myTestName');

$this->assertThat(
$field->input,
$this->equalTo('<input type="checkbox" name="checkbox" id="checkbox" value="1" />'),
'Line:'.__LINE__.' The getInput method should return something without error.'
$this->assertEquals(
'<input type="checkbox" name="myTestName" id="myTestId" value="red" disabled="disabled" />',
TestReflection::invoke($formField, 'getInput'),
'The field set to disabled did not produce the right html'
);
}
}

0 comments on commit 9869873

Please sign in to comment.