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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  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(
  27. :create,
  28. :params => {
  29. :id => 1,
  30. :comment => {
  31. :comments => 'This is a test comment'
  32. }
  33. }
  34. )
  35. assert_redirected_to '/news/1'
  36. comment = News.find(1).comments.last
  37. assert_not_nil comment
  38. assert_equal 'This is a test comment', comment.comments
  39. assert_equal User.find(2), comment.author
  40. end
  41. def test_empty_comment_should_not_be_added
  42. @request.session[:user_id] = 2
  43. assert_no_difference 'Comment.count' do
  44. post(
  45. :create,
  46. :params => {
  47. :id => 1,
  48. :comment => {
  49. :comments => ''
  50. }
  51. }
  52. )
  53. assert_response :redirect
  54. assert_redirected_to '/news/1'
  55. end
  56. end
  57. def test_create_should_be_denied_if_news_is_not_commentable
  58. News.any_instance.stubs(:commentable?).returns(false)
  59. @request.session[:user_id] = 2
  60. assert_no_difference 'Comment.count' do
  61. post(
  62. :create,
  63. :params => {
  64. :id => 1,
  65. :comment => {
  66. :comments => 'This is a test comment'
  67. }
  68. }
  69. )
  70. assert_response 403
  71. end
  72. end
  73. def test_destroy_comment
  74. comments_count = News.find(1).comments.size
  75. @request.session[:user_id] = 2
  76. delete(
  77. :destroy,
  78. :params => {
  79. :id => 1,
  80. :comment_id => 2
  81. }
  82. )
  83. assert_redirected_to '/news/1'
  84. assert_nil Comment.find_by_id(2)
  85. assert_equal comments_count - 1, News.find(1).comments.size
  86. end
  87. end