From 223f582aa7291743570d75bb44c1abcfc7189bef Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 28 Nov 2018 15:19:19 -0700 Subject: [PATCH] Fix search error on nonexistent collection in recursive mode And don't return results for a nonexistent parent search --- chrome/content/zotero/xpcom/data/search.js | 27 +++++++++++----------- test/tests/searchTest.js | 23 ++++++++++++++++++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/search.js b/chrome/content/zotero/xpcom/data/search.js index f729b54320..ad00bdd71c 100644 --- a/chrome/content/zotero/xpcom/data/search.js +++ b/chrome/content/zotero/xpcom/data/search.js @@ -1171,6 +1171,7 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () { let objKey = condition.value; let objectType = condition.name == 'collection' ? 'collection' : 'search'; let objectTypeClass = Zotero.DataObjectUtilities.getObjectsClassForObjectType(objectType); + let forceNoResults = false; // libraryID assigned on search if (this.libraryID !== null) { @@ -1202,24 +1203,24 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () { objLibraryID, objKey ); } - if (!obj) { - var msg = objectType.charAt(0).toUpperCase() + objectType.substr(1) + if (obj) { + if (objectType == 'search' && obj == this) { + Zotero.warn(`Search "${this.name}" references itself -- skipping condition`); + continue; + } + } + else { + let msg = objectType.charAt(0).toUpperCase() + objectType.substr(1) + " " + objKey + " specified in search not found"; Zotero.debug(msg, 2); Zotero.log(msg, 'warning', 'chrome://zotero/content/xpcom/search.js'); - if (objectType == 'search') { - continue; - } - obj = { - id: 0 - }; - } - if (objectType == 'search' && obj == this) { - Zotero.warn(`Search "${this.name}" references itself -- skipping condition`); - continue; + forceNoResults = true; } - if (objectType == 'collection') { + if (forceNoResults) { + condSQL += '0=1'; + } + else if (objectType == 'collection') { let ids = [obj.id]; // Search descendent collections if recursive search diff --git a/test/tests/searchTest.js b/test/tests/searchTest.js index ea4ed86821..43f5a9fc6d 100644 --- a/test/tests/searchTest.js +++ b/test/tests/searchTest.js @@ -193,6 +193,18 @@ describe("Zotero.Search", function() { var matches = yield s.search(); assert.sameMembers(matches, [item.id]); }); + + it("should return no results for a collection that doesn't exist in recursive mode", async function () { + var item = await createDataObject('item'); + + var s = new Zotero.Search(); + s.libraryID = item.libraryID; + s.name = "Test"; + s.addCondition('collection', 'is', Zotero.DataObjectUtilities.generateKey()); + s.addCondition('recursive', 'true'); + var matches = await s.search(); + assert.lengthOf(matches, 0); + }); }); describe("fileTypeID", function () { @@ -361,6 +373,17 @@ describe("Zotero.Search", function() { var matches = yield s.search(); assert.notInclude(matches, item.id); }); + + it("should return no results for a search that doesn't exist", async function () { + var item = await createDataObject('item'); + + var s = new Zotero.Search(); + s.libraryID = item.libraryID; + s.name = "Test"; + s.addCondition('savedSearch', 'is', Zotero.DataObjectUtilities.generateKey()); + var matches = await s.search(); + assert.lengthOf(matches, 0); + }); }); describe("unfiled", function () {