From 2575b8ec65f680a22a666fcc517e038f3ad75b9a Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Fri, 28 Jul 2017 06:51:16 -0700 Subject: [PATCH 1/3] Fixes #13181 - Check values before adding them to filter --- .../kibana/public/dashboard/dashboard_context.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/core_plugins/kibana/public/dashboard/dashboard_context.js b/src/core_plugins/kibana/public/dashboard/dashboard_context.js index 5788905f58ed6..1824ba3f21bd0 100644 --- a/src/core_plugins/kibana/public/dashboard/dashboard_context.js +++ b/src/core_plugins/kibana/public/dashboard/dashboard_context.js @@ -13,7 +13,8 @@ export function dashboardContextProvider(Private, getAppState) { if (queryBarQuery.language === 'lucene') { // Add the query bar filter, its handled differently. - bool.must.push(luceneStringToDsl(queryBarQuery.query)); + const query = luceneStringToDsl(queryBarQuery.query); + if (query) bool.must.push(query); } // Add each of the filter bar filters @@ -26,10 +27,10 @@ export function dashboardContextProvider(Private, getAppState) { if (filter.meta.disabled) return; if (filter.meta.negate) { bool.must_not = bool.must_not || []; - bool.must_not.push(esFilter.query || esFilter); + if (esFilter.query || esFilter) bool.must_not.push(esFilter.query || esFilter); } else { bool.must = bool.must || []; - bool.must.push(esFilter.query || esFilter); + if (esFilter.query || esFilter) bool.must.push(esFilter.query || esFilter); } }); From 060e1d5682f7d544d1cd2475dd1a3ec2a9e6e0cc Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Fri, 28 Jul 2017 10:27:51 -0700 Subject: [PATCH 2/3] Adding tests --- .../dashboard/__tests__/dashboard_context.js | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js diff --git a/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js b/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js new file mode 100644 index 0000000000000..6081ceb5020cb --- /dev/null +++ b/src/core_plugins/kibana/public/dashboard/__tests__/dashboard_context.js @@ -0,0 +1,131 @@ +import sinon from 'sinon'; +import { expect } from 'chai'; +import { dashboardContextProvider } from '../dashboard_context'; + +describe('Dashboard Context', () => { + + describe('with query bar', () => { + let Private; + let getAppState; + let getDashboardContext; + beforeEach(() => { + Private = sinon.stub().returns({ + getFilters() { + return []; + } + }); + }); + + it('should return an empty must and must not when there are no filters or queries', () => { + getAppState = sinon.stub().returns({ + query: { + language: 'lucene', + query: null + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [], + must_not: [] + } + }); + }); + + it('should add a valid query to must', () => { + getAppState = sinon.stub().returns({ + query: { + language: 'lucene', + query: '*' + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [ + { + query_string: { + query: '*' + } + } + ], + must_not: [] + } + }); + }); + + }); + + describe('with filter bar', () => { + let Private; + let getAppState; + let getDashboardContext; + beforeEach(() => { + getAppState = sinon.stub().returns({ query: { language: 'something-else' } }); + }); + + afterEach(() => { + getAppState.reset(); + }); + + it('should add a valid filter to must', () => { + Private = sinon.stub().returns({ + getFilters() { + return [ + { meta: { negate: false }, term: { foo: 'bar' } } + ]; + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [{ term: { foo: 'bar' } }], + must_not: [] + } + }); + }); + + it('should add a valid filter to must_not', () => { + Private = sinon.stub().returns({ + getFilters() { + return [ + { meta: { negate: true }, term: { foo: 'bar' } } + ]; + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [], + must_not: [{ term: { foo: 'bar' } }] + } + }); + }); + + it('should not add a disabled filter', () => { + Private = sinon.stub().returns({ + getFilters() { + return [ + { meta: { negate: true, disabled: true }, term: { foo: 'bar' } } + ]; + } + }); + getDashboardContext = dashboardContextProvider(Private, getAppState); + const context = getDashboardContext(); + expect(context).to.eql({ + bool: { + must: [], + must_not: [] + } + }); + }); + + }); + +}); + + From 014c6bf91adecc44de36c09a9667b1bad7671709 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Fri, 28 Jul 2017 10:34:38 -0700 Subject: [PATCH 3/3] Adding check to make sure query is never undefined --- src/ui/public/courier/saved_object/saved_object.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ui/public/courier/saved_object/saved_object.js b/src/ui/public/courier/saved_object/saved_object.js index 050f36439c048..45770af5fb4e5 100644 --- a/src/ui/public/courier/saved_object/saved_object.js +++ b/src/ui/public/courier/saved_object/saved_object.js @@ -107,7 +107,9 @@ export function SavedObjectProvider(Promise, Private, Notifier, confirmModalProm this.searchSource.set(_.defaults(state, fnProps)); - this.searchSource.set('query', migrateLegacyQuery(this.searchSource.getOwn('query'))); + if (!_.isUndefined(this.searchSource.getOwn('query'))) { + this.searchSource.set('query', migrateLegacyQuery(this.searchSource.getOwn('query'))); + } }; /**