From a61e6e7cc2e0bee11c69aab64fee9d375959eb3e Mon Sep 17 00:00:00 2001 From: Katharina Irrgang Date: Sun, 4 Jun 2017 12:52:22 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=20fix=20settings=20cache=20(#85?= =?UTF-8?q?06)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #8505 - cache.get(..) auto converted "1" to integer --- core/server/settings/cache.js | 5 +++++ core/test/unit/settings/cache_spec.js | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 core/test/unit/settings/cache_spec.js diff --git a/core/server/settings/cache.js b/core/server/settings/cache.js index 10bdaf3fc3..9911b974ea 100644 --- a/core/server/settings/cache.js +++ b/core/server/settings/cache.js @@ -49,6 +49,11 @@ module.exports = { // Default behaviour is to try to resolve the value and return that try { + // CASE: if a string contains a number e.g. "1", JSON.parse will auto convert into integer + if (settingsCache[key].value.match(/^\d+$/)) { + return settingsCache[key].value; + } + return JSON.parse(settingsCache[key].value); } catch (err) { return settingsCache[key].value; diff --git a/core/test/unit/settings/cache_spec.js b/core/test/unit/settings/cache_spec.js new file mode 100644 index 0000000000..b59c1499ff --- /dev/null +++ b/core/test/unit/settings/cache_spec.js @@ -0,0 +1,21 @@ +var rewire = require('rewire'), + should = require('should'), + cache = rewire('../../../server/settings/cache'); + +should.equal(true, true); + +describe('UNIT: settings cache', function () { + it('does not auto convert string into number', function () { + cache.set('key1', {value: '1'}); + (typeof cache.get('key1')).should.eql('string'); + }); + + it('stringified JSON get\'s parsed', function () { + cache.set('key2', {value: '{"a":"1","b":"hallo","c":{"d":[]},"e":2}'}); + (typeof cache.get('key2')).should.eql('object'); + cache.get('key2').a.should.eql('1'); + cache.get('key2').b.should.eql('hallo'); + cache.get('key2').c.should.eql({d: []}); + cache.get('key2').e.should.eql(2); + }); +});