0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00

Dynamic Routing Beta: Added redirect helper to ParentRouter

refs #9601

- this function will help us to determine if a redirect should happen or not
This commit is contained in:
kirrg001 2018-06-24 00:32:03 +02:00 committed by Katharina Irrgang
parent 935f064357
commit 1952c55d33
2 changed files with 117 additions and 0 deletions

View file

@ -101,6 +101,18 @@ class ParentRouter extends EventEmitter {
return urlService.utils.createUrl(this.route.value, options.absolute, options.secure);
}
isRedirectEnabled(routerType, slug) {
if (!this.data || !Object.keys(this.data.router)) {
return false;
}
return _.find(this.data.router, function (entries, type) {
if (routerType === type) {
return _.find(entries, {redirect: true, slug: slug});
}
});
}
reset() {}
}

View file

@ -0,0 +1,105 @@
const should = require('should'),
sinon = require('sinon'),
settingsCache = require('../../../../server/services/settings/cache'),
common = require('../../../../server/lib/common'),
ParentRouter = require('../../../../server/services/routing/ParentRouter'),
sandbox = sinon.sandbox.create();
describe('UNIT - services/routing/ParentRouter', function () {
let req, res, next;
beforeEach(function () {
sandbox.stub(settingsCache, 'get').withArgs('permalinks').returns('/:slug/');
sandbox.stub(common.events, 'emit');
sandbox.stub(common.events, 'on');
req = sandbox.stub();
res = sandbox.stub();
next = sandbox.stub();
res.locals = {};
});
afterEach(function () {
sandbox.restore();
});
describe('fn: isRedirectEnabled', function () {
it('no data key defined', function () {
const parentRouter = new ParentRouter();
parentRouter.data = undefined;
parentRouter.isRedirectEnabled('tags', 'bacon').should.be.false();
});
it('no data key defined', function () {
const parentRouter = new ParentRouter();
parentRouter.data = {query: {}, router: {}};
should.not.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
});
it('no redirect', function () {
const parentRouter = new ParentRouter();
parentRouter.data = {
query: {},
router: {
tags: [{redirect: true}]
}
};
should.not.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
});
it('no redirect', function () {
const parentRouter = new ParentRouter();
parentRouter.data = {
query: {},
router: {
tags: [{redirect: true, slug: 'cheese'}]
}
};
should.not.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
});
it('no redirect', function () {
const parentRouter = new ParentRouter();
parentRouter.data = {
query: {},
router: {
tags: [{redirect: false, slug: 'bacon'}]
}
};
should.not.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
});
it('redirect', function () {
const parentRouter = new ParentRouter();
parentRouter.data = {
query: {},
router: {
tags: [{redirect: true, slug: 'bacon'}]
}
};
should.exist(parentRouter.isRedirectEnabled('tags', 'bacon'));
});
it('redirect', function () {
const parentRouter = new ParentRouter();
parentRouter.data = {
query: {},
router: {
pages: [{redirect: true, slug: 'home'}]
}
};
should.exist(parentRouter.isRedirectEnabled('pages', 'home'));
});
});
});