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

Fixed settingsCache returning falsy as null

refs: e68cb8b314

- a couple of months ago when improving the test coverage here I found some weird behaviour with falsey values
- turned out it didn't matter at the time because we didn't have any settings that are false
- with the introduction of calculated settings we will have: https://github.com/TryGhost/Ghost/pull/14766
- whilst building that, I found settings that should be returned as false were being returned as null
- fixing it in a separate commit to keep the work clean
This commit is contained in:
Hannah Wolfe 2022-05-10 20:05:56 +01:00
parent bb1a5707d8
commit ddb718f0bb
No known key found for this signature in database
GPG key ID: AB586C3B5AE5C037
2 changed files with 14 additions and 6 deletions

View file

@ -33,6 +33,11 @@ const doGet = (key, options) => {
// Default behaviour is to try to resolve the value and return that // Default behaviour is to try to resolve the value and return that
try { try {
// CASE: handle literal false
if (settingsCache[key].value === false || settingsCache[key].value === 'false') {
return false;
}
// CASE: if a string contains a number e.g. "1", JSON.parse will auto convert into integer // CASE: if a string contains a number e.g. "1", JSON.parse will auto convert into integer
if (!isNaN(Number(settingsCache[key].value))) { if (!isNaN(Number(settingsCache[key].value))) {
return settingsCache[key].value || null; return settingsCache[key].value || null;
@ -99,7 +104,7 @@ module.exports = {
let settings = {}; let settings = {};
_.each(publicSettings, (key, newKey) => { _.each(publicSettings, (key, newKey) => {
settings[newKey] = doGet(key) || null; settings[newKey] = doGet(key) ?? null;
}); });
return settings; return settings;
@ -110,7 +115,7 @@ module.exports = {
* Optionally takes a collection of settings & can populate the cache with these. * Optionally takes a collection of settings & can populate the cache with these.
* *
* @param {EventEmitter} events * @param {EventEmitter} events
* @param {Bookshelf.Collection<Settings>} [settingsCollection] * @param {Bookshelf.Collection<Settings>} settingsCollection
* @return {object} * @return {object}
*/ */
init(events, settingsCollection) { init(events, settingsCollection) {

View file

@ -11,7 +11,7 @@ should.equal(true, true);
describe('UNIT: settings cache', function () { describe('UNIT: settings cache', function () {
beforeEach(function () { beforeEach(function () {
cache.init(events); cache.init(events, {});
}); });
afterEach(function () { afterEach(function () {
@ -64,7 +64,7 @@ describe('UNIT: settings cache', function () {
should(cache.get('nan')).eql(null); should(cache.get('nan')).eql(null);
should(cache.get('true')).eql(true); should(cache.get('true')).eql(true);
should(cache.get('false')).eql(null); // EEEK! this is falsy but should be false should(cache.get('false')).eql(false);
should(cache.get('object')).eql({}); should(cache.get('object')).eql({});
should(cache.get('array')).eql([]); should(cache.get('array')).eql([]);
@ -81,7 +81,7 @@ describe('UNIT: settings cache', function () {
should(cache.get('stringnull')).eql(null); should(cache.get('stringnull')).eql(null);
should(cache.get('stringnan')).eql('NaN'); should(cache.get('stringnan')).eql('NaN');
should(cache.get('stringtrue')).eql(true); should(cache.get('stringtrue')).eql(true);
should(cache.get('stringfalse')).eql(null); // EEEK! this is falsy but should be false should(cache.get('stringfalse')).eql(false);
should(cache.get('stringobj')).eql({}); should(cache.get('stringobj')).eql({});
should(cache.get('stringarr')).eql([]); should(cache.get('stringarr')).eql([]);
}); });
@ -96,16 +96,19 @@ describe('UNIT: settings cache', function () {
cache.set('key1', {value: 'something'}); cache.set('key1', {value: 'something'});
cache.set('title', {value: 'hello world'}); cache.set('title', {value: 'hello world'});
cache.set('timezone', {value: 'PST'}); cache.set('timezone', {value: 'PST'});
cache.set('secondary_navigation', {value: false});
cache.getAll().should.eql({ cache.getAll().should.eql({
key1: {value: 'something'}, key1: {value: 'something'},
title: {value: 'hello world'}, title: {value: 'hello world'},
timezone: {value: 'PST'} timezone: {value: 'PST'},
secondary_navigation: {value: false}
}); });
let values = _.zipObject(_.keys(publicSettings), _.fill(Array(_.size(publicSettings)), null)); let values = _.zipObject(_.keys(publicSettings), _.fill(Array(_.size(publicSettings)), null));
values.title = 'hello world'; values.title = 'hello world';
values.timezone = 'PST'; values.timezone = 'PST';
values.secondary_navigation = false;
cache.getPublic().should.eql(values); cache.getPublic().should.eql(values);
}); });