0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

🐛 Ensure deprecated labs flags are unset

fixes #11343

- solves the case where themes depends on old labs flags that are now always false, but the DB still has the feature set to true
- add concept of deprecated labs flags to the labs service
- make sure that the labs service gets used in our theme middleware
- added tests and other small fixes
This commit is contained in:
Hannah Wolfe 2019-11-06 14:42:39 +07:00
parent 27dd442059
commit 7a36200e24
3 changed files with 69 additions and 6 deletions

View file

@ -4,6 +4,7 @@ const urlUtils = require('../../../server/lib/url-utils');
const config = require('../../../server/config');
const common = require('../../../server/lib/common');
const settingsCache = require('../../../server/services/settings/cache');
const labs = require('../../../server/services/labs');
const activeTheme = require('./active');
// ### Ensure Active Theme
@ -75,7 +76,8 @@ function updateGlobalTemplateOptions(req, res, next) {
// @TODO: bind this once and then update based on events?
// @TODO: decouple theme layer from settings cache using the Content API
const siteData = settingsCache.getPublic();
const labsData = _.cloneDeep(settingsCache.get('labs'));
const labsData = labs.getAll();
const themeData = {
posts_per_page: activeTheme.get().config('posts_per_page'),
image_sizes: activeTheme.get().config('image_sizes')

View file

@ -3,15 +3,24 @@ const _ = require('lodash');
const Promise = require('bluebird');
const SafeString = require('../../frontend/services/themes/engine').SafeString;
const common = require('../lib/common');
const deprecatedFeatures = ['subscribers', 'publicAPI'];
let labs = module.exports = {};
labs.isSet = function isSet(flag) {
var labsConfig = settingsCache.get('labs');
return labsConfig && labsConfig[flag] && labsConfig[flag] === true;
labs.getAll = () => {
let labs = _.cloneDeep(settingsCache.get('labs')) || {};
// Remove old labs flags that should always be false now
deprecatedFeatures.forEach((feature) => {
delete labs[feature];
});
return labs;
};
labs.getAll = () => {
return settingsCache.get('labs');
labs.isSet = function isSet(flag) {
var labsConfig = labs.getAll();
return !!(labsConfig && labsConfig[flag] && labsConfig[flag] === true);
};
labs.enabledHelper = function enabledHelper(options, callback) {

View file

@ -0,0 +1,52 @@
const should = require('should');
const sinon = require('sinon');
const settingsCache = require('../../../server/services/settings/cache');
const labs = require('../../../server/services/labs');
describe('Labs Service', function () {
let labsCacheStub;
beforeEach(function () {
labsCacheStub = sinon.stub(settingsCache, 'get').withArgs('labs');
});
afterEach(function () {
sinon.restore();
});
it('can getAll, even if empty', function () {
labs.getAll().should.eql({});
});
it('can getAll from cache', function () {
labsCacheStub.returns({members: true, foo: 'bar'});
labs.getAll().should.eql({members: true, foo: 'bar'});
});
it('can getAll from cache, ignoring deprecated', function () {
labsCacheStub.returns({members: true, foo: 'bar', subscribers: false, publicAPI: true});
labs.getAll().should.eql({members: true, foo: 'bar'});
});
it('isSet returns true string flag', function () {
labsCacheStub.returns({foo: 'bar'});
labs.isSet('foo').should.be.true;
});
it('isSet returns false for undefined', function () {
labsCacheStub.returns({foo: 'bar'});
labs.isSet('bar').should.be.false;
});
it('isSet always returns false for deprecated', function () {
labsCacheStub.returns({subscribers: true, publicAPI: true});
labs.isSet('subscribers').should.be.false;
labs.isSet('publicAPI').should.be.false;
});
});