Ghost/test/unit/frontend/meta/description.test.js

640 lines
21 KiB
JavaScript
Raw Normal View History

const should = require('should');
const sinon = require('sinon');
const getMetaDescription = require('../../../../core/frontend/meta/description');
const settingsCache = require('../../../../core/shared/settings-cache');
describe('getMetaDescription', function () {
let localSettingsCache = {};
before(function () {
sinon.stub(settingsCache, 'get').callsFake(function (key) {
return localSettingsCache[key];
});
});
after(function () {
sinon.restore();
});
beforeEach(function () {
localSettingsCache.description = 'Site description';
localSettingsCache.meta_description = 'Site meta description';
localSettingsCache.og_description = 'Site og description';
localSettingsCache.twitter_description = 'Site twitter description';
});
afterEach(function () {
localSettingsCache = {};
});
it('should return meta_description if on data root', function () {
const description = getMetaDescription({
meta_description: 'My data meta description'
}, {
context: 'home'
});
description.should.equal('My data meta description');
});
// <meta name="description">
describe('property: null', function () {
it('has correct fallbacks for context: home', function () {
getMetaDescription({}, {context: 'home'})
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
getMetaDescription({}, {context: 'home'})
.should.equal('Site description');
localSettingsCache.description = '';
should(
getMetaDescription({}, {context: 'home'})
).equal(null);
});
it('has correct fallbacks for context: post', function () {
const post = {
meta_description: 'Post meta description'
};
getMetaDescription({post}, {context: 'post'})
.should.equal('Post meta description');
post.meta_description = '';
should(
getMetaDescription({post}, {context: 'post'})
).equal(null);
});
it('has correct fallbacks for context: page', function () {
const page = {
meta_description: 'Page meta description'
};
getMetaDescription({page}, {context: 'page'})
.should.equal('Page meta description');
page.meta_description = '';
should(
getMetaDescription({page}, {context: 'page'})
).equal(null);
});
// NOTE: this is a legacy format and should be resolved with https://github.com/TryGhost/Ghost/issues/10042
it('has correct fallbacks for context: page (legacy format)', function () {
const post = {
meta_description: 'Page meta description'
};
getMetaDescription({post}, {context: 'page'})
.should.equal('Page meta description');
post.meta_description = '';
should(
getMetaDescription({post}, {context: 'page'})
).equal(null);
});
it('has correct fallbacks for context: author', function () {
const author = {
meta_description: 'Author meta description',
bio: 'Author bio'
};
getMetaDescription({author}, {context: 'author'})
.should.equal('Author meta description');
author.meta_description = '';
getMetaDescription({author}, {context: 'author'})
.should.equal('Author bio');
author.bio = '';
should(
getMetaDescription({author}, {context: 'author'})
).equal(null);
});
it('has correct fallbacks for context: author_paged', function () {
const author = {
meta_description: 'Author meta description',
bio: 'Author bio'
};
should(
getMetaDescription({author}, {context: ['author', 'paged']})
).equal(null);
});
it('has correct fallbacks for context: tag', function () {
const tag = {
meta_description: 'Tag meta description',
description: 'Tag description'
};
getMetaDescription({tag}, {context: 'tag'})
.should.equal('Tag meta description');
tag.meta_description = '';
getMetaDescription({tag}, {context: 'tag'})
.should.equal('Tag description');
tag.description = '';
should(
getMetaDescription({tag}, {context: 'tag'})
).equal(null);
});
it('has correct fallbacks for context: tag_paged', function () {
const tag = {
meta_description: 'Tag meta description',
description: 'Tag description'
};
should(
getMetaDescription({tag}, {context: ['tag', 'paged']})
).equal(null);
});
});
describe('property: og', function () {
let options;
beforeEach(function () {
options = {property: 'og'};
});
it('has correct fallbacks for context: home', function () {
getMetaDescription({}, {context: 'home'}, options)
.should.equal('Site og description');
localSettingsCache.og_description = '';
getMetaDescription({}, {context: 'home'}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
getMetaDescription({}, {context: 'home'}, options)
.should.equal('Site description');
localSettingsCache.description = '';
should(
getMetaDescription({}, {context: 'home'}, options)
).equal(null);
});
it('has correct fallbacks for context: post', function () {
const post = {
html: '<p>Post html</p>',
custom_excerpt: 'Post custom excerpt',
meta_description: 'Post meta description',
og_description: 'Post og description'
};
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Post og description');
post.og_description = '';
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Post custom excerpt');
post.custom_excerpt = '';
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Post meta description');
post.meta_description = '';
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Post html');
post.html = '';
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Site description');
});
it('has correct fallbacks for context: page', function () {
const page = {
html: '<p>Page html</p>',
custom_excerpt: 'Page custom excerpt',
meta_description: 'Page meta description',
og_description: 'Page og description'
};
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Page og description');
page.og_description = '';
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Page custom excerpt');
page.custom_excerpt = '';
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Page meta description');
page.meta_description = '';
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Page html');
page.html = '';
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Site description');
});
// NOTE: this is a legacy format and should be resolved with https://github.com/TryGhost/Ghost/issues/10042
it('has correct fallbacks for context: page (legacy format)', function () {
const post = {
html: '<p>Page html</p>',
custom_excerpt: 'Page custom excerpt',
meta_description: 'Page meta description',
og_description: 'Page og description'
};
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Page og description');
post.og_description = '';
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Page custom excerpt');
post.custom_excerpt = '';
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Page meta description');
post.meta_description = '';
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Page html');
post.html = '';
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Site description');
});
it('has correct fallbacks for context: author', function () {
const author = {
meta_description: 'Author meta description',
bio: 'Author bio'
};
getMetaDescription({author}, {context: 'author'}, options)
.should.equal('Author meta description');
author.meta_description = '';
getMetaDescription({author}, {context: 'author'}, options)
.should.equal('Author bio');
author.bio = '';
getMetaDescription({author}, {context: 'author'}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
should(
getMetaDescription({author}, {context: 'author'}, options)
).equal(null);
✨ [FEATURE] AMP⚡ (#7229) closes #6588, #7095 * `ImageObject` with image dimensions (#7152, #7151, #7153) - Returns meta data as promise - returns a new Promise from meta data - uses `Promise.props()` to resolve `getClient()` and `getMetaData()` - Adds 'image-size' util The util returns an object like this ``` { height: 50, url: 'http://myblog.com/images/cat.jpg', width: 50 }; ``` if the dimensions can be fetched and rejects with error, if not. In case we get a locally stored image or a not complete url (like `//www.gravatar.com/andsoon`), we add the protocol to the incomplete one and use `urlFor()` to get the absolute URL. If the request fails or `image-size` is not able to read the file, we reject with error. - adds 'image-size' module to dependencies - adds `getImageSizeFromUrl` function that returns image dimensions - In preparation of AMP support and to improve our schema.org JSON-LD and structured data, I made the following changes: - Changes the following properties to be `Objects`, which have a `url` property by default and a `dimensions` property, if `width` and `height` are available: - `metaData.coverImage` - `metaData.authorImage` - `metaData.blog.logo` - Checks cache by calling `getCachedImageSizeFromUrl`. If image dimensions were fetched already, returns them from cache instead of fetching them again. - If we have image dimensions on hand, the output in our JSON-LD changes from normal urls to be full `ImageObjects`. Applies to all images and logos. - Special case for `publisher.logo` as it has size restrictions: if the image doesn't fulfil the restrictions (<=600 width and <=60 height), we simply output the url instead, so like before. - Adds new property for schema.org JSON-LD: `mainEntityOfPage` as an Object. - Adds additional Open Graph data (if we have the image size): `og:image:width` and `og:image:height` - Adds/updates tests * AMP router and controller (#7171, #7157) Implements AMP in `/apps/`: - renders `amp.hbs` if route is `/:slug/amp/` - updates `setResponseContext` to set context to `['amp', 'post']` for a amp post and `['amp', 'page']` for a page, but will not render amp template for a page - updates `context_spec` - registers 'amp' as new internal app - adds the `amp.hbs` template to `core/server/apps/amp` which will be the default template for AMP posts. - adds `isAmpURL` to `post-lookup` * 🎨 Use `context` in meta as array (#7205) Instead of reading the first value of the context array, we're checking if it includes certain context values. This is a preparation change for AMP, where the context will be delivered as `['amp', 'post']`. * ✨ AMP helpers (#7174, #7216, #7215, #7223) - Adds AMP helpers `{{amp_content}}`, `{{amp_component}}` and `{{amp_ghost_head}}` to support AMP: - `{{amp_content}}`: - Adds `Amperize` as dependency - AMP app uses new helper `{{amp_content}}` to render AMP HTML - `Amperize` transforms regular HTML into AMP HTML - Adds test for `{{amp_content}}` helper - Adds 'Sanitize-HTML` as dependendy - After the HTML get 'amperized' we still might have some HTML tags, which are prohibited in AMP HTML, so we use `sanitize-html` to remove those. With every update, `Amperize` gets and it is able to transform more HTML tags, they valid AMP HTML tags (e. g. `video` and `amp-video`) and will therefore not be removed. - `{{amp_ghost_head}}`: - registers `{{amp_ghost_head}}` helper, but uses `{{ghost_head}}` code - uses `{{amp_ghost_head}}` in `amp.hbs` instead of `{{ghost_head}}` - `{{ghost_head}}`: - Render `amphtml` link in metadata for post, which links to the amp post (`getAmpUrl`) - Updates all test in metadata to support `amp` context - Changes context conditionals to work with full array instead of first array value - Adds conditionals, so no additional javascript gets rendered in `{{ghost_head}}` - Removes trailing `/amp/` in URLs, so only `amphtml` link on regular post renders it - Adds a conditional, so no code injection will be included, for an `amp` context. - `{{amp_components}}`: - AMP app uses new helper `{{amp_components}}` to render necessary script tags for AMP extended components as `amp-iframe`, `amp-anime` and `amp-form` - Adds test for `{{amp_components}}`
2016-08-22 19:49:27 +03:00
});
it('has correct fallbacks for context: author_paged', function () {
const author = {
meta_description: 'Author meta description',
bio: 'Author bio'
};
getMetaDescription({author}, {context: ['author', 'paged']}, options)
.should.equal('Author meta description');
author.meta_description = '';
getMetaDescription({author}, {context: ['author', 'paged']}, options)
.should.equal('Author bio');
author.bio = '';
getMetaDescription({author}, {context: ['author', 'paged']}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
should(
getMetaDescription({author}, {context: ['author', 'paged']}, options)
).equal(null);
});
it('has correct fallbacks for context: tag', function () {
const tag = {
meta_description: 'Tag meta description',
description: 'Tag description'
};
getMetaDescription({tag}, {context: 'tag'}, options)
.should.equal('Tag meta description');
tag.meta_description = '';
getMetaDescription({tag}, {context: 'tag'}, options)
.should.equal('Tag description');
tag.description = '';
getMetaDescription({tag}, {context: 'tag'}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
should(
getMetaDescription({tag}, {context: 'tag'}, options)
).equal(null);
});
it('has correct fallbacks for context: tag_paged', function () {
const tag = {
meta_description: 'Tag meta description',
description: 'Tag description'
};
getMetaDescription({tag}, {context: ['tag', 'paged']}, options)
.should.equal('Tag meta description');
tag.meta_description = '';
getMetaDescription({tag}, {context: ['tag', 'paged']}, options)
.should.equal('Tag description');
tag.description = '';
getMetaDescription({tag}, {context: ['tag', 'paged']}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
should(
getMetaDescription({tag}, {context: ['tag', 'paged']}, options)
).equal(null);
});
});
describe('property: twitter', function () {
let options;
beforeEach(function () {
options = {property: 'twitter'};
});
it('has correct fallbacks for context: home', function () {
getMetaDescription({}, {context: 'home'}, options)
.should.equal('Site twitter description');
localSettingsCache.twitter_description = '';
getMetaDescription({}, {context: 'home'}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
getMetaDescription({}, {context: 'home'}, options)
.should.equal('Site description');
localSettingsCache.description = '';
should(
getMetaDescription({}, {context: 'home'}, options)
).equal(null);
});
it('has correct fallbacks for context: post', function () {
const post = {
html: '<p>Post html</p>',
custom_excerpt: 'Post custom excerpt',
meta_description: 'Post meta description',
twitter_description: 'Post twitter description'
};
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Post twitter description');
post.twitter_description = '';
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Post custom excerpt');
post.custom_excerpt = '';
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Post meta description');
post.meta_description = '';
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Post html');
post.html = '';
getMetaDescription({post}, {context: 'post'}, options)
.should.equal('Site description');
});
it('has correct fallbacks for context: page', function () {
const page = {
html: '<p>Page html</p>',
custom_excerpt: 'Page custom excerpt',
meta_description: 'Page meta description',
twitter_description: 'Page twitter description'
};
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Page twitter description');
page.twitter_description = '';
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Page custom excerpt');
page.custom_excerpt = '';
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Page meta description');
page.meta_description = '';
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Page html');
page.html = '';
getMetaDescription({page}, {context: 'page'}, options)
.should.equal('Site description');
});
// NOTE: this is a legacy format and should be resolved with https://github.com/TryGhost/Ghost/issues/10042
it('has correct fallbacks for context: page (legacy format)', function () {
const post = {
html: '<p>Page html</p>',
custom_excerpt: 'Page custom excerpt',
meta_description: 'Page meta description',
twitter_description: 'Page twitter description'
};
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Page twitter description');
post.twitter_description = '';
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Page custom excerpt');
post.custom_excerpt = '';
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Page meta description');
post.meta_description = '';
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Page html');
post.html = '';
getMetaDescription({post}, {context: 'page'}, options)
.should.equal('Site description');
});
it('has correct fallbacks for context: author', function () {
const author = {
meta_description: 'Author meta description',
bio: 'Author bio'
};
getMetaDescription({author}, {context: 'author'}, options)
.should.equal('Author meta description');
author.meta_description = '';
getMetaDescription({author}, {context: 'author'}, options)
.should.equal('Author bio');
author.bio = '';
getMetaDescription({author}, {context: 'author'}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
should(
getMetaDescription({author}, {context: 'author'}, options)
).equal(null);
});
it('has correct fallbacks for context: author_paged', function () {
const author = {
meta_description: 'Author meta description',
bio: 'Author bio'
};
getMetaDescription({author}, {context: ['author', 'paged']}, options)
.should.equal('Author meta description');
author.meta_description = '';
getMetaDescription({author}, {context: ['author', 'paged']}, options)
.should.equal('Author bio');
author.bio = '';
getMetaDescription({author}, {context: ['author', 'paged']}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
should(
getMetaDescription({author}, {context: ['author', 'paged']}, options)
).equal(null);
});
it('has correct fallbacks for context: tag', function () {
const tag = {
meta_description: 'Tag meta description',
description: 'Tag description'
};
getMetaDescription({tag}, {context: 'tag'}, options)
.should.equal('Tag meta description');
tag.meta_description = '';
getMetaDescription({tag}, {context: 'tag'}, options)
.should.equal('Tag description');
tag.description = '';
getMetaDescription({tag}, {context: 'tag'}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
should(
getMetaDescription({tag}, {context: 'tag'}, options)
).equal(null);
});
it('has correct fallbacks for context: tag_paged', function () {
const tag = {
meta_description: 'Tag meta description',
description: 'Tag description'
};
getMetaDescription({tag}, {context: ['tag', 'paged']}, options)
.should.equal('Tag meta description');
tag.meta_description = '';
getMetaDescription({tag}, {context: ['tag', 'paged']}, options)
.should.equal('Tag description');
tag.description = '';
getMetaDescription({tag}, {context: ['tag', 'paged']}, options)
.should.equal('Site meta description');
localSettingsCache.meta_description = '';
should(
getMetaDescription({tag}, {context: ['tag', 'paged']}, options)
).equal(null);
});
});
it('v3: should return data page meta description if on root context contains page', function () {
const description = getMetaDescription({
page: {
meta_description: 'Best page ever!'
}
}, {
context: ['page']
});
description.should.equal('Best page ever!');
});
});