Ghost/test/unit/helpers/img_url_spec.js

207 lines
7.6 KiB
JavaScript
Raw Normal View History

const should = require('should');
const sinon = require('sinon');
const urlUtils = require('../../utils/urlUtils');
// Stuff we are testing
const helpers = require('../../../core/frontend/helpers');
const logging = require('@tryghost/logging');
describe('{{image}} helper', function () {
let logWarnStub;
beforeEach(function () {
logWarnStub = sinon.stub(logging, 'warn');
});
afterEach(function () {
sinon.restore();
});
describe('without sub-directory', function () {
let sandbox;
before(function () {
sandbox = sinon.createSandbox();
urlUtils.stubUrlUtils({url: 'http://localhost:65535/'}, sandbox);
});
after(function () {
sandbox.restore();
urlUtils.restore();
});
🐛 Fixed img-url to output relative url by default (#10596) closes #10595 * Added breaking test for img-url helper Input from the content API was absolute, adding this test to verify my fix * Updated existing test to breaking test for img-url Had made a dumb assumption when building images sizes, this updates the test to fail so we can verify the fix * Refactored img-url to return as early as possible This makes it a little easier to parse what's going on, and it also allows us to remove the check for existence of the image url in the getImageSizes function * Refactored img-url config parsing to clean up core logic Superficial refactor to make future changes able to focus on what rather than how. * Refactored internal image detection into helper We're gonna need to know if the image is internal or not, when we force the path to relative, if we pull this out now, we have access in the top level logic * Removed duplicate checks for internal image Cleaning up and moving "higher-level" logic into the main function of the module * Renamed attr -> requestedImageUrl Superficial refactor, trying to be more explicit about identifiers * 🐛 Fixed img-url to output relative url by default Includes a check to isInternalImage as we never want to make external images relative. * Returned early if img-url recieves external url After realising we never want to deal with external urls, we can continue to return as early as possible, letting us remove checks and simplify the more complex logic for internal images. * Cleaned up the internal image logic Defining the three functions in order helps to see what operations are going to happen and in which order, we can then return the result of each operation applied to the next operation.
2019-03-11 17:20:05 +03:00
it('should output relative url of image', function () {
const rendered = helpers.img_url('/content/images/image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should output relative url of image if the input is absolute', function () {
const rendered = helpers.img_url('http://localhost:65535/content/images/image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should output absolute url of image if the option is present ', function () {
const rendered = helpers.img_url('/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
should.exist(rendered);
rendered.should.equal('http://localhost:65535/content/images/image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should NOT output absolute url of image if the option is "false" ', function () {
const rendered = helpers.img_url('/content/images/image-relative-url.png', {hash: {absolute: 'false'}});
should.exist(rendered);
rendered.should.equal('/content/images/image-relative-url.png');
});
it('should output author url', function () {
const rendered = helpers.img_url('/content/images/author-image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/content/images/author-image-relative-url.png');
logWarnStub.called.should.be.false();
});
it('should have no output if the image attributeis not provided (with warning)', function () {
const rendered = helpers.img_url({hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.true();
});
it('should have no output if the image attribute evaluates to undefined (with warning)', function () {
const rendered = helpers.img_url(undefined, {hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.true();
});
it('should have no output if the image attribute evaluates to null (no waring)', function () {
const rendered = helpers.img_url(null, {hash: {absolute: 'true'}});
should.not.exist(rendered);
logWarnStub.calledOnce.should.be.false();
});
});
describe('with sub-directory', function () {
let sandbox;
before(function () {
sandbox = sinon.createSandbox();
urlUtils.stubUrlUtils({url: 'http://localhost:65535/blog'}, sandbox);
});
after(function () {
sandbox.restore();
urlUtils.restore();
});
it('should output relative url of image', function () {
const rendered = helpers.img_url('/blog/content/images/image-relative-url.png', {});
should.exist(rendered);
rendered.should.equal('/blog/content/images/image-relative-url.png');
});
it('should output absolute url of image if the option is present ', function () {
const rendered = helpers.img_url('/blog/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
should.exist(rendered);
rendered.should.equal('http://localhost:65535/blog/content/images/image-relative-url.png');
});
it('should not change output for an external url', function () {
const rendered = helpers.img_url('http://example.com/picture.jpg', {});
should.exist(rendered);
rendered.should.equal('http://example.com/picture.jpg');
});
});
describe('image_sizes', function () {
let sandbox;
before(function () {
sandbox = sinon.createSandbox();
urlUtils.stubUrlUtils({url: 'http://localhost:65535/'}, sandbox);
});
after(function () {
sandbox.restore();
urlUtils.restore();
});
it('should output correct url for absolute paths which are internal', function () {
const rendered = helpers.img_url('http://localhost:65535/content/images/my-coole-img.jpg', {
hash: {
size: 'medium'
},
data: {
config: {
image_sizes: {
medium: {
width: 400
}
}
}
}
});
should.exist(rendered);
🐛 Fixed img-url to output relative url by default (#10596) closes #10595 * Added breaking test for img-url helper Input from the content API was absolute, adding this test to verify my fix * Updated existing test to breaking test for img-url Had made a dumb assumption when building images sizes, this updates the test to fail so we can verify the fix * Refactored img-url to return as early as possible This makes it a little easier to parse what's going on, and it also allows us to remove the check for existence of the image url in the getImageSizes function * Refactored img-url config parsing to clean up core logic Superficial refactor to make future changes able to focus on what rather than how. * Refactored internal image detection into helper We're gonna need to know if the image is internal or not, when we force the path to relative, if we pull this out now, we have access in the top level logic * Removed duplicate checks for internal image Cleaning up and moving "higher-level" logic into the main function of the module * Renamed attr -> requestedImageUrl Superficial refactor, trying to be more explicit about identifiers * 🐛 Fixed img-url to output relative url by default Includes a check to isInternalImage as we never want to make external images relative. * Returned early if img-url recieves external url After realising we never want to deal with external urls, we can continue to return as early as possible, letting us remove checks and simplify the more complex logic for internal images. * Cleaned up the internal image logic Defining the three functions in order helps to see what operations are going to happen and in which order, we can then return the result of each operation applied to the next operation.
2019-03-11 17:20:05 +03:00
rendered.should.equal('/content/images/size/w400/my-coole-img.jpg');
});
it('should output the correct url for protocol relative urls', function () {
const rendered = helpers.img_url('//website.com/whatever/my-coole-img.jpg', {
hash: {
size: 'medium'
},
data: {
config: {
image_sizes: {
medium: {
width: 400
}
}
}
}
});
should.exist(rendered);
rendered.should.equal('//website.com/whatever/my-coole-img.jpg');
});
it('should output the correct url for relative paths', function () {
const rendered = helpers.img_url('/content/images/my-coole-img.jpg', {
hash: {
size: 'medium'
},
data: {
config: {
image_sizes: {
medium: {
width: 400
}
}
}
}
});
should.exist(rendered);
rendered.should.equal('/content/images/size/w400/my-coole-img.jpg');
});
it('should output the correct url for relative paths without leading slash', function () {
const rendered = helpers.img_url('content/images/my-coole-img.jpg', {
hash: {
size: 'medium'
},
data: {
config: {
image_sizes: {
medium: {
width: 400
}
}
}
}
});
should.exist(rendered);
rendered.should.equal('content/images/size/w400/my-coole-img.jpg');
});
});
});