diff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rb new file mode 100644 index 00000000000..a22f92d8766 --- /dev/null +++ b/spec/components/post_destroyer_spec.rb @@ -0,0 +1,146 @@ +require 'spec_helper' +require 'post_destroyer' + +describe PostDestroyer do + + let(:moderator) { Fabricate(:moderator) } + let(:post) { Fabricate(:post) } + + describe 'basic destroying' do + + let(:moderator) { Fabricate(:moderator) } + + context "as the creator of the post" do + before do + PostDestroyer.new(post.user, post).destroy + post.reload + end + + it "doesn't delete the post" do + post.deleted_at.should be_blank + end + + it "updates the text of the post" do + post.raw.should == I18n.t('js.post.deleted_by_author') + end + + it "creates a new version" do + post.version.should == 2 + end + end + + context "as a moderator" do + before do + PostDestroyer.new(moderator, post).destroy + end + + it "deletes the post" do + post.deleted_at.should be_present + end + end + + end + + context 'deleting the second post in a topic' do + + let(:user) { Fabricate(:user) } + let!(:post) { Fabricate(:post, user: user) } + let(:topic) { post.topic } + let(:second_user) { Fabricate(:coding_horror) } + let!(:second_post) { Fabricate(:post, topic: topic, user: second_user) } + + before do + PostDestroyer.new(moderator, second_post).destroy + end + + it 'resets the last_poster_id back to the OP' do + topic.last_post_user_id.should == user.id + end + + it 'resets the last_posted_at back to the OP' do + topic.last_posted_at.to_i.should == post.created_at.to_i + end + + context 'topic_user' do + + let(:topic_user) { second_user.topic_users.where(topic_id: topic.id).first } + + it 'clears the posted flag for the second user' do + topic_user.posted?.should be_false + end + + it "sets the second user's last_read_post_number back to 1" do + topic_user.last_read_post_number.should == 1 + end + + it "sets the second user's last_read_post_number back to 1" do + topic_user.seen_post_count.should == 1 + end + + end + + end + + describe 'after delete' do + + let!(:coding_horror) { Fabricate(:coding_horror) } + let!(:post) { Fabricate(:post, raw: "Hello @CodingHorror") } + + it "should feature the users again (in case they've changed)" do + Jobs.expects(:enqueue).with(:feature_topic_users, has_entries(topic_id: post.topic_id, except_post_id: post.id)) + PostDestroyer.new(moderator, post).destroy + end + + describe 'with a reply' do + + let!(:reply) { Fabricate(:basic_reply, user: coding_horror, topic: post.topic) } + let!(:post_reply) { PostReply.create(post_id: post.id, reply_id: reply.id) } + + it 'changes the post count of the topic' do + post.reload + lambda { + PostDestroyer.new(moderator, reply).destroy + post.topic.reload + }.should change(post.topic, :posts_count).by(-1) + end + + it 'lowers the reply_count when the reply is deleted' do + lambda { + PostDestroyer.new(moderator, reply).destroy + }.should change(post.post_replies, :count).by(-1) + end + + it 'should increase the post_number when there are deletion gaps' do + PostDestroyer.new(moderator, reply).destroy + p = Fabricate(:post, user: post.user, topic: post.topic) + p.post_number.should == 3 + end + + end + + end + + context '@mentions' do + let!(:evil_trout) { Fabricate(:evil_trout) } + let!(:mention_post) { Fabricate(:post, raw: 'Hello @eviltrout')} + + it 'removes notifications when deleted' do + lambda { + PostDestroyer.new(Fabricate(:moderator), mention_post).destroy + }.should change(evil_trout.notifications, :count).by(-1) + end + end + + describe "flag counts" do + let(:codinghorror) { Fabricate(:coding_horror) } + let(:bookmark) { PostAction.new(user_id: post.user_id, post_action_type_id: PostActionType.types[:bookmark] , post_id: post.id) } + + it "should reset counts when a post is deleted" do + second_post = Fabricate(:post, topic_id: post.topic_id) + PostAction.act(codinghorror, second_post, PostActionType.types[:off_topic]) + -> { PostDestroyer.new(moderator, second_post).destroy }.should change(PostAction, :flagged_posts_count).by(-1) + end + end + +end + diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 67b78edb575..70a0d4af60e 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -35,13 +35,6 @@ describe PostAction do PostAction.flagged_posts_count.should == 0 end - it "should reset counts when a post is deleted" do - post2 = Fabricate(:post, topic_id: post.topic_id) - PostAction.act(codinghorror, post2, PostActionType.types[:off_topic]) - PostDestroyer.new(moderator, post2).destroy - PostAction.flagged_posts_count.should == 0 - end - end it "increases the post's bookmark count when saved" do diff --git a/spec/models/post_alert_observer_spec.rb b/spec/models/post_alert_observer_spec.rb index 05e546cf7de..b9366fea62c 100644 --- a/spec/models/post_alert_observer_spec.rb +++ b/spec/models/post_alert_observer_spec.rb @@ -78,8 +78,6 @@ describe PostAlertObserver do }.should_not change(evil_trout.notifications, :count) end - - it "doesn't notify the user who created the topic in regular mode" do topic.notify_regular!(user) mention_post @@ -88,12 +86,6 @@ describe PostAlertObserver do }.should_not change(user.notifications, :count).by(1) end - it 'removes notifications' do - post = mention_post - lambda { - PostDestroyer.new(Fabricate(:moderator), post).destroy - }.should change(evil_trout.notifications, :count).by(-1) - end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 229f22eaccd..cd20084dd35 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -479,91 +479,6 @@ describe Post do end - describe 'delete_by' do - - let(:moderator) { Fabricate(:moderator) } - let(:post) { Fabricate(:post) } - - context "as the creator of the post" do - - before do - PostDestroyer.new(post.user, post).destroy - post.reload - end - - it "doesn't delete the post" do - post.deleted_at.should be_blank - end - - it "updates the text of the post" do - post.raw.should == I18n.t('js.post.deleted_by_author') - end - - - it "creates a new version" do - post.version.should == 2 - end - - end - - context "as a moderator" do - - before do - PostDestroyer.new(moderator, post).destroy - post.reload - end - - it "deletes the post" do - post.deleted_at.should be_present - end - - end - - end - - describe 'after delete' do - - let(:moderator) { Fabricate(:moderator) } - let!(:coding_horror) { Fabricate(:coding_horror) } - let!(:post) { Fabricate(:post, post_args.merge(raw: "Hello @CodingHorror")) } - - it "should feature the users again (in case they've changed)" do - Jobs.expects(:enqueue).with(:feature_topic_users, has_entries(topic_id: post.topic_id, except_post_id: post.id)) - PostDestroyer.new(moderator, post).destroy - end - - describe 'with a reply' do - - let!(:reply) { Fabricate(:basic_reply, user: coding_horror, topic: post.topic) } - let!(:post_reply) { PostReply.create(post_id: post.id, reply_id: reply.id) } - - it 'changes the post count of the topic' do - post.reload - lambda { - reply.destroy - post.topic.reload - }.should change(post.topic, :posts_count).by(-1) - end - - it 'lowers the reply_count when the reply is deleted' do - lambda { - PostDestroyer.new(moderator, reply).destroy - post.reload - }.should change(post.post_replies, :count).by(-1) - end - - it 'should increase the post_number when there are deletion gaps' do - reply.destroy - p = Fabricate(:post, user: post.user, topic: post.topic) - p.post_number.should == 3 - end - - end - - end - - - describe 'after save' do let(:post) { Fabricate(:post, post_args) } diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index 31ed358ca52..92632dfc75d 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -804,46 +804,6 @@ describe Topic do topic_user = @second_user.topic_users.where(topic_id: @topic.id).first topic_user.posted?.should be_true end - - - context 'after deleting that post' do - - before do - PostDestroyer.new(Fabricate(:moderator), @new_post).destroy - Topic.reset_highest(@topic.id) - @topic.reload - end - - it 'resets the last_poster_id back to the OP' do - @topic.last_post_user_id.should == @user.id - end - - it 'resets the last_posted_at back to the OP' do - @topic.last_posted_at.to_i.should == @post.created_at.to_i - end - - context 'topic_user' do - before do - @topic_user = @second_user.topic_users.where(topic_id: @topic.id).first - end - - it 'clears the posted flag for the second user' do - @topic_user.posted?.should be_false - end - - it "sets the second user's last_read_post_number back to 1" do - @topic_user.last_read_post_number.should == 1 - end - - it "sets the second user's last_read_post_number back to 1" do - @topic_user.seen_post_count.should == 1 - end - - end - - - end - end end