mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
Maintain tag order for posts
refs #5727, #5602 - Add new 'order' column to posts_tags table - Migrate all existing posts_tags to have a correct value for 'order' - Rewrite updateTags to not remove all tags, and to correctly maintain order - Add transaction support for tag operations - Many tests
This commit is contained in:
parent
f22796ff7d
commit
a3f107da8e
6 changed files with 647 additions and 335 deletions
|
@ -245,7 +245,7 @@ to004 = function to004() {
|
||||||
});
|
});
|
||||||
ops.push(upgradeOp);
|
ops.push(upgradeOp);
|
||||||
|
|
||||||
// add ghost-frontend client if missing
|
// clean up broken tags
|
||||||
upgradeOp = models.Tag.findAll(options).then(function (tags) {
|
upgradeOp = models.Tag.findAll(options).then(function (tags) {
|
||||||
var tagOps = [];
|
var tagOps = [];
|
||||||
if (tags) {
|
if (tags) {
|
||||||
|
@ -267,7 +267,27 @@ to004 = function to004() {
|
||||||
}
|
}
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
});
|
});
|
||||||
|
ops.push(upgradeOp);
|
||||||
|
|
||||||
|
// Add post_tag order
|
||||||
|
upgradeOp = models.Post.findAll(_.extend({}, options, {withRelated: ['tags']})).then(function (posts) {
|
||||||
|
var tagOps = [];
|
||||||
|
if (posts) {
|
||||||
|
posts.each(function (post) {
|
||||||
|
var order = 0;
|
||||||
|
post.related('tags').each(function (tag) {
|
||||||
|
tagOps.push(post.tags().updatePivot(
|
||||||
|
{sort_order: order}, _.extend({}, options, {query: {where: {tag_id: tag.id}}})
|
||||||
|
));
|
||||||
|
order += 1;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (tagOps.length > 0) {
|
||||||
|
logInfo('Updating order on ' + tagOps.length + ' tags');
|
||||||
|
return Promise.all(tagOps);
|
||||||
|
}
|
||||||
|
});
|
||||||
ops.push(upgradeOp);
|
ops.push(upgradeOp);
|
||||||
|
|
||||||
return Promise.all(ops);
|
return Promise.all(ops);
|
||||||
|
|
|
@ -117,7 +117,8 @@ var db = {
|
||||||
posts_tags: {
|
posts_tags: {
|
||||||
id: {type: 'increments', nullable: false, primary: true},
|
id: {type: 'increments', nullable: false, primary: true},
|
||||||
post_id: {type: 'integer', nullable: false, unsigned: true, references: 'posts.id'},
|
post_id: {type: 'integer', nullable: false, unsigned: true, references: 'posts.id'},
|
||||||
tag_id: {type: 'integer', nullable: false, unsigned: true, references: 'tags.id'}
|
tag_id: {type: 'integer', nullable: false, unsigned: true, references: 'tags.id'},
|
||||||
|
sort_order: {type: 'integer', nullable: false, unsigned: true, defaultTo: 0}
|
||||||
},
|
},
|
||||||
apps: {
|
apps: {
|
||||||
id: {type: 'increments', nullable: false, primary: true},
|
id: {type: 'increments', nullable: false, primary: true},
|
||||||
|
|
|
@ -2,10 +2,11 @@
|
||||||
* # Utils
|
* # Utils
|
||||||
* Parts of the model code which can be split out and unit tested
|
* Parts of the model code which can be split out and unit tested
|
||||||
*/
|
*/
|
||||||
var _ = require('lodash'),
|
var _ = require('lodash'),
|
||||||
collectionQuery,
|
collectionQuery,
|
||||||
filtering,
|
filtering,
|
||||||
addPostCount;
|
addPostCount,
|
||||||
|
tagUpdate;
|
||||||
|
|
||||||
addPostCount = function addPostCount(options, itemCollection) {
|
addPostCount = function addPostCount(options, itemCollection) {
|
||||||
if (options.include && options.include.indexOf('post_count') > -1) {
|
if (options.include && options.include.indexOf('post_count') > -1) {
|
||||||
|
@ -66,6 +67,69 @@ filtering = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
tagUpdate = {
|
||||||
|
fetchCurrentPost: function fetchCurrentPost(PostModel, id, options) {
|
||||||
|
return PostModel.forge({id: id}).fetch(_.extend({}, options, {withRelated: ['tags']}));
|
||||||
|
},
|
||||||
|
|
||||||
|
fetchMatchingTags: function fetchMatchingTags(TagModel, tagsToMatch, options) {
|
||||||
|
if (_.isEmpty(tagsToMatch)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return TagModel.forge()
|
||||||
|
.query('whereIn', 'name', _.pluck(tagsToMatch, 'name')).fetchAll(options);
|
||||||
|
},
|
||||||
|
|
||||||
|
detachTagFromPost: function detachTagFromPost(post, tag, options) {
|
||||||
|
return function () {
|
||||||
|
// See tgriesser/bookshelf#294 for an explanation of _.omit(options, 'query')
|
||||||
|
return post.tags().detach(tag.id, _.omit(options, 'query'));
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
attachTagToPost: function attachTagToPost(post, tag, index, options) {
|
||||||
|
return function () {
|
||||||
|
// See tgriesser/bookshelf#294 for an explanation of _.omit(options, 'query')
|
||||||
|
return post.tags().attach({tag_id: tag.id, sort_order: index}, _.omit(options, 'query'));
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
createTagThenAttachTagToPost: function createTagThenAttachTagToPost(TagModel, post, tag, index, options) {
|
||||||
|
return function () {
|
||||||
|
return TagModel.add({name: tag.name}, options).then(function then(createdTag) {
|
||||||
|
return tagUpdate.attachTagToPost(post, createdTag, index, options)();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
updateTagOrderForPost: function updateTagOrderForPost(post, tag, index, options) {
|
||||||
|
return function () {
|
||||||
|
return post.tags().updatePivot(
|
||||||
|
{sort_order: index}, _.extend({}, options, {query: {where: {tag_id: tag.id}}})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
|
// Test if two tags are the same, checking ID first, and falling back to name
|
||||||
|
tagsAreEqual: function tagsAreEqual(tag1, tag2) {
|
||||||
|
if (tag1.hasOwnProperty('id') && tag2.hasOwnProperty('id')) {
|
||||||
|
return parseInt(tag1.id, 10) === parseInt(tag2.id, 10);
|
||||||
|
}
|
||||||
|
return tag1.name.toString() === tag2.name.toString();
|
||||||
|
},
|
||||||
|
tagSetsAreEqual: function tagSetsAreEqual(tags1, tags2) {
|
||||||
|
// If the lengths are different, they cannot be the same
|
||||||
|
if (tags1.length !== tags2.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Return if no item is not the same (double negative is horrible)
|
||||||
|
return !_.any(tags1, function (tag1, index) {
|
||||||
|
return !tagUpdate.tagsAreEqual(tag1, tags2[index]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.filtering = filtering;
|
module.exports.filtering = filtering;
|
||||||
module.exports.collectionQuery = collectionQuery;
|
module.exports.collectionQuery = collectionQuery;
|
||||||
module.exports.addPostCount = addPostCount;
|
module.exports.addPostCount = addPostCount;
|
||||||
|
module.exports.tagUpdate = tagUpdate;
|
||||||
|
|
|
@ -9,6 +9,7 @@ var _ = require('lodash'),
|
||||||
ghostBookshelf = require('./base'),
|
ghostBookshelf = require('./base'),
|
||||||
events = require('../events'),
|
events = require('../events'),
|
||||||
config = require('../config'),
|
config = require('../config'),
|
||||||
|
baseUtils = require('./base/utils'),
|
||||||
permalinkSetting = '',
|
permalinkSetting = '',
|
||||||
getPermalinkSetting,
|
getPermalinkSetting,
|
||||||
Post,
|
Post,
|
||||||
|
@ -180,62 +181,101 @@ Post = ghostBookshelf.Model.extend({
|
||||||
* @return {Promise(ghostBookshelf.Models.Post)} Updated Post model
|
* @return {Promise(ghostBookshelf.Models.Post)} Updated Post model
|
||||||
*/
|
*/
|
||||||
updateTags: function updateTags(savedModel, response, options) {
|
updateTags: function updateTags(savedModel, response, options) {
|
||||||
var self = this;
|
var newTags = this.myTags,
|
||||||
|
TagModel = ghostBookshelf.model('Tag');
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
|
||||||
if (!this.myTags) {
|
function doTagUpdates(options) {
|
||||||
return;
|
return Promise.props({
|
||||||
}
|
currentPost: baseUtils.tagUpdate.fetchCurrentPost(Post, savedModel.id, options),
|
||||||
|
existingTags: baseUtils.tagUpdate.fetchMatchingTags(TagModel, newTags, options)
|
||||||
|
}).then(function fetchedData(results) {
|
||||||
|
var currentTags = results.currentPost.related('tags').toJSON(options),
|
||||||
|
existingTags = results.existingTags ? results.existingTags.toJSON(options) : [],
|
||||||
|
tagOps = [],
|
||||||
|
tagsToRemove,
|
||||||
|
tagsToCreate;
|
||||||
|
|
||||||
return Post.forge({id: savedModel.id}).fetch({withRelated: ['tags'], transacting: options.transacting}).then(function then(post) {
|
if (baseUtils.tagUpdate.tagSetsAreEqual(newTags, currentTags)) {
|
||||||
var tagOps = [];
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// remove all existing tags from the post
|
// Tags from the current tag array which don't exist in the new tag array should be removed
|
||||||
// _.omit(options, 'query') is a fix for using bookshelf 0.6.8
|
tagsToRemove = _.reject(currentTags, function (currentTag) {
|
||||||
// (https://github.com/tgriesser/bookshelf/issues/294)
|
if (newTags.length === 0) {
|
||||||
tagOps.push(post.tags().detach(null, _.omit(options, 'query')));
|
return false;
|
||||||
|
}
|
||||||
if (_.isEmpty(self.myTags)) {
|
return _.any(newTags, function (newTag) {
|
||||||
return Promise.all(tagOps);
|
return baseUtils.tagUpdate.tagsAreEqual(currentTag, newTag);
|
||||||
}
|
|
||||||
|
|
||||||
return ghostBookshelf.collection('Tags').forge().query('whereIn', 'name', _.pluck(self.myTags, 'name')).fetch(options).then(function then(existingTags) {
|
|
||||||
var doNotExist = [],
|
|
||||||
sequenceTasks = [];
|
|
||||||
|
|
||||||
existingTags = existingTags.toJSON(options);
|
|
||||||
|
|
||||||
doNotExist = _.reject(self.myTags, function (tag) {
|
|
||||||
return _.any(existingTags, function (existingTag) {
|
|
||||||
return existingTag.name === tag.name;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create tags that don't exist and attach to post
|
// Tags from the new tag array which don't exist in the DB should be created
|
||||||
_.each(doNotExist, function (tag) {
|
tagsToCreate = _.pluck(_.reject(newTags, function (newTag) {
|
||||||
var createAndAttachOperation = function createAndAttachOperation() {
|
return _.any(existingTags, function (existingTag) {
|
||||||
return ghostBookshelf.model('Tag').add({name: tag.name}, options).then(function then(createdTag) {
|
return baseUtils.tagUpdate.tagsAreEqual(existingTag, newTag);
|
||||||
// _.omit(options, 'query') is a fix for using bookshelf 0.6.8
|
});
|
||||||
// (https://github.com/tgriesser/bookshelf/issues/294)
|
}), 'name');
|
||||||
return post.tags().attach(createdTag.id, _.omit(options, 'query'));
|
|
||||||
|
// Remove any tags which don't exist anymore
|
||||||
|
_.each(tagsToRemove, function (tag) {
|
||||||
|
tagOps.push(baseUtils.tagUpdate.detachTagFromPost(savedModel, tag, options));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Loop through the new tags and either add them, attach them, or update them
|
||||||
|
_.each(newTags, function (newTag, index) {
|
||||||
|
var tag;
|
||||||
|
|
||||||
|
if (tagsToCreate.indexOf(newTag.name) > -1) {
|
||||||
|
tagOps.push(baseUtils.tagUpdate.createTagThenAttachTagToPost(TagModel, savedModel, newTag, index, options));
|
||||||
|
} else {
|
||||||
|
// try to find a tag on the current post which matches
|
||||||
|
tag = _.find(currentTags, function (currentTag) {
|
||||||
|
return baseUtils.tagUpdate.tagsAreEqual(currentTag, newTag);
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
sequenceTasks.push(createAndAttachOperation);
|
if (tag) {
|
||||||
|
tagOps.push(baseUtils.tagUpdate.updateTagOrderForPost(savedModel, tag, index, options));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// else finally, find the existing tag which matches
|
||||||
|
tag = _.find(existingTags, function (existingTag) {
|
||||||
|
return baseUtils.tagUpdate.tagsAreEqual(existingTag, newTag);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (tag) {
|
||||||
|
tagOps.push(baseUtils.tagUpdate.attachTagToPost(savedModel, tag, index, options));
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
tagOps = tagOps.concat(sequence(sequenceTasks));
|
return sequence(tagOps);
|
||||||
|
|
||||||
// attach the tags that already existed
|
|
||||||
_.each(existingTags, function (tag) {
|
|
||||||
// _.omit(options, 'query') is a fix for using bookshelf 0.6.8
|
|
||||||
// (https://github.com/tgriesser/bookshelf/issues/294)
|
|
||||||
tagOps.push(post.tags().attach(tag.id, _.omit(options, 'query')));
|
|
||||||
});
|
|
||||||
|
|
||||||
return Promise.all(tagOps);
|
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
// Handle updating tags in a transaction, unless we're already in one
|
||||||
|
if (options.transacting) {
|
||||||
|
return doTagUpdates(options);
|
||||||
|
} else {
|
||||||
|
return ghostBookshelf.transaction(function (t) {
|
||||||
|
options.transacting = t;
|
||||||
|
|
||||||
|
return doTagUpdates(options);
|
||||||
|
}).then(function () {
|
||||||
|
// Don't do anything, the transaction processed ok
|
||||||
|
}).catch(function failure(error) {
|
||||||
|
errors.logError(
|
||||||
|
error,
|
||||||
|
'Unable to save tags.',
|
||||||
|
'Your post was saved, but your tags were not updated.'
|
||||||
|
);
|
||||||
|
return Promise.reject(new errors.InternalServerError(
|
||||||
|
'Unable to save tags. Your post was saved, but your tags were not updated. ' + error
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// Relations
|
// Relations
|
||||||
|
@ -256,7 +296,7 @@ Post = ghostBookshelf.Model.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
tags: function tags() {
|
tags: function tags() {
|
||||||
return this.belongsToMany('Tag');
|
return this.belongsToMany('Tag').withPivot('sort_order').query('orderBy', 'sort_order', 'ASC');
|
||||||
},
|
},
|
||||||
|
|
||||||
fields: function fields() {
|
fields: function fields() {
|
||||||
|
|
|
@ -116,379 +116,566 @@ describe('Tag Model', function () {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('a Post', function () {
|
describe('Post tag handling, post with NO tags', function () {
|
||||||
it('can add a tag', function (done) {
|
var postJSON,
|
||||||
var newPost = testUtils.DataGenerator.forModel.posts[0],
|
tagJSON,
|
||||||
newTag = testUtils.DataGenerator.forModel.tags[0],
|
editOptions,
|
||||||
createdPostID;
|
createTag = testUtils.DataGenerator.forKnex.createTag;
|
||||||
|
|
||||||
Promise.all([
|
beforeEach(function (done) {
|
||||||
PostModel.add(newPost, context),
|
tagJSON = [];
|
||||||
TagModel.add(newTag, context)
|
|
||||||
]).then(function (models) {
|
|
||||||
var createdPost = models[0],
|
|
||||||
createdTag = models[1];
|
|
||||||
|
|
||||||
eventSpy.calledTwice.should.be.true;
|
var post = testUtils.DataGenerator.forModel.posts[0],
|
||||||
eventSpy.calledWith('post.added').should.be.true;
|
extraTag1 = createTag({name: 'existing tag a'}),
|
||||||
eventSpy.calledWith('tag.added').should.be.true;
|
extraTag2 = createTag({name: 'existing-tag-b'}),
|
||||||
|
extraTag3 = createTag({name: 'existing_tag_c'});
|
||||||
|
|
||||||
|
return Promise.props({
|
||||||
|
post: PostModel.add(post, _.extend({}, context, {withRelated: ['tags']})),
|
||||||
|
tag1: TagModel.add(extraTag1, context),
|
||||||
|
tag2: TagModel.add(extraTag2, context),
|
||||||
|
tag3: TagModel.add(extraTag3, context)
|
||||||
|
}).then(function (result) {
|
||||||
|
postJSON = result.post.toJSON({include: ['tags']});
|
||||||
|
tagJSON.push(result.tag1.toJSON());
|
||||||
|
tagJSON.push(result.tag2.toJSON());
|
||||||
|
tagJSON.push(result.tag3.toJSON());
|
||||||
|
editOptions = _.extend({}, context, {id: postJSON.id, withRelated: ['tags']});
|
||||||
|
|
||||||
createdPostID = createdPost.id;
|
|
||||||
return createdPost.tags().attach(createdTag);
|
|
||||||
}).then(function () {
|
|
||||||
return PostModel.findOne({id: createdPostID, status: 'all'}, {withRelated: ['tags']});
|
|
||||||
}).then(function (postWithTag) {
|
|
||||||
postWithTag.related('tags').length.should.equal(1);
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can remove a tag', function (done) {
|
it('should create the test data correctly', function () {
|
||||||
// The majority of this test is ripped from above, which is obviously a Bad Thing.
|
// creates two test tags
|
||||||
// Would be nice to find a way to seed data with relations for cases like this,
|
should.exist(tagJSON);
|
||||||
// because there are more DB hits than needed
|
tagJSON.should.be.an.Array.with.lengthOf(3);
|
||||||
var newPost = testUtils.DataGenerator.forModel.posts[0],
|
tagJSON.should.have.enumerable(0).with.property('name', 'existing tag a');
|
||||||
newTag = testUtils.DataGenerator.forModel.tags[0],
|
tagJSON.should.have.enumerable(1).with.property('name', 'existing-tag-b');
|
||||||
createdTagID,
|
tagJSON.should.have.enumerable(2).with.property('name', 'existing_tag_c');
|
||||||
createdPostID;
|
|
||||||
|
|
||||||
Promise.all([
|
// creates a test post with no tags
|
||||||
PostModel.add(newPost, context),
|
should.exist(postJSON);
|
||||||
TagModel.add(newTag, context)
|
postJSON.title.should.eql('HTML Ipsum');
|
||||||
]).then(function (models) {
|
should.exist(postJSON.tags);
|
||||||
var createdPost = models[0],
|
|
||||||
createdTag = models[1];
|
|
||||||
|
|
||||||
eventSpy.calledTwice.should.be.true;
|
|
||||||
eventSpy.calledWith('post.added').should.be.true;
|
|
||||||
eventSpy.calledWith('tag.added').should.be.true;
|
|
||||||
|
|
||||||
createdPostID = createdPost.id;
|
|
||||||
createdTagID = createdTag.id;
|
|
||||||
return createdPost.tags().attach(createdTag);
|
|
||||||
}).then(function () {
|
|
||||||
return PostModel.findOne({id: createdPostID, status: 'all'}, {withRelated: ['tags']});
|
|
||||||
}).then(function (postWithTag) {
|
|
||||||
return postWithTag.tags().detach(createdTagID);
|
|
||||||
}).then(function () {
|
|
||||||
eventSpy.calledTwice.should.be.true;
|
|
||||||
return PostModel.findOne({id: createdPostID, status: 'all'}, {withRelated: ['tags']});
|
|
||||||
}).then(function (postWithoutTag) {
|
|
||||||
postWithoutTag.related('tags').length.should.equal(0);
|
|
||||||
done();
|
|
||||||
}).catch(done);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('setting tags from an array on update', function () {
|
describe('Adding brand new tags', function () {
|
||||||
// When a Post is updated, iterate through the existing tags, and detach any that have since been removed.
|
it('can add a single tag', function (done) {
|
||||||
// It can be assumed that any remaining tags in the update data are newly added.
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
// Create new tags if needed, and attach them to the Post
|
|
||||||
|
|
||||||
function seedTags(tagNames) {
|
// Add a single tag to the end of the array
|
||||||
var createOperations = [
|
newJSON.tags.push(createTag({name: 'tag1'}));
|
||||||
PostModel.add(testUtils.DataGenerator.forModel.posts[0], context)
|
|
||||||
],
|
|
||||||
tagModels = tagNames.map(function (tagName) { return TagModel.add({name: tagName}, context); });
|
|
||||||
|
|
||||||
createOperations = createOperations.concat(tagModels);
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
return Promise.all(createOperations).then(function (models) {
|
updatedPost.tags.should.have.lengthOf(1);
|
||||||
var postModel = models[0],
|
updatedPost.tags.should.have.enumerable(0).with.property('name', 'tag1');
|
||||||
attachOperations,
|
|
||||||
i;
|
|
||||||
|
|
||||||
attachOperations = [];
|
|
||||||
for (i = 1; i < models.length; i += 1) {
|
|
||||||
attachOperations.push(postModel.tags().attach(models[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.all(attachOperations).then(function () {
|
|
||||||
return postModel;
|
|
||||||
});
|
|
||||||
}).then(function (postModel) {
|
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
it('does nothing if tags haven\'t changed', function (done) {
|
|
||||||
var seededTagNames = ['tag1', 'tag2', 'tag3'];
|
|
||||||
|
|
||||||
seedTags(seededTagNames).then(function (postModel) {
|
|
||||||
// the tag API expects tags to be provided like {id: 1, name: 'draft'}
|
|
||||||
var existingTagData = seededTagNames.map(function (tagName, i) {
|
|
||||||
return {id: i + 1, name: tagName};
|
|
||||||
});
|
|
||||||
|
|
||||||
postModel = postModel.toJSON();
|
|
||||||
postModel.tags = existingTagData;
|
|
||||||
|
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id, withRelated: ['tags']}));
|
|
||||||
}).then(function (postModel) {
|
|
||||||
var tagNames = postModel.related('tags').models.map(function (t) { return t.attributes.name; });
|
|
||||||
tagNames.sort().should.eql(seededTagNames);
|
|
||||||
|
|
||||||
return TagModel.findAll();
|
|
||||||
}).then(function (tagsFromDB) {
|
|
||||||
tagsFromDB.length.should.eql(seededTagNames.length);
|
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('detaches tags that have been removed', function (done) {
|
it('can add multiple tags', function (done) {
|
||||||
var seededTagNames = ['tag1', 'tag2', 'tag3'];
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
seedTags(seededTagNames).then(function (postModel) {
|
// Add a bunch of tags to the end of the array
|
||||||
// the tag API expects tags to be provided like {id: 1, name: 'draft'}
|
newJSON.tags.push(createTag({name: 'tag1'}));
|
||||||
var tagData = seededTagNames.map(function (tagName, i) { return {id: i + 1, name: tagName}; });
|
newJSON.tags.push(createTag({name: 'tag2'}));
|
||||||
|
newJSON.tags.push(createTag({name: 'tag3'}));
|
||||||
|
newJSON.tags.push(createTag({name: 'tag4'}));
|
||||||
|
newJSON.tags.push(createTag({name: 'tag5'}));
|
||||||
|
|
||||||
// remove the second tag, and save
|
// Edit the post
|
||||||
tagData.splice(1, 1);
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
postModel = postModel.toJSON();
|
updatedPost.tags.should.have.lengthOf(5);
|
||||||
postModel.tags = tagData;
|
updatedPost.tags.should.have.enumerable(0).with.property('name', 'tag1');
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.property('name', 'tag2');
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id, withRelated: ['tags']}));
|
updatedPost.tags.should.have.enumerable(2).with.property('name', 'tag3');
|
||||||
}).then(function (postModel) {
|
updatedPost.tags.should.have.enumerable(3).with.property('name', 'tag4');
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
updatedPost.tags.should.have.enumerable(4).with.property('name', 'tag5');
|
||||||
}).then(function (reloadedPost) {
|
|
||||||
var tagNames = reloadedPost.related('tags').models.map(function (t) { return t.attributes.name; });
|
|
||||||
tagNames.sort().should.eql(['tag1', 'tag3']);
|
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('attaches tags that are new to the post, but already exist in the database', function (done) {
|
it('can add multiple tags with conflicting slugs', function (done) {
|
||||||
var seededTagNames = ['tag1', 'tag2'],
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
postModel;
|
|
||||||
|
|
||||||
seedTags(seededTagNames).then(function (_postModel) {
|
// Add conflicting tags to the end of the array
|
||||||
postModel = _postModel;
|
newJSON.tags.push({name: 'C'});
|
||||||
return TagModel.add({name: 'tag3'}, context);
|
newJSON.tags.push({name: 'C++'});
|
||||||
}).then(function () {
|
newJSON.tags.push({name: 'C#'});
|
||||||
// the tag API expects tags to be provided like {id: 1, name: 'draft'}
|
|
||||||
var tagData = seededTagNames.map(function (tagName, i) { return {id: i + 1, name: tagName}; });
|
|
||||||
|
|
||||||
// add the additional tag, and save
|
// Edit the post
|
||||||
tagData.push({id: 3, name: 'tag3'});
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
postModel = postModel.toJSON();
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
postModel.tags = tagData;
|
|
||||||
|
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id, withRelated: ['tags']}));
|
updatedPost.tags.should.have.lengthOf(3);
|
||||||
}).then(function () {
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'C', slug: 'c'});
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'C++', slug: 'c-2'});
|
||||||
}).then(function (reloadedPost) {
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'C#', slug: 'c-3'});
|
||||||
var tagModels = reloadedPost.related('tags').models,
|
|
||||||
tagNames = tagModels.map(function (t) { return t.attributes.name; }),
|
|
||||||
tagIds = _.pluck(tagModels, 'id');
|
|
||||||
tagNames.sort().should.eql(['tag1', 'tag2', 'tag3']);
|
|
||||||
|
|
||||||
// make sure it hasn't just added a new tag with the same name
|
done();
|
||||||
// Don't expect a certain order in results - check for number of items!
|
}).catch(done);
|
||||||
Math.max.apply(Math, tagIds).should.eql(3);
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Adding pre-existing tags', function () {
|
||||||
|
it('can add a single tag', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
|
// Add a single pre-existing tag
|
||||||
|
newJSON.tags.push(tagJSON[0]);
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(1);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates and attaches a tag that is new to the Tags table', function (done) {
|
it('can add multiple tags', function (done) {
|
||||||
var seededTagNames = ['tag1', 'tag2'];
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
seedTags(seededTagNames).then(function (postModel) {
|
// Add many preexisting tags
|
||||||
// the tag API expects tags to be provided like {id: 1, name: 'draft'}
|
newJSON.tags.push(tagJSON[0]);
|
||||||
var tagData = seededTagNames.map(function (tagName, i) { return {id: i + 1, name: tagName}; });
|
newJSON.tags.push(tagJSON[1]);
|
||||||
|
newJSON.tags.push(tagJSON[2]);
|
||||||
|
|
||||||
// add the additional tag, and save
|
// Edit the post
|
||||||
tagData.push({id: null, name: 'tag3'});
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
postModel = postModel.toJSON();
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
postModel.tags = tagData;
|
|
||||||
|
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id, withRelated: ['tags']}));
|
updatedPost.tags.should.have.lengthOf(3);
|
||||||
}).then(function (postModel) {
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});
|
||||||
}).then(function (reloadedPost) {
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'existing_tag_c', id: tagJSON[2].id});
|
||||||
var tagNames = reloadedPost.related('tags').models.map(function (t) { return t.attributes.name; });
|
|
||||||
tagNames.sort().should.eql(['tag1', 'tag2', 'tag3']);
|
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('creates and attaches multiple tags that are new to the Tags table', function (done) {
|
it('can add multiple tags in wrong order', function (done) {
|
||||||
var seededTagNames = ['tag1'];
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
seedTags(seededTagNames).then(function (postModel) {
|
// Add tags to the array
|
||||||
// the tag API expects tags to be provided like {id: 1, name: 'draft'}
|
newJSON.tags.push(tagJSON[2]);
|
||||||
var tagData = seededTagNames.map(function (tagName, i) { return {id: i + 1, name: tagName}; });
|
newJSON.tags.push(tagJSON[0]);
|
||||||
|
newJSON.tags.push(tagJSON[1]);
|
||||||
|
|
||||||
// add the additional tags, and save
|
// Edit the post
|
||||||
tagData.push({id: null, name: 'tag2'});
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
tagData.push({id: null, name: 'tag3'});
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
postModel = postModel.toJSON();
|
updatedPost.tags.should.have.lengthOf(3);
|
||||||
postModel.tags = tagData;
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing_tag_c', id: tagJSON[2].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});
|
||||||
|
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id, withRelated: ['tags']}));
|
done();
|
||||||
}).then(function (postModel) {
|
}).catch(done);
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
});
|
||||||
}).then(function (reloadedPost) {
|
});
|
||||||
var tagNames = reloadedPost.related('tags').models.map(function (t) { return t.attributes.name; });
|
|
||||||
tagNames.sort().should.eql(['tag1', 'tag2', 'tag3']);
|
describe('Adding combinations', function () {
|
||||||
|
it('can add a combination of new and pre-existing tags', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
|
// Add a bunch of new and existing tags to the array
|
||||||
|
newJSON.tags.push({name: 'tag1'});
|
||||||
|
newJSON.tags.push({name: 'existing tag a'});
|
||||||
|
newJSON.tags.push({name: 'tag3'});
|
||||||
|
newJSON.tags.push({name: 'existing-tag-b'});
|
||||||
|
newJSON.tags.push({name: 'tag5'});
|
||||||
|
newJSON.tags.push({name: 'existing_tag_c'});
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(6);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.property('name', 'tag1');
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.property('name', 'tag3');
|
||||||
|
updatedPost.tags.should.have.enumerable(3).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(4).with.property('name', 'tag5');
|
||||||
|
updatedPost.tags.should.have.enumerable(5).with.properties({name: 'existing_tag_c', id: tagJSON[2].id});
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Post tag handling, post with tags', function () {
|
||||||
|
var postJSON,
|
||||||
|
tagJSON,
|
||||||
|
editOptions,
|
||||||
|
createTag = testUtils.DataGenerator.forKnex.createTag;
|
||||||
|
|
||||||
|
beforeEach(function (done) {
|
||||||
|
tagJSON = [];
|
||||||
|
|
||||||
|
var post = testUtils.DataGenerator.forModel.posts[0],
|
||||||
|
postTags = [
|
||||||
|
createTag({name: 'tag1'}),
|
||||||
|
createTag({name: 'tag2'}),
|
||||||
|
createTag({name: 'tag3'})
|
||||||
|
],
|
||||||
|
extraTags = [
|
||||||
|
createTag({name: 'existing tag a'}),
|
||||||
|
createTag({name: 'existing-tag-b'}),
|
||||||
|
createTag({name: 'existing_tag_c'})
|
||||||
|
];
|
||||||
|
|
||||||
|
post.tags = postTags;
|
||||||
|
|
||||||
|
return Promise.props({
|
||||||
|
post: PostModel.add(post, _.extend({}, context, {withRelated: ['tags']})),
|
||||||
|
tag1: TagModel.add(extraTags[0], context),
|
||||||
|
tag2: TagModel.add(extraTags[1], context),
|
||||||
|
tag3: TagModel.add(extraTags[2], context)
|
||||||
|
}).then(function (result) {
|
||||||
|
postJSON = result.post.toJSON({include: ['tags']});
|
||||||
|
tagJSON.push(result.tag1.toJSON());
|
||||||
|
tagJSON.push(result.tag2.toJSON());
|
||||||
|
tagJSON.push(result.tag3.toJSON());
|
||||||
|
editOptions = _.extend({}, context, {id: postJSON.id, withRelated: ['tags']});
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create the test data correctly', function () {
|
||||||
|
// creates a test tag
|
||||||
|
should.exist(tagJSON);
|
||||||
|
tagJSON.should.be.an.Array.with.lengthOf(3);
|
||||||
|
tagJSON.should.have.enumerable(0).with.property('name', 'existing tag a');
|
||||||
|
tagJSON.should.have.enumerable(1).with.property('name', 'existing-tag-b');
|
||||||
|
tagJSON.should.have.enumerable(2).with.property('name', 'existing_tag_c');
|
||||||
|
|
||||||
|
// creates a test post with an array of tags in the correct order
|
||||||
|
should.exist(postJSON);
|
||||||
|
postJSON.title.should.eql('HTML Ipsum');
|
||||||
|
should.exist(postJSON.tags);
|
||||||
|
postJSON.tags.should.be.an.Array.and.have.lengthOf(3);
|
||||||
|
postJSON.tags.should.have.enumerable(0).with.property('name', 'tag1');
|
||||||
|
postJSON.tags.should.have.enumerable(1).with.property('name', 'tag2');
|
||||||
|
postJSON.tags.should.have.enumerable(2).with.property('name', 'tag3');
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Adding brand new tags', function () {
|
||||||
|
it('can add a single tag to the end of the tags array', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
|
// Add a single tag to the end of the array
|
||||||
|
newJSON.tags.push(createTag({name: 'tag4'}));
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(4);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(3).with.property('name', 'tag4');
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('attaches one tag that exists in the Tags database and one tag that is new', function (done) {
|
it('can add a single tag to the beginning of the tags array', function (done) {
|
||||||
var seededTagNames = ['tag1'],
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
postModel;
|
|
||||||
|
|
||||||
seedTags(seededTagNames).then(function (_postModel) {
|
// Add a single tag to the beginning of the array
|
||||||
postModel = _postModel;
|
newJSON.tags = [createTag({name: 'tag4'})].concat(postJSON.tags);
|
||||||
return TagModel.add({name: 'tag2'}, context);
|
|
||||||
}).then(function () {
|
|
||||||
// the tag API expects tags to be provided like {id: 1, name: 'draft'}
|
|
||||||
var tagData = seededTagNames.map(function (tagName, i) { return {id: i + 1, name: tagName}; });
|
|
||||||
|
|
||||||
// Add the tag that exists in the database
|
// Edit the post
|
||||||
tagData.push({id: 2, name: 'tag2'});
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
// Add the tag that doesn't exist in the database
|
updatedPost.tags.should.have.lengthOf(4);
|
||||||
tagData.push({id: 3, name: 'tag3'});
|
updatedPost.tags.should.have.enumerable(0).with.property('name', 'tag4');
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(3).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
|
||||||
postModel = postModel.toJSON();
|
done();
|
||||||
postModel.tags = tagData;
|
}).catch(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id, withRelated: ['tags']}));
|
describe('Adding pre-existing tags', function () {
|
||||||
}).then(function () {
|
it('can add a single tag to the end of the tags array', function (done) {
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
}).then(function (reloadedPost) {
|
|
||||||
var tagModels = reloadedPost.related('tags').models,
|
|
||||||
tagNames = tagModels.map(function (t) { return t.attributes.name; }),
|
|
||||||
tagIds = _.pluck(tagModels, 'id');
|
|
||||||
|
|
||||||
tagNames.sort().should.eql(['tag1', 'tag2', 'tag3']);
|
// Add a single pre-existing tag to the end of the array
|
||||||
|
newJSON.tags.push(tagJSON[0]);
|
||||||
|
|
||||||
// make sure it hasn't just added a new tag with the same name
|
// Edit the post
|
||||||
// Don't expect a certain order in results - check for number of items!
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
Math.max.apply(Math, tagIds).should.eql(3);
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(4);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(3).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('attaches one tag that exists in the Tags database and two tags that are new', function (done) {
|
it('can add a single tag to the beginning of the tags array', function (done) {
|
||||||
var seededTagNames = ['tag1'],
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
postModel;
|
|
||||||
|
|
||||||
seedTags(seededTagNames).then(function (_postModel) {
|
// Add an existing tag to the beginning of the array
|
||||||
postModel = _postModel;
|
newJSON.tags = [tagJSON[0]].concat(postJSON.tags);
|
||||||
return TagModel.add({name: 'tag2'}, context);
|
|
||||||
}).then(function () {
|
|
||||||
// the tag API expects tags to be provided like {id: 1, name: 'draft'}
|
|
||||||
var tagData = seededTagNames.map(function (tagName, i) { return {id: i + 1, name: tagName}; });
|
|
||||||
|
|
||||||
// Add the tag that exists in the database
|
// Edit the post
|
||||||
tagData.push({id: 2, name: 'tag2'});
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
// Add the tags that doesn't exist in the database
|
updatedPost.tags.should.have.lengthOf(4);
|
||||||
tagData.push({id: 3, name: 'tag3'});
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
tagData.push({id: 4, name: 'tag4'});
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
postModel = postModel.toJSON();
|
updatedPost.tags.should.have.enumerable(3).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
postModel.tags = tagData;
|
|
||||||
|
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id, withRelated: ['tags']}));
|
|
||||||
}).then(function () {
|
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
|
||||||
}).then(function (reloadedPost) {
|
|
||||||
var tagModels = reloadedPost.related('tags').models,
|
|
||||||
tagNames = tagModels.map(function (t) { return t.get('name'); }),
|
|
||||||
tagIds = _.pluck(tagModels, 'id');
|
|
||||||
|
|
||||||
tagNames.sort().should.eql(['tag1', 'tag2', 'tag3', 'tag4']);
|
|
||||||
|
|
||||||
// make sure it hasn't just added a new tag with the same name
|
|
||||||
// Don't expect a certain order in results - check for number of items!
|
|
||||||
Math.max.apply(Math, tagIds).should.eql(4);
|
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('attaches two new tags and resolves conflicting slugs', function (done) {
|
it('can add a single tag to the middle of the tags array', function (done) {
|
||||||
var tagData = [];
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
// Add the tags that don't exist in the database and have potentially
|
// Add a single pre-existing tag to the middle of the array
|
||||||
// conflicting slug names
|
newJSON.tags = postJSON.tags.slice(0, 1).concat([tagJSON[0]]).concat(postJSON.tags.slice(1));
|
||||||
tagData.push({id: 1, name: 'C'});
|
|
||||||
tagData.push({id: 2, name: 'C++'});
|
|
||||||
|
|
||||||
PostModel.add(testUtils.DataGenerator.forModel.posts[0], context).then(function (postModel) {
|
// Edit the post
|
||||||
postModel = postModel.toJSON();
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
postModel.tags = tagData;
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id})).then(function () {
|
updatedPost.tags.should.have.lengthOf(4);
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
}).then(function (reloadedPost) {
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
var tagModels = reloadedPost.related('tags').models,
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
tagNames = tagModels.map(function (t) { return t.get('name'); }),
|
updatedPost.tags.should.have.enumerable(3).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
tagIds = _.pluck(tagModels, 'id');
|
|
||||||
|
|
||||||
tagNames.sort().should.eql(['C', 'C++']);
|
done();
|
||||||
|
|
||||||
// make sure it hasn't just added a new tag with the same name
|
|
||||||
// Don't expect a certain order in results - check for number of items!
|
|
||||||
Math.max.apply(Math, tagIds).should.eql(2);
|
|
||||||
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('correctly creates non-conflicting slugs', function (done) {
|
describe('Removing tags', function () {
|
||||||
var seededTagNames = ['tag1'],
|
it('can remove a single tag from the end of the tags array', function (done) {
|
||||||
postModel;
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
seedTags(seededTagNames).then(function (_postModel) {
|
// Remove a single tag from the end of the array
|
||||||
postModel = _postModel;
|
newJSON.tags = postJSON.tags.slice(0, -1);
|
||||||
return TagModel.add({name: 'tagc'}, context);
|
|
||||||
}).then(function () {
|
|
||||||
// the tag API expects tags to be provided like {id: 1, name: 'draft'}
|
|
||||||
var tagData = [];
|
|
||||||
|
|
||||||
tagData.push({id: 2, name: 'tagc'});
|
// Edit the post
|
||||||
tagData.push({id: 3, name: 'tagc++'});
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
postModel = postModel.toJSON();
|
updatedPost.tags.should.have.lengthOf(2);
|
||||||
postModel.tags = tagData;
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
return PostModel.edit(postModel, _.extend({}, context, {id: postModel.id, withRelated: ['tags']}));
|
|
||||||
}).then(function () {
|
|
||||||
return PostModel.findOne({id: postModel.id, status: 'all'}, {withRelated: ['tags']});
|
|
||||||
}).then(function (reloadedPost) {
|
|
||||||
var tagModels = reloadedPost.related('tags').models,
|
|
||||||
tagSlugs = tagModels.map(function (t) { return t.get('slug'); }),
|
|
||||||
tagIds = _.pluck(tagModels, 'id');
|
|
||||||
|
|
||||||
tagSlugs.sort().should.eql(['tagc', 'tagc-2']);
|
|
||||||
|
|
||||||
// make sure it hasn't just added a new tag with the same name
|
|
||||||
// Don't expect a certain order in results - check for number of items!
|
|
||||||
Math.max.apply(Math, tagIds).should.eql(3);
|
|
||||||
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can add a tag to a post on creation', function (done) {
|
it('can remove a single tag from the beginning of the tags array', function (done) {
|
||||||
var newPost = _.extend({}, testUtils.DataGenerator.forModel.posts[0], {tags: [{name: 'test_tag_1'}]});
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
|
// Remove a single tag from the beginning of the array
|
||||||
|
newJSON.tags = postJSON.tags.slice(1);
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(2);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can remove all tags', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
|
// Remove all the tags
|
||||||
|
newJSON.tags = [];
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(0);
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Reordering tags', function () {
|
||||||
|
it('can reorder the first tag to be the last', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON),
|
||||||
|
firstTag = [postJSON.tags[0]];
|
||||||
|
|
||||||
|
// Reorder the tags, so that the first tag is moved to the end
|
||||||
|
newJSON.tags = postJSON.tags.slice(1).concat(firstTag);
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(3);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can reorder the last tag to be the first', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON),
|
||||||
|
lastTag = [postJSON.tags[2]];
|
||||||
|
|
||||||
|
// Reorder the tags, so that the last tag is moved to the beginning
|
||||||
|
newJSON.tags = lastTag.concat(postJSON.tags.slice(0, -1));
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(3);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Combination updates', function () {
|
||||||
|
it('can add a combination of new and pre-existing tags', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON);
|
||||||
|
|
||||||
|
// Push a bunch of new and existing tags to the end of the array
|
||||||
|
newJSON.tags.push({name: 'tag4'});
|
||||||
|
newJSON.tags.push({name: 'existing tag a'});
|
||||||
|
newJSON.tags.push({name: 'tag5'});
|
||||||
|
newJSON.tags.push({name: 'existing-tag-b'});
|
||||||
|
newJSON.tags.push({name: 'bob'});
|
||||||
|
newJSON.tags.push({name: 'existing_tag_c'});
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(9);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(3).with.property('name', 'tag4');
|
||||||
|
updatedPost.tags.should.have.enumerable(4).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(5).with.property('name', 'tag5');
|
||||||
|
updatedPost.tags.should.have.enumerable(6).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(7).with.property('name', 'bob');
|
||||||
|
updatedPost.tags.should.have.enumerable(8).with.properties({name: 'existing_tag_c', id: tagJSON[2].id});
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can reorder the first tag to be the last and add a tag to the beginning', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON),
|
||||||
|
firstTag = [postJSON.tags[0]];
|
||||||
|
|
||||||
|
// Add a new tag to the beginning, and move the original first tag to the end
|
||||||
|
newJSON.tags = [tagJSON[0]].concat(postJSON.tags.slice(1)).concat(firstTag);
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(4);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(3).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can reorder the first tag to be the last, remove the original last tag & add a tag to the beginning', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON),
|
||||||
|
firstTag = [newJSON.tags[0]];
|
||||||
|
|
||||||
|
// And an existing tag to the beginning of the array, move the original first tag to the end and remove the original last tag
|
||||||
|
newJSON.tags = [tagJSON[0]].concat(newJSON.tags.slice(1, -1)).concat(firstTag);
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(3);
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
|
||||||
|
done();
|
||||||
|
}).catch(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can reorder original tags, remove one, and add new and existing tags', function (done) {
|
||||||
|
var newJSON = _.cloneDeep(postJSON),
|
||||||
|
firstTag = [newJSON.tags[0]];
|
||||||
|
|
||||||
|
// Reorder original 3 so that first is at the end
|
||||||
|
newJSON.tags = newJSON.tags.slice(1).concat(firstTag);
|
||||||
|
|
||||||
|
// add an existing tag in the middle
|
||||||
|
newJSON.tags = newJSON.tags.slice(0, 1).concat({name: 'existing-tag-b'}).concat(newJSON.tags.slice(1));
|
||||||
|
|
||||||
|
// add a brand new tag in the middle
|
||||||
|
newJSON.tags = newJSON.tags.slice(0, 3).concat({name: 'betty'}).concat(newJSON.tags.slice(3));
|
||||||
|
|
||||||
|
// Add some more tags to the end
|
||||||
|
newJSON.tags.push({name: 'bob'});
|
||||||
|
newJSON.tags.push({name: 'existing tag a'});
|
||||||
|
|
||||||
|
// Edit the post
|
||||||
|
return PostModel.edit(newJSON, editOptions).then(function (updatedPost) {
|
||||||
|
updatedPost = updatedPost.toJSON({include: ['tags']});
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.lengthOf(7);
|
||||||
|
|
||||||
|
updatedPost.tags.should.have.enumerable(0).with.properties({name: 'tag2', id: postJSON.tags[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(1).with.properties({name: 'existing-tag-b', id: tagJSON[1].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(2).with.properties({name: 'tag3', id: postJSON.tags[2].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(3).with.property('name', 'betty');
|
||||||
|
updatedPost.tags.should.have.enumerable(4).with.properties({name: 'tag1', id: postJSON.tags[0].id});
|
||||||
|
updatedPost.tags.should.have.enumerable(5).with.property('name', 'bob');
|
||||||
|
updatedPost.tags.should.have.enumerable(6).with.properties({name: 'existing tag a', id: tagJSON[0].id});
|
||||||
|
|
||||||
PostModel.add(newPost, context).then(function (createdPost) {
|
|
||||||
return PostModel.findOne({id: createdPost.id, status: 'all'}, {withRelated: ['tags']});
|
|
||||||
}).then(function (postWithTag) {
|
|
||||||
postWithTag.related('tags').length.should.equal(1);
|
|
||||||
done();
|
done();
|
||||||
}).catch(done);
|
}).catch(done);
|
||||||
});
|
});
|
||||||
|
|
|
@ -20,7 +20,7 @@ describe('Migrations', function () {
|
||||||
describe('DB version integrity', function () {
|
describe('DB version integrity', function () {
|
||||||
// Only these variables should need updating
|
// Only these variables should need updating
|
||||||
var currentDbVersion = '004',
|
var currentDbVersion = '004',
|
||||||
currentSchemaHash = '5e6c9d6b9df6a0b77aff2ec582f536d9',
|
currentSchemaHash = 'a195562bf4915e3f3f610f6d178aba01',
|
||||||
currentPermissionsHash = '42e486732270cda623fc5efc04808c0c';
|
currentPermissionsHash = '42e486732270cda623fc5efc04808c0c';
|
||||||
|
|
||||||
// If this test is failing, then it is likely a change has been made that requires a DB version bump,
|
// If this test is failing, then it is likely a change has been made that requires a DB version bump,
|
||||||
|
|
Loading…
Add table
Reference in a new issue