From 0a0aaf01b25403ea95191ea8fffddb7e5d9f33f6 Mon Sep 17 00:00:00 2001 From: vdemedes Date: Tue, 27 Oct 2015 12:15:09 +0100 Subject: [PATCH] Replace missing title with "(Untitled)" when creating a post closes #6014 - replace missing title with "(Untitled)" when creating a post - add a test for creating post without title --- core/server/models/post.js | 4 +++- core/test/integration/model/model_posts_spec.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/server/models/post.js b/core/server/models/post.js index 8869ccff84..7e40561b8d 100644 --- a/core/server/models/post.js +++ b/core/server/models/post.js @@ -115,6 +115,7 @@ Post = ghostBookshelf.Model.extend({ saving: function saving(model, attr, options) { var self = this, tagsToCheck, + title, i; options = options || {}; @@ -138,7 +139,8 @@ Post = ghostBookshelf.Model.extend({ // disabling sanitization until we can implement a better version // this.set('title', this.sanitize('title').trim()); - this.set('title', this.get('title').trim()); + title = this.get('title') || '(Untitled)'; + this.set('title', title.trim()); // ### Business logic for published_at and published_by // If the current status is 'published' and published_at is not set, set it to now diff --git a/core/test/integration/model/model_posts_spec.js b/core/test/integration/model/model_posts_spec.js index d595f2c9d5..980761ee4d 100644 --- a/core/test/integration/model/model_posts_spec.js +++ b/core/test/integration/model/model_posts_spec.js @@ -653,6 +653,17 @@ describe('Post Model', function () { }).catch(done); }); + it('can add default title, if it\'s missing', function (done) { + PostModel.add({ + markdown: 'Content' + }, context).then(function (newPost) { + should.exist(newPost); + newPost.get('title').should.equal('(Untitled)'); + + done(); + }).catch(done); + }); + it('can trim title', function (done) { var untrimmedCreateTitle = ' test trimmed create title ', untrimmedUpdateTitle = ' test trimmed update title ',