From 9ba0767f983763895ad1655184cffce6ffa0e027 Mon Sep 17 00:00:00 2001
From: Christophe Matthieu <chm@odoo.com>
Date: Wed, 3 May 2017 14:01:09 +0200
Subject: [PATCH] [FIX] web: unable to open User FormView from Settings

Uncaught TypeError: Cannot read property 'split' of undefined.
Crash when the value of widget 'timezone_mismatch' is false in form view.
---
 .../static/src/js/fields/special_fields.js    |  4 +--
 .../web/static/tests/fields/special_fields.js | 35 ++++++++++++++++++-
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/addons/web/static/src/js/fields/special_fields.js b/addons/web/static/src/js/fields/special_fields.js
index 7f4c73788821..69647902afec 100644
--- a/addons/web/static/src/js/fields/special_fields.js
+++ b/addons/web/static/src/js/fields/special_fields.js
@@ -71,6 +71,7 @@ var FieldTimezoneMismatch = FieldSelection.extend({
         // we need to clean the warning to have maximum one alert
         this.$el.last().filter('.o_tz_warning').remove();
         this.$el = this.$el.first();
+        var value = this.$el.val();
 
         if (this.$option) {
             this.$option.html(this.$option.html().split(' ')[0]);
@@ -78,7 +79,7 @@ var FieldTimezoneMismatch = FieldSelection.extend({
 
         var userOffset = this.recordData.tz_offset;
         this.mismatch = false;
-        if (userOffset) {
+        if (userOffset && value !== "" && value !== "false") {
             var offset = -(new Date().getTimezoneOffset());
             var browserOffset = (offset < 0) ? "-" : "+";
             browserOffset += _.str.sprintf("%02d", Math.abs(offset / 60));
@@ -92,7 +93,6 @@ var FieldTimezoneMismatch = FieldSelection.extend({
             $span.attr('title', _t("Timezone Mismatch : The timezone of your browser doesn't match the selected one. The time in Odoo is displayed according to your field timezone."));
             this.$el = this.$el.add($span);
 
-            var value = this.$el.val();
             this.$option = this.$('option').filter(function () {
                 return $(this).attr('value') === value;
             });
diff --git a/addons/web/static/tests/fields/special_fields.js b/addons/web/static/tests/fields/special_fields.js
index af725a007f1d..6786f6783b79 100644
--- a/addons/web/static/tests/fields/special_fields.js
+++ b/addons/web/static/tests/fields/special_fields.js
@@ -1,6 +1,7 @@
 odoo.define('web.special_fields_tests', function (require) {
 "use strict";
 
+var FormView = require('web.FormView');
 var ListView = require('web.ListView');
 var testUtils = require('web.test_utils');
 
@@ -143,7 +144,10 @@ QUnit.module('special_fields', {
     QUnit.test('widget timezone_mismatch in a list view', function (assert) {
         assert.expect(5);
 
-        this.data.partner.fields.tz_offset = {string: "tz_offset", type: "integer"};
+        this.data.partner.fields.tz_offset = {
+            string: "tz_offset",
+            type: "char"
+        };
         this.data.partner.records.forEach(function (r) {
             r.color = 'red';
             r.tz_offset = 0;
@@ -180,6 +184,35 @@ QUnit.module('special_fields', {
         list.destroy();
     });
 
+    QUnit.test('widget timezone_mismatch in a form view', function (assert) {
+        assert.expect(1);
+
+        this.data.partner.fields.tz_offset = {
+            string: "tz_offset",
+            type: "char"
+        };
+        this.data.partner.fields.tz = {
+            type: "selection",
+            selection: [['Europe/Brussels', "Europe/Brussels"], ['America/Los_Angeles', "America/Los_Angeles"]],
+        };
+        this.data.partner.records[0].tz = false;
+        this.data.partner.records[0].tz_offset = '+4800';
+
+        var form = createView({
+            View: FormView,
+            model: 'partner',
+            res_id: 1,
+            data: this.data,
+            arch: '<form>' +
+                    '<field name="tz_offset" invisible="True"/>' +
+                    '<field name="tz" widget="timezone_mismatch"/>' +
+                '</form>',
+        });
+        form.$buttons.find('.o_form_button_edit').click();
+        assert.strictEqual(form.$('select').length, 1, "should have the select field");
+        form.destroy();
+    });
+
 });
 });
 });
-- 
GitLab