diff --git a/lizmap/modules/lizmap/lib/Form/QgisFormControl.php b/lizmap/modules/lizmap/lib/Form/QgisFormControl.php index 28c4ad05dd..9dab55ca45 100644 --- a/lizmap/modules/lizmap/lib/Form/QgisFormControl.php +++ b/lizmap/modules/lizmap/lib/Form/QgisFormControl.php @@ -506,14 +506,35 @@ protected function fillControlDatasource() case 'ValueMap': $valueMap = $this->properties->getValueMap(); if (is_array($valueMap)) { - // remove the QGIS null value if the control value is not - // required, as the widget will have a possibility to choose - // no value (so null value... ?) - if (!$this->required - && $this->fieldDataType == 'boolean' - && isset($valueMap[self::QGIS_NULL_VALUE]) - ) { - unset($valueMap[self::QGIS_NULL_VALUE]); + // Override values for boolean + if ($this->fieldDataType == 'boolean') { + // remove the QGIS null value if the control value is not + // required, as the widget will have a possibility to choose + // no value (so null value... ?) + if (!$this->required + && isset($valueMap[self::QGIS_NULL_VALUE]) + ) { + unset($valueMap[self::QGIS_NULL_VALUE]); + } + // transform values from QGIS ValueMap to Postgres Boolean + $booleanValues = array(); + foreach ($valueMap as $v => $label) { + $strV = strtolower($v); + if ($v === self::QGIS_NULL_VALUE) { + $booleanValues[self::QGIS_NULL_VALUE] = $label; + } elseif ($strV === 'true' || $strV === 't' + || intval($v) === 1 || $strV === 'on') { + // Postgres true + $booleanValues['t'] = $label; + } elseif ($strV === 'false' || $strV === 'f' + || intval($v) === 0 || $strV === 'off') { + // Postgres false + $booleanValues['f'] = $label; + } else { + $booleanValues[$strV] = $label; + } + } + $valueMap = $booleanValues; } // we don't use array_merge, because this function reindexes keys if they are // numerical values, and this is not what we want. diff --git a/tests/end2end/cypress/integration/form_edition_all_field_type-ghaction.js b/tests/end2end/cypress/integration/form_edition_all_field_type-ghaction.js index fce0961620..6d45ef8372 100644 --- a/tests/end2end/cypress/integration/form_edition_all_field_type-ghaction.js +++ b/tests/end2end/cypress/integration/form_edition_all_field_type-ghaction.js @@ -103,19 +103,6 @@ describe('Form edition all field type', function() { cy.get('#lizmap-edition-message').should('be.visible') }) - it('boolean, dropdown menu', function () { - // `boolean_nullable` should show a dropdown menu with : - // * an empty value - cy.get('#jforms_view_edition_boolean_nullable').select('') - cy.get('#jforms_view_edition_boolean_nullable').should('have.value', '') - // * a true value - cy.get('#jforms_view_edition_boolean_nullable').select('True') - cy.get('#jforms_view_edition_boolean_nullable').should('have.value', 'true') - // * a false value - cy.get('#jforms_view_edition_boolean_nullable').select('False') - cy.get('#jforms_view_edition_boolean_nullable').should('have.value', 'false') - }) - it('boolean, not null', function () { cy.get('#jforms_view_edition_boolean_notnull_for_checkbox').should('have.class', 'jforms-ctrl-checkbox') cy.get('#jforms_view_edition__submit_submit').click() diff --git a/tests/end2end/playwright/edition-form.spec.ts b/tests/end2end/playwright/edition-form.spec.ts new file mode 100644 index 0000000000..e25b0cf49a --- /dev/null +++ b/tests/end2end/playwright/edition-form.spec.ts @@ -0,0 +1,59 @@ +import { test, expect } from '@playwright/test'; + +test.beforeEach(async ({ page }) => { + const url = '/index.php/view/map/?repository=testsrepository&project=form_edition_all_field_type'; + await page.goto(url, { waitUntil: 'networkidle' }); +}); + +test.describe('Edition Form Validation', () => { + + test('Input type number with range and step', async ({ page }) => { + // display form + await page.locator('#button-edition').click(); + await page.locator('a#edition-draw').click(); + + // ensure input attributes match with field config defined in project + await expect(page.locator('#jforms_view_edition input[name="integer_field"]')).toHaveAttribute('type','number') + await expect(page.locator('#jforms_view_edition input[name="integer_field"]')).toHaveAttribute('step','5'); + await expect(page.locator('#jforms_view_edition input[name="integer_field"]')).toHaveAttribute('min','-200'); + await expect(page.locator('#jforms_view_edition input[name="integer_field"]')).toHaveAttribute('max','200'); + + // add data + await page.locator('#jforms_view_edition input[name="integer_field"]').fill('50'); + + // submit form + await page.locator('#jforms_view_edition__submit_submit').click(); + // will close & show message + await expect(page.locator('#edition-form-container')).toBeHidden(); + await expect(page.locator('#lizmap-edition-message')).toBeVisible(); + }) + + test('Boolean nullable w/ value map', async ({ page }) => { + + let editFeatureRequestPromise = page.waitForResponse(response => response.url().includes('editFeature')); + + await page.locator('#button-edition').click(); + await page.locator('#edition-layer').selectOption('many_bool_formats_7aa4cb8a_09ae_4a5b_92e4_189a42ca3a2f'); + await page.locator('#edition-draw').click(); + await page.locator('#jforms_view_edition_liz_future_action').selectOption('edit'); + await page.getByLabel('bool_simple_null_vm').selectOption('t'); + await page.locator('#jforms_view_edition__submit_submit').click(); + + await editFeatureRequestPromise; + + // Wait a bit for the UI to refresh + await page.waitForTimeout(300); + + await expect(page.getByLabel('bool_simple_null_vm')).toHaveValue('t'); + + await page.getByLabel('bool_simple_null_vm').selectOption(''); + await page.locator('#jforms_view_edition__submit_submit').click(); + + await editFeatureRequestPromise; + + // Wait a bit for the UI to refresh + await page.waitForTimeout(300); + + await expect(page.getByLabel('bool_simple_null_vm')).toHaveValue(''); + }) +}) \ No newline at end of file diff --git a/tests/units/classes/Form/QgisFormControlTest.php b/tests/units/classes/Form/QgisFormControlTest.php index e4fb9b4c9b..520b89d2b1 100644 --- a/tests/units/classes/Form/QgisFormControlTest.php +++ b/tests/units/classes/Form/QgisFormControlTest.php @@ -367,9 +367,9 @@ public function testConstructCheckbox() $this->assertEquals($control->fieldDataType, 'boolean'); $this->assertEquals($control->fieldEditType, 'CheckBox'); $this->assertEquals($control->ctrl->getWidgetType(), 'checkbox'); - $this->assertEquals($control->ctrl->valueOnCheck, 'true'); + $this->assertEquals($control->ctrl->valueOnCheck, 't'); $this->assertEquals($control->ctrl->valueLabelOnCheck, 'Yes'); - $this->assertEquals($control->ctrl->valueOnUncheck, 'false'); + $this->assertEquals($control->ctrl->valueOnUncheck, 'f'); $this->assertEquals($control->ctrl->valueLabelOnUncheck, 'No'); } }