From 0e6c66ac2002136517662399bca9d838c80d9739 Mon Sep 17 00:00:00 2001 From: Matijs van Zuijlen Date: Sun, 13 Feb 2022 13:54:23 +0100 Subject: [PATCH] Do not allow comments on Article if not published --- publify_core/app/models/article.rb | 2 +- .../spec/controllers/comments_controller_spec.rb | 15 +++++++++++++++ publify_core/spec/models/article_spec.rb | 14 +++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/publify_core/app/models/article.rb b/publify_core/app/models/article.rb index 548d959657..2bef0b2238 100644 --- a/publify_core/app/models/article.rb +++ b/publify_core/app/models/article.rb @@ -204,7 +204,7 @@ def notify_user_via_email(user) end def comments_closed? - !(allow_comments? && in_feedback_window?) + !(allow_comments? && published? && in_feedback_window?) end def html_urls diff --git a/publify_core/spec/controllers/comments_controller_spec.rb b/publify_core/spec/controllers/comments_controller_spec.rb index f53178f418..dd926ea97f 100644 --- a/publify_core/spec/controllers/comments_controller_spec.rb +++ b/publify_core/spec/controllers/comments_controller_spec.rb @@ -58,6 +58,21 @@ expect(response.body).to have_text "content" end end + + it "does not allow commenting if article does not allow comments" do + no_comments = create(:article, allow_comments: false) + expect do + post :create, xhr: true, params: { comment: comment_params, + article_id: no_comments.id } + end.not_to change(no_comments.comments, :count) + end + + it "does not allow commenting if article is draft" do + draft = create(:article, state: "draft") + expect do + post :create, xhr: true, params: { comment: comment_params, article_id: draft.id } + end.not_to change(draft.comments, :count) + end end describe "#preview" do diff --git a/publify_core/spec/models/article_spec.rb b/publify_core/spec/models/article_spec.rb index 0357748eb8..b400f4140f 100644 --- a/publify_core/spec/models/article_spec.rb +++ b/publify_core/spec/models/article_spec.rb @@ -856,8 +856,9 @@ it "returns only published articles" do article = create(:article) create(:comment, article: article) - unpublished_article = create(:article, state: "draft") + unpublished_article = create(:article) create(:comment, article: unpublished_article) + unpublished_article.update!(state: "draft") expect(described_class.published).to eq([article]) expect(described_class.bestof).to eq([article]) end @@ -955,6 +956,17 @@ context "when auto_close setting is zero" do let(:auto_close_value) { 0 } + it "does not allow comments for a draft article" do + art = build :article, state: "draft", blog: blog + assert art.comments_closed? + end + + it "does not allow comments for an article that will be published in the future" do + art = build :article, state: "publication_pending", + published_at: 1.day.from_now, blog: blog + assert art.comments_closed? + end + it "allows comments for a newly published article" do art = build :article, published_at: 1.second.ago, blog: blog assert !art.comments_closed?