controllers/category tests

This commit is contained in:
Barış Soner Uşaklı
2017-05-25 20:01:07 -04:00
parent 5af1161e09
commit 79a48cec5f
4 changed files with 350 additions and 90 deletions

View File

@@ -14,7 +14,7 @@ var helpers = require('./helpers');
var utils = require('../utils');
var translator = require('../translator');
var categoryController = {};
var categoryController = module.exports;
categoryController.get = function (req, res, callback) {
var cid = req.params.category_id;
@@ -129,97 +129,102 @@ categoryController.get = function (req, res, callback) {
if (categoryData.link) {
db.incrObjectField('category:' + categoryData.cid, 'timesClicked');
return res.redirect(categoryData.link);
return helpers.redirect(res, categoryData.link);
}
var breadcrumbs = [
{
text: categoryData.name,
url: nconf.get('relative_path') + '/category/' + categoryData.slug,
},
];
helpers.buildCategoryBreadcrumbs(categoryData.parentCid, function (err, crumbs) {
if (err) {
return next(err);
}
categoryData.breadcrumbs = crumbs.concat(breadcrumbs);
next(null, categoryData);
});
buildBreadcrumbs(categoryData, next);
},
function (categoryData, next) {
if (!categoryData.children.length) {
return next(null, categoryData);
}
var allCategories = [];
categories.flattenCategories(allCategories, categoryData.children);
categories.getRecentTopicReplies(allCategories, req.uid, function (err) {
next(err, categoryData);
});
},
], function (err, categoryData) {
if (err) {
return callback(err);
}
function (categoryData) {
categoryData.description = translator.escape(categoryData.description);
categoryData.privileges = userPrivileges;
categoryData.showSelect = categoryData.privileges.editable;
categoryData.description = translator.escape(categoryData.description);
categoryData.privileges = userPrivileges;
categoryData.showSelect = categoryData.privileges.editable;
addTags(categoryData, res);
res.locals.metaTags = [
{
name: 'title',
content: categoryData.name,
},
{
property: 'og:title',
content: categoryData.name,
},
{
name: 'description',
content: categoryData.description,
},
{
property: 'og:type',
content: 'website',
},
];
if (parseInt(req.uid, 10)) {
categories.markAsRead([cid], req.uid);
}
if (categoryData.backgroundImage) {
res.locals.metaTags.push({
name: 'og:image',
content: categoryData.backgroundImage,
categoryData['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
categoryData.rssFeedUrl = nconf.get('relative_path') + '/category/' + categoryData.cid + '.rss';
categoryData.title = translator.escape(categoryData.name);
pageCount = Math.max(1, Math.ceil(categoryData.topic_count / settings.topicsPerPage));
categoryData.pagination = pagination.create(currentPage, pageCount, req.query);
categoryData.pagination.rel.forEach(function (rel) {
rel.href = nconf.get('url') + '/category/' + categoryData.slug + rel.href;
res.locals.linkTags.push(rel);
});
}
res.locals.linkTags = [
{
rel: 'alternate',
type: 'application/rss+xml',
href: nconf.get('url') + '/category/' + cid + '.rss',
},
{
rel: 'up',
href: nconf.get('url'),
},
];
if (parseInt(req.uid, 10)) {
categories.markAsRead([cid], req.uid);
}
categoryData['feeds:disableRSS'] = parseInt(meta.config['feeds:disableRSS'], 10) === 1;
categoryData.rssFeedUrl = nconf.get('relative_path') + '/category/' + categoryData.cid + '.rss';
categoryData.title = translator.escape(categoryData.name);
pageCount = Math.max(1, Math.ceil(categoryData.topic_count / settings.topicsPerPage));
categoryData.pagination = pagination.create(currentPage, pageCount, req.query);
categoryData.pagination.rel.forEach(function (rel) {
rel.href = nconf.get('url') + '/category/' + categoryData.slug + rel.href;
res.locals.linkTags.push(rel);
});
res.render('category', categoryData);
});
res.render('category', categoryData);
},
], callback);
};
function buildBreadcrumbs(categoryData, callback) {
var breadcrumbs = [
{
text: categoryData.name,
url: nconf.get('relative_path') + '/category/' + categoryData.slug,
},
];
async.waterfall([
function (next) {
helpers.buildCategoryBreadcrumbs(categoryData.parentCid, next);
},
function (crumbs, next) {
categoryData.breadcrumbs = crumbs.concat(breadcrumbs);
next(null, categoryData);
},
], callback);
}
module.exports = categoryController;
function addTags(categoryData, res) {
res.locals.metaTags = [
{
name: 'title',
content: categoryData.name,
},
{
property: 'og:title',
content: categoryData.name,
},
{
name: 'description',
content: categoryData.description,
},
{
property: 'og:type',
content: 'website',
},
];
if (categoryData.backgroundImage) {
res.locals.metaTags.push({
name: 'og:image',
content: categoryData.backgroundImage,
});
}
res.locals.linkTags = [
{
rel: 'alternate',
type: 'application/rss+xml',
href: nconf.get('url') + '/category/' + categoryData.cid + '.rss',
},
{
rel: 'up',
href: nconf.get('url'),
},
];
}