From 5b9cfff00cbbdb6bbd06026c39fe8acd050aae85 Mon Sep 17 00:00:00 2001 From: std-odoo Date: Wed, 28 Feb 2024 15:54:28 +0100 Subject: [PATCH] [FIX] web: properties, date / datetime default values are never set Bug === Since c5b87e130bd69996c9121227bb6d085035c362fa , the Date / Datetime are not formatted the way they suppose to be. So the value is not correct (it contains the timezone, etc). Because of that, the string "Invalid Date" is stored in database instead of "YYY-mm-dd" when we change the default value in the definition. Task-3774178 closes odoo/odoo#155749 Signed-off-by: Thibault Delavallee (tde) --- .../views/fields/properties/property_value.js | 8 ++- .../views/fields/properties_field_tests.js | 63 ++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/views/fields/properties/property_value.js b/addons/web/static/src/views/fields/properties/property_value.js index 38d8072160a..87e7e41007b 100644 --- a/addons/web/static/src/views/fields/properties/property_value.js +++ b/addons/web/static/src/views/fields/properties/property_value.js @@ -12,6 +12,8 @@ import { deserializeDateTime, formatDate, formatDateTime, + serializeDate, + serializeDateTime, } from "@web/core/l10n/dates"; import { _t } from "@web/core/l10n/translation"; import { TagsList } from "@web/core/tags_list/tags_list"; @@ -202,7 +204,11 @@ export class PropertyValue extends Component { * @param {object} newValue */ async onValueChange(newValue) { - if (this.props.type === "integer") { + if (this.props.type === "datetime") { + newValue = newValue && serializeDateTime(newValue); + } else if (this.props.type === "date") { + newValue = newValue && serializeDate(newValue); + } else if (this.props.type === "integer") { try { newValue = parseInteger(newValue) || 0; } catch { diff --git a/addons/web/static/tests/views/fields/properties_field_tests.js b/addons/web/static/tests/views/fields/properties_field_tests.js index 8e6f85ba7bd..d37c0008f62 100644 --- a/addons/web/static/tests/views/fields/properties_field_tests.js +++ b/addons/web/static/tests/views/fields/properties_field_tests.js @@ -11,6 +11,7 @@ import { nextTick, patchWithCleanup, triggerEvent, + patchDate, } from "@web/../tests/helpers/utils"; import { toggleActionMenu } from "@web/../tests/search/helpers"; import { createWebClient, doAction } from "@web/../tests/webclient/helpers"; @@ -39,6 +40,7 @@ async function changeType(target, propertyType) { char: 1, integer: 3, float: 4, + date: 5, datetime: 6, selection: 7, tags: 8, @@ -513,7 +515,7 @@ QUnit.module("Fields", (hooks) => { assert.containsOnce( target, ".o_cp_action_menus span:contains(Add Properties)", - "The add button must be in the cog menu", + "The add button must be in the cog menu" ); // Create a new property @@ -1863,6 +1865,65 @@ QUnit.module("Fields", (hooks) => { } }); + QUnit.test("properties: default value date", async function (assert) { + async function mockRPC(route, { method, model, kwargs }) { + if (["check_access_rights", "check_access_rule"].includes(method)) { + return true; + } + } + + patchDate(2022, 0, 3, 8, 0, 0); + + await makeView({ + type: "form", + resModel: "partner", + resId: 1, + serverData, + arch: ` +
+ + + + + + +
`, + mockRPC, + actionMenus: {}, + }); + + const field = target.querySelector(".o_field_properties"); + assert.ok(field, "The field must be in the view"); + + // add a new date property + await toggleActionMenu(target); + await click(target, ".o_cp_action_menus span .fa-cogs"); + await nextTick(); + let popover = target.querySelector(".o_property_field_popover"); + assert.ok(popover, "Should have opened the definition popover"); + await changeType(target, "date"); + const type = popover.querySelector(".o_field_property_definition_type input"); + assert.strictEqual(type.value, "Date", "Should have changed the property type"); + // choose a default value and check that it is propagated on the property field + await click(target, ".o_field_property_definition_value .o_datetime_input"); + assert.containsOnce(target, ".o_date_picker"); + await click(getPickerCell("3").at(0)); + await closePopover(target); + assert.strictEqual( + target.querySelector(".o_datetime_input").value, + "01/03/2022", + "The default date value should have been propagated" + ); + // save the form and check that the default value is not reset + await click(target, ".o_form_button_save"); + await click(target, ".o_property_field:nth-last-child(2) .o_field_property_open_popover"); + popover = target.querySelector(".o_property_field_popover"); + assert.strictEqual( + popover.querySelector(".o_field_property_definition_value input").value, + "01/03/2022" + ); + }); + /** * check if property field popover closes when clicking on delete property icon. */