2020-04-29 18:44:27 +03:00
|
|
|
const should = require('should');
|
|
|
|
const sinon = require('sinon');
|
2021-10-06 12:52:46 +03:00
|
|
|
const configUtils = require('../../../utils/configUtils');
|
2014-10-21 14:50:34 +04:00
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
// Stuff we are testing
|
2021-10-06 12:52:46 +03:00
|
|
|
const img_url = require('../../../../core/frontend/helpers/img_url');
|
2020-04-29 18:44:27 +03:00
|
|
|
|
2021-06-15 17:36:27 +03:00
|
|
|
const logging = require('@tryghost/logging');
|
2014-10-21 14:50:34 +04:00
|
|
|
|
2021-06-21 11:57:29 +03:00
|
|
|
describe('{{img_url}} helper', function () {
|
2020-04-29 18:44:27 +03:00
|
|
|
let logWarnStub;
|
2017-08-27 20:06:53 +03:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2020-05-28 21:30:23 +03:00
|
|
|
logWarnStub = sinon.stub(logging, 'warn');
|
2017-08-27 20:06:53 +03:00
|
|
|
});
|
|
|
|
|
2014-10-21 14:50:34 +04:00
|
|
|
afterEach(function () {
|
2019-01-21 19:53:44 +03:00
|
|
|
sinon.restore();
|
2014-10-21 14:50:34 +04:00
|
|
|
});
|
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
describe('without sub-directory', function () {
|
|
|
|
before(function () {
|
2021-06-21 11:57:29 +03:00
|
|
|
configUtils.set({url: 'http://localhost:65535/'});
|
2019-06-18 16:13:55 +03:00
|
|
|
});
|
2014-10-21 14:50:34 +04:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
after(function () {
|
2021-06-21 11:57:29 +03:00
|
|
|
configUtils.restore();
|
2019-06-18 16:13:55 +03:00
|
|
|
});
|
🐛 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
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
it('should output relative url of image', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('/content/images/image-relative-url.png', {});
|
2019-06-18 16:13:55 +03:00
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('/content/images/image-relative-url.png');
|
|
|
|
logWarnStub.called.should.be.false();
|
|
|
|
});
|
2014-10-21 14:50:34 +04:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
it('should output relative url of image if the input is absolute', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('http://localhost:65535/content/images/image-relative-url.png', {});
|
2019-06-18 16:13:55 +03:00
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('/content/images/image-relative-url.png');
|
|
|
|
logWarnStub.called.should.be.false();
|
|
|
|
});
|
2019-03-04 13:23:42 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
it('should output absolute url of image if the option is present ', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
|
2019-06-18 16:13:55 +03:00
|
|
|
should.exist(rendered);
|
2019-08-12 11:31:42 +03:00
|
|
|
rendered.should.equal('http://localhost:65535/content/images/image-relative-url.png');
|
2019-06-18 16:13:55 +03:00
|
|
|
logWarnStub.called.should.be.false();
|
|
|
|
});
|
2014-10-21 14:50:34 +04:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
it('should NOT output absolute url of image if the option is "false" ', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('/content/images/image-relative-url.png', {hash: {absolute: 'false'}});
|
2019-06-18 16:13:55 +03:00
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('/content/images/image-relative-url.png');
|
|
|
|
});
|
2017-08-27 20:06:53 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
it('should output author url', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('/content/images/author-image-relative-url.png', {});
|
2019-06-18 16:13:55 +03:00
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('/content/images/author-image-relative-url.png');
|
|
|
|
logWarnStub.called.should.be.false();
|
|
|
|
});
|
|
|
|
|
2021-06-21 11:57:29 +03:00
|
|
|
it('should have no output if the image attribute is not provided (with warning)', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url({hash: {absolute: 'true'}});
|
2019-06-18 16:13:55 +03:00
|
|
|
should.not.exist(rendered);
|
|
|
|
logWarnStub.calledOnce.should.be.true();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have no output if the image attribute evaluates to undefined (with warning)', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url(undefined, {hash: {absolute: 'true'}});
|
2019-06-18 16:13:55 +03:00
|
|
|
should.not.exist(rendered);
|
|
|
|
logWarnStub.calledOnce.should.be.true();
|
|
|
|
});
|
2014-12-28 22:38:29 +03:00
|
|
|
|
2019-06-18 16:13:55 +03:00
|
|
|
it('should have no output if the image attribute evaluates to null (no waring)', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url(null, {hash: {absolute: 'true'}});
|
2019-06-18 16:13:55 +03:00
|
|
|
should.not.exist(rendered);
|
|
|
|
logWarnStub.calledOnce.should.be.false();
|
|
|
|
});
|
2014-10-21 14:50:34 +04:00
|
|
|
});
|
2014-11-17 06:38:10 +03:00
|
|
|
|
2015-12-14 23:05:11 +03:00
|
|
|
describe('with sub-directory', function () {
|
|
|
|
before(function () {
|
2021-06-21 11:57:29 +03:00
|
|
|
configUtils.set({url: 'http://localhost:65535/blog'});
|
2015-12-14 23:05:11 +03:00
|
|
|
});
|
2019-06-18 16:13:55 +03:00
|
|
|
|
2015-12-14 23:05:11 +03:00
|
|
|
after(function () {
|
2021-06-21 11:57:29 +03:00
|
|
|
configUtils.restore();
|
2015-12-14 23:05:11 +03:00
|
|
|
});
|
2014-11-17 06:38:10 +03:00
|
|
|
|
2015-12-14 23:05:11 +03:00
|
|
|
it('should output relative url of image', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('/blog/content/images/image-relative-url.png', {});
|
2015-12-14 23:05:11 +03:00
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('/blog/content/images/image-relative-url.png');
|
2014-12-28 22:38:29 +03:00
|
|
|
});
|
|
|
|
|
2015-12-14 23:05:11 +03:00
|
|
|
it('should output absolute url of image if the option is present ', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('/blog/content/images/image-relative-url.png', {hash: {absolute: 'true'}});
|
2015-12-14 23:05:11 +03:00
|
|
|
should.exist(rendered);
|
2019-08-12 11:31:42 +03:00
|
|
|
rendered.should.equal('http://localhost:65535/blog/content/images/image-relative-url.png');
|
2014-12-28 22:38:29 +03:00
|
|
|
});
|
|
|
|
|
2015-12-14 23:05:11 +03:00
|
|
|
it('should not change output for an external url', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('http://example.com/picture.jpg', {});
|
2015-12-14 23:05:11 +03:00
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('http://example.com/picture.jpg');
|
2014-12-28 22:38:29 +03:00
|
|
|
});
|
2014-12-02 17:24:51 +03:00
|
|
|
});
|
2018-12-13 15:14:08 +03:00
|
|
|
|
|
|
|
describe('image_sizes', function () {
|
|
|
|
before(function () {
|
2021-06-21 11:57:29 +03:00
|
|
|
configUtils.set({url: 'http://localhost:65535/'});
|
2018-12-13 15:14:08 +03:00
|
|
|
});
|
2019-06-18 16:13:55 +03:00
|
|
|
|
2018-12-13 15:14:08 +03:00
|
|
|
after(function () {
|
2021-06-21 11:57:29 +03:00
|
|
|
configUtils.restore();
|
2018-12-13 15:14:08 +03:00
|
|
|
});
|
2019-06-18 16:13:55 +03:00
|
|
|
|
2018-12-13 15:14:08 +03:00
|
|
|
it('should output correct url for absolute paths which are internal', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('http://localhost:65535/content/images/my-coole-img.jpg', {
|
2018-12-13 15:14:08 +03:00
|
|
|
hash: {
|
2019-07-05 14:40:43 +03:00
|
|
|
size: 'medium'
|
2018-12-13 15:14:08 +03:00
|
|
|
},
|
|
|
|
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');
|
2019-01-21 13:41:23 +03:00
|
|
|
});
|
|
|
|
it('should output the correct url for protocol relative urls', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('//website.com/whatever/my-coole-img.jpg', {
|
2019-01-21 13:41:23 +03:00
|
|
|
hash: {
|
2019-07-05 14:40:43 +03:00
|
|
|
size: 'medium'
|
2019-01-21 13:41:23 +03:00
|
|
|
},
|
|
|
|
data: {
|
|
|
|
config: {
|
|
|
|
image_sizes: {
|
|
|
|
medium: {
|
|
|
|
width: 400
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('//website.com/whatever/my-coole-img.jpg');
|
|
|
|
});
|
2018-12-13 15:14:08 +03:00
|
|
|
it('should output the correct url for relative paths', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('/content/images/my-coole-img.jpg', {
|
2018-12-13 15:14:08 +03:00
|
|
|
hash: {
|
2019-07-05 14:40:43 +03:00
|
|
|
size: 'medium'
|
2018-12-13 15:14:08 +03:00
|
|
|
},
|
|
|
|
data: {
|
|
|
|
config: {
|
|
|
|
image_sizes: {
|
|
|
|
medium: {
|
|
|
|
width: 400
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('/content/images/size/w400/my-coole-img.jpg');
|
|
|
|
});
|
2019-07-29 11:54:09 +03:00
|
|
|
|
|
|
|
it('should output the correct url for relative paths without leading slash', function () {
|
2021-10-04 18:30:54 +03:00
|
|
|
const rendered = img_url('content/images/my-coole-img.jpg', {
|
2019-07-29 11:54:09 +03:00
|
|
|
hash: {
|
|
|
|
size: 'medium'
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
config: {
|
|
|
|
image_sizes: {
|
|
|
|
medium: {
|
|
|
|
width: 400
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
should.exist(rendered);
|
|
|
|
rendered.should.equal('content/images/size/w400/my-coole-img.jpg');
|
|
|
|
});
|
2018-12-13 15:14:08 +03:00
|
|
|
});
|
2014-11-17 06:38:10 +03:00
|
|
|
});
|