Ghost/core/test/unit/utils/blog-icon_spec.js

193 lines
7.7 KiB
JavaScript
Raw Normal View History

🙇 Blog icon utils and publisher.logo for JSON-LD (#8297) refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
2017-04-11 19:32:06 +03:00
// jshint unused: false
var should = require('should'),
sinon = require('sinon'),
_ = require('lodash'),
settingsCache = require('../../../server/settings/cache'),
configUtils = require('../../utils/configUtils'),
testUtils = require('../../utils'),
config = configUtils.config,
path = require('path'),
rewire = require('rewire'),
🙇 Blog icon utils and publisher.logo for JSON-LD (#8297) refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
2017-04-11 19:32:06 +03:00
// stuff we are testing
blogIcon = rewire('../../../server/utils/blog-icon'),
🙇 Blog icon utils and publisher.logo for JSON-LD (#8297) refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
2017-04-11 19:32:06 +03:00
sandbox = sinon.sandbox.create();
describe('Blog Icon', function () {
before(function () {
configUtils.restore();
});
afterEach(function () {
configUtils.restore();
sandbox.restore();
rewire('../../../server/utils/blog-icon');
🙇 Blog icon utils and publisher.logo for JSON-LD (#8297) refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
2017-04-11 19:32:06 +03:00
});
describe('getIconUrl', function () {
it('custom uploaded ico blog icon', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.ico');
blogIcon.getIconUrl().should.eql('/favicon.ico');
});
it('custom uploaded png blog icon', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
blogIcon.getIconUrl().should.eql('/favicon.png');
});
it('default ico blog icon', function () {
blogIcon.getIconUrl().should.eql('/favicon.ico');
});
describe('absolute URL', function () {
it('custom uploaded ico blog icon', function () {
configUtils.set({url: 'http://my-ghost-blog.com/'});
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.ico');
blogIcon.getIconUrl(true).should.eql('http://my-ghost-blog.com/favicon.ico');
});
it('custom uploaded png blog icon', function () {
configUtils.set({url: 'http://my-ghost-blog.com/'});
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
blogIcon.getIconUrl(true).should.eql('http://my-ghost-blog.com/favicon.png');
});
it('default ico blog icon', function () {
configUtils.set({url: 'http://my-ghost-blog.com/'});
blogIcon.getIconUrl(true).should.eql('http://my-ghost-blog.com/favicon.ico');
});
});
describe('with subdirectory', function () {
it('custom uploaded ico blog icon', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.ico');
configUtils.set({url: 'http://my-ghost-blog.com/blog'});
blogIcon.getIconUrl().should.eql('/blog/favicon.ico');
});
it('custom uploaded png blog icon', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
configUtils.set({url: 'http://my-ghost-blog.com/blog'});
blogIcon.getIconUrl().should.eql('/blog/favicon.png');
});
it('default ico blog icon', function () {
configUtils.set({url: 'http://my-ghost-blog.com/blog'});
blogIcon.getIconUrl().should.eql('/blog/favicon.ico');
});
});
});
describe('getIconPath', function () {
it('custom uploaded ico blog icon', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.ico');
blogIcon.getIconPath().should.eql('/2017/04/my-icon.ico');
});
it('custom uploaded png blog icon', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/content/images/2017/04/my-icon.png');
blogIcon.getIconPath().should.eql('/2017/04/my-icon.png');
});
it('default ico blog icon', function () {
blogIcon.getIconPath().should.eql(path.join(__dirname, '../../../server/public/favicon.ico'));
});
describe('with subdirectory', function () {
it('custom uploaded ico blog icon', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/blog/content/images/2017/04/my-icon.ico');
configUtils.set({url: 'http://my-ghost-blog.com/blog'});
blogIcon.getIconPath().should.eql('/2017/04/my-icon.ico');
});
it('custom uploaded png blog icon', function () {
sandbox.stub(settingsCache, 'get').withArgs('icon').returns('/blog/content/images/2017/04/my-icon.png');
configUtils.set({url: 'http://my-ghost-blog.com/blog'});
blogIcon.getIconPath().should.eql('/2017/04/my-icon.png');
});
it('default ico blog icon', function () {
configUtils.set({url: 'http://my-ghost-blog.com/blog'});
blogIcon.getIconPath().should.eql(path.join(__dirname, '../../../server/public/favicon.ico'));
});
});
});
describe('isIcoImageType', function () {
it('returns true, if icon is .ico filetype', function () {
blogIcon.isIcoImageType('icon.ico').should.be.true();
});
it('returns false, if icon is not .ico filetype', function () {
blogIcon.isIcoImageType('icon.png').should.be.false();
});
});
describe('getIconType', function () {
it('returns x-icon for ico icons', function () {
blogIcon.getIconType('favicon.ico').should.eql('x-icon');
});
it('returns png for png icon', function () {
blogIcon.getIconType('favicon.png').should.eql('png');
});
});
describe('getIconDimensions', function () {
it('[success] returns .ico dimensions', function (done) {
blogIcon.getIconDimensions(path.join(__dirname, '../../utils/fixtures/images/favicon.ico'))
.then(function (result) {
should.exist(result);
result.should.eql({
width: 48,
height: 48
});
done();
}).catch(done);
});
it('[success] returns .png dimensions', function (done) {
blogIcon.getIconDimensions(path.join(__dirname, '../../utils/fixtures/images/favicon.png'))
.then(function (result) {
should.exist(result);
result.should.eql({
width: 100,
height: 100
});
done();
}).catch(done);
});
it('[success] returns .ico dimensions for icon with multiple sizes', function (done) {
blogIcon.getIconDimensions(path.join(__dirname, '../../utils/fixtures/images/favicon_multi_sizes.ico'))
.then(function (result) {
should.exist(result);
result.should.eql({
width: 64,
height: 64
});
done();
}).catch(done);
🙇 Blog icon utils and publisher.logo for JSON-LD (#8297) refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
2017-04-11 19:32:06 +03:00
});
it('[failure] return error message', function (done) {
var sizeOfStub = sandbox.stub();
sizeOfStub.throws({error: 'image-size could not find dimensions'});
blogIcon.__set__('sizeOf', sizeOfStub);
blogIcon.getIconDimensions(path.join(__dirname, '../../utils/fixtures/images/favicon_multi_sizes.ico'))
.catch(function (error) {
should.exist(error);
error.message.should.eql('Could not fetch icon dimensions.');
done();
});
🙇 Blog icon utils and publisher.logo for JSON-LD (#8297) refs #8221, closes #7688, refs #7558 🙇 Improve meta data publisher logo behaviour This is a follow-up PR for #8285. Reasons: The code changes of #8285 caused error messages when falling back to the default `favicon.ico`, as the `image-size` tool doesn't support `ico` files. This PR takes the logic to decide which logo needs to be listed in our schema into a new fn `blog_logo.js`. There we have now three decisions: 1. If we have a publication **logo**, we'll take that one 2. If we have no publication logo, but an **icon** we'll use this one. 3. If we have none of the above things, we fall back to our default `favicon.ico` Additional, we're hard coding image dimensions for whenever the logo is an `.ico` file and built and extra decision to not call `image-size` when the dimension are already given. I will create another follow-up PR, which checks the extension type for the file and offers it as a util. 🛠 Blog icon util refs #7688 Serve functionality around the blog icon in its own util: - getIconDimensions -> async function that takes the filepath of on ico file and returns its dimensions - isIcoImageType -> returns true if file has `.ico` extension - getIconType -> returns icon-type (`x-icon` or `png`) - getIconUrl -> returns the absolut or relativ URL for the favicon: `[subdirectory or not]favicon.[ico or png]` 📖 Get .ico sizes for meta data & logo improvement refs #7558 refs #8221 Use the new `blogIconUtil` in meta data to fetch the dimensions of `.ico` files. Improvements for `publisher.logo`: We're now returning a hard-coded 'faked' image dimensions value to render an `imageObject` and prevent error our schema (Google structured data). As soon as an image (`.ico` or non-`.ico`) is too large, but - in case of non-`.ico` - a square format, be set the image-dimensions to 60px width and height. This reduces the chances of getting constantly error messages from Googles' webmaster tools. - add getIconPath util
2017-04-11 19:32:06 +03:00
});
});
});