1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
odoo.define('web.control_panel_tests', function (require) {
"use strict";
const testUtils = require('web.test_utils');
const cpHelpers = testUtils.controlPanel;
const { createControlPanel } = testUtils;
QUnit.module('ControlPanel', {
beforeEach() {
this.fields = {
display_name: { string: "Displayed name", type: 'char', searchable: true },
foo: { string: "Foo", type: "char", default: "My little Foo Value", store: true, sortable: true, searchable: true },
date_field: { string: "Date", type: "date", store: true, sortable: true, searchable: true },
float_field: { string: "Float", type: "float", searchable: true },
bar: { string: "Bar", type: "many2one", relation: 'partner', searchable: true },
};
}
}, function () {
QUnit.test('default field operator', async function (assert) {
assert.expect(2);
const fields = {
foo_op: { string: "Foo Op", type: "char", store: true, sortable: true, searchable: true },
foo: { string: "Foo", type: "char", store: true, sortable: true, searchable: true },
bar_op: { string: "Bar Op", type: "many2one", relation: 'partner', searchable: true },
bar: { string: "Bar", type: "many2one", relation: 'partner', searchable: true },
selec: { string: "Selec", type: "selection", selection: [['red', "Red"], ['black', "Black"]] },
};
const arch = `
<search>
<field name="bar"/>
<field name="bar_op" operator="child_of"/>
<field name="foo"/>
<field name="foo_op" operator="="/>
<field name="selec"/>
</search>`;
const searchMenuTypes = [];
const params = {
cpModelConfig: {
arch,
fields,
context: {
show_filterC: true,
search_default_bar: 10,
search_default_bar_op: 10,
search_default_foo: "foo",
search_default_foo_op: "foo_op",
search_default_selec: 'red',
},
searchMenuTypes,
},
cpProps: { fields, searchMenuTypes },
env: {
session: {
async rpc() {
return [[10, "Deco Addict"]];
},
},
},
};
const controlPanel = await createControlPanel(params);
assert.deepEqual(
cpHelpers.getFacetTexts(controlPanel).map(t => t.replace(/\s/g, "")),
[
"BarDecoAddict",
"BarOpDecoAddict",
"Foofoo",
"FooOpfoo_op",
"SelecRed"
]
);
assert.deepEqual(
controlPanel.getQuery().domain,
[
"&", "&", "&", "&",
["bar", "=", 10],
["bar_op", "child_of", 10],
["foo", "ilike", "foo"],
["foo_op", "=", "foo_op"],
["selec", "=", "red"],
]
);
controlPanel.destroy();
});
QUnit.module('Keyboard navigation');
QUnit.test('remove a facet with backspace', async function (assert) {
assert.expect(2);
const params = {
cpModelConfig: {
arch: `<search> <field name="foo"/></search>`,
fields: this.fields,
context: { search_default_foo: "a" },
searchMenuTypes: ['filter'],
},
cpProps: { fields: this.fields },
};
const controlPanel = await createControlPanel(params);
assert.deepEqual(cpHelpers.getFacetTexts(controlPanel), ['Foo\na']);
// delete a facet
const searchInput = controlPanel.el.querySelector('input.o_searchview_input');
await testUtils.dom.triggerEvent(searchInput, 'keydown', { key: 'Backspace' });
assert.containsNone(controlPanel, 'div.o_searchview div.o_searchview_facet');
// delete nothing (should not crash)
await testUtils.dom.triggerEvent(searchInput, 'keydown', { key: 'Backspace' });
controlPanel.destroy();
});
QUnit.test('fields and filters with groups/invisible attribute', async function (assert) {
// navigation and automatic menu closure don't work here (i don't know why yet) -->
// should be tested separatly
assert.expect(16);
const arch = `
<search>
<field name="display_name" string="Foo B" invisible="1"/>
<field name="foo" string="Foo A"/>
<filter name="filterA" string="FA" domain="[]"/>
<filter name="filterB" string="FB" invisible="1" domain="[]"/>
<filter name="filterC" string="FC" invisible="not context.get('show_filterC')" domain="[]"/>
<filter name="groupByA" string="GA" context="{ 'group_by': 'date_field:day' }"/>
<filter name="groupByB" string="GB" context="{ 'group_by': 'date_field:day' }" invisible="1"/>
</search>`;
const searchMenuTypes = ['filter', 'groupBy'];
const params = {
cpModelConfig: {
arch,
fields: this.fields,
context: {
show_filterC: true,
search_default_display_name: 'value',
search_default_filterB: true,
search_default_groupByB: true
},
searchMenuTypes
},
cpProps: { fields: this.fields, searchMenuTypes },
};
const controlPanel = await createControlPanel(params);
function selectorContainsValue(selector, value, shouldContain) {
const elements = [...controlPanel.el.querySelectorAll(selector)];
const regExp = new RegExp(value);
const matches = elements.filter(el => regExp.test(el.innerText.replace(/\s/g, "")));
assert.strictEqual(matches.length, shouldContain ? 1 : 0,
`${selector} in the control panel should${shouldContain ? '' : ' not'} contain "${value}".`
);
}
// default filters/fields should be activated even if invisible
assert.containsN(controlPanel, 'div.o_searchview_facet', 3);
selectorContainsValue('.o_searchview_facet', "FooBvalue", true);
selectorContainsValue('.o_searchview_facet .o_facet_values', "FB", true);
selectorContainsValue('.o_searchview_facet .o_facet_values', "GB", true);
await cpHelpers.toggleFilterMenu(controlPanel);
selectorContainsValue('.o_menu_item a', "FA", true);
selectorContainsValue('.o_menu_item a', "FB", false);
selectorContainsValue('.o_menu_item a', "FC", true);
await cpHelpers.toggleGroupByMenu(controlPanel);
selectorContainsValue('.o_menu_item a', "GA", true);
selectorContainsValue('.o_menu_item a', "GB", false);
// 'a' to filter nothing on bar
await cpHelpers.editSearch(controlPanel, 'a');
// the only item in autocomplete menu should be FooA: a
selectorContainsValue('.o_searchview_autocomplete', "SearchFooAfor:a", true);
await cpHelpers.validateSearch(controlPanel);
selectorContainsValue('.o_searchview_facet', "FooAa", true);
// The items in the Filters menu and the Group By menu should be the same as before
await cpHelpers.toggleFilterMenu(controlPanel);
selectorContainsValue('.o_menu_item a', "FA", true);
selectorContainsValue('.o_menu_item a', "FB", false);
selectorContainsValue('.o_menu_item a', "FC", true);
await cpHelpers.toggleGroupByMenu(controlPanel);
selectorContainsValue('.o_menu_item a', "GA", true);
selectorContainsValue('.o_menu_item a', "GB", false);
controlPanel.destroy();
});
QUnit.test('invisible fields and filters with unknown related fields should not be rendered', async function (assert) {
assert.expect(2);
// This test case considers that the current user is not a member of
// the "base.group_system" group and both "bar" and "date_field" fields
// have field-level access control that limit access to them only from
// that group.
//
// As MockServer currently does not support "groups" access control, we:
//
// - emulate field-level access control of fields_get() by removing
// "bar" and "date_field" from the model fields
// - set filters with groups="base.group_system" as `invisible=1` in
// view to emulate the behavior of fields_view_get()
// [see ir.ui.view `_apply_group()`]
delete this.fields.bar;
delete this.fields.date_field;
const searchMenuTypes = [];
const params = {
cpProps: { fields: this.fields, searchMenuTypes },
};
const controlPanel = await createControlPanel(params);
assert.containsNone(controlPanel.el, 'div.o_search_options div.o_filter_menu',
"there should not be filter dropdown");
assert.containsNone(controlPanel.el, 'div.o_search_options div.o_group_by_menu',
"there should not be groupby dropdown");
controlPanel.destroy();
});
QUnit.test('groupby menu is not rendered if searchMenuTypes does not have groupBy', async function (assert) {
assert.expect(2);
const arch = `<search/>`;
const searchMenuTypes = ['filter'];
const params = {
cpModelConfig: {
arch,
fields: this.fields,
searchMenuTypes,
},
cpProps: { fields: this.fields, searchMenuTypes },
};
const controlPanel = await createControlPanel(params);
assert.containsOnce(controlPanel.el, 'div.o_search_options div.o_filter_menu');
assert.containsNone(controlPanel.el, 'div.o_search_options div.o_group_by_menu');
controlPanel.destroy();
});
});
});
|