Support deleted property for collections and searches

This lays the groundwork for moving collections and searches to the
trash instead of deleting them outright. We're not doing that yet, so
the `deleted` property will never be set (except for items), but this
will allow clients from this point forward to sync collections and
searches with that property for when it's used in the future. For now,
such objects will just be hidden from the collections pane as if they
had been deleted.
This commit is contained in:
Dan Stillman 2021-01-13 00:40:13 -05:00
parent 5a4d78578b
commit e45ca4edad
16 changed files with 346 additions and 136 deletions

View file

@ -160,6 +160,35 @@ describe("Zotero.DataObject", function() {
});
})
describe("#deleted", function () {
it("should set trash status", async function () {
for (let type of types) {
let plural = Zotero.DataObjectUtilities.getObjectTypePlural(type)
let pluralClass = Zotero[Zotero.Utilities.capitalize(plural)];
// Set to true
var obj = await createDataObject(type);
assert.isFalse(obj.deleted, type);
obj.deleted = true;
// Sanity check for itemsTest#trash()
if (type == 'item') {
assert.isTrue(obj._changedData.deleted, type);
}
await obj.saveTx();
var id = obj.id;
await pluralClass.reload(id, false, true);
assert.isTrue(obj.deleted, type);
// Set to false
obj.deleted = false;
await obj.saveTx();
await pluralClass.reload(id, false, true);
assert.isFalse(obj.deleted, type);
}
});
});
describe("#loadPrimaryData()", function () {
it("should load unloaded primary data if partially set", function* () {
var objs = {};
@ -574,5 +603,79 @@ describe("Zotero.DataObject", function() {
assert.equal(item1.getField('dateModified'), dateModified);
})
})
})
});
describe("#fromJSON()", function () {
it("should remove object from trash if 'deleted' property not provided", async function () {
for (let type of types) {
let obj = await createDataObject(type, { deleted: true });
assert.isTrue(obj.deleted, type);
let json = obj.toJSON();
delete json.deleted;
obj.fromJSON(json);
await obj.saveTx();
assert.isFalse(obj.deleted, type);
}
});
});
describe("#toJSON()", function () {
it("should output 'deleted' as true", function () {
for (let type of types) {
let obj = createUnsavedDataObject(type);
obj.deleted = true;
let json = obj.toJSON();
assert.isTrue(json.deleted, type);
}
});
it("shouldn't include 'deleted' if not set in default mode", function () {
for (let type of types) {
let obj = createUnsavedDataObject(type);
let json = obj.toJSON();
assert.notProperty(json, 'deleted', type);
}
});
describe("'patch' mode", function () {
it("should include changed 'deleted' field", async function () {
for (let type of types) {
let plural = Zotero.DataObjectUtilities.getObjectTypePlural(type)
let pluralClass = Zotero[Zotero.Utilities.capitalize(plural)];
// True to false
let obj = createUnsavedDataObject(type)
obj.deleted = true;
let id = await obj.saveTx();
obj = await pluralClass.getAsync(id);
let patchBase = obj.toJSON();
obj.deleted = false;
let json = obj.toJSON({
patchBase: patchBase
})
assert.isUndefined(json.title, type);
assert.isFalse(json.deleted, type);
// False to true
obj = createUnsavedDataObject(type);
obj.deleted = false;
id = await obj.saveTx();
obj = await pluralClass.getAsync(id);
patchBase = obj.toJSON();
obj.deleted = true;
json = obj.toJSON({
patchBase: patchBase
})
assert.isUndefined(json.title, type);
assert.isTrue(json.deleted, type);
}
});
});
});
})