From 301076e2cec0c9fdbdd23262f2fcd801fa274a77 Mon Sep 17 00:00:00 2001 From: "Louis Wicket (wil)" Date: Fri, 13 May 2022 07:44:36 +0000 Subject: [PATCH] [FIX] mail: fix default value of date in clock model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first date should be the date of the record creation instead of the time the model was initialized. closes odoo/odoo#91372 X-original-commit: 2635b29193ba16d85f62aabd8bddd7d7692007f0 Signed-off-by: Sébastien Theys (seb) Signed-off-by: Louis Wicket (wil) --- .../mail/static/src/models/activity_view.js | 3 +++ addons/mail/static/src/models/clock.js | 16 ++++++++++++-- addons/mail/static/src/models/message_view.js | 3 +++ .../qunit_suite_tests/models/clock_tests.js | 22 +++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/addons/mail/static/src/models/activity_view.js b/addons/mail/static/src/models/activity_view.js index ad50978c349..82f90882fba 100644 --- a/addons/mail/static/src/models/activity_view.js +++ b/addons/mail/static/src/models/activity_view.js @@ -78,6 +78,9 @@ registerModel({ if (!this.activity.dateDeadline) { return clear(); } + if (!this.clockWatcher.clock.date) { + return clear(); + } const today = moment(this.clockWatcher.clock.date.getTime()).startOf('day'); const momentDeadlineDate = moment(auto_str_to_date(this.activity.dateDeadline)); // true means no rounding diff --git a/addons/mail/static/src/models/clock.js b/addons/mail/static/src/models/clock.js index e4a1a6720e1..02ad0cf1c4f 100644 --- a/addons/mail/static/src/models/clock.js +++ b/addons/mail/static/src/models/clock.js @@ -16,6 +16,17 @@ registerModel({ }, }, recordMethods: { + /** + * The date is set by a compute rather than using a default value. Thus, + * the date set at first is the time of the record creation, and not the + * time of the model initialization. + * + * @private + * @returns {Date} + */ + _computeDate() { + return new Date(); + }, /** * @private * @returns {integer} @@ -40,10 +51,11 @@ registerModel({ }, fields: { /** - * A Date object set at the current date, updated at every tick. + * A Date object set to the current date at the time the record is + * created, then updated at every tick. */ date: attr({ - default: new Date(), + compute: '_computeDate', }), /** * An integer representing the frequency in milliseconds at which `date` diff --git a/addons/mail/static/src/models/message_view.js b/addons/mail/static/src/models/message_view.js index 949847641ae..e70c7b0d71d 100644 --- a/addons/mail/static/src/models/message_view.js +++ b/addons/mail/static/src/models/message_view.js @@ -189,6 +189,9 @@ registerModel({ if (!this.message.date) { return clear(); } + if (!this.clockWatcher.clock.date) { + return clear(); + } const now = moment(this.clockWatcher.clock.date.getTime()); if (now.diff(this.message.date, 'seconds') < 45) { return this.env._t("now"); diff --git a/addons/mail/static/tests/qunit_suite_tests/models/clock_tests.js b/addons/mail/static/tests/qunit_suite_tests/models/clock_tests.js index 42f73abe3ee..e759a1e2492 100644 --- a/addons/mail/static/tests/qunit_suite_tests/models/clock_tests.js +++ b/addons/mail/static/tests/qunit_suite_tests/models/clock_tests.js @@ -3,6 +3,8 @@ import { insertAndReplace } from '@mail/model/model_field_command'; import { start } from '@mail/../tests/helpers/test_utils'; +import { patchDate } from '@web/../tests/helpers/utils'; + QUnit.module('mail', {}, function () { QUnit.module('models', {}, function () { QUnit.module('clock_tests.js'); @@ -24,5 +26,25 @@ QUnit.test('Deleting all the watchers of a clock should result in the deletion o ); }); +QUnit.test('[technical] Before ticking for the first time, the clock should indicate the date of creation of the record.', async function (assert) { + assert.expect(1); + + const { messaging } = await start(); + // The date is patched AFTER startup, so if the date field in Clock was set + // at initialization (which we don't want), it will now look completely + // different from the patched date. + patchDate(2016, 8, 8, 14, 55, 15, 352); + + const { clock } = messaging.models['ClockWatcher'].insert({ + clock: insertAndReplace({ frequency: 3600 * 1000 }), + qunitTestOwner: insertAndReplace(), + }); + assert.strictEqual( + clock.date.getFullYear(), // no need to be more precise than the year + 2016, + "before ticking for the first time, the clock should indicate the date of creation of the record." + ); +}); + }); });