mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 03:01:08 +01:00
categories update test
This commit is contained in:
@@ -21,24 +21,30 @@ module.exports = function (Categories) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function updateCategory(cid, modifiedFields, callback) {
|
function updateCategory(cid, modifiedFields, callback) {
|
||||||
Categories.exists(cid, function (err, exists) {
|
var category;
|
||||||
if (err || !exists) {
|
async.waterfall([
|
||||||
return callback(err);
|
function (next) {
|
||||||
}
|
Categories.exists(cid, next);
|
||||||
|
},
|
||||||
|
function (exists, next) {
|
||||||
if (modifiedFields.hasOwnProperty('name')) {
|
if (!exists) {
|
||||||
translator.translate(modifiedFields.name, function (translated) {
|
return callback();
|
||||||
modifiedFields.slug = cid + '/' + utils.slugify(translated);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
plugins.fireHook('filter:category.update', {category: modifiedFields}, function (err, categoryData) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var category = categoryData.category;
|
if (modifiedFields.hasOwnProperty('name')) {
|
||||||
|
translator.translate(modifiedFields.name, function (translated) {
|
||||||
|
modifiedFields.slug = cid + '/' + utils.slugify(translated);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
plugins.fireHook('filter:category.update', {category: modifiedFields}, next);
|
||||||
|
},
|
||||||
|
function (categoryData, next) {
|
||||||
|
category = categoryData.category;
|
||||||
var fields = Object.keys(category);
|
var fields = Object.keys(category);
|
||||||
// move parent to front, so its updated first
|
// move parent to front, so its updated first
|
||||||
var parentCidIndex = fields.indexOf('parentCid');
|
var parentCidIndex = fields.indexOf('parentCid');
|
||||||
@@ -48,15 +54,13 @@ module.exports = function (Categories) {
|
|||||||
|
|
||||||
async.eachSeries(fields, function (key, next) {
|
async.eachSeries(fields, function (key, next) {
|
||||||
updateCategoryField(cid, key, category[key], next);
|
updateCategoryField(cid, key, category[key], next);
|
||||||
}, function (err) {
|
}, next);
|
||||||
if (err) {
|
},
|
||||||
return callback(err);
|
function (next) {
|
||||||
}
|
plugins.fireHook('action:category.update', {cid: cid, modified: category});
|
||||||
plugins.fireHook('action:category.update', {cid: cid, modified: category});
|
next();
|
||||||
callback();
|
}
|
||||||
});
|
], callback);
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCategoryField(cid, key, value, callback) {
|
function updateCategoryField(cid, key, value, callback) {
|
||||||
@@ -64,73 +68,80 @@ module.exports = function (Categories) {
|
|||||||
return updateParent(cid, value, callback);
|
return updateParent(cid, value, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
db.setObjectField('category:' + cid, key, value, function (err) {
|
async.waterfall([
|
||||||
if (err) {
|
function (next) {
|
||||||
return callback(err);
|
db.setObjectField('category:' + cid, key, value, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
if (key === 'order') {
|
||||||
|
updateOrder(cid, value, next);
|
||||||
|
} else if (key === 'description') {
|
||||||
|
Categories.parseDescription(cid, value, next);
|
||||||
|
} else {
|
||||||
|
next();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
], callback);
|
||||||
if (key === 'order') {
|
|
||||||
updateOrder(cid, value, callback);
|
|
||||||
} else if (key === 'description') {
|
|
||||||
Categories.parseDescription(cid, value, callback);
|
|
||||||
} else {
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateParent(cid, newParent, callback) {
|
function updateParent(cid, newParent, callback) {
|
||||||
if (parseInt(cid, 10) === parseInt(newParent, 10)) {
|
if (parseInt(cid, 10) === parseInt(newParent, 10)) {
|
||||||
return callback(new Error('[[error:cant-set-self-as-parent]]'));
|
return callback(new Error('[[error:cant-set-self-as-parent]]'));
|
||||||
}
|
}
|
||||||
Categories.getCategoryField(cid, 'parentCid', function (err, oldParent) {
|
async.waterfall([
|
||||||
if (err) {
|
function (next) {
|
||||||
return callback(err);
|
Categories.getCategoryField(cid, 'parentCid', next);
|
||||||
|
},
|
||||||
|
function (oldParent, next) {
|
||||||
|
async.series([
|
||||||
|
function (next) {
|
||||||
|
oldParent = parseInt(oldParent, 10) || 0;
|
||||||
|
db.sortedSetRemove('cid:' + oldParent + ':children', cid, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
newParent = parseInt(newParent, 10) || 0;
|
||||||
|
db.sortedSetAdd('cid:' + newParent + ':children', cid, cid, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
db.setObjectField('category:' + cid, 'parentCid', newParent, next);
|
||||||
|
}
|
||||||
|
], next);
|
||||||
}
|
}
|
||||||
|
], function (err) {
|
||||||
async.series([
|
callback(err);
|
||||||
function (next) {
|
|
||||||
oldParent = parseInt(oldParent, 10) || 0;
|
|
||||||
db.sortedSetRemove('cid:' + oldParent + ':children', cid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
newParent = parseInt(newParent, 10) || 0;
|
|
||||||
db.sortedSetAdd('cid:' + newParent + ':children', cid, cid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
db.setObjectField('category:' + cid, 'parentCid', newParent, next);
|
|
||||||
}
|
|
||||||
], function (err) {
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateOrder(cid, order, callback) {
|
function updateOrder(cid, order, callback) {
|
||||||
Categories.getCategoryField(cid, 'parentCid', function (err, parentCid) {
|
async.waterfall([
|
||||||
if (err) {
|
function (next) {
|
||||||
return callback(err);
|
Categories.getCategoryField(cid, 'parentCid', next);
|
||||||
|
},
|
||||||
|
function (parentCid, next) {
|
||||||
|
async.parallel([
|
||||||
|
function (next) {
|
||||||
|
db.sortedSetAdd('categories:cid', order, cid, next);
|
||||||
|
},
|
||||||
|
function (next) {
|
||||||
|
parentCid = parseInt(parentCid, 10) || 0;
|
||||||
|
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
|
||||||
|
}
|
||||||
|
], next);
|
||||||
}
|
}
|
||||||
|
], function (err) {
|
||||||
async.parallel([
|
callback(err);
|
||||||
function (next) {
|
|
||||||
db.sortedSetAdd('categories:cid', order, cid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
parentCid = parseInt(parentCid, 10) || 0;
|
|
||||||
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
|
|
||||||
}
|
|
||||||
], callback);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Categories.parseDescription = function (cid, description, callback) {
|
Categories.parseDescription = function (cid, description, callback) {
|
||||||
plugins.fireHook('filter:parse.raw', description, function (err, parsedDescription) {
|
async.waterfall([
|
||||||
if (err) {
|
function (next) {
|
||||||
return callback(err);
|
plugins.fireHook('filter:parse.raw', description, next);
|
||||||
|
},
|
||||||
|
function (parsedDescription, next) {
|
||||||
|
Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, next);
|
||||||
}
|
}
|
||||||
Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, callback);
|
], callback);
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -11,20 +11,27 @@ var db = require('./mocks/databasemock');
|
|||||||
var Categories = require('../src/categories');
|
var Categories = require('../src/categories');
|
||||||
var Topics = require('../src/topics');
|
var Topics = require('../src/topics');
|
||||||
var User = require('../src/user');
|
var User = require('../src/user');
|
||||||
|
var groups = require('../src/groups');
|
||||||
|
|
||||||
describe('Categories', function () {
|
describe('Categories', function () {
|
||||||
var categoryObj;
|
var categoryObj;
|
||||||
var posterUid;
|
var posterUid;
|
||||||
|
var adminUid;
|
||||||
|
|
||||||
before(function (done) {
|
before(function (done) {
|
||||||
User.create({username: 'poster'}, function (err, _posterUid) {
|
groups.resetCache();
|
||||||
if (err) {
|
async.parallel({
|
||||||
return done(err);
|
posterUid: function (next) {
|
||||||
|
User.create({username: 'poster'}, next);
|
||||||
|
},
|
||||||
|
adminUid: function (next) {
|
||||||
|
User.create({username: 'admin'}, next);
|
||||||
}
|
}
|
||||||
|
}, function (err, results) {
|
||||||
posterUid = _posterUid;
|
assert.ifError(err);
|
||||||
|
posterUid = results.posterUid;
|
||||||
done();
|
adminUid = results.adminUid;
|
||||||
|
groups.join('administrators', adminUid, done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -299,8 +306,66 @@ describe('Categories', function () {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('admin socket methods', function () {
|
||||||
|
var socketCategories = require('../src/socket.io/admin/categories');
|
||||||
|
var cid;
|
||||||
|
before(function (done) {
|
||||||
|
Categories.create({
|
||||||
|
name: 'update name',
|
||||||
|
description: 'update description',
|
||||||
|
parentCid: categoryObj.cid,
|
||||||
|
icon: 'fa-check',
|
||||||
|
order: '5'
|
||||||
|
}, function (err, category) {
|
||||||
|
assert.ifError(err);
|
||||||
|
|
||||||
|
cid = category.cid;
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return error with invalid data', function (done) {
|
||||||
|
socketCategories.update({uid: adminUid}, null, function (err) {
|
||||||
|
assert.equal(err.message, '[[error:invalid-data]]');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should error if you try to set parent as self', function (done) {
|
||||||
|
var updateData = {};
|
||||||
|
updateData[cid] = {
|
||||||
|
parentCid: cid
|
||||||
|
};
|
||||||
|
socketCategories.update({uid: adminUid}, updateData, function (err) {
|
||||||
|
assert.equal(err.message, '[[error:cant-set-self-as-parent]]');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should update category data', function (done) {
|
||||||
|
var updateData = {};
|
||||||
|
updateData[cid] = {
|
||||||
|
name: 'new name',
|
||||||
|
description: 'new description',
|
||||||
|
parentCid: 0,
|
||||||
|
order: 3,
|
||||||
|
icon: 'fa-hammer'
|
||||||
|
};
|
||||||
|
socketCategories.update({uid: adminUid}, updateData, function (err) {
|
||||||
|
assert.ifError(err);
|
||||||
|
Categories.getCategoryData(cid, function (err, data) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.equal(data.name, updateData[cid].name);
|
||||||
|
assert.equal(data.description, updateData[cid].description);
|
||||||
|
assert.equal(data.parentCid, updateData[cid].parentCid);
|
||||||
|
assert.equal(data.order, updateData[cid].order);
|
||||||
|
assert.equal(data.icon, updateData[cid].icon);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user