From 6397ce7eb0c4ad912259ab6d6ee3a9fbf16d0c64 Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Wed, 4 Apr 2018 13:28:35 +0200 Subject: [PATCH] [FIX] web: loss of context when duplicating MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create a vendor bill, save and validate - Duplicate the bill, save - Click on "Create" A customer invoice is created, while the user expects a vendor bill. After duplication, the new record is loaded but the context is lost. Since the default value for the invoice type is based on the context key `type`, a loss of context leads to an incorrect invoice type. opw-1826453 Co-authored-by: Géry Debongnie --- .../static/src/js/views/basic/basic_model.js | 4 ++- addons/web/static/tests/views/form_tests.js | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/js/views/basic/basic_model.js b/addons/web/static/src/js/views/basic/basic_model.js index 11e2347eba7..d7d91a65405 100644 --- a/addons/web/static/src/js/views/basic/basic_model.js +++ b/addons/web/static/src/js/views/basic/basic_model.js @@ -375,11 +375,12 @@ var BasicModel = AbstractModel.extend({ duplicateRecord: function (recordID) { var self = this; var record = this.localData[recordID]; + var context = this._getContext(record); return this._rpc({ model: record.model, method: 'copy', args: [record.data.id], - context: this._getContext(record), + context: context, }) .then(function (res_id) { var index = record.res_ids.indexOf(record.res_id); @@ -391,6 +392,7 @@ var BasicModel = AbstractModel.extend({ res_id: res_id, res_ids: record.res_ids.slice(0), viewType: record.viewType, + context: context, }); }); }, diff --git a/addons/web/static/tests/views/form_tests.js b/addons/web/static/tests/views/form_tests.js index fd70eef18a4..8acae55b915 100644 --- a/addons/web/static/tests/views/form_tests.js +++ b/addons/web/static/tests/views/form_tests.js @@ -1623,6 +1623,37 @@ QUnit.module('Views', { form.destroy(); }); + QUnit.test('duplicating a record preserve the context', function (assert) { + assert.expect(2); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: '
' + + '' + + '', + res_id: 1, + viewOptions: {sidebar: true, context: {hey: 'hoy'}}, + mockRPC: function (route, args) { + if (args.method === 'read') { + // should have 2 read, one for initial load, second for + // read after duplicating + assert.strictEqual(args.kwargs.context.hey, 'hoy', + "should have send the correct context"); + } + if (args.method === 'search_read' && args.model === 'ir.attachment') { + return $.when([]); + } + return this._super.apply(this, arguments); + }, + }); + + form.sidebar.$('a:contains(Duplicate)').click(); + + form.destroy(); + }); + QUnit.test('cannot duplicate a record', function (assert) { assert.expect(2);