From 25d81c99ca8fdd8a01e7a15e7b48ea2a9423fc52 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 31 Jan 2023 21:01:02 +0800 Subject: [PATCH 1/2] Fixed {{price}} helper to render empty instead of throwing refs https://github.com/TryGhost/Toolbox/issues/497 refs https://github.com/TryGhost/gscan/commit/fb7532bf5d2f74afde6dfbf3de0af27a17ef41da - We downgraded the 'GS090-NO-PRICE-DATA-CURRENCY-CONTEXT' rule in gscan to non-fatal, meaning Ghost should not be throwing an error but instead render an empty value for {{price}} helper when price data is empty. - For example, a legacy syntax like this: '{{price currency=@price.currency}}' should not cause a page render error but return an empty price string. - The pattern of returning an empty string instead of crashing is used in other helpers like {{img_url}} and and {{url}} --- ghost/core/core/frontend/helpers/price.js | 19 +++++------ .../test/unit/frontend/helpers/price.test.js | 32 ++++++++++++------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/ghost/core/core/frontend/helpers/price.js b/ghost/core/core/frontend/helpers/price.js index de670c1bda..48b9b945dd 100644 --- a/ghost/core/core/frontend/helpers/price.js +++ b/ghost/core/core/frontend/helpers/price.js @@ -8,10 +8,10 @@ // Usage: `{{price 500 currency="USD"}}` // Usage: `{{price currency="USD"}}` // -// Returns amount equal to the dominant denomintation of the currency. +// Returns amount equal to the dominant denomination of the currency. // For example, if 2100 is passed, it will return 21. -const errors = require('@tryghost/errors'); const tpl = require('@tryghost/tpl'); +const logging = require('@tryghost/logging'); const _ = require('lodash'); const messages = { @@ -82,22 +82,19 @@ module.exports = function price(planOrAmount, options) { // CASE: if no amount is passed, e.g. `{{price}}` we throw an error if (arguments.length < 2) { - throw new errors.IncorrectUsageError({ - message: tpl(messages.attrIsRequired) - }); + logging.warn(tpl(messages.attrIsRequired)); + return ''; } // CASE: if amount is passed, but it is undefined we throw an error if (amount === undefined) { - throw new errors.IncorrectUsageError({ - message: tpl(messages.attrIsRequired) - }); + logging.warn(tpl(messages.attrIsRequired)); + return ''; } if (!_.isNumber(amount)) { - throw new errors.IncorrectUsageError({ - message: tpl(messages.attrMustBeNumeric) - }); + logging.warn(tpl(messages.attrMustBeNumeric)); + return ''; } return amount / 100; diff --git a/ghost/core/test/unit/frontend/helpers/price.test.js b/ghost/core/test/unit/frontend/helpers/price.test.js index 5731a2f04f..3b1b46fc38 100644 --- a/ghost/core/test/unit/frontend/helpers/price.test.js +++ b/ghost/core/test/unit/frontend/helpers/price.test.js @@ -1,9 +1,21 @@ -const should = require('should'); +const sinon = require('sinon'); const price = require('../../../../core/frontend/helpers/price'); -const {registerHelper, shouldCompileToError, shouldCompileToExpected} = require('./utils/handlebars'); +const {registerHelper, shouldCompileToExpected} = require('./utils/handlebars'); + +const logging = require('@tryghost/logging'); describe('{{price}} helper', function () { + let logWarnStub; + + beforeEach(function () { + logWarnStub = sinon.stub(logging, 'warn'); + }); + + afterEach(function () { + sinon.restore(); + }); + before(function () { registerHelper('price'); }); @@ -11,24 +23,22 @@ describe('{{price}} helper', function () { it('throws an error for no provided parameters', function () { const templateString = '{{price}}'; - shouldCompileToError(templateString, {}, { - name: 'IncorrectUsageError' - }); + shouldCompileToExpected(templateString, {}, ''); + logWarnStub.calledOnce.should.be.true(); }); it('throws an error for undefined parameter', function () { const templateString = '{{price @dont.exist}}'; - shouldCompileToError(templateString, {}, { - name: 'IncorrectUsageError' - }); + shouldCompileToExpected(templateString, {}, ''); + logWarnStub.calledOnce.should.be.true(); }); it('throws if argument is not a number', function () { const templateString = '{{price "not_a_number"}}'; - shouldCompileToError(templateString, {}, { - name: 'IncorrectUsageError' - }); + + shouldCompileToExpected(templateString, {}, ''); + logWarnStub.calledOnce.should.be.true(); }); it('will format decimal adjusted amount', function () { From 444ca6f5e59240351b2c4072eebc4415fae746bd Mon Sep 17 00:00:00 2001 From: Ghost CI <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 31 Jan 2023 14:37:44 +0000 Subject: [PATCH 2/2] v5.33.2 --- ghost/admin/package.json | 2 +- ghost/core/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ghost/admin/package.json b/ghost/admin/package.json index b5a6614bc5..7c5c8dda64 100644 --- a/ghost/admin/package.json +++ b/ghost/admin/package.json @@ -1,6 +1,6 @@ { "name": "ghost-admin", - "version": "5.33.1", + "version": "5.33.2", "description": "Ember.js admin client for Ghost", "author": "Ghost Foundation", "homepage": "http://ghost.org", diff --git a/ghost/core/package.json b/ghost/core/package.json index 9b79c01822..7e4d1c68de 100644 --- a/ghost/core/package.json +++ b/ghost/core/package.json @@ -1,6 +1,6 @@ { "name": "ghost", - "version": "5.33.1", + "version": "5.33.2", "description": "The professional publishing platform", "author": "Ghost Foundation", "homepage": "https://ghost.org",