From 7764db7a34f05b55b56c2301f9c0b2a0098c2939 Mon Sep 17 00:00:00 2001 From: len-odoo Date: Wed, 27 Jun 2018 13:54:36 +0200 Subject: [FIX] web: handle onchange on a on2many with an embedded one2many on multiple pages Suppose there is a one2many field embedded in a one2many, (say we are viewing record A with one2many field B itself with a one2many C). Furthermore, there is a second page of the one2many field B. If an onchange is applied on B, then the server replies with a 1 command for all B records attached to the currently viewed object A. This includes a list of commands [[5], [4, C_id]*] for each B record, in particular B records that are on the second page. Since they are on the second page, they might not have been fetched by the frontend; in that case the corresponding C_ids are thus unknown. As a consequence, on save the frontend sends a command 0 to create the records for all unknown C_ids. Since it doesn't have any value to give to the 0 command, the row creation usually crash in the backend. Another wrong behaviour is fixed at the same time: if the server sends an update on the list of C ids with 4 commands. In that case, if the record has not been prefetched, we have no way to know if the list changed or not. To solve this, when we receive the 4 commands by the server, we send back 4 commands. coauthored by @aab-odoo opw 1835936 --- .../static/tests/fields/relational_fields_tests.js | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) (limited to 'addons/web/static/tests/fields/relational_fields_tests.js') diff --git a/addons/web/static/tests/fields/relational_fields_tests.js b/addons/web/static/tests/fields/relational_fields_tests.js index f11da4d323a..52707d9b60d 100644 --- a/addons/web/static/tests/fields/relational_fields_tests.js +++ b/addons/web/static/tests/fields/relational_fields_tests.js @@ -3206,6 +3206,136 @@ QUnit.module('relational_fields', { form.destroy(); }); + QUnit.test('onchange for embedded one2many in a one2many with a second page', function (assert) { + assert.expect(1); + + this.data.turtle.fields.partner_ids.type = 'one2many'; + this.data.turtle.records[0].partner_ids = [1]; + // we need a second page, so we set two records and only display one per page + this.data.partner.records[0].turtles = [1, 2]; + + this.data.partner.onchanges = { + turtles: function (obj) { + obj.turtles = [ + [5], + [1, 1, { + turtle_foo: "hop", + partner_ids: [[5], [4, 1]], + }], + [1, 2, { + turtle_foo: "blip", + partner_ids: [[5], [4, 2], [4, 4]], + }], + ]; + }, + }; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'
' + + '' + + '' + + '' + + '' + + '' + + '' + + '
', + res_id: 1, + mockRPC: function (route, args) { + if (args.method === 'write') { + var expectedResultTurtles = [ + [1, 1, { + turtle_foo: "hop", + }], + [1, 2, { + partner_ids: [[4, 2, false], [4, 4, false]], + turtle_foo: "blip", + }], + ]; + assert.deepEqual(args.args[1].turtles, expectedResultTurtles, + "the right values should be written"); + } + return this._super.apply(this, arguments); + } + }); + + form.$buttons.find('.o_form_button_edit').click(); + form.$('.o_data_cell').eq(1).click(); + var $cell = form.$('.o_selected_row .o_input[name=turtle_foo]'); + $cell.val("hop").trigger('change'); + form.$buttons.find('.o_form_button_save').click(); + + form.destroy(); + }); + + QUnit.test('onchange for embedded one2many in a one2many updated by server', function (assert) { + // here we test that after an onchange, the embedded one2many field has + // been updated by a new list of ids by the server response, to this new + // list should be correctly sent back at save time + assert.expect(3); + + this.data.turtle.fields.partner_ids.type = 'one2many'; + this.data.partner.records[0].turtles = [2]; + this.data.turtle.records[1].partner_ids = [2]; + + this.data.partner.onchanges = { + turtles: function (obj) { + obj.turtles = [ + [5], + [1, 2, { + turtle_foo: "hop", + partner_ids: [[5], [4, 2], [4, 4]], + }], + ]; + }, + }; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + arch:'
' + + '' + + '' + + '' + + '' + + '' + + '' + + '
', + res_id: 1, + mockRPC: function (route, args) { + if (route === '/web/dataset/call_kw/partner/write') { + var expectedResultTurtles = [ + [1, 2, { + partner_ids: [[4, 2, false], [4, 4, false]], + turtle_foo: "hop", + }], + ]; + assert.deepEqual(args.args[1].turtles, expectedResultTurtles, + 'The right values should be written'); + } + return this._super.apply(this, arguments); + } + }); + + assert.deepEqual(form.$('.o_many2many_tags_cell').text().trim(), "second record", + "the partner_ids should be as specified at initialization"); + + form.$buttons.find('.o_form_button_edit').click(); + form.$('.o_data_cell').eq(1).click(); + var $cell = form.$('.o_selected_row .o_input[name=turtle_foo]'); + $cell.val("hop").trigger("change"); + form.$buttons.find('.o_form_button_save').click(); + + assert.deepEqual(form.$('.o_many2many_tags_cell').text().trim().split(/\s+/), + [ "second", "record", "aaa" ], + 'The partner_ids should have been updated'); + + form.destroy(); + }); + QUnit.test('onchange for embedded one2many with handle widget', function (assert) { assert.expect(2); -- cgit v1.2.3