0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Fixed {{price}} helper to render empty instead of throwing

refs https://github.com/TryGhost/Toolbox/issues/497
refs fb7532bf5d

- 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}}
This commit is contained in:
Naz 2023-01-31 21:01:02 +08:00
parent 62ab5073ac
commit 9390f0953f
No known key found for this signature in database
2 changed files with 29 additions and 22 deletions

View file

@ -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;

View file

@ -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 () {