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

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