##// END OF EJS Templates
Rails4: replace deprecated Relation#first with finder options at NewsControllerTest...
Toshi MARUYAMA -
r12247:184ea832d601
parent child
Show More
@@ -1,165 +1,165
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class NewsControllerTest < ActionController::TestCase
20 class NewsControllerTest < ActionController::TestCase
21 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :news, :comments
21 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :news, :comments
22
22
23 def setup
23 def setup
24 User.current = nil
24 User.current = nil
25 end
25 end
26
26
27 def test_index
27 def test_index
28 get :index
28 get :index
29 assert_response :success
29 assert_response :success
30 assert_template 'index'
30 assert_template 'index'
31 assert_not_nil assigns(:newss)
31 assert_not_nil assigns(:newss)
32 assert_nil assigns(:project)
32 assert_nil assigns(:project)
33 end
33 end
34
34
35 def test_index_with_project
35 def test_index_with_project
36 get :index, :project_id => 1
36 get :index, :project_id => 1
37 assert_response :success
37 assert_response :success
38 assert_template 'index'
38 assert_template 'index'
39 assert_not_nil assigns(:newss)
39 assert_not_nil assigns(:newss)
40 end
40 end
41
41
42 def test_index_with_invalid_project_should_respond_with_404
42 def test_index_with_invalid_project_should_respond_with_404
43 get :index, :project_id => 999
43 get :index, :project_id => 999
44 assert_response 404
44 assert_response 404
45 end
45 end
46
46
47 def test_show
47 def test_show
48 get :show, :id => 1
48 get :show, :id => 1
49 assert_response :success
49 assert_response :success
50 assert_template 'show'
50 assert_template 'show'
51 assert_tag :tag => 'h2', :content => /eCookbook first release/
51 assert_tag :tag => 'h2', :content => /eCookbook first release/
52 end
52 end
53
53
54 def test_show_should_show_attachments
54 def test_show_should_show_attachments
55 attachment = Attachment.first
55 attachment = Attachment.first
56 attachment.container = News.find(1)
56 attachment.container = News.find(1)
57 attachment.save!
57 attachment.save!
58
58
59 get :show, :id => 1
59 get :show, :id => 1
60 assert_response :success
60 assert_response :success
61 assert_tag 'a', :content => attachment.filename
61 assert_tag 'a', :content => attachment.filename
62 end
62 end
63
63
64 def test_show_not_found
64 def test_show_not_found
65 get :show, :id => 999
65 get :show, :id => 999
66 assert_response 404
66 assert_response 404
67 end
67 end
68
68
69 def test_get_new
69 def test_get_new
70 @request.session[:user_id] = 2
70 @request.session[:user_id] = 2
71 get :new, :project_id => 1
71 get :new, :project_id => 1
72 assert_response :success
72 assert_response :success
73 assert_template 'new'
73 assert_template 'new'
74 end
74 end
75
75
76 def test_post_create
76 def test_post_create
77 ActionMailer::Base.deliveries.clear
77 ActionMailer::Base.deliveries.clear
78 @request.session[:user_id] = 2
78 @request.session[:user_id] = 2
79
79
80 with_settings :notified_events => %w(news_added) do
80 with_settings :notified_events => %w(news_added) do
81 post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
81 post :create, :project_id => 1, :news => { :title => 'NewsControllerTest',
82 :description => 'This is the description',
82 :description => 'This is the description',
83 :summary => '' }
83 :summary => '' }
84 end
84 end
85 assert_redirected_to '/projects/ecookbook/news'
85 assert_redirected_to '/projects/ecookbook/news'
86
86
87 news = News.find_by_title('NewsControllerTest')
87 news = News.find_by_title('NewsControllerTest')
88 assert_not_nil news
88 assert_not_nil news
89 assert_equal 'This is the description', news.description
89 assert_equal 'This is the description', news.description
90 assert_equal User.find(2), news.author
90 assert_equal User.find(2), news.author
91 assert_equal Project.find(1), news.project
91 assert_equal Project.find(1), news.project
92 assert_equal 1, ActionMailer::Base.deliveries.size
92 assert_equal 1, ActionMailer::Base.deliveries.size
93 end
93 end
94
94
95 def test_post_create_with_attachment
95 def test_post_create_with_attachment
96 set_tmp_attachments_directory
96 set_tmp_attachments_directory
97 @request.session[:user_id] = 2
97 @request.session[:user_id] = 2
98 assert_difference 'News.count' do
98 assert_difference 'News.count' do
99 assert_difference 'Attachment.count' do
99 assert_difference 'Attachment.count' do
100 post :create, :project_id => 1,
100 post :create, :project_id => 1,
101 :news => { :title => 'Test', :description => 'This is the description' },
101 :news => { :title => 'Test', :description => 'This is the description' },
102 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
102 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
103 end
103 end
104 end
104 end
105 attachment = Attachment.first(:order => 'id DESC')
105 attachment = Attachment.order('id DESC').first
106 news = News.first(:order => 'id DESC')
106 news = News.order('id DESC').first
107 assert_equal news, attachment.container
107 assert_equal news, attachment.container
108 end
108 end
109
109
110 def test_post_create_with_validation_failure
110 def test_post_create_with_validation_failure
111 @request.session[:user_id] = 2
111 @request.session[:user_id] = 2
112 post :create, :project_id => 1, :news => { :title => '',
112 post :create, :project_id => 1, :news => { :title => '',
113 :description => 'This is the description',
113 :description => 'This is the description',
114 :summary => '' }
114 :summary => '' }
115 assert_response :success
115 assert_response :success
116 assert_template 'new'
116 assert_template 'new'
117 assert_not_nil assigns(:news)
117 assert_not_nil assigns(:news)
118 assert assigns(:news).new_record?
118 assert assigns(:news).new_record?
119 assert_error_tag :content => /title can&#x27;t be blank/i
119 assert_error_tag :content => /title can&#x27;t be blank/i
120 end
120 end
121
121
122 def test_get_edit
122 def test_get_edit
123 @request.session[:user_id] = 2
123 @request.session[:user_id] = 2
124 get :edit, :id => 1
124 get :edit, :id => 1
125 assert_response :success
125 assert_response :success
126 assert_template 'edit'
126 assert_template 'edit'
127 end
127 end
128
128
129 def test_put_update
129 def test_put_update
130 @request.session[:user_id] = 2
130 @request.session[:user_id] = 2
131 put :update, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
131 put :update, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
132 assert_redirected_to '/news/1'
132 assert_redirected_to '/news/1'
133 news = News.find(1)
133 news = News.find(1)
134 assert_equal 'Description changed by test_post_edit', news.description
134 assert_equal 'Description changed by test_post_edit', news.description
135 end
135 end
136
136
137 def test_put_update_with_attachment
137 def test_put_update_with_attachment
138 set_tmp_attachments_directory
138 set_tmp_attachments_directory
139 @request.session[:user_id] = 2
139 @request.session[:user_id] = 2
140 assert_no_difference 'News.count' do
140 assert_no_difference 'News.count' do
141 assert_difference 'Attachment.count' do
141 assert_difference 'Attachment.count' do
142 put :update, :id => 1,
142 put :update, :id => 1,
143 :news => { :description => 'This is the description' },
143 :news => { :description => 'This is the description' },
144 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
144 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
145 end
145 end
146 end
146 end
147 attachment = Attachment.order('id DESC').first
147 attachment = Attachment.order('id DESC').first
148 assert_equal News.find(1), attachment.container
148 assert_equal News.find(1), attachment.container
149 end
149 end
150
150
151 def test_update_with_failure
151 def test_update_with_failure
152 @request.session[:user_id] = 2
152 @request.session[:user_id] = 2
153 put :update, :id => 1, :news => { :description => '' }
153 put :update, :id => 1, :news => { :description => '' }
154 assert_response :success
154 assert_response :success
155 assert_template 'edit'
155 assert_template 'edit'
156 assert_error_tag :content => /description can&#x27;t be blank/i
156 assert_error_tag :content => /description can&#x27;t be blank/i
157 end
157 end
158
158
159 def test_destroy
159 def test_destroy
160 @request.session[:user_id] = 2
160 @request.session[:user_id] = 2
161 delete :destroy, :id => 1
161 delete :destroy, :id => 1
162 assert_redirected_to '/projects/ecookbook/news'
162 assert_redirected_to '/projects/ecookbook/news'
163 assert_nil News.find_by_id(1)
163 assert_nil News.find_by_id(1)
164 end
164 end
165 end
165 end
General Comments 0
You need to be logged in to leave comments. Login now