##// END OF EJS Templates
Tests/cleanup VersionsController....
Jean-Philippe Lang -
r7918:8bbb5d9686fb
parent child
Show More
@@ -1,192 +1,186
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class VersionsController < ApplicationController
19 19 menu_item :roadmap
20 20 model_object Version
21 21 before_filter :find_model_object, :except => [:index, :new, :create, :close_completed]
22 22 before_filter :find_project_from_association, :except => [:index, :new, :create, :close_completed]
23 23 before_filter :find_project, :only => [:index, :new, :create, :close_completed]
24 24 before_filter :authorize
25 25
26 26 accept_api_auth :index, :create, :update, :destroy
27 27
28 28 helper :custom_fields
29 29 helper :projects
30 30
31 31 def index
32 32 respond_to do |format|
33 33 format.html {
34 34 @trackers = @project.trackers.find(:all, :order => 'position')
35 35 retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?})
36 36 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
37 37 project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id]
38 38
39 39 @versions = @project.shared_versions || []
40 40 @versions += @project.rolled_up_versions.visible if @with_subprojects
41 41 @versions = @versions.uniq.sort
42 42 @versions.reject! {|version| version.closed? || version.completed? } unless params[:completed]
43 43
44 44 @issues_by_version = {}
45 45 unless @selected_tracker_ids.empty?
46 46 @versions.each do |version|
47 47 issues = version.fixed_issues.visible.find(:all,
48 48 :include => [:project, :status, :tracker, :priority],
49 49 :conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids},
50 50 :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id")
51 51 @issues_by_version[version] = issues
52 52 end
53 53 end
54 54 @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?}
55 55 }
56 56 format.api {
57 57 @versions = @project.shared_versions.all
58 58 }
59 59 end
60 60 end
61 61
62 62 def show
63 63 respond_to do |format|
64 64 format.html {
65 65 @issues = @version.fixed_issues.visible.find(:all,
66 66 :include => [:status, :tracker, :priority],
67 67 :order => "#{Tracker.table_name}.position, #{Issue.table_name}.id")
68 68 }
69 69 format.api
70 70 end
71 71 end
72 72
73 73 def new
74 @version = @project.versions.build
75 if params[:version]
76 attributes = params[:version].dup
77 attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
78 @version.attributes = attributes
79 end
74 @version = @project.versions.build(params[:version])
80 75 end
81 76
82 77 def create
83 # TODO: refactor with code above in #new
84 78 @version = @project.versions.build
85 79 if params[:version]
86 80 attributes = params[:version].dup
87 81 attributes.delete('sharing') unless attributes.nil? || @version.allowed_sharings.include?(attributes['sharing'])
88 82 @version.attributes = attributes
89 83 end
90 84
91 85 if request.post?
92 86 if @version.save
93 87 respond_to do |format|
94 88 format.html do
95 89 flash[:notice] = l(:notice_successful_create)
96 90 redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
97 91 end
98 92 format.js do
99 93 # IE doesn't support the replace_html rjs method for select box options
100 94 render(:update) {|page| page.replace "issue_fixed_version_id",
101 95 content_tag('select', '<option></option>' + version_options_for_select(@project.shared_versions.open, @version), :id => 'issue_fixed_version_id', :name => 'issue[fixed_version_id]')
102 96 }
103 97 end
104 98 format.api do
105 99 render :action => 'show', :status => :created, :location => version_url(@version)
106 100 end
107 101 end
108 102 else
109 103 respond_to do |format|
110 104 format.html { render :action => 'new' }
111 105 format.js do
112 106 render(:update) {|page| page.alert(@version.errors.full_messages.join('\n')) }
113 107 end
114 108 format.api { render_validation_errors(@version) }
115 109 end
116 110 end
117 111 end
118 112 end
119 113
120 114 def edit
121 115 end
122 116
123 117 def update
124 118 if request.put? && params[:version]
125 119 attributes = params[:version].dup
126 120 attributes.delete('sharing') unless @version.allowed_sharings.include?(attributes['sharing'])
127 121 if @version.update_attributes(attributes)
128 122 respond_to do |format|
129 123 format.html {
130 124 flash[:notice] = l(:notice_successful_update)
131 125 redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
132 126 }
133 127 format.api { head :ok }
134 128 end
135 129 else
136 130 respond_to do |format|
137 131 format.html { render :action => 'edit' }
138 132 format.api { render_validation_errors(@version) }
139 133 end
140 134 end
141 135 end
142 136 end
143 137
144 138 def close_completed
145 139 if request.put?
146 140 @project.close_completed_versions
147 141 end
148 142 redirect_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
149 143 end
150 144
151 145 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
152 146 def destroy
153 147 if @version.fixed_issues.empty?
154 148 @version.destroy
155 149 respond_to do |format|
156 150 format.html { redirect_back_or_default :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project }
157 151 format.api { head :ok }
158 152 end
159 153 else
160 154 respond_to do |format|
161 155 format.html {
162 156 flash[:error] = l(:notice_unable_delete_version)
163 157 redirect_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => @project
164 158 }
165 159 format.api { head :unprocessable_entity }
166 160 end
167 161 end
168 162 end
169 163
170 164 def status_by
171 165 respond_to do |format|
172 166 format.html { render :action => 'show' }
173 167 format.js { render(:update) {|page| page.replace_html 'status_by', render_issue_status_by(@version, params[:status_by])} }
174 168 end
175 169 end
176 170
177 171 private
178 172 def find_project
179 173 @project = Project.find(params[:project_id])
180 174 rescue ActiveRecord::RecordNotFound
181 175 render_404
182 176 end
183 177
184 178 def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
185 179 if ids = params[:tracker_ids]
186 180 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
187 181 else
188 182 @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s }
189 183 end
190 184 end
191 185
192 186 end
@@ -1,154 +1,181
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19 require 'versions_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class VersionsController; def rescue_action(e) raise e end; end
23 23
24 24 class VersionsControllerTest < ActionController::TestCase
25 25 fixtures :projects, :versions, :issues, :users, :roles, :members, :member_roles, :enabled_modules, :issue_statuses
26 26
27 27 def setup
28 28 @controller = VersionsController.new
29 29 @request = ActionController::TestRequest.new
30 30 @response = ActionController::TestResponse.new
31 31 User.current = nil
32 32 end
33 33
34 34 def test_index
35 35 get :index, :project_id => 1
36 36 assert_response :success
37 37 assert_template 'index'
38 38 assert_not_nil assigns(:versions)
39 39 # Version with no date set appears
40 40 assert assigns(:versions).include?(Version.find(3))
41 41 # Completed version doesn't appear
42 42 assert !assigns(:versions).include?(Version.find(1))
43 43 # Context menu on issues
44 44 assert_select "script", :text => Regexp.new(Regexp.escape("new ContextMenu('/issues/context_menu')"))
45 45 end
46 46
47 47 def test_index_with_completed_versions
48 48 get :index, :project_id => 1, :completed => 1
49 49 assert_response :success
50 50 assert_template 'index'
51 51 assert_not_nil assigns(:versions)
52 52 # Version with no date set appears
53 53 assert assigns(:versions).include?(Version.find(3))
54 54 # Completed version appears
55 55 assert assigns(:versions).include?(Version.find(1))
56 56 end
57 57
58 def test_index_with_tracker_ids
59 get :index, :project_id => 1, :tracker_ids => [1, 3]
60 assert_response :success
61 assert_template 'index'
62 assert_not_nil assigns(:issues_by_version)
63 assert_nil assigns(:issues_by_version).values.flatten.detect {|issue| issue.tracker_id == 2}
64 end
65
58 66 def test_index_showing_subprojects_versions
59 67 @subproject_version = Version.generate!(:project => Project.find(3))
60 68 get :index, :project_id => 1, :with_subprojects => 1
61 69 assert_response :success
62 70 assert_template 'index'
63 71 assert_not_nil assigns(:versions)
64 72
65 73 assert assigns(:versions).include?(Version.find(4)), "Shared version not found"
66 74 assert assigns(:versions).include?(@subproject_version), "Subproject version not found"
67 75 end
68 76
69 77 def test_show
70 78 get :show, :id => 2
71 79 assert_response :success
72 80 assert_template 'show'
73 81 assert_not_nil assigns(:version)
74 82
75 83 assert_tag :tag => 'h2', :content => /1.0/
76 84 end
77 85
86 def test_new
87 @request.session[:user_id] = 2
88 get :new, :project_id => '1'
89 assert_response :success
90 assert_template 'new'
91 end
92
78 93 def test_create
79 94 @request.session[:user_id] = 2 # manager
80 95 assert_difference 'Version.count' do
81 96 post :create, :project_id => '1', :version => {:name => 'test_add_version'}
82 97 end
83 98 assert_redirected_to '/projects/ecookbook/settings/versions'
84 99 version = Version.find_by_name('test_add_version')
85 100 assert_not_nil version
86 101 assert_equal 1, version.project_id
87 102 end
88 103
89 104 def test_create_from_issue_form
90 105 @request.session[:user_id] = 2 # manager
91 106 assert_difference 'Version.count' do
92 107 xhr :post, :create, :project_id => '1', :version => {:name => 'test_add_version_from_issue_form'}
93 108 end
94 109 assert_response :success
95 110 assert_select_rjs :replace, 'issue_fixed_version_id'
96 111 version = Version.find_by_name('test_add_version_from_issue_form')
97 112 assert_not_nil version
98 113 assert_equal 1, version.project_id
99 114 end
100 115
101 116 def test_get_edit
102 117 @request.session[:user_id] = 2
103 118 get :edit, :id => 2
104 119 assert_response :success
105 120 assert_template 'edit'
106 121 end
107 122
108 123 def test_close_completed
109 124 Version.update_all("status = 'open'")
110 125 @request.session[:user_id] = 2
111 126 put :close_completed, :project_id => 'ecookbook'
112 127 assert_redirected_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => 'ecookbook'
113 128 assert_not_nil Version.find_by_status('closed')
114 129 end
115 130
116 131 def test_post_update
117 132 @request.session[:user_id] = 2
118 133 put :update, :id => 2,
119 134 :version => { :name => 'New version name',
120 135 :effective_date => Date.today.strftime("%Y-%m-%d")}
121 136 assert_redirected_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => 'ecookbook'
122 137 version = Version.find(2)
123 138 assert_equal 'New version name', version.name
124 139 assert_equal Date.today, version.effective_date
125 140 end
126 141
127 142 def test_post_update_with_validation_failure
128 143 @request.session[:user_id] = 2
129 144 put :update, :id => 2,
130 145 :version => { :name => '',
131 146 :effective_date => Date.today.strftime("%Y-%m-%d")}
132 147 assert_response :success
133 148 assert_template 'edit'
134 149 end
135 150
136 151 def test_destroy
137 152 @request.session[:user_id] = 2
138 delete :destroy, :id => 3
153 assert_difference 'Version.count', -1 do
154 delete :destroy, :id => 3
155 end
139 156 assert_redirected_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => 'ecookbook'
140 157 assert_nil Version.find_by_id(3)
141 158 end
142 159
160 def test_destroy_version_in_use_should_fail
161 @request.session[:user_id] = 2
162 assert_no_difference 'Version.count' do
163 delete :destroy, :id => 2
164 end
165 assert_redirected_to :controller => 'projects', :action => 'settings', :tab => 'versions', :id => 'ecookbook'
166 assert flash[:error].match(/Unable to delete version/)
167 assert Version.find_by_id(2)
168 end
169
143 170 def test_issue_status_by
144 171 xhr :get, :status_by, :id => 2
145 172 assert_response :success
146 173 assert_template '_issue_counts'
147 174 end
148 175
149 176 def test_issue_status_by_status
150 177 xhr :get, :status_by, :id => 2, :status_by => 'status'
151 178 assert_response :success
152 179 assert_template '_issue_counts'
153 180 end
154 181 end
General Comments 0
You need to be logged in to leave comments. Login now