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.

news_controller_test.rb 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 NewsControllerTest < Redmine::ControllerTest
  19. fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
  20. :enabled_modules, :news, :comments,
  21. :attachments
  22. def setup
  23. User.current = nil
  24. end
  25. def test_index
  26. get :index
  27. assert_response :success
  28. assert_select 'h3 a', :text => 'eCookbook first release !'
  29. end
  30. def test_index_with_project
  31. get :index, :params => {
  32. :project_id => 1
  33. }
  34. assert_response :success
  35. assert_select 'h3 a', :text => 'eCookbook first release !'
  36. end
  37. def test_index_with_invalid_project_should_respond_with_404
  38. get :index, :params => {
  39. :project_id => 999
  40. }
  41. assert_response 404
  42. end
  43. def test_index_without_permission_should_fail
  44. Role.all.each {|r| r.remove_permission! :view_news}
  45. @request.session[:user_id] = 2
  46. get :index
  47. assert_response 403
  48. end
  49. def test_show
  50. get :show, :params => {
  51. :id => 1
  52. }
  53. assert_response :success
  54. assert_select 'h2', :text => 'eCookbook first release !'
  55. end
  56. def test_show_should_show_attachments
  57. attachment = Attachment.first
  58. attachment.container = News.find(1)
  59. attachment.save!
  60. get :show, :params => {
  61. :id => 1
  62. }
  63. assert_response :success
  64. assert_select 'a', :text => attachment.filename
  65. end
  66. def test_show_with_comments_in_reverse_order
  67. user = User.find(1)
  68. user.pref[:comments_sorting] = 'desc'
  69. user.pref.save!
  70. @request.session[:user_id] = 1
  71. get :show, :params => {
  72. :id => 1
  73. }
  74. assert_response :success
  75. comments = css_select('#comments .wiki').map(&:text).map(&:strip)
  76. assert_equal ["This is an other comment", "my first comment"], comments
  77. end
  78. def test_show_not_found
  79. get :show, :params => {
  80. :id => 999
  81. }
  82. assert_response 404
  83. end
  84. def test_get_new
  85. @request.session[:user_id] = 2
  86. get :new, :params => {
  87. :project_id => 1
  88. }
  89. assert_response :success
  90. assert_select 'input[name=?]', 'news[title]'
  91. end
  92. def test_post_create
  93. ActionMailer::Base.deliveries.clear
  94. @request.session[:user_id] = 2
  95. with_settings :notified_events => %w(news_added) do
  96. post :create, :params => {
  97. :project_id => 1,
  98. :news => {
  99. :title => 'NewsControllerTest',
  100. :description => 'This is the description',
  101. :summary => ''
  102. }
  103. }
  104. end
  105. assert_redirected_to '/projects/ecookbook/news'
  106. news = News.find_by_title('NewsControllerTest')
  107. assert_not_nil news
  108. assert_equal 'This is the description', news.description
  109. assert_equal User.find(2), news.author
  110. assert_equal Project.find(1), news.project
  111. assert_equal 2, ActionMailer::Base.deliveries.size
  112. end
  113. def test_post_create_with_attachment
  114. set_tmp_attachments_directory
  115. @request.session[:user_id] = 2
  116. assert_difference 'News.count' do
  117. assert_difference 'Attachment.count' do
  118. post :create, :params => {
  119. :project_id => 1,
  120. :news => {
  121. :title => 'Test',
  122. :description => 'This is the description'
  123. },
  124. :attachments => {
  125. '1' => {
  126. 'file' => uploaded_test_file('testfile.txt', 'text/plain')}
  127. }
  128. }
  129. end
  130. end
  131. attachment = Attachment.order('id DESC').first
  132. news = News.order('id DESC').first
  133. assert_equal news, attachment.container
  134. end
  135. def test_post_create_with_validation_failure
  136. @request.session[:user_id] = 2
  137. post :create, :params => {
  138. :project_id => 1,
  139. :news => {
  140. :title => '',
  141. :description => 'This is the description',
  142. :summary => ''
  143. }
  144. }
  145. assert_response :success
  146. assert_select_error /title cannot be blank/i
  147. end
  148. def test_get_edit
  149. @request.session[:user_id] = 2
  150. get :edit, :params => {
  151. :id => 1
  152. }
  153. assert_response :success
  154. assert_select 'input[name=?][value=?]', 'news[title]', 'eCookbook first release !'
  155. end
  156. def test_put_update
  157. @request.session[:user_id] = 2
  158. put :update, :params => {
  159. :id => 1,
  160. :news => {
  161. :description => 'Description changed by test_post_edit'
  162. }
  163. }
  164. assert_redirected_to '/news/1'
  165. news = News.find(1)
  166. assert_equal 'Description changed by test_post_edit', news.description
  167. end
  168. def test_put_update_with_attachment
  169. set_tmp_attachments_directory
  170. @request.session[:user_id] = 2
  171. assert_no_difference 'News.count' do
  172. assert_difference 'Attachment.count' do
  173. put :update, :params => {
  174. :id => 1,
  175. :news => {
  176. :description => 'This is the description'
  177. },
  178. :attachments => {
  179. '1' => {
  180. 'file' => uploaded_test_file('testfile.txt', 'text/plain')}
  181. }
  182. }
  183. end
  184. end
  185. attachment = Attachment.order('id DESC').first
  186. assert_equal News.find(1), attachment.container
  187. end
  188. def test_update_with_failure
  189. @request.session[:user_id] = 2
  190. put :update, :params => {
  191. :id => 1,
  192. :news => {
  193. :description => ''
  194. }
  195. }
  196. assert_response :success
  197. assert_select_error /description cannot be blank/i
  198. end
  199. def test_destroy
  200. @request.session[:user_id] = 2
  201. delete :destroy, :params => {
  202. :id => 1
  203. }
  204. assert_redirected_to '/projects/ecookbook/news'
  205. assert_nil News.find_by_id(1)
  206. end
  207. end