You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comments_controller_test.rb 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class CommentsControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles, :enabled_modules, :news, :comments
  21. def setup
  22. User.current = nil
  23. end
  24. def test_add_comment
  25. @request.session[:user_id] = 2
  26. post :create, :params => {
  27. :id => 1,
  28. :comment => {
  29. :comments => 'This is a test comment'
  30. }
  31. }
  32. assert_redirected_to '/news/1'
  33. comment = News.find(1).comments.last
  34. assert_not_nil comment
  35. assert_equal 'This is a test comment', comment.comments
  36. assert_equal User.find(2), comment.author
  37. end
  38. def test_empty_comment_should_not_be_added
  39. @request.session[:user_id] = 2
  40. assert_no_difference 'Comment.count' do
  41. post :create, :params => {
  42. :id => 1,
  43. :comment => {
  44. :comments => ''
  45. }
  46. }
  47. assert_response :redirect
  48. assert_redirected_to '/news/1'
  49. end
  50. end
  51. def test_create_should_be_denied_if_news_is_not_commentable
  52. News.any_instance.stubs(:commentable?).returns(false)
  53. @request.session[:user_id] = 2
  54. assert_no_difference 'Comment.count' do
  55. post :create, :params => {
  56. :id => 1,
  57. :comment => {
  58. :comments => 'This is a test comment'
  59. }
  60. }
  61. assert_response 403
  62. end
  63. end
  64. def test_destroy_comment
  65. comments_count = News.find(1).comments.size
  66. @request.session[:user_id] = 2
  67. delete :destroy, :params => {
  68. :id => 1,
  69. :comment_id => 2
  70. }
  71. assert_redirected_to '/news/1'
  72. assert_nil Comment.find_by_id(2)
  73. assert_equal comments_count - 1, News.find(1).comments.size
  74. end
  75. end