0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -05:00

Structured Data 3.0

closes #6534
- new input fields in general settings incl. validation
- facebook and twitter as new models in settings.js
- adds values for facebook and twitter to default-settings.js
- adds blog helpers for facebook and twittter
- rather than saving the whole URL, the Twitter username incl. '@' will be extracted from URL and saved in the settings. The User will still input the full URL. After saving the blog setting, the stored Twitter username will be parsed again as the full URL and available in the input field. A custom transform is used for this.
- adding meta fields to be rendered in {{ghost_head}}:
	- '<meta property="article:publisher" content="https://www.facebook.com/page" />' and
	- '<meta name="twitter:site" content="@user"/>'
- adds facebook and twitter to unit test for structured data
- adds unit test for general settings
- adds acceptance test for new input fields in general settings
- adds a custom transform for twitter model to save only the username to the server
- adds unit test for transform
This commit is contained in:
Aileen Nowak 2016-03-03 10:52:27 +02:00
parent 183e53371f
commit c8d0e25923
13 changed files with 490 additions and 19 deletions

View file

@ -6,7 +6,8 @@ const {
Controller,
computed,
inject: {service},
observer
observer,
run
} = Ember;
export default Controller.extend(SettingsSaveMixin, {
@ -16,6 +17,8 @@ export default Controller.extend(SettingsSaveMixin, {
notifications: service(),
config: service(),
_scratchFacebook: null,
_scratchTwitter: null,
selectedTheme: computed('model.activeTheme', 'themes', function () {
let activeTheme = this.get('model.activeTheme');
@ -88,6 +91,7 @@ export default Controller.extend(SettingsSaveMixin, {
if (error) {
notifications.showAPIError(error, {key: 'settings.save'});
}
throw error;
});
},
@ -110,6 +114,106 @@ export default Controller.extend(SettingsSaveMixin, {
toggleUploadLogoModal() {
this.toggleProperty('showUploadLogoModal');
},
validateFacebookUrl() {
let newUrl = this.get('_scratchFacebook');
let oldUrl = this.get('model.facebook');
let errMessage = '';
if (!newUrl) {
// Clear out the Facebook url
this.set('model.facebook', '');
this.get('model.errors').remove('facebook');
return;
}
// If new url didn't change, exit
if (newUrl === oldUrl) {
return;
}
if (!newUrl.match(/(^https:\/\/www\.facebook\.com\/)(\S+)/g)) {
if (newUrl.match(/(?:facebook\.com\/)(\S+)/) || (!validator.isURL(newUrl) && newUrl.match(/([a-zA-Z0-9\.]+)/))) {
let [ , username] = newUrl.match(/(?:facebook\.com\/)(\S+)/) || newUrl.match(/([a-zA-Z0-9\.]+)/);
newUrl = `https://www.facebook.com/${username}`;
this.set('model.facebook', newUrl);
this.get('model.errors').remove('facebook');
this.get('model.hasValidated').pushObject('facebook');
// User input is validated
return this.save().then(() => {
this.set('model.facebook', '');
run.schedule('afterRender', this, function () {
this.set('model.facebook', newUrl);
});
});
} else if (validator.isURL(newUrl)) {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourPage';
this.get('model.errors').add('facebook', errMessage);
this.get('model.hasValidated').pushObject('facebook');
return;
} else {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourPage';
this.get('model.errors').add('facebook', errMessage);
this.get('model.hasValidated').pushObject('facebook');
return;
}
}
},
validateTwitterUrl() {
let newUrl = this.get('_scratchTwitter');
let oldUrl = this.get('model.twitter');
let errMessage = '';
if (!newUrl) {
// Clear out the Facebook url
this.set('model.twitter', '');
this.get('model.errors').remove('twitter');
return;
}
// If new url didn't change, exit
if (newUrl === oldUrl) {
return;
}
if (!newUrl.match(/(^https:\/\/twitter\.com\/)(\S+)/g)) {
if (newUrl.match(/(?:twitter\.com\/)(\S+)/) || (!validator.isURL(newUrl) && newUrl.match(/([a-zA-Z0-9\.]+)/))) {
let [ , username] = newUrl.match(/(?:twitter\.com\/)(\S+)/) || newUrl.match(/([a-zA-Z0-9\.]+)/);
newUrl = `https://twitter.com/${username}`;
this.set('model.twitter', newUrl);
this.get('model.errors').remove('twitter');
this.get('model.hasValidated').pushObject('twitter');
// User input is validated
return this.save().then(() => {
this.set('model.twitter', '');
run.schedule('afterRender', this, function () {
this.set('model.twitter', newUrl);
});
});
} else if (validator.isURL(newUrl)) {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('model.errors').add('twitter', errMessage);
this.get('model.hasValidated').pushObject('twitter');
return;
} else {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('model.errors').add('twitter', errMessage);
this.get('model.hasValidated').pushObject('twitter');
return;
}
}
}
}
});

View file

@ -1,12 +1,14 @@
import Ember from 'ember';
import isNumber from 'ghost/utils/isNumber';
import boundOneWay from 'ghost/utils/bound-one-way';
import { invoke } from 'ember-invoke-action';
const {
Controller,
RSVP,
computed,
inject: {service},
run,
isArray
} = Ember;
const {alias, and, not, or, readOnly} = computed;
@ -18,6 +20,8 @@ export default Controller.extend({
showTransferOwnerModal: false,
showUploadCoverModal: false,
showUplaodImageModal: false,
_scratchFacebook: null,
_scratchTwitter: null,
ajax: service(),
dropdown: service(),
@ -148,6 +152,7 @@ export default Controller.extend({
});
this.set('lastPromise', promise);
return promise;
},
deleteUser() {
@ -239,6 +244,110 @@ export default Controller.extend({
this.set('lastPromise', promise);
},
validateFacebookUrl() {
let newUrl = this.get('_scratchFacebook');
let oldUrl = this.get('user.facebook');
let errMessage = '';
if (!newUrl) {
// Clear out the Facebook url
this.set('user.facebook', '');
this.get('user.errors').remove('facebook');
return;
}
// If new url didn't change, exit
if (newUrl === oldUrl) {
return;
}
// TODO: put the validation here into a validator
if (!newUrl.match(/(^https:\/\/www\.facebook\.com\/)(\S+)/g)) {
if (newUrl.match(/(?:facebook\.com\/)(\S+)/) || (!validator.isURL(newUrl) && newUrl.match(/([a-zA-Z0-9\.]+)/))) {
let [ , username] = newUrl.match(/(?:facebook\.com\/)(\S+)/) || newUrl.match(/([a-zA-Z0-9\.]+)/);
newUrl = `https://www.facebook.com/${username}`;
this.set('user.facebook', newUrl);
this.get('user.errors').remove('facebook');
this.get('user.hasValidated').pushObject('facebook');
// User input is validated
invoke(this, 'save').then(() => {
// necessary to update the value in the input field
this.set('user.facebook', '');
run.schedule('afterRender', this, function () {
this.set('user.facebook', newUrl);
});
});
} else if (validator.isURL(newUrl)) {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourUsername';
this.get('user.errors').add('facebook', errMessage);
this.get('user.hasValidated').pushObject('facebook');
return;
} else {
errMessage = 'The URL must be in a format like ' +
'https://www.facebook.com/yourUsername';
this.get('user.errors').add('facebook', errMessage);
this.get('user.hasValidated').pushObject('facebook');
return;
}
}
},
validateTwitterUrl() {
let newUrl = this.get('_scratchTwitter');
let oldUrl = this.get('user.twitter');
let errMessage = '';
if (!newUrl) {
// Clear out the Twitter url
this.set('user.twitter', '');
this.get('user.errors').remove('twitter');
return;
}
// If new url didn't change, exit
if (newUrl === oldUrl) {
return;
}
// TODO: put the validation here into a validator
if (!newUrl.match(/(^https:\/\/twitter\.com\/)(\S+)/g)) {
if (newUrl.match(/(?:twitter\.com\/)(\S+)/) || (!validator.isURL(newUrl) && newUrl.match(/([a-zA-Z0-9\.]+)/))) {
let [ , username] = newUrl.match(/(?:twitter\.com\/)(\S+)/) || newUrl.match(/([a-zA-Z0-9\.]+)/);
newUrl = `https://twitter.com/${username}`;
this.set('user.twitter', newUrl);
this.get('user.errors').remove('twitter');
this.get('user.hasValidated').pushObject('twitter');
// User input is validated
invoke(this, 'save').then(() => {
// necessary to update the value in the input field
this.set('user.twitter', '');
run.schedule('afterRender', this, function () {
this.set('user.twitter', newUrl);
});
});
} else if (validator.isURL(newUrl)) {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('user.errors').add('twitter', errMessage);
this.get('user.hasValidated').pushObject('twitter');
return;
} else {
errMessage = 'The URL must be in a format like ' +
'https://twitter.com/yourUsername';
this.get('user.errors').add('twitter', errMessage);
this.get('user.hasValidated').pushObject('twitter');
return;
}
}
},
transferOwnership() {
let user = this.get('user');
let url = this.get('ghostPaths.url').api('users', 'owner');

View file

@ -347,4 +347,14 @@ export function testConfig() {
users: [db.users.find(request.params.id)]
};
});
this.put('/users/:id/', function (db, request) {
let {id} = request.params;
let [attrs] = JSON.parse(request.requestBody).users;
let record = db.users.update(id, attrs);
return {
user: record
};
});
}

View file

@ -179,6 +179,28 @@ export default [
uuid: 'dd4ebaa8-dedb-40ff-a663-ec64a92d4111',
value: '[{"url":""}]'
},
{
created_at: '2016-05-05T15:40:12.133Z',
created_by: 1,
id: 23,
key: 'facebook',
type: 'blog',
updated_at: '2016-05-08T15:20:25.953Z',
updated_by: 1,
uuid: 'd4387e5c-3230-46dd-a89b-0d8a40365c35',
value: ''
},
{
created_at: '2016-05-05T15:40:12.134Z',
created_by: 1,
id: 24,
key: 'twitter',
type: 'blog',
updated_at: '2016-05-08T15:20:25.954Z',
updated_by: 1,
uuid: '5130441f-e4c7-4750-9692-a22d841ab049',
value: ''
},
{
key: 'availableThemes',
value: [

View file

@ -18,6 +18,8 @@ export default Model.extend(ValidationEngine, {
availableThemes: attr(),
ghost_head: attr('string'),
ghost_foot: attr('string'),
facebook: attr('string'),
twitter: attr('twitter-url-user'),
labs: attr('string'),
navigation: attr('navigation-settings'),
isPrivate: attr('boolean'),

View file

@ -38,6 +38,8 @@ export default Model.extend(ValidationEngine, {
async: false
}),
count: attr('raw'),
facebook: attr('string'),
twitter: attr('string'),
ghostPaths: service(),
ajax: service(),

View file

@ -15,9 +15,7 @@ export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, {
},
model() {
return this.store.query('setting', {type: 'blog,theme,private'}).then((records) => {
return records.get('firstObject');
});
return this.store.queryRecord('setting', {type: 'blog,theme,private'});
},
actions: {

View file

@ -35,7 +35,7 @@
{{else}}
<button type="button" class="btn btn-green js-modal-logo" {{action "toggleUploadLogoModal"}}>Upload Image</button>
{{/if}}
<p>Display a sexy logo for your publication</p>
<p>Display a logo for your publication</p>
{{#if showUploadLogoModal}}
{{gh-fullscreen-modal "upload-image"
@ -96,6 +96,23 @@
<p>Select a theme for your blog</p>
</div>
<div class="form-group">
{{#gh-form-group errors=model.errors hasValidated=model.hasValidated property="facebook"}}
<label for="facebook">Facebook Page</label>
<input value={{model.facebook}} oninput={{action (mut _scratchFacebook) value="target.value"}} {{action "validateFacebookUrl" on="focusOut"}} type="url" class="gh-input" id="facebook" name="general[facebook]" placeholder="https://www.facebook.com/ghost" autocorrect="off" />
{{gh-error-message errors=model.errors property="facebook"}}
<p>URL of your blog's Facebook Page</p>
{{/gh-form-group}}
</div>
<div class="form-group">
{{#gh-form-group errors=model.errors hasValidated=model.hasValidated property="twitter"}}
<label for="twitter">Twitter Profile</label>
<input value={{model.twitter}} oninput={{action (mut _scratchTwitter) value="target.value"}} {{action "validateTwitterUrl" on="focusOut"}} type="url" class="gh-input" id="facebook" name="general[twitter]" placeholder="https://twitter.com/tryghost" autocorrect="off" />
{{gh-error-message errors=model.errors property="twitter"}}
<p>URL of your blog's Twitter profile</p>
{{/gh-form-group}}
</div>
<div class="form-group for-checkbox">
<label for="isPrivate">Make this blog private</label>
<label class="checkbox" for="isPrivate">
@ -114,6 +131,7 @@
{{/gh-form-group}}
{{/if}}
</fieldset>
</form>
</section>
</section>

View file

@ -140,6 +140,20 @@
<p>Have a website or blog other than this one? Link it!</p>
{{/gh-form-group}}
{{#gh-form-group errors=user.errors hasValidated=user.hasValidated property="facebook"}}
<label for="user-facebook">Facebook Profile</label>
<input value={{user.facebook}} oninput={{action (mut _scratchFacebook) value="target.value"}} {{action "validateFacebookUrl" on="focusOut"}} type="url" class="gh-input" id="user-facebook" name="user[facebook]" placeholder="https://www.facebook.com/username" autocorrect="off" />
{{gh-error-message errors=user.errors property="facebook"}}
<p>URL of your personal Facebook Profile</p>
{{/gh-form-group}}
{{#gh-form-group errors=user.errors hasValidated=user.hasValidated property="twitter"}}
<label for="user-twitter">Twitter Profile</label>
<input value={{user.twitter}} oninput={{action (mut _scratchTwitter) value="target.value"}} {{action "validateTwitterUrl" on="focusOut"}} type="url" class="gh-input" id="user-twitter" name="user[twitter]" placeholder="https://twitter.com/username" autocorrect="off" />
{{gh-error-message errors=user.errors property="twitter"}}
<p>URL of your personal Twitter profile</p>
{{/gh-form-group}}
{{#gh-form-group errors=user.errors hasValidated=user.hasValidated property="bio" class="bio-container"}}
<label for="user-bio">Bio</label>
{{textarea id="user-bio" class="gh-input" value=user.bio focusOut=(action "validate" "bio" target=user)}}

View file

@ -0,0 +1,21 @@
import Transform from 'ember-data/transform';
export default Transform.extend({
deserialize(serialized) {
if (serialized) {
let [ , user ] = serialized.match(/@?([^\/]*)/);
return `https://twitter.com/${user}`;
}
return serialized;
},
serialize(deserialized) {
if (deserialized) {
let [ , user] = deserialized.match(/(?:https:\/\/)(?:twitter\.com)\/(?:#!\/)?@?([^\/]*)/);
return `@${user}`;
}
return deserialized;
}
});

View file

@ -119,25 +119,15 @@ describe('Acceptance: Settings - General', function () {
andThen(() => {
expect(find('.fullscreen-modal').length).to.equal(0);
});
});
it('renders theme selector correctly', function () {
visit('/settings/general');
// renders theme selector correctly
andThen(() => {
expect(currentURL(), 'currentURL').to.equal('/settings/general');
expect(find('#activeTheme select option').length, 'available themes').to.equal(1);
expect(find('#activeTheme select option').text().trim()).to.equal('Blog - 1.0');
});
});
it('handles private blog settings correctly', function () {
visit('/settings/general');
// handles private blog settings correctly
andThen(() => {
expect(currentURL(), 'currentURL').to.equal('/settings/general');
expect(find('input#isPrivate').prop('checked'), 'isPrivate checkbox').to.be.false;
});
@ -150,12 +140,91 @@ describe('Acceptance: Settings - General', function () {
});
fillIn('#settings-general input[name="general[password]"]', '');
click('.view-header .view-actions .btn-blue');
triggerEvent('#settings-general input[name="general[password]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('Password must be supplied');
});
fillIn('#settings-general input[name="general[password]"]', 'asdfg');
triggerEvent('#settings-general input[name="general[password]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
// validates a facebook url correctly
fillIn('#settings-general input[name="general[facebook]"]', 'facebook.com/username');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/username');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[facebook]"]', '*(&*(%%))');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('The URL must be in a format like https://www.facebook.com/yourPage');
});
fillIn('#settings-general input[name="general[facebook]"]', 'http://github.com/username');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('The URL must be in a format like https://www.facebook.com/yourPage');
});
fillIn('#settings-general input[name="general[facebook]"]', 'testuser');
triggerEvent('#settings-general input[name="general[facebook]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[facebook]"]').val()).to.be.equal('https://www.facebook.com/testuser');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
// validates a twitter url correctly
fillIn('#settings-general input[name="general[twitter]"]', 'twitter.com/username');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[twitter]"]').val()).to.be.equal('https://twitter.com/username');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
fillIn('#settings-general input[name="general[twitter]"]', '*(&*(%%))');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('The URL must be in a format like https://twitter.com/yourUsername');
});
fillIn('#settings-general input[name="general[twitter]"]', 'http://github.com/username');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('The URL must be in a format like https://twitter.com/yourUsername');
});
fillIn('#settings-general input[name="general[twitter]"]', 'testuser');
triggerEvent('#settings-general input[name="general[twitter]"]', 'blur');
andThen(() => {
expect(find('#settings-general input[name="general[twitter]"]').val()).to.be.equal('https://twitter.com/testuser');
expect(find('#settings-general .error .response').text().trim(), 'inline validation response')
.to.equal('');
});
});
});
});

View file

@ -240,6 +240,8 @@ describe('Acceptance: Team', function () {
beforeEach(function () {
server.create('user', {slug: 'test-1', name: 'Test User'});
server.loadFixtures();
});
it('input fields reset and validate correctly', function () {
@ -311,12 +313,80 @@ describe('Acceptance: Team', function () {
expect(find('.user-details-bottom .form-group:nth-of-type(4)').hasClass('error'), 'website input should be in error state').to.be.true;
});
fillIn('#user-facebook', '');
fillIn('#user-facebook', ')(*&%^%)');
triggerEvent('#user-facebook', 'blur');
andThen(() => {
expect(find('.user-details-bottom .form-group:nth-of-type(5)').hasClass('error'), 'facebook input should be in error state').to.be.true;
});
fillIn('#user-facebook', '');
fillIn('#user-facebook', 'name');
triggerEvent('#user-facebook', 'blur');
andThen(() => {
expect(find('#user-facebook').val()).to.be.equal('https://www.facebook.com/name');
expect(find('.user-details-bottom .form-group:nth-of-type(5)').hasClass('error'), 'facebook input should be in error state').to.be.false;
});
fillIn('#user-facebook', '');
fillIn('#user-facebook', 'http://twitter.com/user');
triggerEvent('#user-facebook', 'blur');
andThen(() => {
expect(find('.user-details-bottom .form-group:nth-of-type(5)').hasClass('error'), 'facebook input should be in error state').to.be.true;
});
fillIn('#user-facebook', '');
fillIn('#user-facebook', 'facebook.com/user');
triggerEvent('#user-facebook', 'blur');
andThen(() => {
expect(find('#user-facebook').val()).to.be.equal('https://www.facebook.com/user');
expect(find('.user-details-bottom .form-group:nth-of-type(5)').hasClass('error'), 'facebook input should be in error state').to.be.false;
});
fillIn('#user-twitter', '');
fillIn('#user-twitter', ')(*&%^%)');
triggerEvent('#user-twitter', 'blur');
andThen(() => {
expect(find('.user-details-bottom .form-group:nth-of-type(6)').hasClass('error'), 'twitter input should be in error state').to.be.true;
});
fillIn('#user-twitter', '');
fillIn('#user-twitter', 'name');
triggerEvent('#user-twitter', 'blur');
andThen(() => {
expect(find('#user-twitter').val()).to.be.equal('https://twitter.com/name');
expect(find('.user-details-bottom .form-group:nth-of-type(6)').hasClass('error'), 'twitter input should be in error state').to.be.false;
});
fillIn('#user-twitter', '');
fillIn('#user-twitter', 'http://github.com/user');
triggerEvent('#user-twitter', 'blur');
andThen(() => {
expect(find('.user-details-bottom .form-group:nth-of-type(6)').hasClass('error'), 'twitter input should be in error state').to.be.true;
});
fillIn('#user-twitter', '');
fillIn('#user-twitter', 'twitter.com/user');
triggerEvent('#user-twitter', 'blur');
andThen(() => {
expect(find('#user-twitter').val()).to.be.equal('https://twitter.com/user');
expect(find('.user-details-bottom .form-group:nth-of-type(6)').hasClass('error'), 'twitter input should be in error state').to.be.false;
});
fillIn('#user-website', '');
fillIn('#user-bio', new Array(210).join('a'));
triggerEvent('#user-bio', 'blur');
andThen(() => {
expect(find('.user-details-bottom .form-group:nth-of-type(5)').hasClass('error'), 'bio input should be in error state').to.be.true;
expect(find('.user-details-bottom .form-group:nth-of-type(7)').hasClass('error'), 'bio input should be in error state').to.be.true;
});
});
});

View file

@ -0,0 +1,32 @@
/* jshint expr:true */
import { expect } from 'chai';
import { describeModule, it } from 'ember-mocha';
import Ember from 'ember';
const emberA = Ember.A;
describeModule(
'transform:twitter-url-user',
'Unit: Transform: twitter-url-user',
{
// Specify the other units that are required for this test.
// needs: ['transform:foo']
},
function() {
it('deserializes twitter url', function () {
let transform = this.subject();
let serialized = '@testuser';
let result = transform.deserialize(serialized);
expect(result).to.equal('https://twitter.com/testuser');
});
it('serializes url to twitter username', function () {
let transform = this.subject();
let deserialized = 'https://twitter.com/testuser';
let result = transform.serialize(deserialized);
expect(result).to.equal('@testuser');
});
}
);