Commit 81fd97d9 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Checkbox: add first unit test.

parent 06ad3237
Pipeline #2898 failed with stages
in 2 minutes and 44 seconds
......@@ -772,6 +772,11 @@ EOF;
if ($wrapName == FE_WRAP_LABEL) {
$wrapArray[0] = Support::insertAttribute($wrapArray[0], 'style', 'text-align: ' . $formElement[F_FE_LABEL_ALIGN] . ';'); // might be problematic, if there is already a 'class' defined.
}
// Insert 'required="required"' for checkboxes and radios
if ($wrapName == FE_WRAP_INPUT && $formElement[FE_MODE] == FE_MODE_REQUIRED &&
($formElement[FE_TYPE] == FE_TYPE_CHECKBOX || $formElement[FE_TYPE] == FE_TYPE_RADIO)) {
$wrapArray[0] = Support::insertAttribute($wrapArray[0], 'required', 'required'); // might be problematic, if there is already a 'class' defined.
}
}
return $wrapArray[0] . $htmlElement . $wrapArray[1];
......
......@@ -1252,6 +1252,7 @@ const FE_INPUT_AUTOCOMPLETE = 'autocomplete';
const FE_TMP_EXTRA_BUTTON_HTML = '_extraButtonHtml'; // will be filled on the fly during building extrabutton
const FE_CHECKBOX_CHECKED = 'checked';
const FE_CHECKBOX_UNCHECKED = 'unchecked';
const FE_ITEM_LIST = 'itemList';
const FE_RECORD_DESTINATION_TABLE = 'recordDestinationTable';
const FE_RECORD_SOURCE_TABLE = 'recordSourceTable';
const FE_TRANSLATE_ID_COLUMN = 'translateIdColumn';
......
......@@ -50,12 +50,22 @@ class Checkbox {
/**
* Builds HTML 'checkbox' element.
*
* Checkboxes will only be submitted, if they are checked. Therefore, a hidden element with the unchecked value
* will be transferred first.
* There are 6 different modes:
*
* Format: <input type="hidden" name="$htmlFormElementName" value="$valueUnChecked">
* <input name="$htmlFormElementName" type="checkbox" [autofocus="autofocus"]
* [required="required"] [disabled="disabled"] value="<value>" [checked="checked"] >
* * buildCheckboxSingle
*
* * SB) constructCheckboxSingleBootstrap
* * SP) constructCheckboxSinglePlain
*
* * buildCheckboxMulti
*
* * constructCheckboxMultiButton
* * MBH) Horizontal
* * MBV) Vertical
*
* * constructCheckboxMultiPlain
* * MPH) Horizontal
* * MPV) Vertical
*
* @param array $formElement
* @param string $htmlFormElementName
......@@ -68,7 +78,7 @@ class Checkbox {
* @throws \UserFormException
* @throws \UserReportException
*/
public function build(array $formElement, $htmlFormElementName, $value, array &$json, $mode = FORM_LOAD) {
public function build(array $formElement, $htmlFormElementName, $value, array &$json, $mode) {
$itemKey = array();
$itemValue = array();
......@@ -110,7 +120,7 @@ class Checkbox {
}
/**
* For CheckBox's with only one checkbox: if no parameter:checked|unchecked is defined, take defaults:
* For CheckBox's with only one checkbox: if parameter.checked|unchecked is not defined, take defaults:
*
* checked: first Element in $itemKey
* unchecked: ''
......@@ -336,7 +346,7 @@ class Checkbox {
// }
$checkboxLabelId = HelperFormElement::getCheckboxRadioOptionId($formElement[FE_HTML_ID], $ii, HTML_ID_EXTENSION_LABEL);
$htmlElement = Support::wrapTag("<label class='$checkboxClass' $attributeBaseLabel id='$checkboxLabelId'>", $htmlElement, true);
$htmlElement = Support::wrapTag("<label class=\"$checkboxClass\" $attributeBaseLabel id=\"$checkboxLabelId\">", $htmlElement, true);
// control orientation
if ($formElement[FE_MAX_LENGTH] > 1) {
......
......@@ -612,8 +612,8 @@ EOF;
$itemValue = array_column($formElement[FE_SQL1], $keys[$idx]);
}
}
} elseif (isset($formElement['itemList']) && strlen($formElement['itemList']) > 0) {
$arr = KeyValueStringParser::parse($formElement['itemList'], ':', ',', KVP_IF_VALUE_EMPTY_COPY_KEY);
} elseif (isset($formElement[FE_ITEM_LIST]) && strlen($formElement[FE_ITEM_LIST]) > 0) {
$arr = KeyValueStringParser::parse($formElement[FE_ITEM_LIST], ':', ',', KVP_IF_VALUE_EMPTY_COPY_KEY);
$itemValue = array_values($arr);
$itemKey = array_keys($arr);
} elseif ($fieldType === 'enum' || $fieldType === 'set') {
......
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/2/16
* Time: 9:16 PM
*/
namespace IMATHUZH\Qfq\Tests\Unit\Core\Store;
use IMATHUZH\Qfq\Core\Form\Checkbox;
use PHPUnit\Framework\TestCase;
/**
* Class CheckboxTest
* @package qfq
*/
class CheckboxTest extends TestCase {
/**
*
* @throws \CodeException
* @throws \UserFormException
* @throws \UserReportException
*/
public function testCheckboxMultiPlainHorizontal() {
$formElement = array();
$formElementName = '1-2-3-4';
$rcJson = array();
$checkbox = new Checkbox();
$formElement[FE_ID] = '123';
$formElement[FE_NAME] = 'gender';
$formElement[FE_TYPE] = FE_TYPE_CHECKBOX;
$formElement[FE_MODE] = FE_MODE_SHOW;
$formElement[FE_SQL1] = '';
$formElement[FE_ITEM_LIST] = 'a,b,c';
$formElement[FE_DYNAMIC_UPDATE] = 'no';
$formElement[F_FE_MIN_WIDTH] = F_FE_MIN_WIDTH_DEFAULT;
$formElement[FE_MAX_LENGTH] = 10;
$formElement[FE_HTML_ID] = $formElementName . '-i';
$formElement[FE_DATA_REFERENCE] = '';
$html = $checkbox->build($formElement, $formElementName, '', $rcJson, FORM_LOAD);
$expected = '<label class="checkbox-inline" style="min-width: 80px; font-weight: 400;" id="1-2-3-4-i-0-l"><input data-hidden="no" data-disabled="no" data-required="no" type="checkbox" id="1-2-3-4-i-0" name="1-2-3-4[]" data-reference="-0" value="a" >a</label><label class="checkbox-inline" style="min-width: 80px; font-weight: 400;" id="1-2-3-4-i-1-l"><input data-hidden="no" data-disabled="no" data-required="no" type="checkbox" id="1-2-3-4-i-1" name="1-2-3-4[]" data-reference="-1" value="b" >b</label><label class="checkbox-inline" style="min-width: 80px; font-weight: 400;" id="1-2-3-4-i-2-l"><input data-hidden="no" data-disabled="no" data-required="no" type="checkbox" id="1-2-3-4-i-2" name="1-2-3-4[]" data-reference="-2" value="c" >c</label><div class="help-block with-errors hidden"></div>';
$this->assertEquals($expected, $html);
}
// protected function setUp() {
// $_GET = array();
// $_POST = array();
// }
//
}
......@@ -13,7 +13,7 @@ use IMATHUZH\Qfq\Core\Store\Config;
use PHPUnit\Framework\TestCase;
/**
* Class StoreTest
* Class ConfigTest
* @package qfq
*/
class ConfigTest extends TestCase {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment