diff --git a/chrome/content/zotero/xpcom/data/collection.js b/chrome/content/zotero/xpcom/data/collection.js index c3c44c6d9..6aa1deecd 100644 --- a/chrome/content/zotero/xpcom/data/collection.js +++ b/chrome/content/zotero/xpcom/data/collection.js @@ -27,8 +27,6 @@ Zotero.Collection = function() { Zotero.Collection._super.apply(this); this._name = null; - this._parentID = null; - this._parentKey = null; this._hasChildCollections = null; this._childCollections = []; diff --git a/chrome/content/zotero/xpcom/data/dataObject.js b/chrome/content/zotero/xpcom/data/dataObject.js index 2b900cb3f..a4ac74233 100644 --- a/chrome/content/zotero/xpcom/data/dataObject.js +++ b/chrome/content/zotero/xpcom/data/dataObject.js @@ -45,6 +45,8 @@ Zotero.DataObject = function () { this._version = null; this._synced = null; this._identified = false; + this._parentID = null; + this._parentKey = null; this._loaded = {}; this._markAllDataTypeLoadStates(false); @@ -162,18 +164,22 @@ Zotero.DataObject.prototype._setParentID = function (id) { return this._setParentKey( id ? this.ObjectsClass.getLibraryAndKeyFromID(Zotero.DataObjectUtilities.checkDataID(id)).key - : null + : false ); } /** * Set the key of the parent object * - * @param {String|null} [key=null] + * @param {String|false} [key=false] * @return {Boolean} True if changed, false if stayed the same */ Zotero.DataObject.prototype._setParentKey = function(key) { - key = Zotero.DataObjectUtilities.checkKey(key); + if (this._objectType == 'search') { + throw new Error("Cannot set parent key for search"); + } + + key = Zotero.DataObjectUtilities.checkKey(key) || false; if (this._parentKey == key) { return false; diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 294aea092..cbef0b31b 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -46,8 +46,6 @@ Zotero.Item = function(itemTypeOrID) { this._numNotesEmbeddedTrashed = null; this._numAttachments = null; this._numAttachmentsTrashed = null; - this._parentID = null; - this._parentKey = null; this._attachmentCharset = null; this._attachmentLinkMode = null; this._attachmentContentType = null; diff --git a/test/tests/collectionTest.js b/test/tests/collectionTest.js index 85197826c..9991a0929 100644 --- a/test/tests/collectionTest.js +++ b/test/tests/collectionTest.js @@ -1,8 +1,9 @@ +"use strict"; + describe("Zotero.Collection", function() { describe("#save()", function () { it("should save a new collection", function* () { var name = "Test"; - var collection = new Zotero.Collection; collection.name = name; var id = yield collection.save(); @@ -10,4 +11,70 @@ describe("Zotero.Collection", function() { assert.equal(collection.name, name); }); }) + + describe("#version", function () { + it("should set object version", function* () { + var version = 100; + var collection = new Zotero.Collection + collection.version = version; + collection.name = "Test"; + var id = yield collection.save(); + collection = yield Zotero.Collections.getAsync(id); + assert.equal(collection.version, version); + }); + }) + + describe("#parent", function () { + it("should set parent collection for new collections", function* () { + var parentCol = new Zotero.Collection + parentCol.name = "Parent"; + var parentID = yield parentCol.save(); + var {libraryID, key: parentKey} = Zotero.Collections.getLibraryAndKeyFromID(parentID); + + var col = new Zotero.Collection + col.name = "Child"; + col.parentKey = parentKey; + var id = yield col.save(); + col = yield Zotero.Collections.getAsync(id); + assert.equal(col.parentKey, parentKey); + }); + + it("should change parent collection for existing collections", function* () { + // Create initial parent collection + var parentCol = new Zotero.Collection + parentCol.name = "Parent"; + var parentID = yield parentCol.save(); + var {libraryID, key: parentKey} = Zotero.Collections.getLibraryAndKeyFromID(parentID); + + // Create subcollection + var col = new Zotero.Collection + col.name = "Child"; + col.parentKey = parentKey; + var id = yield col.save(); + col = yield Zotero.Collections.getAsync(id); + + // Create new parent collection + var newParentCol = new Zotero.Collection + newParentCol.name = "New Parent"; + var newParentID = yield newParentCol.save(); + var {libraryID, key: newParentKey} = Zotero.Collections.getLibraryAndKeyFromID(newParentID); + + // Change parent collection + col.parentKey = newParentKey; + yield col.save(); + col = yield Zotero.Collections.getAsync(id); + assert.equal(col.parentKey, newParentKey); + }); + + it("should not resave a collection with no parent if set to false", function* () { + var col = new Zotero.Collection + col.name = "Test"; + var id = yield col.save(); + col = yield Zotero.Collections.getAsync(id); + + col.parentKey = false; + var ret = yield col.save(); + assert.isFalse(ret); + }); + }) })