0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/test/unit/metadata/image-dimensions_spec.js
Aileen Nowak e19e91044d 🙇 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 18:32:06 +02:00

284 lines
11 KiB
JavaScript

var should = require('should'),
sinon = require('sinon'),
rewire = require('rewire'),
// Stuff we are testing
getImageDimensions = rewire('../../../server/data/meta/image-dimensions'),
getCachedImageSizeFromUrl = rewire('../../../server/utils/cached-image-size-from-url'),
sandbox = sinon.sandbox.create();
describe('getImageDimensions', function () {
var sizeOfStub;
beforeEach(function () {
sizeOfStub = sandbox.stub();
});
afterEach(function () {
sandbox.restore();
getCachedImageSizeFromUrl.__set__('imageSizeCache', {});
});
it('should return dimension for images', function (done) {
var metaData = {
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg'
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg'
},
blog: {
logo: {
url: 'http://mysite.com/author/image/url/logo.jpg'
}
}
};
sizeOfStub.returns({
width: 50,
height: 50,
type: 'jpg'
});
getImageDimensions.__set__('getCachedImageSizeFromUrl', sizeOfStub);
getImageDimensions(metaData).then(function (result) {
should.exist(result);
sizeOfStub.calledWith(metaData.coverImage.url).should.be.true();
sizeOfStub.calledWith(metaData.authorImage.url).should.be.true();
sizeOfStub.calledWith(metaData.blog.logo.url).should.be.true();
result.coverImage.should.have.property('dimensions');
result.coverImage.should.have.property('url');
result.blog.logo.should.have.property('dimensions');
result.coverImage.dimensions.should.have.property('height', 50);
result.coverImage.dimensions.should.have.property('width', 50);
result.blog.logo.should.have.property('dimensions');
result.blog.logo.dimensions.should.have.property('height', 50);
result.blog.logo.dimensions.should.have.property('width', 50);
result.authorImage.should.have.property('dimensions');
result.authorImage.dimensions.should.have.property('height', 50);
result.authorImage.dimensions.should.have.property('width', 50);
result.blog.logo.should.have.property('url');
result.authorImage.should.have.property('dimensions');
result.authorImage.should.have.property('url');
done();
}).catch(done);
});
it('should return metaData if url is undefined or null', function (done) {
var metaData = {
coverImage: {
url: undefined
},
authorImage: {
url: null
},
blog: {
logo: {
url: 'noUrl'
}
}
};
sizeOfStub.returns({});
getImageDimensions.__set__('getCachedImageSizeFromUrl', sizeOfStub);
getImageDimensions(metaData).then(function (result) {
should.exist(result);
sizeOfStub.calledWith(metaData.coverImage.url).should.be.true();
sizeOfStub.calledWith(metaData.authorImage.url).should.be.true();
sizeOfStub.calledWith(metaData.blog.logo.url).should.be.true();
result.coverImage.should.not.have.property('dimensions');
result.blog.logo.should.not.have.property('dimensions');
result.authorImage.should.not.have.property('dimensions');
result.coverImage.should.have.property('url');
result.blog.logo.should.have.property('url');
result.authorImage.should.have.property('url');
done();
}).catch(done);
});
it('should not try to fetch image dimensions for logo if already set', function (done) {
var metaData = {
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg'
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg'
},
blog: {
logo: {
url: 'http://mysite.com/author/image/url/favicon.ico',
dimensions: {
width: 60,
height: 60
}
}
}
};
sizeOfStub.returns({
width: 480,
height: 80,
type: 'jpg'
});
getImageDimensions.__set__('getCachedImageSizeFromUrl', sizeOfStub);
getImageDimensions(metaData).then(function (result) {
should.exist(result);
sizeOfStub.calledWith(metaData.coverImage.url).should.be.true();
sizeOfStub.calledWith(metaData.authorImage.url).should.be.true();
sizeOfStub.calledWith(metaData.blog.logo.url).should.be.false();
result.coverImage.should.have.property('dimensions');
result.coverImage.dimensions.should.have.property('height', 80);
result.coverImage.dimensions.should.have.property('width', 480);
result.blog.logo.should.have.property('dimensions');
result.blog.logo.dimensions.should.have.property('height', 60);
result.blog.logo.dimensions.should.have.property('width', 60);
result.authorImage.should.have.property('dimensions');
result.authorImage.dimensions.should.have.property('height', 80);
result.authorImage.dimensions.should.have.property('width', 480);
result.coverImage.should.have.property('url');
result.blog.logo.should.have.property('url');
result.authorImage.should.have.property('url');
done();
}).catch(done);
});
it('should fake image dimension for publisher.logo if .ico file is too big', function (done) {
var metaData = {
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg'
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg'
},
blog: {
logo: {
url: 'http://mysite.com/author/image/url/favicon.ico',
dimensions: {
width: 128,
height: 128
}
}
}
};
sizeOfStub.returns({
width: 480,
height: 480,
type: 'jpg'
});
getImageDimensions.__set__('getCachedImageSizeFromUrl', sizeOfStub);
getImageDimensions(metaData).then(function (result) {
should.exist(result);
sizeOfStub.calledWith(metaData.coverImage.url).should.be.true();
sizeOfStub.calledWith(metaData.authorImage.url).should.be.true();
sizeOfStub.calledWith(metaData.blog.logo.url).should.be.false();
result.coverImage.should.have.property('dimensions');
result.coverImage.dimensions.should.have.property('height', 480);
result.coverImage.dimensions.should.have.property('width', 480);
result.blog.logo.should.have.property('dimensions');
result.blog.logo.dimensions.should.have.property('height', 60);
result.blog.logo.dimensions.should.have.property('width', 60);
result.authorImage.should.have.property('dimensions');
result.authorImage.dimensions.should.have.property('height', 480);
result.authorImage.dimensions.should.have.property('width', 480);
result.coverImage.should.have.property('url');
result.blog.logo.should.have.property('url');
result.authorImage.should.have.property('url');
done();
}).catch(done);
});
it('should fake image dimension for publisher.logo if non-.ico file is too big and square', function (done) {
var metaData = {
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg'
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg'
},
blog: {
logo: {
url: 'http://mysite.com/author/image/url/favicon.png'
}
}
};
sizeOfStub.returns({
width: 480,
height: 480,
type: 'jpg'
});
getImageDimensions.__set__('getCachedImageSizeFromUrl', sizeOfStub);
getImageDimensions(metaData).then(function (result) {
should.exist(result);
sizeOfStub.calledWith(metaData.coverImage.url).should.be.true();
sizeOfStub.calledWith(metaData.authorImage.url).should.be.true();
sizeOfStub.calledWith(metaData.blog.logo.url).should.be.true();
result.coverImage.should.have.property('dimensions');
result.coverImage.dimensions.should.have.property('height', 480);
result.coverImage.dimensions.should.have.property('width', 480);
result.blog.logo.should.have.property('dimensions');
result.blog.logo.dimensions.should.have.property('height', 60);
result.blog.logo.dimensions.should.have.property('width', 60);
result.authorImage.should.have.property('dimensions');
result.authorImage.dimensions.should.have.property('height', 480);
result.authorImage.dimensions.should.have.property('width', 480);
result.coverImage.should.have.property('url');
result.blog.logo.should.have.property('url');
result.authorImage.should.have.property('url');
done();
}).catch(done);
});
it('should not fake dimension for publisher.logo if a logo is too big but not square', function (done) {
var metaData = {
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg'
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg'
},
blog: {
logo: {
url: 'http://mysite.com/author/image/url/logo.jpg'
}
}
};
sizeOfStub.returns({
width: 80,
height: 480,
type: 'jpg'
});
getImageDimensions.__set__('getCachedImageSizeFromUrl', sizeOfStub);
getImageDimensions(metaData).then(function (result) {
should.exist(result);
sizeOfStub.calledWith(metaData.coverImage.url).should.be.true();
sizeOfStub.calledWith(metaData.authorImage.url).should.be.true();
sizeOfStub.calledWith(metaData.blog.logo.url).should.be.true();
result.coverImage.should.have.property('dimensions');
result.coverImage.dimensions.should.have.property('height', 480);
result.coverImage.dimensions.should.have.property('width', 80);
result.blog.logo.should.not.have.property('dimensions');
result.authorImage.should.have.property('dimensions');
result.authorImage.dimensions.should.have.property('height', 480);
result.authorImage.dimensions.should.have.property('width', 80);
result.coverImage.should.have.property('url');
result.blog.logo.should.have.property('url');
result.authorImage.should.have.property('url');
done();
}).catch(done);
});
});