A platform for community discussion. Free, open, simple.
FIX: `Guardian#post_can_act?` shouldn't raise an error if user of post has been deleted.
Guo Xiang Tanfae8757cd48e
diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rbindex 25a5c644ea6..ce3864ecc8a 100644--- a/lib/guardian/post_guardian.rb+++ b/lib/guardian/post_guardian.rb@@ -43,7 +43,7 @@ module PostGuardian# post made by staff, but we don't allow staff flagsreturn false if is_flag &&(!SiteSetting.allow_flagging_staff?) &&- post.user.staff?+ post&.user&.staff?if [:notify_user, :notify_moderators].include?(action_key) &&(!SiteSetting.enable_personal_messages? ||diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rbindex 5a91e78463f..bddb5890316 100644--- a/spec/components/guardian_spec.rb+++ b/spec/components/guardian_spec.rb@@ -120,16 +120,26 @@ describe Guardian doexpect(Guardian.new(user).post_can_act?(staff_post, :spam)).to be_truthyend- it "doesn't allow flagging of staff posts when allow_flagging_staff is false" do- SiteSetting.allow_flagging_staff = false- staff_post = Fabricate(:post, user: Fabricate(:moderator))- expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(false)- end+ describe 'when allow_flagging_staff is false' do+ let(:staff_post) { Fabricate(:post, user: Fabricate(:moderator)) }- it "allows liking of staff when allow_flagging_staff is false" do- SiteSetting.allow_flagging_staff = false- staff_post = Fabricate(:post, user: Fabricate(:moderator))- expect(Guardian.new(user).post_can_act?(staff_post, :like)).to eq(true)+ before do+ SiteSetting.allow_flagging_staff = false+ end++ it "doesn't allow flagging of staff posts" do+ expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(false)+ end++ it "allows flagging of staff posts when staff has been deleted" do+ staff_post.user.destroy!+ staff_post.reload+ expect(Guardian.new(user).post_can_act?(staff_post, :spam)).to eq(true)+ end++ it "allows liking of staff" do+ expect(Guardian.new(user).post_can_act?(staff_post, :like)).to eq(true)+ endendit "returns false when liking yourself" do