From 1952c55d3347fa5182537e44dd4d84423181cc47 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Sun, 24 Jun 2018 00:32:03 +0200 Subject: [PATCH] Dynamic Routing Beta: Added redirect helper to ParentRouter refs #9601 - this function will help us to determine if a redirect should happen or not --- core/server/services/routing/ParentRouter.js | 12 ++ .../services/routing/ParentRouter_spec.js | 105 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 core/test/unit/services/routing/ParentRouter_spec.js diff --git a/core/server/services/routing/ParentRouter.js b/core/server/services/routing/ParentRouter.js index 11d42212c6..a9c511795c 100644 --- a/core/server/services/routing/ParentRouter.js +++ b/core/server/services/routing/ParentRouter.js @@ -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() {} } diff --git a/core/test/unit/services/routing/ParentRouter_spec.js b/core/test/unit/services/routing/ParentRouter_spec.js new file mode 100644 index 0000000000..9fba2d60b2 --- /dev/null +++ b/core/test/unit/services/routing/ParentRouter_spec.js @@ -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')); + }); + }); +});