2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
describe CategoryFeaturedTopic do
|
|
|
|
|
2014-12-31 08:55:03 -06:00
|
|
|
it { is_expected.to belong_to :category }
|
|
|
|
it { is_expected.to belong_to :topic }
|
2013-02-25 10:42:20 -06:00
|
|
|
|
2013-06-20 15:07:53 -05:00
|
|
|
context 'feature_topics_for' do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:category) { Fabricate(:category) }
|
2013-07-17 17:58:25 -05:00
|
|
|
let!(:category_post) { PostCreator.create(user, raw: "I put this post in the category", title: "categorize THIS", category: category.id) }
|
2013-06-04 20:22:47 -05:00
|
|
|
|
2019-07-16 06:41:03 -05:00
|
|
|
before do
|
|
|
|
CategoryFeaturedTopic.clear_exclude_category_ids
|
|
|
|
end
|
|
|
|
|
2017-12-19 20:58:05 -06:00
|
|
|
it "works in batched mode" do
|
|
|
|
category2 = Fabricate(:category)
|
|
|
|
post2 = create_post(category: category2.id)
|
|
|
|
|
|
|
|
CategoryFeaturedTopic.destroy_all
|
|
|
|
CategoryFeaturedTopic.clear_batch!
|
|
|
|
|
|
|
|
size = Category.order(:id).where('id < ?', category.id).count + 1
|
|
|
|
|
|
|
|
CategoryFeaturedTopic.feature_topics(batched: true, batch_size: size)
|
|
|
|
|
|
|
|
expect(CategoryFeaturedTopic.where(topic_id: category_post.topic_id).count).to eq(1)
|
|
|
|
expect(CategoryFeaturedTopic.where(topic_id: post2.topic_id).count).to eq(0)
|
|
|
|
|
|
|
|
CategoryFeaturedTopic.feature_topics(batched: true, batch_size: size)
|
|
|
|
|
|
|
|
expect(CategoryFeaturedTopic.where(topic_id: post2.topic_id).count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2013-06-20 15:07:53 -05:00
|
|
|
it "should feature topics for a secure category" do
|
2013-06-04 20:22:47 -05:00
|
|
|
|
2013-06-20 15:07:53 -05:00
|
|
|
# so much dancing, I am thinking fixures make sense here.
|
2014-09-05 00:20:39 -05:00
|
|
|
user.change_trust_level!(TrustLevel[1])
|
2013-06-04 20:22:47 -05:00
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
category.set_permissions(trust_level_1: :full)
|
2013-06-20 15:07:53 -05:00
|
|
|
category.save
|
2013-06-04 20:22:47 -05:00
|
|
|
|
2014-09-05 00:20:39 -05:00
|
|
|
_uncategorized_post = PostCreator.create(user, raw: "this is my new post 123 post", title: "hello world")
|
2013-06-04 20:22:47 -05:00
|
|
|
|
2013-06-20 15:07:53 -05:00
|
|
|
CategoryFeaturedTopic.feature_topics_for(category)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(CategoryFeaturedTopic.count).to be(1)
|
2013-06-20 15:07:53 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not include invisible topics' do
|
2013-07-17 17:58:25 -05:00
|
|
|
invisible_post = PostCreator.create(user, raw: "Don't look at this post because it's awful.", title: "not visible to anyone", category: category.id)
|
2013-06-20 15:07:53 -05:00
|
|
|
invisible_post.topic.update_status('visible', false, Fabricate(:admin))
|
|
|
|
CategoryFeaturedTopic.feature_topics_for(category)
|
2014-12-31 08:55:03 -06:00
|
|
|
expect(CategoryFeaturedTopic.count).to be(1)
|
2013-06-20 15:07:53 -05:00
|
|
|
end
|
2015-02-24 21:24:25 -06:00
|
|
|
|
2019-07-15 09:32:03 -05:00
|
|
|
it 'should not include topics from suppressed categories' do
|
|
|
|
CategoryFeaturedTopic.feature_topics_for(category)
|
|
|
|
expect(
|
|
|
|
CategoryFeaturedTopic.where(category_id: category.id).order('rank asc').pluck(:topic_id)
|
|
|
|
).to contain_exactly(category_post.topic.id)
|
|
|
|
|
|
|
|
category.update(suppress_from_latest: true)
|
|
|
|
|
|
|
|
CategoryFeaturedTopic.feature_topics_for(category)
|
|
|
|
expect(
|
|
|
|
CategoryFeaturedTopic.where(category_id: category.id).order('rank asc').pluck(:topic_id)
|
|
|
|
).to_not contain_exactly(category_post.topic.id)
|
|
|
|
end
|
|
|
|
|
2015-02-24 21:24:25 -06:00
|
|
|
it 'should feature stuff in the correct order' do
|
2017-03-01 11:03:12 -06:00
|
|
|
category = Fabricate(:category, num_featured_topics: 2)
|
2017-08-17 14:26:31 -05:00
|
|
|
_t5 = Fabricate(:topic, category_id: category.id, bumped_at: 12.minutes.ago)
|
2015-06-10 13:36:47 -05:00
|
|
|
t4 = Fabricate(:topic, category_id: category.id, bumped_at: 10.minutes.ago)
|
|
|
|
t3 = Fabricate(:topic, category_id: category.id, bumped_at: 7.minutes.ago)
|
2015-02-24 21:24:25 -06:00
|
|
|
t2 = Fabricate(:topic, category_id: category.id, bumped_at: 4.minutes.ago)
|
|
|
|
t1 = Fabricate(:topic, category_id: category.id, bumped_at: 5.minutes.ago)
|
|
|
|
pinned = Fabricate(:topic, category_id: category.id, pinned_at: 10.minutes.ago, bumped_at: 10.minutes.ago)
|
|
|
|
|
|
|
|
CategoryFeaturedTopic.feature_topics_for(category)
|
|
|
|
|
2017-03-01 11:03:12 -06:00
|
|
|
# Should find more than we need: pinned topics first, then num_featured_topics * 2
|
2015-02-24 21:24:25 -06:00
|
|
|
expect(
|
2017-08-17 14:26:31 -05:00
|
|
|
CategoryFeaturedTopic.where(category_id: category.id).order('rank asc').pluck(:topic_id)
|
2015-09-21 14:14:05 -05:00
|
|
|
).to eq([pinned.id, t2.id, t1.id, t3.id, t4.id])
|
2015-02-24 21:24:25 -06:00
|
|
|
|
|
|
|
end
|
2013-06-04 20:22:47 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|