2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
require 'topic_view'
|
|
|
|
|
|
|
|
describe TopicView do
|
|
|
|
|
2013-07-22 01:06:53 -04:00
|
|
|
let(:topic) { create_topic }
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:coding_horror) { Fabricate(:coding_horror) }
|
|
|
|
let(:first_poster) { topic.user }
|
2013-03-26 11:58:49 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
let(:topic_view) { TopicView.new(topic.id, coding_horror) }
|
|
|
|
|
|
|
|
it "raises a not found error if the topic doesn't exist" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect { TopicView.new(1231232, coding_horror) }.to raise_error(Discourse::NotFound)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-09-11 14:37:36 -04:00
|
|
|
# see also spec/controllers/topics_controller_spec.rb TopicsController::show::permission errors
|
2013-02-05 14:16:51 -05:00
|
|
|
it "raises an error if the user can't see the topic" do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(topic).returns(false)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect { topic_view }.to raise_error(Discourse::InvalidAccess)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-11 16:38:46 -04:00
|
|
|
it "handles deleted topics" do
|
2014-05-12 15:26:36 -04:00
|
|
|
admin = Fabricate(:admin)
|
|
|
|
topic.trash!(admin)
|
2015-09-11 14:37:36 -04:00
|
|
|
expect { TopicView.new(topic.id, Fabricate(:user)) }.to raise_error(Discourse::InvalidAccess)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect { TopicView.new(topic.id, admin) }.not_to raise_error
|
2013-07-11 16:38:46 -04:00
|
|
|
end
|
|
|
|
|
2014-12-12 11:47:20 -05:00
|
|
|
context "chunk_size" do
|
2014-12-14 18:57:34 -05:00
|
|
|
it "returns `chunk_size` by default" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(topic.id, coding_horror).chunk_size).to eq(TopicView.chunk_size)
|
2014-12-12 11:47:20 -05:00
|
|
|
end
|
|
|
|
|
2014-12-14 18:57:34 -05:00
|
|
|
it "returns `slow_chunk_size` when slow_platform is true" do
|
2014-12-12 11:47:20 -05:00
|
|
|
tv = TopicView.new(topic.id, coding_horror, slow_platform: true)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(tv.chunk_size).to eq(TopicView.slow_chunk_size)
|
2014-12-12 11:47:20 -05:00
|
|
|
end
|
2016-08-07 19:01:23 -04:00
|
|
|
|
|
|
|
it "returns `print_chunk_size` when print param is true" do
|
|
|
|
tv = TopicView.new(topic.id, coding_horror, print: true)
|
|
|
|
expect(tv.chunk_size).to eq(TopicView.print_chunk_size)
|
|
|
|
end
|
2014-12-12 11:47:20 -05:00
|
|
|
end
|
2013-07-11 16:38:46 -04:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
context "with a few sample posts" do
|
2013-03-28 01:53:11 -04:00
|
|
|
let!(:p1) { Fabricate(:post, topic: topic, user: first_poster, percent_rank: 1 )}
|
|
|
|
let!(:p2) { Fabricate(:post, topic: topic, user: coding_horror, percent_rank: 0.5 )}
|
|
|
|
let!(:p3) { Fabricate(:post, topic: topic, user: first_poster, percent_rank: 0 )}
|
|
|
|
|
2013-07-05 16:07:24 -04:00
|
|
|
let(:moderator) { Fabricate(:moderator) }
|
2016-05-17 13:03:08 -04:00
|
|
|
let(:admin) { Fabricate(:admin) }
|
|
|
|
|
2013-05-22 01:04:30 -04:00
|
|
|
it "it can find the best responses" do
|
2013-07-02 20:21:10 -04:00
|
|
|
|
2013-06-28 12:20:06 -04:00
|
|
|
best2 = TopicView.new(topic.id, coding_horror, best: 2)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best2.posts.count).to eq(2)
|
|
|
|
expect(best2.posts[0].id).to eq(p2.id)
|
|
|
|
expect(best2.posts[1].id).to eq(p3.id)
|
2013-05-22 01:04:30 -04:00
|
|
|
|
|
|
|
topic.update_status('closed', true, Fabricate(:admin))
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic.posts.count).to eq(4)
|
2013-05-22 01:04:30 -04:00
|
|
|
|
|
|
|
# should not get the status post
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(2)
|
|
|
|
expect(best.filtered_post_ids.size).to eq(3)
|
|
|
|
expect(best.current_post_ids).to match_array([p2.id, p3.id])
|
2013-07-01 07:29:45 -04:00
|
|
|
|
|
|
|
# should get no results for trust level too low
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, min_trust_level: coding_horror.trust_level + 1)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-01 07:29:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
# should filter out the posts with a score that is too low
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, min_score: 99)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-01 07:29:45 -04:00
|
|
|
|
|
|
|
# should filter out everything if min replies not met
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, min_replies: 99)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-02 20:21:10 -04:00
|
|
|
|
|
|
|
# should punch through posts if the score is high enough
|
|
|
|
p2.update_column(:score, 100)
|
|
|
|
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, bypass_trust_level_score: 100, min_trust_level: coding_horror.trust_level + 1)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(1)
|
2013-07-02 20:21:10 -04:00
|
|
|
|
|
|
|
# 0 means ignore
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, bypass_trust_level_score: 0, min_trust_level: coding_horror.trust_level + 1)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-05 16:07:24 -04:00
|
|
|
|
|
|
|
# If we restrict to posts a moderator liked, return none
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, only_moderator_liked: true)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-05 16:07:24 -04:00
|
|
|
|
|
|
|
# It doesn't count likes from admins
|
|
|
|
PostAction.act(admin, p3, PostActionType.types[:like])
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, only_moderator_liked: true)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(0)
|
2013-07-05 16:07:24 -04:00
|
|
|
|
|
|
|
# It should find the post liked by the moderator
|
|
|
|
PostAction.act(moderator, p2, PostActionType.types[:like])
|
|
|
|
best = TopicView.new(topic.id, nil, best: 99, only_moderator_liked: true)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(best.posts.count).to eq(1)
|
2013-07-05 16:07:24 -04:00
|
|
|
|
2013-03-28 01:53:11 -04:00
|
|
|
end
|
2013-03-26 11:58:49 -04:00
|
|
|
|
2013-03-28 01:53:11 -04:00
|
|
|
it "raises NotLoggedIn if the user isn't logged in and is trying to view a private message" do
|
|
|
|
Topic.any_instance.expects(:private_message?).returns(true)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect { TopicView.new(topic.id, nil) }.to raise_error(Discourse::NotLoggedIn)
|
2013-03-28 01:53:11 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
it "provides an absolute url" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.absolute_url).to be_present
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-03-07 17:31:06 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
it "provides a summary of the first post" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.summary).to be_present
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-03-07 17:31:06 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
describe "#get_canonical_path" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:topic) { Fabricate(:topic) }
|
|
|
|
let(:path) { "/1234" }
|
2013-02-10 13:50:26 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
before do
|
2013-07-22 01:06:53 -04:00
|
|
|
topic.stubs(:relative_url).returns(path)
|
|
|
|
TopicView.any_instance.stubs(:find_topic).with(1234).returns(topic)
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-10 13:50:26 -05:00
|
|
|
|
2013-07-22 01:06:53 -04:00
|
|
|
it "generates canonical path correctly" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(1234, user).canonical_path).to eql(path)
|
|
|
|
expect(TopicView.new(1234, user, page: 5).canonical_path).to eql("/1234?page=5")
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-10 13:50:26 -05:00
|
|
|
|
2013-07-22 01:06:53 -04:00
|
|
|
it "generates a canonical correctly for paged results" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(1234, user, post_number: 10 * TopicView.chunk_size )
|
|
|
|
.canonical_path).to eql("/1234?page=10")
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
describe "#next_page" do
|
2013-07-05 14:45:54 -04:00
|
|
|
let(:p2) { stub(post_number: 2) }
|
2013-03-26 11:58:49 -04:00
|
|
|
let(:topic) do
|
2013-07-22 01:06:53 -04:00
|
|
|
topic = create_topic
|
2013-03-26 11:58:49 -04:00
|
|
|
topic.stubs(:highest_post_number).returns(5)
|
|
|
|
topic
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
2013-03-26 11:58:49 -04:00
|
|
|
let(:user) { Fabricate(:user) }
|
2013-02-10 13:50:26 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
before do
|
2013-07-22 01:06:53 -04:00
|
|
|
TopicView.any_instance.expects(:find_topic).with(1234).returns(topic)
|
|
|
|
TopicView.any_instance.stubs(:filter_posts)
|
|
|
|
TopicView.any_instance.stubs(:last_post).returns(p2)
|
2014-12-14 18:57:34 -05:00
|
|
|
TopicView.stubs(:chunk_size).returns(2)
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-10 13:50:26 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
it "should return the next page" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(1234, user).next_page).to eql(2)
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
|
|
|
|
2013-03-26 14:06:24 -04:00
|
|
|
context '.post_counts_by_user' do
|
2013-03-26 11:58:49 -04:00
|
|
|
it 'returns the two posters with their counts' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.post_counts_by_user.to_a).to match_array([[first_poster.id, 2], [coding_horror.id, 1]])
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2015-01-15 15:39:26 -05:00
|
|
|
|
|
|
|
it "doesn't return counts for posts with authors who have been deleted" do
|
|
|
|
p2.user_id = nil
|
|
|
|
p2.save!
|
|
|
|
expect(topic_view.post_counts_by_user.to_a).to match_array([[first_poster.id, 2]])
|
|
|
|
end
|
2013-02-10 13:50:26 -05:00
|
|
|
end
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
context '.participants' do
|
|
|
|
it 'returns the two participants hashed by id' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.participants.to_a).to match_array([[first_poster.id, first_poster], [coding_horror.id, coding_horror]])
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
context '.all_post_actions' do
|
|
|
|
it 'is blank at first' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.all_post_actions).to be_blank
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
it 'returns the like' do
|
|
|
|
PostAction.act(coding_horror, p1, PostActionType.types[:like])
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.all_post_actions[p1.id][PostActionType.types[:like]]).to be_present
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-08-04 11:29:01 -04:00
|
|
|
context '.all_active_flags' do
|
|
|
|
it 'is blank at first' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.all_active_flags).to be_blank
|
2014-08-04 11:29:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the active flags' do
|
|
|
|
PostAction.act(moderator, p1, PostActionType.types[:off_topic])
|
|
|
|
PostAction.act(coding_horror, p1, PostActionType.types[:off_topic])
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.all_active_flags[p1.id][PostActionType.types[:off_topic]].count).to eq(2)
|
2014-08-04 11:29:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only the active flags' do
|
|
|
|
PostAction.act(moderator, p1, PostActionType.types[:off_topic])
|
|
|
|
PostAction.act(coding_horror, p1, PostActionType.types[:off_topic])
|
|
|
|
|
|
|
|
PostAction.defer_flags!(p1, moderator)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.all_active_flags[p1.id]).to eq(nil)
|
2014-08-04 11:29:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
context '.read?' do
|
2013-07-22 01:06:53 -04:00
|
|
|
it 'tracks correctly' do
|
2014-06-02 21:48:52 -04:00
|
|
|
# anon is assumed to have read everything
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(topic.id).read?(1)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-07-22 01:06:53 -04:00
|
|
|
# random user has nothing
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.read?(1)).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-04-14 12:05:09 -04:00
|
|
|
coding_horror.created_at = 2.days.ago
|
|
|
|
|
2013-07-22 01:06:53 -04:00
|
|
|
# a real user that just read it should have it marked
|
|
|
|
PostTiming.process_timings(coding_horror, topic.id, 1, [[1,1000]])
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(topic.id, coding_horror).read?(1)).to eq(true)
|
|
|
|
expect(TopicView.new(topic.id, coding_horror).topic_user).to be_present
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
context '.topic_user' do
|
|
|
|
it 'returns nil when there is no user' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(topic.id, nil).topic_user).to be_blank
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
context '#recent_posts' do
|
|
|
|
before do
|
2013-08-20 15:45:58 -04:00
|
|
|
24.times do |t| # our let()s have already created 3
|
|
|
|
Fabricate(:post, topic: topic, user: first_poster, created_at: t.seconds.from_now)
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
|
|
|
end
|
2013-08-20 15:45:58 -04:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
it 'returns at most 25 recent posts ordered newest first' do
|
|
|
|
recent_posts = topic_view.recent_posts
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
# count
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(recent_posts.count).to eq(25)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
# ordering
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(recent_posts.include?(p1)).to eq(false)
|
|
|
|
expect(recent_posts.include?(p3)).to eq(true)
|
|
|
|
expect(recent_posts.first.created_at).to be > recent_posts.last.created_at
|
2013-03-26 11:58:49 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-09-10 16:01:23 -04:00
|
|
|
context 'whispers' do
|
|
|
|
it "handles their visibility properly" do
|
|
|
|
p1 = Fabricate(:post, topic: topic, user: coding_horror)
|
|
|
|
p2 = Fabricate(:post, topic: topic, user: coding_horror, post_type: Post.types[:whisper])
|
|
|
|
p3 = Fabricate(:post, topic: topic, user: coding_horror)
|
|
|
|
|
|
|
|
ch_posts = TopicView.new(topic.id, coding_horror).posts
|
|
|
|
expect(ch_posts.map(&:id)).to eq([p1.id, p2.id, p3.id])
|
|
|
|
|
|
|
|
anon_posts = TopicView.new(topic.id).posts
|
|
|
|
expect(anon_posts.map(&:id)).to eq([p1.id, p3.id])
|
|
|
|
|
|
|
|
admin_posts = TopicView.new(topic.id, Fabricate(:moderator)).posts
|
|
|
|
expect(admin_posts.map(&:id)).to eq([p1.id, p2.id, p3.id])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context '.posts' do
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
# Create the posts in a different order than the sort_order
|
|
|
|
let!(:p5) { Fabricate(:post, topic: topic, user: coding_horror)}
|
|
|
|
let!(:p2) { Fabricate(:post, topic: topic, user: coding_horror)}
|
2013-09-03 17:19:29 -04:00
|
|
|
let!(:p6) { Fabricate(:post, topic: topic, user: Fabricate(:user), deleted_at: Time.now)}
|
2013-03-26 11:58:49 -04:00
|
|
|
let!(:p4) { Fabricate(:post, topic: topic, user: coding_horror, deleted_at: Time.now)}
|
|
|
|
let!(:p1) { Fabricate(:post, topic: topic, user: first_poster)}
|
2014-07-15 17:02:43 -04:00
|
|
|
let!(:p7) { Fabricate(:post, topic: topic, user: coding_horror, deleted_at: Time.now)}
|
2013-03-26 11:58:49 -04:00
|
|
|
let!(:p3) { Fabricate(:post, topic: topic, user: first_poster)}
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
before do
|
2014-12-14 18:57:34 -05:00
|
|
|
TopicView.stubs(:chunk_size).returns(3)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
# Update them to the sort order we're checking for
|
2014-07-15 17:02:43 -04:00
|
|
|
[p1, p2, p3, p4, p5, p6, p7].each_with_index do |p, idx|
|
2013-03-26 11:58:49 -04:00
|
|
|
p.sort_order = idx + 1
|
|
|
|
p.save
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-09-03 17:19:29 -04:00
|
|
|
p6.user_id = nil # user got nuked
|
|
|
|
p6.save!
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-12-04 15:56:09 -05:00
|
|
|
describe "contains_gaps?" do
|
2014-05-29 07:55:55 -04:00
|
|
|
it "works" do
|
|
|
|
# does not contain contains_gaps with default filtering
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.contains_gaps?).to eq(false)
|
2014-05-29 07:55:55 -04:00
|
|
|
# contains contains_gaps when filtered by username" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(topic.id, coding_horror, username_filters: ['eviltrout']).contains_gaps?).to eq(true)
|
2014-05-29 07:55:55 -04:00
|
|
|
# contains contains_gaps when filtered by summary
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(topic.id, coding_horror, filter: 'summary').contains_gaps?).to eq(true)
|
2014-05-29 07:55:55 -04:00
|
|
|
# contains contains_gaps when filtered by best
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(topic.id, coding_horror, best: 5).contains_gaps?).to eq(true)
|
2013-12-04 15:56:09 -05:00
|
|
|
end
|
2014-05-29 07:55:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "#restricts to correct topic" do
|
|
|
|
t2 = Fabricate(:topic)
|
|
|
|
|
|
|
|
category = Fabricate(:category, name: "my test")
|
|
|
|
category.set_permissions(Group[:admins] => :full)
|
|
|
|
category.save
|
|
|
|
|
|
|
|
topic.category_id = category.id
|
|
|
|
topic.save!
|
|
|
|
|
|
|
|
expect{
|
|
|
|
TopicView.new(topic.id, coding_horror).posts.count
|
|
|
|
}.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(TopicView.new(t2.id, coding_horror, post_ids: [p1.id,p2.id]).posts.count).to eq(0)
|
2013-12-04 15:56:09 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-05-18 16:11:01 -04:00
|
|
|
describe '#filter_posts_paged' do
|
2014-12-14 18:57:34 -05:00
|
|
|
before { TopicView.stubs(:chunk_size).returns(2) }
|
2013-05-18 16:11:01 -04:00
|
|
|
|
2013-05-19 20:29:49 -04:00
|
|
|
it 'returns correct posts for all pages' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(topic_view.filter_posts_paged(1)).to eq([p1, p2])
|
|
|
|
expect(topic_view.filter_posts_paged(2)).to eq([p3, p5])
|
|
|
|
expect(topic_view.filter_posts_paged(3)).to eq([])
|
|
|
|
expect(topic_view.filter_posts_paged(100)).to eq([])
|
2013-05-18 16:11:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
describe "filter_posts_near" do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-07-15 17:02:43 -04:00
|
|
|
def topic_view_near(post, show_deleted=false)
|
|
|
|
TopicView.new(topic.id, coding_horror, post_number: post.post_number, show_deleted: show_deleted)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
it "snaps to the lower boundary" do
|
2013-03-26 14:18:35 -04:00
|
|
|
near_view = topic_view_near(p1)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.desired_post).to eq(p1)
|
|
|
|
expect(near_view.posts).to eq([p1, p2, p3])
|
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
it "snaps to the upper boundary" do
|
2013-03-26 14:18:35 -04:00
|
|
|
near_view = topic_view_near(p5)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.desired_post).to eq(p5)
|
|
|
|
expect(near_view.posts).to eq([p2, p3, p5])
|
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-03-26 11:58:49 -04:00
|
|
|
it "returns the posts in the middle" do
|
2013-03-26 14:18:35 -04:00
|
|
|
near_view = topic_view_near(p2)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.desired_post).to eq(p2)
|
|
|
|
expect(near_view.posts).to eq([p1, p2, p3])
|
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-15 17:02:43 -04:00
|
|
|
it "gaps deleted posts to an admin" do
|
2013-03-26 11:58:49 -04:00
|
|
|
coding_horror.admin = true
|
2013-03-26 14:18:35 -04:00
|
|
|
near_view = topic_view_near(p3)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.desired_post).to eq(p3)
|
|
|
|
expect(near_view.posts).to eq([p2, p3, p5])
|
|
|
|
expect(near_view.gaps.before).to eq({p5.id => [p4.id]})
|
|
|
|
expect(near_view.gaps.after).to eq({p5.id => [p6.id, p7.id]})
|
2014-07-15 17:02:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns deleted posts to an admin with show_deleted" do
|
|
|
|
coding_horror.admin = true
|
|
|
|
near_view = topic_view_near(p3, true)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.desired_post).to eq(p3)
|
|
|
|
expect(near_view.posts).to eq([p2, p3, p4])
|
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-02-21 13:20:00 -05:00
|
|
|
end
|
|
|
|
|
2014-07-15 17:02:43 -04:00
|
|
|
it "gaps deleted posts by nuked users to an admin" do
|
2013-09-03 17:19:29 -04:00
|
|
|
coding_horror.admin = true
|
|
|
|
near_view = topic_view_near(p5)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.desired_post).to eq(p5)
|
2014-07-15 17:02:43 -04:00
|
|
|
# note: both p4 and p6 get skipped
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.posts).to eq([p2, p3, p5])
|
|
|
|
expect(near_view.gaps.before).to eq({p5.id => [p4.id]})
|
|
|
|
expect(near_view.gaps.after).to eq({p5.id => [p6.id, p7.id]})
|
2014-07-15 17:02:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns deleted posts by nuked users to an admin with show_deleted" do
|
|
|
|
coding_horror.admin = true
|
|
|
|
near_view = topic_view_near(p5, true)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.desired_post).to eq(p5)
|
|
|
|
expect(near_view.posts).to eq([p4, p5, p6])
|
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
|
|
|
|
2013-05-18 16:11:01 -04:00
|
|
|
context "when 'posts per page' exceeds the number of posts" do
|
2014-12-14 18:57:34 -05:00
|
|
|
before { TopicView.stubs(:chunk_size).returns(100) }
|
2013-02-21 13:20:00 -05:00
|
|
|
|
2013-05-18 16:11:01 -04:00
|
|
|
it 'returns all the posts' do
|
|
|
|
near_view = topic_view_near(p5)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.posts).to eq([p1, p2, p3, p5])
|
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-05-18 16:11:01 -04:00
|
|
|
end
|
2013-09-03 17:19:29 -04:00
|
|
|
|
2014-07-15 17:02:43 -04:00
|
|
|
it 'gaps deleted posts to admins' do
|
2013-09-03 17:19:29 -04:00
|
|
|
coding_horror.admin = true
|
|
|
|
near_view = topic_view_near(p5)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.posts).to eq([p1, p2, p3, p5])
|
|
|
|
expect(near_view.gaps.before).to eq({p5.id => [p4.id]})
|
|
|
|
expect(near_view.gaps.after).to eq({p5.id => [p6.id, p7.id]})
|
2014-07-15 17:02:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns deleted posts to admins' do
|
|
|
|
coding_horror.admin = true
|
|
|
|
near_view = topic_view_near(p5, true)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(near_view.posts).to eq([p1, p2, p3, p4, p5, p6, p7])
|
|
|
|
expect(near_view.contains_gaps?).to eq(false)
|
2013-09-03 17:19:29 -04:00
|
|
|
end
|
2013-05-18 16:11:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-07 16:55:42 -05:00
|
|
|
|
|
|
|
context "page_title" do
|
|
|
|
let(:tag1) { Fabricate(:tag) }
|
|
|
|
let(:tag2) { Fabricate(:tag, topic_count: 2) }
|
|
|
|
|
|
|
|
subject { TopicView.new(topic.id, coding_horror).page_title }
|
|
|
|
|
|
|
|
context "uncategorized topic" do
|
|
|
|
context "topic_page_title_includes_category is false" do
|
|
|
|
before { SiteSetting.topic_page_title_includes_category = false }
|
|
|
|
it { should eq(topic.title) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "topic_page_title_includes_category is true" do
|
|
|
|
before { SiteSetting.topic_page_title_includes_category = true }
|
|
|
|
it { should eq(topic.title) }
|
|
|
|
|
|
|
|
context "tagged topic" do
|
|
|
|
before { topic.tags << [tag1, tag2] }
|
|
|
|
|
|
|
|
context "tagging enabled" do
|
|
|
|
before { SiteSetting.tagging_enabled = true }
|
|
|
|
|
|
|
|
it { should start_with(topic.title) }
|
|
|
|
it { should_not include(tag1.name) }
|
|
|
|
it { should end_with(tag2.name) } # tag2 has higher topic count
|
|
|
|
end
|
|
|
|
|
|
|
|
context "tagging disabled" do
|
|
|
|
before { SiteSetting.tagging_enabled = false }
|
|
|
|
|
|
|
|
it { should start_with(topic.title) }
|
|
|
|
it { should_not include(tag1.name) }
|
|
|
|
it { should_not include(tag2.name) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "categorized topic" do
|
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
|
|
|
|
before { topic.update_attributes(category_id: category.id) }
|
|
|
|
|
|
|
|
context "topic_page_title_includes_category is false" do
|
|
|
|
before { SiteSetting.topic_page_title_includes_category = false }
|
|
|
|
it { should eq(topic.title) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "topic_page_title_includes_category is true" do
|
|
|
|
before { SiteSetting.topic_page_title_includes_category = true }
|
|
|
|
it { should start_with(topic.title) }
|
|
|
|
it { should end_with(category.name) }
|
|
|
|
|
|
|
|
context "tagged topic" do
|
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
topic.tags << [tag1, tag2]
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should start_with(topic.title) }
|
|
|
|
it { should end_with(category.name) }
|
|
|
|
it { should_not include(tag1.name) }
|
|
|
|
it { should_not include(tag2.name) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|