Skip to content

Commit

Permalink
Merge pull request #3879 from nboisteault/fix-nullable-bool-value-map
Browse files Browse the repository at this point in the history
Fix nullable bool value map
  • Loading branch information
nboisteault authored Sep 18, 2023
2 parents 55e89eb + 3081385 commit e35088c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 55 deletions.
37 changes: 29 additions & 8 deletions lizmap/modules/lizmap/lib/Form/QgisFormControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,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()
Expand Down
32 changes: 0 additions & 32 deletions tests/end2end/playwright/edition-form.spec.js

This file was deleted.

59 changes: 59 additions & 0 deletions tests/end2end/playwright/edition-form.spec.ts
Original file line number Diff line number Diff line change
@@ -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('');
})
})
4 changes: 2 additions & 2 deletions tests/units/classes/Form/QgisFormControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit e35088c

Please sign in to comment.