From a0e7160fc9932c0599a95dc75eb5042377f7f367 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Thu, 30 Aug 2018 18:05:29 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BFixed=20`count.posts`=20respecting?= =?UTF-8?q?=20co=20authors=20=20(#9830)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #9822 - Fixed the post count issue for co authors - Corrected and refactored tests related to users post count - Consistency fix, because we return all posts where the author is primary or co author for the author page already --- core/server/models/plugins/include-count.js | 5 ++-- core/test/integration/api/api_users_spec.js | 30 +++++++++------------ core/test/utils/fixtures/data-generator.js | 2 +- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/core/server/models/plugins/include-count.js b/core/server/models/plugins/include-count.js index 1b741a1aba..7535c1fe60 100644 --- a/core/server/models/plugins/include-count.js +++ b/core/server/models/plugins/include-count.js @@ -26,11 +26,12 @@ module.exports = function (Bookshelf) { } }, users: { - posts: function addPostCountToTags(model, options) { + posts: function addPostCountToUsers(model, options) { model.query('columns', 'users.*', function (qb) { qb.count('posts.id') .from('posts') - .whereRaw('posts.author_id = users.id') + .join('posts_authors', 'posts.id', 'posts_authors.post_id') + .whereRaw('posts_authors.author_id = users.id') .as('count__posts'); if (options.context && options.context.public) { diff --git a/core/test/integration/api/api_users_spec.js b/core/test/integration/api/api_users_spec.js index e46900a258..582291a693 100644 --- a/core/test/integration/api/api_users_spec.js +++ b/core/test/integration/api/api_users_spec.js @@ -184,25 +184,19 @@ describe('Users API', function () { should.exist(response.users); response.users.should.have.length(9); - testUtils.API.checkResponse(response.users[0], 'user', 'count'); - testUtils.API.checkResponse(response.users[1], 'user', 'count'); - testUtils.API.checkResponse(response.users[2], 'user', 'count'); - testUtils.API.checkResponse(response.users[3], 'user', 'count'); - testUtils.API.checkResponse(response.users[4], 'user', 'count'); - testUtils.API.checkResponse(response.users[5], 'user', 'count'); - testUtils.API.checkResponse(response.users[6], 'user', 'count'); - testUtils.API.checkResponse(response.users[7], 'user', 'count'); - testUtils.API.checkResponse(response.users[8], 'user', 'count'); + response.users.map((user) => { + testUtils.API.checkResponse(user, 'user', 'count'); + }); - response.users[0].count.posts.should.eql(0); - response.users[1].count.posts.should.eql(0); - response.users[2].count.posts.should.eql(0); - response.users[3].count.posts.should.eql(0); - response.users[4].count.posts.should.eql(0); - response.users[5].count.posts.should.eql(8); - response.users[6].count.posts.should.eql(0); - response.users[7].count.posts.should.eql(0); - response.users[8].count.posts.should.eql(0); + response.users.map((user) => { + if (user.email === 'jbloggs@example.com') { + user.count.posts.should.eql(8); + } else if (user.email === 'jbOgendAth@example.com') { + user.count.posts.should.eql(1); + } else { + response.users[1].count.posts.should.eql(0); + } + }); response.meta.pagination.should.have.property('page', 1); response.meta.pagination.should.have.property('limit', 15); diff --git a/core/test/utils/fixtures/data-generator.js b/core/test/utils/fixtures/data-generator.js index 29519f1b5d..5303df244c 100644 --- a/core/test/utils/fixtures/data-generator.js +++ b/core/test/utils/fixtures/data-generator.js @@ -786,7 +786,7 @@ DataGenerator.forKnex = (function () { { id: ObjectId.generate(), post_id: DataGenerator.Content.posts[3].id, - author_id: DataGenerator.Content.users[2].id, + author_id: _.find(DataGenerator.Content.users, {email: 'jbOgendAth@example.com'}).id, sort_order: 1 }, {