mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 11:11:04 +01:00
categories update test
This commit is contained in:
@@ -21,24 +21,30 @@ module.exports = function (Categories) {
|
||||
};
|
||||
|
||||
function updateCategory(cid, modifiedFields, callback) {
|
||||
Categories.exists(cid, function (err, exists) {
|
||||
if (err || !exists) {
|
||||
return callback(err);
|
||||
var category;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Categories.exists(cid, next);
|
||||
},
|
||||
function (exists, next) {
|
||||
if (!exists) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
|
||||
if (modifiedFields.hasOwnProperty('name')) {
|
||||
translator.translate(modifiedFields.name, function (translated) {
|
||||
modifiedFields.slug = cid + '/' + utils.slugify(translated);
|
||||
next();
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
|
||||
plugins.fireHook('filter:category.update', {category: modifiedFields}, function (err, categoryData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var category = categoryData.category;
|
||||
},
|
||||
function (next) {
|
||||
plugins.fireHook('filter:category.update', {category: modifiedFields}, next);
|
||||
},
|
||||
function (categoryData, next) {
|
||||
category = categoryData.category;
|
||||
var fields = Object.keys(category);
|
||||
// move parent to front, so its updated first
|
||||
var parentCidIndex = fields.indexOf('parentCid');
|
||||
@@ -48,15 +54,13 @@ module.exports = function (Categories) {
|
||||
|
||||
async.eachSeries(fields, function (key, next) {
|
||||
updateCategoryField(cid, key, category[key], next);
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
}, next);
|
||||
},
|
||||
function (next) {
|
||||
plugins.fireHook('action:category.update', {cid: cid, modified: category});
|
||||
callback();
|
||||
});
|
||||
});
|
||||
});
|
||||
next();
|
||||
}
|
||||
], callback);
|
||||
}
|
||||
|
||||
function updateCategoryField(cid, key, value, callback) {
|
||||
@@ -64,30 +68,31 @@ module.exports = function (Categories) {
|
||||
return updateParent(cid, value, callback);
|
||||
}
|
||||
|
||||
db.setObjectField('category:' + cid, key, value, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.setObjectField('category:' + cid, key, value, next);
|
||||
},
|
||||
function (next) {
|
||||
if (key === 'order') {
|
||||
updateOrder(cid, value, callback);
|
||||
updateOrder(cid, value, next);
|
||||
} else if (key === 'description') {
|
||||
Categories.parseDescription(cid, value, callback);
|
||||
Categories.parseDescription(cid, value, next);
|
||||
} else {
|
||||
callback();
|
||||
next();
|
||||
}
|
||||
});
|
||||
}
|
||||
], callback);
|
||||
}
|
||||
|
||||
function updateParent(cid, newParent, callback) {
|
||||
if (parseInt(cid, 10) === parseInt(newParent, 10)) {
|
||||
return callback(new Error('[[error:cant-set-self-as-parent]]'));
|
||||
}
|
||||
Categories.getCategoryField(cid, 'parentCid', function (err, oldParent) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Categories.getCategoryField(cid, 'parentCid', next);
|
||||
},
|
||||
function (oldParent, next) {
|
||||
async.series([
|
||||
function (next) {
|
||||
oldParent = parseInt(oldParent, 10) || 0;
|
||||
@@ -100,18 +105,19 @@ module.exports = function (Categories) {
|
||||
function (next) {
|
||||
db.setObjectField('category:' + cid, 'parentCid', newParent, next);
|
||||
}
|
||||
], next);
|
||||
}
|
||||
], function (err) {
|
||||
callback(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function updateOrder(cid, order, callback) {
|
||||
Categories.getCategoryField(cid, 'parentCid', function (err, parentCid) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Categories.getCategoryField(cid, 'parentCid', next);
|
||||
},
|
||||
function (parentCid, next) {
|
||||
async.parallel([
|
||||
function (next) {
|
||||
db.sortedSetAdd('categories:cid', order, cid, next);
|
||||
@@ -120,17 +126,22 @@ module.exports = function (Categories) {
|
||||
parentCid = parseInt(parentCid, 10) || 0;
|
||||
db.sortedSetAdd('cid:' + parentCid + ':children', order, cid, next);
|
||||
}
|
||||
], callback);
|
||||
], next);
|
||||
}
|
||||
], function (err) {
|
||||
callback(err);
|
||||
});
|
||||
}
|
||||
|
||||
Categories.parseDescription = function (cid, description, callback) {
|
||||
plugins.fireHook('filter:parse.raw', description, function (err, parsedDescription) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
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 Topics = require('../src/topics');
|
||||
var User = require('../src/user');
|
||||
var groups = require('../src/groups');
|
||||
|
||||
describe('Categories', function () {
|
||||
var categoryObj;
|
||||
var posterUid;
|
||||
var adminUid;
|
||||
|
||||
before(function (done) {
|
||||
User.create({username: 'poster'}, function (err, _posterUid) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
groups.resetCache();
|
||||
async.parallel({
|
||||
posterUid: function (next) {
|
||||
User.create({username: 'poster'}, next);
|
||||
},
|
||||
adminUid: function (next) {
|
||||
User.create({username: 'admin'}, next);
|
||||
}
|
||||
|
||||
posterUid = _posterUid;
|
||||
|
||||
done();
|
||||
}, function (err, results) {
|
||||
assert.ifError(err);
|
||||
posterUid = results.posterUid;
|
||||
adminUid = results.adminUid;
|
||||
groups.join('administrators', adminUid, done);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -299,8 +306,66 @@ describe('Categories', function () {
|
||||
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