commit 2894e4f46233c53821468d170b19cb45b8ed9faf
parent 8ba9fe7b80ea2fcb6066d2bbd155bae6f366a10e
Author: Dan Stillman <dstillman@zotero.org>
Date: Fri, 6 May 2016 04:08:09 -0400
Fix search by collection
Diffstat:
2 files changed, 43 insertions(+), 10 deletions(-)
diff --git a/chrome/content/zotero/xpcom/search.js b/chrome/content/zotero/xpcom/search.js
@@ -1152,7 +1152,7 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
}
}
else {
- col = yield objectTypeClass.getByLibraryAndKeyAsync(
+ obj = yield objectTypeClass.getByLibraryAndKeyAsync(
objLibraryID, objKey
);
}
@@ -1175,12 +1175,10 @@ Zotero.Search.prototype._buildQuery = Zotero.Promise.coroutine(function* () {
// Search descendent collections if recursive search
if (recursive){
- var descendents = col.getDescendents(false, 'collection');
- if (descendents){
- for (var k in descendents){
- q.push('?');
- p.push({int:descendents[k]['id']});
- }
+ var descendents = obj.getDescendents(false, 'collection');
+ for (let d of descendents) {
+ q.push('?');
+ p.push(d.id);
}
}
diff --git a/test/tests/searchTest.js b/test/tests/searchTest.js
@@ -101,10 +101,45 @@ describe("Zotero.Search", function() {
if (win) {
win.close();
}
- yield fooItem.erase();
- yield foobarItem.erase();
+ yield fooItem.eraseTx();
+ yield foobarItem.eraseTx();
});
-
+
+ it("should find item in collection", function* () {
+ var col = yield createDataObject('collection');
+ var item = yield createDataObject('item', { collections: [col.id] });
+
+ var s = new Zotero.Search();
+ s.libraryID = item.libraryID;
+ s.addCondition('collection', 'is', col.key);
+ var matches = yield s.search();
+ assert.sameMembers(matches, [item.id]);
+ });
+
+ it("shouldn't find item in collection with no items", function* () {
+ var col = yield createDataObject('collection');
+ var item = yield createDataObject('item');
+
+ var s = new Zotero.Search();
+ s.libraryID = item.libraryID;
+ s.addCondition('collection', 'is', col.key);
+ var matches = yield s.search();
+ assert.lengthOf(matches, 0);
+ });
+
+ it("should find item in subcollection in recursive mode", function* () {
+ var col1 = yield createDataObject('collection');
+ var col2 = yield createDataObject('collection', { parentID: col1.id });
+ var item = yield createDataObject('item', { collections: [col2.id] });
+
+ var s = new Zotero.Search();
+ s.libraryID = item.libraryID;
+ s.addCondition('collection', 'is', col1.key);
+ s.addCondition('recursive', 'true');
+ var matches = yield s.search();
+ assert.sameMembers(matches, [item.id]);
+ });
+
it("should return matches with full-text conditions", function* () {
let s = new Zotero.Search();
s.addCondition('fulltextWord', 'contains', 'foo');