From fcf46546c34ab0eb8272a892b73a5d0ec7605662 Mon Sep 17 00:00:00 2001 From: Waqas Khalid Date: Tue, 9 Sep 2014 18:14:28 +0500 Subject: [PATCH] Don't allow commenting while the thread is closed When someone closes thread there is still option to add comment which cause error when submitted. User shouldn't be able to comment while the thread is closed. TNL-150 --- .../view/discussion_thread_view_spec.coffee | 26 +++++++++++++++ .../view/thread_response_view_spec.coffee | 33 +++++++++++++++++-- .../views/discussion_thread_view.coffee | 1 + .../views/thread_response_view.coffee | 2 ++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/common/static/coffee/spec/discussion/view/discussion_thread_view_spec.coffee b/common/static/coffee/spec/discussion/view/discussion_thread_view_spec.coffee index 84512cd4e9..01e3bc25c9 100644 --- a/common/static/coffee/spec/discussion/view/discussion_thread_view_spec.coffee +++ b/common/static/coffee/spec/discussion/view/discussion_thread_view_spec.coffee @@ -40,6 +40,32 @@ describe "DiscussionThreadView", -> else expect(view.$el.find(".load-response-button").length).toEqual(0) + describe "closed and open Threads", -> + checkCommentForm = (originallyClosed, mode) -> + threadData = DiscussionViewSpecHelper.makeThreadWithProps({closed: originallyClosed}) + thread = new Thread(threadData) + view = new DiscussionThreadView({ model: thread, el: $("#fixture-element"), mode: mode}) + renderWithContent(view, {resp_total: 1, children: [{}]}) + if mode == "inline" + view.expand() + spyOn(DiscussionUtil, "updateWithUndo").andCallFake( + (model, updates, safeAjaxParams, errorMsg) -> + model.set(updates) + ) + expect(view.$('.comment-form').closest('li').is(":visible")).toBe(not originallyClosed) + expect(view.$(".discussion-reply-new").is(":visible")).toBe(not originallyClosed) + view.$(".action-close").click() + expect(view.$('.comment-form').closest('li').is(":visible")).toBe(originallyClosed) + expect(view.$(".discussion-reply-new").is(":visible")).toBe(originallyClosed) + + _.each(["tab", "inline"], (mode) => + it 'Test that in #{mode} mode when a closed thread is opened the comment form is displayed', -> + checkCommentForm(true, mode) + + it 'Test that in #{mode} mode when a open thread is closed the comment form is hidden', -> + checkCommentForm(false, mode) + ) + describe "tab mode", -> beforeEach -> @view = new DiscussionThreadView({ model: @thread, el: $("#fixture-element"), mode: "tab"}) diff --git a/common/static/coffee/spec/discussion/view/thread_response_view_spec.coffee b/common/static/coffee/spec/discussion/view/thread_response_view_spec.coffee index 24776cc0ca..2b4f125a2a 100644 --- a/common/static/coffee/spec/discussion/view/thread_response_view_spec.coffee +++ b/common/static/coffee/spec/discussion/view/thread_response_view_spec.coffee @@ -3,13 +3,42 @@ describe 'ThreadResponseView', -> DiscussionSpecHelper.setUpGlobals() DiscussionSpecHelper.setUnderscoreFixtures() + @thread = new Thread({"thread_type": "discussion"}) @response = new Comment { - children: [{}, {}] + children: [{}, {}], + thread: @thread, } @view = new ThreadResponseView({model: @response, el: $("#fixture-element")}) spyOn(ThreadResponseShowView.prototype, "render") spyOn(ResponseCommentView.prototype, "render") + describe 'closed and open Threads', -> + checkCommentForm = (closed) -> + thread = new Thread({"thread_type": "discussion", "closed": closed}) + commentData = { + id: "dummy", + user_id: "567", + course_id: "TestOrg/TestCourse/TestRun", + body: "this is a comment", + created_at: "2013-04-03T20:08:39Z", + abuse_flaggers: [], + type: "comment", + children: [], + thread: thread, + } + comment = new Comment(commentData) + view = new ThreadResponseView({ + model: comment, el: $("#fixture-element"), + }) + view.render() + expect(view.$('.comment-form').closest('li').is(":visible")).toBe(not closed) + + it 'hides comment form when thread is closed', -> + checkCommentForm(true) + + it 'show comment form when thread is open', -> + checkCommentForm(false) + describe 'renderComments', -> it 'hides "show comments" link if collapseComments is not set', -> @view.render() @@ -17,7 +46,7 @@ describe 'ThreadResponseView', -> expect(@view.$(".action-show-comments")).not.toBeVisible() it 'hides "show comments" link if collapseComments is set but response has no comments', -> - @response = new Comment { children: [] } + @response = new Comment { children: [], thread: @thread } @view = new ThreadResponseView({ model: @response, el: $("#fixture-element"), collapseComments: true diff --git a/common/static/coffee/src/discussion/views/discussion_thread_view.coffee b/common/static/coffee/src/discussion/views/discussion_thread_view.coffee index 746f506ea9..82652b6370 100644 --- a/common/static/coffee/src/discussion/views/discussion_thread_view.coffee +++ b/common/static/coffee/src/discussion/views/discussion_thread_view.coffee @@ -55,6 +55,7 @@ if Backbone? attrRenderer: $.extend({}, DiscussionContentView.prototype.attrRenderer, { closed: (closed) -> @$(".discussion-reply-new").toggle(not closed) + @$('.comment-form').closest('li').toggle(not closed) @renderAddResponseButton() }) diff --git a/common/static/coffee/src/discussion/views/thread_response_view.coffee b/common/static/coffee/src/discussion/views/thread_response_view.coffee index e6a3412c59..da37313d52 100644 --- a/common/static/coffee/src/discussion/views/thread_response_view.coffee +++ b/common/static/coffee/src/discussion/views/thread_response_view.coffee @@ -28,6 +28,8 @@ if Backbone? @renderShowView() @renderAttrs() + if @model.get("thread").get("closed") + @hideCommentForm() @renderComments() @