Ghost/core/test/unit/metadata/schema_spec.js
Aileen Nowak a5c29dfc34 [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 18:49:27 +02:00

470 lines
16 KiB
JavaScript

var getSchema = require('../../../server/data/meta/schema'),
should = require('should');
describe('getSchema', function () {
it('should return post schema if context starts with post', function (done) {
var metadata = {
blog: {
title: 'Blog Title',
url: 'http://mysite.com',
logo: {
url: 'http://mysite.com/author/image/url/logo.jpg',
dimensions: {
width: 500,
height: 500
}
}
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg',
dimensions: {
width: 500,
height: 500
}
},
authorFacebook: 'testuser',
creatorTwitter: '@testuser',
authorUrl: 'http://mysite.com/author/me/',
metaTitle: 'Post Title',
url: 'http://mysite.com/post/my-post-slug/',
publishedDate: '2015-12-25T05:35:01.234Z',
modifiedDate: '2016-01-21T22:13:05.412Z',
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg',
dimensions: {
width: 500,
height: 500
}
},
keywords: ['one', 'two', 'tag'],
metaDescription: 'Post meta description'
}, data = {
context: ['post'],
post: {
author: {
name: 'Post Author',
website: 'http://myblogsite.com/',
bio: 'My author bio.',
facebook: 'testuser',
twitter: '@testuser'
}
}
}, schema = getSchema(metadata, data);
should.deepEqual(schema, {
'@context': 'https://schema.org',
'@type': 'Article',
author: {
'@type': 'Person',
description: 'My author bio.',
image: {
'@type': 'ImageObject',
url: 'http://mysite.com/author/image/url/me.jpg',
width: 500,
height: 500
},
name: 'Post Author',
sameAs: [
'http://myblogsite.com/',
'https://www.facebook.com/testuser',
'https://twitter.com/testuser'
],
url: 'http://mysite.com/author/me/'
},
dateModified: '2016-01-21T22:13:05.412Z',
datePublished: '2015-12-25T05:35:01.234Z',
description: 'Post meta description',
headline: 'Post Title',
image: {
'@type': 'ImageObject',
url: 'http://mysite.com/content/image/mypostcoverimage.jpg',
width: 500,
height: 500
},
keywords: 'one, two, tag',
mainEntityOfPage: {
'@type': 'WebPage',
'@id': 'http://mysite.com'
},
publisher: {
'@type': 'Organization',
name: 'Blog Title',
logo: {
'@type': 'ImageObject',
url: 'http://mysite.com/author/image/url/logo.jpg',
width: 500,
height: 500
}
},
url: 'http://mysite.com/post/my-post-slug/'
});
done();
});
it('should return post schema if context starts with amp', function (done) {
var metadata = {
blog: {
title: 'Blog Title',
url: 'http://mysite.com',
logo: {
url: 'http://mysite.com/author/image/url/logo.jpg',
dimensions: {
width: 500,
height: 500
}
}
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg',
dimensions: {
width: 500,
height: 500
}
},
authorFacebook: 'testuser',
creatorTwitter: '@testuser',
authorUrl: 'http://mysite.com/author/me/',
metaTitle: 'Post Title',
url: 'http://mysite.com/post/my-amp-post-slug/',
publishedDate: '2015-12-25T05:35:01.234Z',
modifiedDate: '2016-01-21T22:13:05.412Z',
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg',
dimensions: {
width: 500,
height: 500
}
},
keywords: ['one', 'two', 'tag'],
metaDescription: 'Post meta description'
}, data = {
context: ['amp', 'post'],
post: {
title: 'Post Title',
slug: 'my-amp-post-slug',
markdown: 'some markdown',
html: 'some html',
author: {
name: 'Post Author',
website: 'http://myblogsite.com/',
bio: 'My author bio.',
facebook: 'testuser',
twitter: '@testuser'
}
}
}, schema = getSchema(metadata, data);
should.deepEqual(schema, {
'@context': 'https://schema.org',
'@type': 'Article',
author: {
'@type': 'Person',
description: 'My author bio.',
image: {
'@type': 'ImageObject',
url: 'http://mysite.com/author/image/url/me.jpg',
width: 500,
height: 500
},
name: 'Post Author',
sameAs: [
'http://myblogsite.com/',
'https://www.facebook.com/testuser',
'https://twitter.com/testuser'
],
url: 'http://mysite.com/author/me/'
},
dateModified: '2016-01-21T22:13:05.412Z',
datePublished: '2015-12-25T05:35:01.234Z',
description: 'Post meta description',
headline: 'Post Title',
image: {
'@type': 'ImageObject',
url: 'http://mysite.com/content/image/mypostcoverimage.jpg',
width: 500,
height: 500
},
keywords: 'one, two, tag',
mainEntityOfPage: {
'@type': 'WebPage',
'@id': 'http://mysite.com'
},
publisher: {
'@type': 'Organization',
name: 'Blog Title',
logo: {
'@type': 'ImageObject',
url: 'http://mysite.com/author/image/url/logo.jpg',
width: 500,
height: 500
}
},
url: 'http://mysite.com/post/my-amp-post-slug/'
});
done();
});
it('should return post schema removing null or undefined values', function (done) {
var metadata = {
blog: {
title: 'Blog Title'
},
authorImage: null,
authorFacebook: undefined,
creatorTwitter: undefined,
authorUrl: 'http://mysite.com/author/me/',
metaTitle: 'Post Title',
url: 'http://mysite.com/post/my-post-slug/',
publishedDate: '2015-12-25T05:35:01.234Z',
modifiedDate: '2016-01-21T22:13:05.412Z',
coverImage: undefined,
keywords: [],
metaDescription: 'Post meta description'
}, data = {
context: ['post'],
post: {
author: {
name: 'Post Author',
website: undefined,
bio: null,
facebook: null,
twitter: null
}
}
}, schema = getSchema(metadata, data);
should.deepEqual(schema, {
'@context': 'https://schema.org',
'@type': 'Article',
author: {
'@type': 'Person',
name: 'Post Author',
sameAs: [],
url: 'http://mysite.com/author/me/'
},
dateModified: '2016-01-21T22:13:05.412Z',
datePublished: '2015-12-25T05:35:01.234Z',
description: 'Post meta description',
headline: 'Post Title',
mainEntityOfPage: {
'@type': 'WebPage',
'@id': null
},
publisher: {
'@type': 'Organization',
name: 'Blog Title',
logo: null
},
url: 'http://mysite.com/post/my-post-slug/'
});
done();
});
it('should return image url instead of ImageObjects if no dimensions supplied', function (done) {
var metadata = {
blog: {
title: 'Blog Title',
url: 'http://mysite.com',
logo: {
url: 'http://mysite.com/author/image/url/logo.jpg'
}
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg'
},
authorFacebook: 'testuser',
creatorTwitter: '@testuser',
authorUrl: 'http://mysite.com/author/me/',
metaTitle: 'Post Title',
url: 'http://mysite.com/post/my-post-slug/',
publishedDate: '2015-12-25T05:35:01.234Z',
modifiedDate: '2016-01-21T22:13:05.412Z',
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg'
},
keywords: ['one', 'two', 'tag'],
metaDescription: 'Post meta description'
}, data = {
context: ['post'],
post: {
author: {
name: 'Post Author',
website: 'http://myblogsite.com/',
bio: 'My author bio.',
facebook: 'testuser',
twitter: '@testuser'
}
}
}, schema = getSchema(metadata, data);
should.deepEqual(schema, {
'@context': 'https://schema.org',
'@type': 'Article',
author: {
'@type': 'Person',
description: 'My author bio.',
image: 'http://mysite.com/author/image/url/me.jpg',
name: 'Post Author',
sameAs: [
'http://myblogsite.com/',
'https://www.facebook.com/testuser',
'https://twitter.com/testuser'
],
url: 'http://mysite.com/author/me/'
},
dateModified: '2016-01-21T22:13:05.412Z',
datePublished: '2015-12-25T05:35:01.234Z',
description: 'Post meta description',
headline: 'Post Title',
image: 'http://mysite.com/content/image/mypostcoverimage.jpg',
keywords: 'one, two, tag',
mainEntityOfPage: {
'@type': 'WebPage',
'@id': 'http://mysite.com'
},
publisher: {
'@type': 'Organization',
name: 'Blog Title',
logo: 'http://mysite.com/author/image/url/logo.jpg'
},
url: 'http://mysite.com/post/my-post-slug/'
});
done();
});
it('should return home schema if context starts with home', function () {
var metadata = {
blog: {
title: 'Blog Title'
},
url: 'http://mysite.com/post/my-post-slug/',
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg',
dimensions: {
width: 500,
height: 500
}
},
metaDescription: 'This is the theme description'
}, data = {
context: ['home']
}, schema = getSchema(metadata, data);
should.deepEqual(schema, {
'@context': 'https://schema.org',
'@type': 'Website',
description: 'This is the theme description',
image: {
'@type': 'ImageObject',
url: 'http://mysite.com/content/image/mypostcoverimage.jpg',
width: 500,
height: 500
},
mainEntityOfPage: {
'@type': 'WebPage',
'@id': null
},
publisher: {
'@type': 'Organization',
name: 'Blog Title',
logo: null
},
url: 'http://mysite.com/post/my-post-slug/'
});
});
it('should return tag schema if context starts with tag', function () {
var metadata = {
blog: {
title: 'Blog Title'
},
url: 'http://mysite.com/post/my-post-slug/',
coverImage: {
url: 'http://mysite.com/content/image/mypostcoverimage.jpg',
dimensions: {
width: 500,
height: 500
}
},
metaDescription: 'This is the tag description!'
}, data = {
context: ['tag'],
tag: {
name: 'Great Tag'
}
}, schema = getSchema(metadata, data);
should.deepEqual(schema, {
'@context': 'https://schema.org',
'@type': 'Series',
description: 'This is the tag description!',
image: {
'@type': 'ImageObject',
url: 'http://mysite.com/content/image/mypostcoverimage.jpg',
width: 500,
height: 500
},
mainEntityOfPage: {
'@type': 'WebPage',
'@id': null
},
name: 'Great Tag',
publisher: {
'@type': 'Organization',
name: 'Blog Title',
logo: null
},
url: 'http://mysite.com/post/my-post-slug/'
});
});
it('should return author schema if context starts with author', function () {
var metadata = {
blog: {
title: 'Blog Title',
url: 'http://mysite.com'
},
authorImage: {
url: 'http://mysite.com/author/image/url/me.jpg',
dimensions: {
width: 500,
height: 500
}
},
authorUrl: 'http://mysite.com/author/me/',
metaDescription: 'This is the author description!'
}, data = {
context: ['author'],
author: {
name: 'Author Name',
website: 'http://myblogsite.com/',
twitter: '@testuser'
}
}, schema = getSchema(metadata, data);
should.deepEqual(schema, {
'@context': 'https://schema.org',
'@type': 'Person',
description: 'This is the author description!',
mainEntityOfPage: {
'@type': 'WebPage',
'@id': 'http://mysite.com'
},
name: 'Author Name',
sameAs: [
'http://myblogsite.com/',
'https://twitter.com/testuser'
],
url: 'http://mysite.com/author/me/'
});
});
it('should return null if not a supported type', function () {
var metadata = {},
data = {},
schema = getSchema(metadata, data);
should.deepEqual(schema, null);
});
});