[FIX] web: BasicField: use getSession

`session` is not an field widget attribute anymore.
One should use `getSession` instead.

This commit also introduces tests for the `binary` field.
This commit is contained in:
Martin Geubelle
2017-04-27 16:56:46 +02:00
parent 0b3b34ae4c
commit 776cd64aa3
2 changed files with 61 additions and 3 deletions
@@ -957,7 +957,7 @@ var AbstractFieldBinary = AbstractField.extend({
self.on_file_uploaded(file.size, file.name, file.type, data);
};
} else {
this.$('form.o_form_binary_form input[name=session_id]').val(this.session.session_id);
this.$('form.o_form_binary_form input[name=session_id]').val(this.getSession().session_id);
this.$('form.o_form_binary_form').submit();
}
this.$('.o_form_binary_progress').show();
@@ -1102,7 +1102,7 @@ var FieldBinaryFile = AbstractFieldBinary.extend({
framework.blockUI();
var c = crash_manager;
var filename_fieldname = this.attrs.filename;
this.session.get_file({
this.getSession().get_file({
'url': '/web/content',
'data': {
'model': this.model,
@@ -1114,7 +1114,7 @@ var FieldBinaryFile = AbstractFieldBinary.extend({
'data': utils.is_bin_size(this.value) ? null : this.value,
},
'complete': framework.unblockUI,
'error': c.rpc_error.bind(c)
'error': c.rpc_error.bind(c),
});
ev.stopPropagation();
}
@@ -5,6 +5,7 @@ var concurrency = require('web.concurrency');
var FormView = require('web.FormView');
var KanbanView = require('web.KanbanView');
var ListView = require('web.ListView');
var session = require('web.session');
var testUtils = require('web.test_utils');
var createView = testUtils.createView;
@@ -31,6 +32,7 @@ QUnit.module('basic_fields', {
currency_id: {string: "Currency", type: "many2one", relation: "currency", searchable: true},
selection: {string: "Selection", type: "selection", searchable:true,
selection: [['normal', 'Normal'],['blocked', 'Blocked'],['done', 'Done']]},
document: {string: "Binary", type: "binary"},
},
records: [{
id: 1,
@@ -45,6 +47,7 @@ QUnit.module('basic_fields', {
timmy: [],
trululu: 4,
selection: 'blocked',
document: 'coucou==\n',
}, {
id: 2,
display_name: "second record",
@@ -755,6 +758,61 @@ QUnit.module('basic_fields', {
form.destroy();
});
QUnit.module('FieldBinary');
QUnit.test('binary fields are correctly rendered', function (assert) {
assert.expect(9);
// save the session function
var oldGetFile = session.get_file;
session.get_file = function (option) {
assert.strictEqual(option.data.field, 'document',
"we should download the field document");
assert.strictEqual(option.data.data, 'coucou==\n',
"we should download the correct data");
option.complete();
return $.when();
};
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form string="Partners">' +
'<field name="document"/>' +
'</form>',
res_id: 1,
});
assert.strictEqual(form.$('a.o_form_field[name="document"] > .fa-download').length, 1,
"the binary field should be rendered as a downloadable link in readonly");
form.$('a.o_form_field[name="document"]').click();
form.$buttons.find('.o_form_button_edit').click();
assert.strictEqual(form.$('a.o_form_field[name="document"] > .fa-download').length, 0,
"the binary field should not be rendered as a downloadable link in edit");
assert.strictEqual(form.$('div.o_form_field_binary_file[name="document"]').length, 1,
"the binary field should be correctly rendered in edit");
assert.strictEqual(form.$('.o_form_field_binary_file > input').attr('readonly'), 'readonly',
"the input should be readonly");
assert.strictEqual(form.$('.o_form_field_binary_file > .o_clear_file_button').length, 1,
"there shoud be a button to clear the file");
form.$('.o_form_field_binary_file > .o_clear_file_button').click();
assert.ok(form.$('.o_form_field_binary_file > input').hasClass('o_hidden'),
"the input should be hidden");
assert.strictEqual(form.$('.o_form_field_binary_file > .o_select_file_button:not(.o_hidden)').length, 1,
"there shoud be a button to upload the file");
form.destroy();
// restore the session function
session.get_file = oldGetFile;
});
QUnit.test('text field rendering in list view', function (assert) {
assert.expect(1);