##// END OF EJS Templates
Add NewsController and TimelogController tests....
Jean-Philippe Lang -
r1255:1c4bcc5b558e
parent child
Show More
@@ -1,10 +1,18
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
1 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2 comments_001:
2 comments_001:
3 commented_type: News
3 commented_type: News
4 commented_id: 1
4 commented_id: 1
5 id: 1
5 id: 1
6 author_id: 1
6 author_id: 1
7 comments: my first comment
7 comments: my first comment
8 created_on: 2006-12-10 18:10:10 +01:00
8 created_on: 2006-12-10 18:10:10 +01:00
9 updated_on: 2006-12-10 18:10:10 +01:00
9 updated_on: 2006-12-10 18:10:10 +01:00
10 comments_002:
11 commented_type: News
12 commented_id: 1
13 id: 2
14 author_id: 2
15 comments: This is an other comment
16 created_on: 2006-12-10 18:12:10 +01:00
17 updated_on: 2006-12-10 18:12:10 +01:00
10 No newline at end of file
18
@@ -1,59 +1,147
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'news_controller'
19 require 'news_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class NewsController; def rescue_action(e) raise e end; end
22 class NewsController; def rescue_action(e) raise e end; end
23
23
24 class NewsControllerTest < Test::Unit::TestCase
24 class NewsControllerTest < Test::Unit::TestCase
25 fixtures :projects, :users, :roles, :members, :enabled_modules
25 fixtures :projects, :users, :roles, :members, :enabled_modules, :news, :comments
26
26
27 def setup
27 def setup
28 @controller = NewsController.new
28 @controller = NewsController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 end
32 end
33
33
34 def test_index
34 def test_index
35 get :index
35 get :index
36 assert_response :success
36 assert_response :success
37 assert_template 'index'
37 assert_template 'index'
38 assert_not_nil assigns(:newss)
38 assert_not_nil assigns(:newss)
39 assert_nil assigns(:project)
39 assert_nil assigns(:project)
40 end
40 end
41
41
42 def test_index_with_project
42 def test_index_with_project
43 get :index, :project_id => 1
43 get :index, :project_id => 1
44 assert_response :success
44 assert_response :success
45 assert_template 'index'
45 assert_template 'index'
46 assert_not_nil assigns(:newss)
46 assert_not_nil assigns(:newss)
47 end
47 end
48
48
49 def test_show
50 get :show, :id => 1
51 assert_response :success
52 assert_template 'show'
53 assert_tag :tag => 'h2', :content => /eCookbook first release/
54 end
55
56 def test_show_not_found
57 get :show, :id => 999
58 assert_response 404
59 end
60
61 def test_get_new
62 @request.session[:user_id] = 2
63 get :new, :project_id => 1
64 assert_response :success
65 assert_template 'new'
66 end
67
68 def test_post_new
69 @request.session[:user_id] = 2
70 post :new, :project_id => 1, :news => { :title => 'NewsControllerTest',
71 :description => 'This is the description',
72 :summary => '' }
73 assert_redirected_to 'projects/ecookbook/news'
74
75 news = News.find_by_title('NewsControllerTest')
76 assert_not_nil news
77 assert_equal 'This is the description', news.description
78 assert_equal User.find(2), news.author
79 assert_equal Project.find(1), news.project
80 end
81
82 def test_get_edit
83 @request.session[:user_id] = 2
84 get :edit, :id => 1
85 assert_response :success
86 assert_template 'edit'
87 end
88
89 def test_post_edit
90 @request.session[:user_id] = 2
91 post :edit, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
92 assert_redirected_to 'news/show/1'
93 news = News.find(1)
94 assert_equal 'Description changed by test_post_edit', news.description
95 end
96
97 def test_post_new_with_validation_failure
98 @request.session[:user_id] = 2
99 post :new, :project_id => 1, :news => { :title => '',
100 :description => 'This is the description',
101 :summary => '' }
102 assert_response :success
103 assert_template 'new'
104 assert_not_nil assigns(:news)
105 assert assigns(:news).new_record?
106 assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' },
107 :content => /1 error/
108 end
109
110 def test_add_comment
111 @request.session[:user_id] = 2
112 post :add_comment, :id => 1, :comment => { :comments => 'This is a NewsControllerTest comment' }
113 assert_redirected_to 'news/show/1'
114
115 comment = News.find(1).comments.find(:first, :order => 'created_on DESC')
116 assert_not_nil comment
117 assert_equal 'This is a NewsControllerTest comment', comment.comments
118 assert_equal User.find(2), comment.author
119 end
120
121 def test_destroy_comment
122 comments_count = News.find(1).comments.size
123 @request.session[:user_id] = 2
124 post :destroy_comment, :id => 1, :comment_id => 2
125 assert_redirected_to 'news/show/1'
126 assert_nil Comment.find_by_id(2)
127 assert_equal comments_count - 1, News.find(1).comments.size
128 end
129
130 def test_destroy
131 @request.session[:user_id] = 2
132 post :destroy, :id => 1
133 assert_redirected_to 'projects/ecookbook/news'
134 assert_nil News.find_by_id(1)
135 end
136
49 def test_preview
137 def test_preview
50 get :preview, :project_id => 1,
138 get :preview, :project_id => 1,
51 :news => {:title => '',
139 :news => {:title => '',
52 :description => 'News description',
140 :description => 'News description',
53 :summary => ''}
141 :summary => ''}
54 assert_response :success
142 assert_response :success
55 assert_template 'common/_preview'
143 assert_template 'common/_preview'
56 assert_tag :tag => 'fieldset', :attributes => { :class => 'preview' },
144 assert_tag :tag => 'fieldset', :attributes => { :class => 'preview' },
57 :content => /News description/
145 :content => /News description/
58 end
146 end
59 end
147 end
@@ -1,155 +1,163
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'timelog_controller'
19 require 'timelog_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class TimelogController; def rescue_action(e) raise e end; end
22 class TimelogController; def rescue_action(e) raise e end; end
23
23
24 class TimelogControllerTest < Test::Unit::TestCase
24 class TimelogControllerTest < Test::Unit::TestCase
25 fixtures :projects, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses
25 fixtures :projects, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses
26
26
27 def setup
27 def setup
28 @controller = TimelogController.new
28 @controller = TimelogController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 end
31 end
32
32
33 def test_create
33 def test_create
34 @request.session[:user_id] = 3
34 @request.session[:user_id] = 3
35 post :edit, :project_id => 1,
35 post :edit, :project_id => 1,
36 :time_entry => {:comments => 'Some work on TimelogControllerTest',
36 :time_entry => {:comments => 'Some work on TimelogControllerTest',
37 :activity_id => '10',
37 :activity_id => '10',
38 :spent_on => '2008-03-14',
38 :spent_on => '2008-03-14',
39 :issue_id => '1',
39 :issue_id => '1',
40 :hours => '7.3'}
40 :hours => '7.3'}
41 assert_redirected_to 'projects/ecookbook/timelog/details'
41 assert_redirected_to 'projects/ecookbook/timelog/details'
42
42
43 i = Issue.find(1)
43 i = Issue.find(1)
44 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
44 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
45 assert_not_nil t
45 assert_not_nil t
46 assert_equal 7.3, t.hours
46 assert_equal 7.3, t.hours
47 assert_equal 3, t.user_id
47 assert_equal 3, t.user_id
48 assert_equal i, t.issue
48 assert_equal i, t.issue
49 assert_equal i.project, t.project
49 assert_equal i.project, t.project
50 end
50 end
51
51
52 def test_update
52 def test_update
53 entry = TimeEntry.find(1)
53 entry = TimeEntry.find(1)
54 assert_equal 1, entry.issue_id
54 assert_equal 1, entry.issue_id
55 assert_equal 2, entry.user_id
55 assert_equal 2, entry.user_id
56
56
57 @request.session[:user_id] = 1
57 @request.session[:user_id] = 1
58 post :edit, :id => 1,
58 post :edit, :id => 1,
59 :time_entry => {:issue_id => '2',
59 :time_entry => {:issue_id => '2',
60 :hours => '8'}
60 :hours => '8'}
61 assert_redirected_to 'projects/ecookbook/timelog/details'
61 assert_redirected_to 'projects/ecookbook/timelog/details'
62 entry.reload
62 entry.reload
63
63
64 assert_equal 8, entry.hours
64 assert_equal 8, entry.hours
65 assert_equal 2, entry.issue_id
65 assert_equal 2, entry.issue_id
66 assert_equal 2, entry.user_id
66 assert_equal 2, entry.user_id
67 end
67 end
68
68
69 def destroy
69 def destroy
70 @request.session[:user_id] = 2
70 @request.session[:user_id] = 2
71 post :destroy, :id => 1
71 post :destroy, :id => 1
72 assert_redirected_to 'projects/ecookbook/timelog/details'
72 assert_redirected_to 'projects/ecookbook/timelog/details'
73 assert_nil TimeEntry.find_by_id(1)
73 assert_nil TimeEntry.find_by_id(1)
74 end
74 end
75
75
76 def test_report_no_criteria
76 def test_report_no_criteria
77 get :report, :project_id => 1
77 get :report, :project_id => 1
78 assert_response :success
78 assert_response :success
79 assert_template 'report'
79 assert_template 'report'
80 end
80 end
81
81
82 def test_report_one_criteria
82 def test_report_one_criteria
83 get :report, :project_id => 1, :period => 'week', :date_from => "2007-04-01", :date_to => "2007-04-30", :criterias => ['project']
83 get :report, :project_id => 1, :period => 'week', :date_from => "2007-04-01", :date_to => "2007-04-30", :criterias => ['project']
84 assert_response :success
84 assert_response :success
85 assert_template 'report'
85 assert_template 'report'
86 assert_not_nil assigns(:total_hours)
86 assert_not_nil assigns(:total_hours)
87 assert_equal "8.65", "%.2f" % assigns(:total_hours)
87 assert_equal "8.65", "%.2f" % assigns(:total_hours)
88 end
88 end
89
89
90 def test_report_two_criterias
90 def test_report_two_criterias
91 get :report, :project_id => 1, :period => 'month', :date_from => "2007-01-01", :date_to => "2007-12-31", :criterias => ["member", "activity"]
91 get :report, :project_id => 1, :period => 'month', :date_from => "2007-01-01", :date_to => "2007-12-31", :criterias => ["member", "activity"]
92 assert_response :success
92 assert_response :success
93 assert_template 'report'
93 assert_template 'report'
94 assert_not_nil assigns(:total_hours)
94 assert_not_nil assigns(:total_hours)
95 assert_equal "162.90", "%.2f" % assigns(:total_hours)
95 assert_equal "162.90", "%.2f" % assigns(:total_hours)
96 end
96 end
97
97
98 def test_report_one_criteria_no_result
98 def test_report_one_criteria_no_result
99 get :report, :project_id => 1, :period => 'week', :date_from => "1998-04-01", :date_to => "1998-04-30", :criterias => ['project']
99 get :report, :project_id => 1, :period => 'week', :date_from => "1998-04-01", :date_to => "1998-04-30", :criterias => ['project']
100 assert_response :success
100 assert_response :success
101 assert_template 'report'
101 assert_template 'report'
102 assert_not_nil assigns(:total_hours)
102 assert_not_nil assigns(:total_hours)
103 assert_equal "0.00", "%.2f" % assigns(:total_hours)
103 assert_equal "0.00", "%.2f" % assigns(:total_hours)
104 end
104 end
105
105
106 def test_details_at_project_level
106 def test_details_at_project_level
107 get :details, :project_id => 1
107 get :details, :project_id => 1
108 assert_response :success
108 assert_response :success
109 assert_template 'details'
109 assert_template 'details'
110 assert_not_nil assigns(:entries)
110 assert_not_nil assigns(:entries)
111 assert_equal 4, assigns(:entries).size
111 assert_equal 4, assigns(:entries).size
112 # project and subproject
112 # project and subproject
113 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
113 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
114 assert_not_nil assigns(:total_hours)
114 assert_not_nil assigns(:total_hours)
115 assert_equal "162.90", "%.2f" % assigns(:total_hours)
115 assert_equal "162.90", "%.2f" % assigns(:total_hours)
116 # display all time by default
116 # display all time by default
117 assert_nil assigns(:from)
117 assert_nil assigns(:from)
118 assert_nil assigns(:to)
118 assert_nil assigns(:to)
119 end
119 end
120
120
121 def test_details_at_project_level_with_date_range
121 def test_details_at_project_level_with_date_range
122 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
122 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
123 assert_response :success
123 assert_response :success
124 assert_template 'details'
124 assert_template 'details'
125 assert_not_nil assigns(:entries)
125 assert_not_nil assigns(:entries)
126 assert_equal 3, assigns(:entries).size
126 assert_equal 3, assigns(:entries).size
127 assert_not_nil assigns(:total_hours)
127 assert_not_nil assigns(:total_hours)
128 assert_equal "12.90", "%.2f" % assigns(:total_hours)
128 assert_equal "12.90", "%.2f" % assigns(:total_hours)
129 assert_equal '2007-03-20'.to_date, assigns(:from)
129 assert_equal '2007-03-20'.to_date, assigns(:from)
130 assert_equal '2007-04-30'.to_date, assigns(:to)
130 assert_equal '2007-04-30'.to_date, assigns(:to)
131 end
131 end
132
132
133 def test_details_at_project_level_with_period
133 def test_details_at_project_level_with_period
134 get :details, :project_id => 1, :period => '7_days'
134 get :details, :project_id => 1, :period => '7_days'
135 assert_response :success
135 assert_response :success
136 assert_template 'details'
136 assert_template 'details'
137 assert_not_nil assigns(:entries)
137 assert_not_nil assigns(:entries)
138 assert_not_nil assigns(:total_hours)
138 assert_not_nil assigns(:total_hours)
139 assert_equal Date.today - 7, assigns(:from)
139 assert_equal Date.today - 7, assigns(:from)
140 assert_equal Date.today, assigns(:to)
140 assert_equal Date.today, assigns(:to)
141 end
141 end
142
142
143 def test_details_at_issue_level
143 def test_details_at_issue_level
144 get :details, :issue_id => 1
144 get :details, :issue_id => 1
145 assert_response :success
145 assert_response :success
146 assert_template 'details'
146 assert_template 'details'
147 assert_not_nil assigns(:entries)
147 assert_not_nil assigns(:entries)
148 assert_equal 2, assigns(:entries).size
148 assert_equal 2, assigns(:entries).size
149 assert_not_nil assigns(:total_hours)
149 assert_not_nil assigns(:total_hours)
150 assert_equal 154.25, assigns(:total_hours)
150 assert_equal 154.25, assigns(:total_hours)
151 # display all time by default
151 # display all time by default
152 assert_nil assigns(:from)
152 assert_nil assigns(:from)
153 assert_nil assigns(:to)
153 assert_nil assigns(:to)
154 end
154 end
155
156 def test_details_csv_export
157 get :details, :project_id => 1, :format => 'csv'
158 assert_response :success
159 assert_equal 'text/csv', @response.content_type
160 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
161 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,2,Feature request,Add ingredients categories,1.0,\"\"\n")
162 end
155 end
163 end
General Comments 0
You need to be logged in to leave comments. Login now