##// END OF EJS Templates
Merged r14053 (#19253)....
Jean-Philippe Lang -
r13680:fc4d0539c0ee
parent child
Show More
@@ -1,439 +1,439
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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 'SVG/Graph/Bar'
19 19 require 'SVG/Graph/BarHorizontal'
20 20 require 'digest/sha1'
21 21 require 'redmine/scm/adapters'
22 22
23 23 class ChangesetNotFound < Exception; end
24 24 class InvalidRevisionParam < Exception; end
25 25
26 26 class RepositoriesController < ApplicationController
27 27 menu_item :repository
28 28 menu_item :settings, :only => [:new, :create, :edit, :update, :destroy, :committers]
29 29 default_search_scope :changesets
30 30
31 31 before_filter :find_project_by_project_id, :only => [:new, :create]
32 32 before_filter :find_repository, :only => [:edit, :update, :destroy, :committers]
33 33 before_filter :find_project_repository, :except => [:new, :create, :edit, :update, :destroy, :committers]
34 34 before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue]
35 35 before_filter :authorize
36 36 accept_rss_auth :revisions
37 37
38 38 rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed
39 39
40 40 def new
41 41 scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first
42 42 @repository = Repository.factory(scm)
43 43 @repository.is_default = @project.repository.nil?
44 44 @repository.project = @project
45 45 end
46 46
47 47 def create
48 48 attrs = pickup_extra_info
49 49 @repository = Repository.factory(params[:repository_scm])
50 50 @repository.safe_attributes = params[:repository]
51 51 if attrs[:attrs_extra].keys.any?
52 52 @repository.merge_extra_info(attrs[:attrs_extra])
53 53 end
54 54 @repository.project = @project
55 55 if request.post? && @repository.save
56 56 redirect_to settings_project_path(@project, :tab => 'repositories')
57 57 else
58 58 render :action => 'new'
59 59 end
60 60 end
61 61
62 62 def edit
63 63 end
64 64
65 65 def update
66 66 attrs = pickup_extra_info
67 67 @repository.safe_attributes = attrs[:attrs]
68 68 if attrs[:attrs_extra].keys.any?
69 69 @repository.merge_extra_info(attrs[:attrs_extra])
70 70 end
71 71 @repository.project = @project
72 72 if @repository.save
73 73 redirect_to settings_project_path(@project, :tab => 'repositories')
74 74 else
75 75 render :action => 'edit'
76 76 end
77 77 end
78 78
79 79 def pickup_extra_info
80 80 p = {}
81 81 p_extra = {}
82 82 params[:repository].each do |k, v|
83 83 if k =~ /^extra_/
84 84 p_extra[k] = v
85 85 else
86 86 p[k] = v
87 87 end
88 88 end
89 89 {:attrs => p, :attrs_extra => p_extra}
90 90 end
91 91 private :pickup_extra_info
92 92
93 93 def committers
94 94 @committers = @repository.committers
95 @users = @project.users
95 @users = @project.users.to_a
96 96 additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id)
97 97 @users += User.where(:id => additional_user_ids).to_a unless additional_user_ids.empty?
98 98 @users.compact!
99 99 @users.sort!
100 100 if request.post? && params[:committers].is_a?(Hash)
101 101 # Build a hash with repository usernames as keys and corresponding user ids as values
102 102 @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h}
103 103 flash[:notice] = l(:notice_successful_update)
104 104 redirect_to settings_project_path(@project, :tab => 'repositories')
105 105 end
106 106 end
107 107
108 108 def destroy
109 109 @repository.destroy if request.delete?
110 110 redirect_to settings_project_path(@project, :tab => 'repositories')
111 111 end
112 112
113 113 def show
114 114 @repository.fetch_changesets if @project.active? && Setting.autofetch_changesets? && @path.empty?
115 115
116 116 @entries = @repository.entries(@path, @rev)
117 117 @changeset = @repository.find_changeset_by_name(@rev)
118 118 if request.xhr?
119 119 @entries ? render(:partial => 'dir_list_content') : render(:nothing => true)
120 120 else
121 121 (show_error_not_found; return) unless @entries
122 122 @changesets = @repository.latest_changesets(@path, @rev)
123 123 @properties = @repository.properties(@path, @rev)
124 124 @repositories = @project.repositories
125 125 render :action => 'show'
126 126 end
127 127 end
128 128
129 129 alias_method :browse, :show
130 130
131 131 def changes
132 132 @entry = @repository.entry(@path, @rev)
133 133 (show_error_not_found; return) unless @entry
134 134 @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i)
135 135 @properties = @repository.properties(@path, @rev)
136 136 @changeset = @repository.find_changeset_by_name(@rev)
137 137 end
138 138
139 139 def revisions
140 140 @changeset_count = @repository.changesets.count
141 141 @changeset_pages = Paginator.new @changeset_count,
142 142 per_page_option,
143 143 params['page']
144 144 @changesets = @repository.changesets.
145 145 limit(@changeset_pages.per_page).
146 146 offset(@changeset_pages.offset).
147 147 includes(:user, :repository, :parents).
148 148 to_a
149 149
150 150 respond_to do |format|
151 151 format.html { render :layout => false if request.xhr? }
152 152 format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") }
153 153 end
154 154 end
155 155
156 156 def raw
157 157 entry_and_raw(true)
158 158 end
159 159
160 160 def entry
161 161 entry_and_raw(false)
162 162 end
163 163
164 164 def entry_and_raw(is_raw)
165 165 @entry = @repository.entry(@path, @rev)
166 166 (show_error_not_found; return) unless @entry
167 167
168 168 # If the entry is a dir, show the browser
169 169 (show; return) if @entry.is_dir?
170 170
171 171 @content = @repository.cat(@path, @rev)
172 172 (show_error_not_found; return) unless @content
173 173 if is_raw ||
174 174 (@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
175 175 ! is_entry_text_data?(@content, @path)
176 176 # Force the download
177 177 send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
178 178 send_type = Redmine::MimeType.of(@path)
179 179 send_opt[:type] = send_type.to_s if send_type
180 180 send_opt[:disposition] = (Redmine::MimeType.is_type?('image', @path) && !is_raw ? 'inline' : 'attachment')
181 181 send_data @content, send_opt
182 182 else
183 183 # Prevent empty lines when displaying a file with Windows style eol
184 184 # TODO: UTF-16
185 185 # Is this needs? AttachmentsController reads file simply.
186 186 @content.gsub!("\r\n", "\n")
187 187 @changeset = @repository.find_changeset_by_name(@rev)
188 188 end
189 189 end
190 190 private :entry_and_raw
191 191
192 192 def is_entry_text_data?(ent, path)
193 193 # UTF-16 contains "\x00".
194 194 # It is very strict that file contains less than 30% of ascii symbols
195 195 # in non Western Europe.
196 196 return true if Redmine::MimeType.is_type?('text', path)
197 197 # Ruby 1.8.6 has a bug of integer divisions.
198 198 # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
199 199 return false if ent.is_binary_data?
200 200 true
201 201 end
202 202 private :is_entry_text_data?
203 203
204 204 def annotate
205 205 @entry = @repository.entry(@path, @rev)
206 206 (show_error_not_found; return) unless @entry
207 207
208 208 @annotate = @repository.scm.annotate(@path, @rev)
209 209 if @annotate.nil? || @annotate.empty?
210 210 (render_error l(:error_scm_annotate); return)
211 211 end
212 212 ann_buf_size = 0
213 213 @annotate.lines.each do |buf|
214 214 ann_buf_size += buf.size
215 215 end
216 216 if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte
217 217 (render_error l(:error_scm_annotate_big_text_file); return)
218 218 end
219 219 @changeset = @repository.find_changeset_by_name(@rev)
220 220 end
221 221
222 222 def revision
223 223 respond_to do |format|
224 224 format.html
225 225 format.js {render :layout => false}
226 226 end
227 227 end
228 228
229 229 # Adds a related issue to a changeset
230 230 # POST /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues
231 231 def add_related_issue
232 232 issue_id = params[:issue_id].to_s.sub(/^#/,'')
233 233 @issue = @changeset.find_referenced_issue_by_id(issue_id)
234 234 if @issue && (!@issue.visible? || @changeset.issues.include?(@issue))
235 235 @issue = nil
236 236 end
237 237
238 238 if @issue
239 239 @changeset.issues << @issue
240 240 end
241 241 end
242 242
243 243 # Removes a related issue from a changeset
244 244 # DELETE /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues/:issue_id
245 245 def remove_related_issue
246 246 @issue = Issue.visible.find_by_id(params[:issue_id])
247 247 if @issue
248 248 @changeset.issues.delete(@issue)
249 249 end
250 250 end
251 251
252 252 def diff
253 253 if params[:format] == 'diff'
254 254 @diff = @repository.diff(@path, @rev, @rev_to)
255 255 (show_error_not_found; return) unless @diff
256 256 filename = "changeset_r#{@rev}"
257 257 filename << "_r#{@rev_to}" if @rev_to
258 258 send_data @diff.join, :filename => "#{filename}.diff",
259 259 :type => 'text/x-patch',
260 260 :disposition => 'attachment'
261 261 else
262 262 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
263 263 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
264 264
265 265 # Save diff type as user preference
266 266 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
267 267 User.current.pref[:diff_type] = @diff_type
268 268 User.current.preference.save
269 269 end
270 270 @cache_key = "repositories/diff/#{@repository.id}/" +
271 271 Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}")
272 272 unless read_fragment(@cache_key)
273 273 @diff = @repository.diff(@path, @rev, @rev_to)
274 274 show_error_not_found unless @diff
275 275 end
276 276
277 277 @changeset = @repository.find_changeset_by_name(@rev)
278 278 @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil
279 279 @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to)
280 280 end
281 281 end
282 282
283 283 def stats
284 284 end
285 285
286 286 def graph
287 287 data = nil
288 288 case params[:graph]
289 289 when "commits_per_month"
290 290 data = graph_commits_per_month(@repository)
291 291 when "commits_per_author"
292 292 data = graph_commits_per_author(@repository)
293 293 end
294 294 if data
295 295 headers["Content-Type"] = "image/svg+xml"
296 296 send_data(data, :type => "image/svg+xml", :disposition => "inline")
297 297 else
298 298 render_404
299 299 end
300 300 end
301 301
302 302 private
303 303
304 304 def find_repository
305 305 @repository = Repository.find(params[:id])
306 306 @project = @repository.project
307 307 rescue ActiveRecord::RecordNotFound
308 308 render_404
309 309 end
310 310
311 311 REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i
312 312
313 313 def find_project_repository
314 314 @project = Project.find(params[:id])
315 315 if params[:repository_id].present?
316 316 @repository = @project.repositories.find_by_identifier_param(params[:repository_id])
317 317 else
318 318 @repository = @project.repository
319 319 end
320 320 (render_404; return false) unless @repository
321 321 @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s
322 322 @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip
323 323 @rev_to = params[:rev_to]
324 324
325 325 unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE)
326 326 if @repository.branches.blank?
327 327 raise InvalidRevisionParam
328 328 end
329 329 end
330 330 rescue ActiveRecord::RecordNotFound
331 331 render_404
332 332 rescue InvalidRevisionParam
333 333 show_error_not_found
334 334 end
335 335
336 336 def find_changeset
337 337 if @rev.present?
338 338 @changeset = @repository.find_changeset_by_name(@rev)
339 339 end
340 340 show_error_not_found unless @changeset
341 341 end
342 342
343 343 def show_error_not_found
344 344 render_error :message => l(:error_scm_not_found), :status => 404
345 345 end
346 346
347 347 # Handler for Redmine::Scm::Adapters::CommandFailed exception
348 348 def show_error_command_failed(exception)
349 349 render_error l(:error_scm_command_failed, exception.message)
350 350 end
351 351
352 352 def graph_commits_per_month(repository)
353 353 @date_to = Date.today
354 354 @date_from = @date_to << 11
355 355 @date_from = Date.civil(@date_from.year, @date_from.month, 1)
356 356 commits_by_day = Changeset.
357 357 where("repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
358 358 group(:commit_date).
359 359 count
360 360 commits_by_month = [0] * 12
361 361 commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
362 362
363 363 changes_by_day = Change.
364 364 joins(:changeset).
365 365 where("#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to).
366 366 group(:commit_date).
367 367 count
368 368 changes_by_month = [0] * 12
369 369 changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last }
370 370
371 371 fields = []
372 372 12.times {|m| fields << month_name(((Date.today.month - 1 - m) % 12) + 1)}
373 373
374 374 graph = SVG::Graph::Bar.new(
375 375 :height => 300,
376 376 :width => 800,
377 377 :fields => fields.reverse,
378 378 :stack => :side,
379 379 :scale_integers => true,
380 380 :step_x_labels => 2,
381 381 :show_data_values => false,
382 382 :graph_title => l(:label_commits_per_month),
383 383 :show_graph_title => true
384 384 )
385 385
386 386 graph.add_data(
387 387 :data => commits_by_month[0..11].reverse,
388 388 :title => l(:label_revision_plural)
389 389 )
390 390
391 391 graph.add_data(
392 392 :data => changes_by_month[0..11].reverse,
393 393 :title => l(:label_change_plural)
394 394 )
395 395
396 396 graph.burn
397 397 end
398 398
399 399 def graph_commits_per_author(repository)
400 400 #data
401 401 stats = repository.stats_by_author
402 402 fields, commits_data, changes_data = [], [], []
403 403 stats.each do |name, hsh|
404 404 fields << name
405 405 commits_data << hsh[:commits_count]
406 406 changes_data << hsh[:changes_count]
407 407 end
408 408
409 409 #expand to 10 values if needed
410 410 fields = fields + [""]*(10 - fields.length) if fields.length<10
411 411 commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10
412 412 changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10
413 413
414 414 # Remove email address in usernames
415 415 fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') }
416 416
417 417 #prepare graph
418 418 graph = SVG::Graph::BarHorizontal.new(
419 419 :height => 30 * commits_data.length,
420 420 :width => 800,
421 421 :fields => fields,
422 422 :stack => :side,
423 423 :scale_integers => true,
424 424 :show_data_values => false,
425 425 :rotate_y_labels => false,
426 426 :graph_title => l(:label_commits_per_author),
427 427 :show_graph_title => true
428 428 )
429 429 graph.add_data(
430 430 :data => commits_data,
431 431 :title => l(:label_revision_plural)
432 432 )
433 433 graph.add_data(
434 434 :data => changes_data,
435 435 :title => l(:label_change_plural)
436 436 )
437 437 graph.burn
438 438 end
439 439 end
@@ -1,298 +1,307
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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
20 20 class RepositoriesControllerTest < ActionController::TestCase
21 21 fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles, :enabled_modules,
22 22 :repositories, :issues, :issue_statuses, :changesets, :changes,
23 23 :issue_categories, :enumerations, :custom_fields, :custom_values, :trackers
24 24
25 25 def setup
26 26 User.current = nil
27 27 end
28 28
29 29 def test_new
30 30 @request.session[:user_id] = 1
31 31 get :new, :project_id => 'subproject1'
32 32 assert_response :success
33 33 assert_template 'new'
34 34 assert_kind_of Repository::Subversion, assigns(:repository)
35 35 assert assigns(:repository).new_record?
36 36 assert_select 'input[name=?]:not([disabled])', 'repository[url]'
37 37 end
38 38
39 39 def test_new_should_propose_enabled_scm_only
40 40 @request.session[:user_id] = 1
41 41 with_settings :enabled_scm => ['Mercurial', 'Git'] do
42 42 get :new, :project_id => 'subproject1'
43 43 end
44 44 assert_response :success
45 45 assert_template 'new'
46 46 assert_kind_of Repository::Mercurial, assigns(:repository)
47 47
48 48 assert_select 'select[name=repository_scm]' do
49 49 assert_select 'option', 3
50 50 assert_select 'option[value=Mercurial][selected=selected]'
51 51 assert_select 'option[value=Git]:not([selected])'
52 52 end
53 53 end
54 54
55 55 def test_create
56 56 @request.session[:user_id] = 1
57 57 assert_difference 'Repository.count' do
58 58 post :create, :project_id => 'subproject1',
59 59 :repository_scm => 'Subversion',
60 60 :repository => {:url => 'file:///test', :is_default => '1', :identifier => ''}
61 61 end
62 62 assert_response 302
63 63 repository = Repository.order('id DESC').first
64 64 assert_kind_of Repository::Subversion, repository
65 65 assert_equal 'file:///test', repository.url
66 66 end
67 67
68 68 def test_create_with_failure
69 69 @request.session[:user_id] = 1
70 70 assert_no_difference 'Repository.count' do
71 71 post :create, :project_id => 'subproject1',
72 72 :repository_scm => 'Subversion',
73 73 :repository => {:url => 'invalid'}
74 74 end
75 75 assert_response :success
76 76 assert_template 'new'
77 77 assert_kind_of Repository::Subversion, assigns(:repository)
78 78 assert assigns(:repository).new_record?
79 79 end
80 80
81 81 def test_edit
82 82 @request.session[:user_id] = 1
83 83 get :edit, :id => 11
84 84 assert_response :success
85 85 assert_template 'edit'
86 86 assert_equal Repository.find(11), assigns(:repository)
87 87 assert_select 'input[name=?][value=?][disabled=disabled]', 'repository[url]', 'svn://localhost/test'
88 88 end
89 89
90 90 def test_update
91 91 @request.session[:user_id] = 1
92 92 put :update, :id => 11, :repository => {:password => 'test_update'}
93 93 assert_response 302
94 94 assert_equal 'test_update', Repository.find(11).password
95 95 end
96 96
97 97 def test_update_with_failure
98 98 @request.session[:user_id] = 1
99 99 put :update, :id => 11, :repository => {:password => 'x'*260}
100 100 assert_response :success
101 101 assert_template 'edit'
102 102 assert_equal Repository.find(11), assigns(:repository)
103 103 end
104 104
105 105 def test_destroy
106 106 @request.session[:user_id] = 1
107 107 assert_difference 'Repository.count', -1 do
108 108 delete :destroy, :id => 11
109 109 end
110 110 assert_response 302
111 111 assert_nil Repository.find_by_id(11)
112 112 end
113 113
114 114 def test_show_with_autofetch_changesets_enabled_should_fetch_changesets
115 115 Repository::Subversion.any_instance.expects(:fetch_changesets).once
116 116
117 117 with_settings :autofetch_changesets => '1' do
118 118 get :show, :id => 1
119 119 end
120 120 end
121 121
122 122 def test_show_with_autofetch_changesets_disabled_should_not_fetch_changesets
123 123 Repository::Subversion.any_instance.expects(:fetch_changesets).never
124 124
125 125 with_settings :autofetch_changesets => '0' do
126 126 get :show, :id => 1
127 127 end
128 128 end
129 129
130 130 def test_show_with_closed_project_should_not_fetch_changesets
131 131 Repository::Subversion.any_instance.expects(:fetch_changesets).never
132 132 Project.find(1).close
133 133
134 134 with_settings :autofetch_changesets => '1' do
135 135 get :show, :id => 1
136 136 end
137 137 end
138 138
139 139 def test_revisions
140 140 get :revisions, :id => 1
141 141 assert_response :success
142 142 assert_template 'revisions'
143 143 assert_equal Repository.find(10), assigns(:repository)
144 144 assert_not_nil assigns(:changesets)
145 145 end
146 146
147 147 def test_revisions_for_other_repository
148 148 repository = Repository::Subversion.create!(:project_id => 1, :identifier => 'foo', :url => 'file:///foo')
149 149
150 150 get :revisions, :id => 1, :repository_id => 'foo'
151 151 assert_response :success
152 152 assert_template 'revisions'
153 153 assert_equal repository, assigns(:repository)
154 154 assert_not_nil assigns(:changesets)
155 155 end
156 156
157 157 def test_revisions_for_invalid_repository
158 158 get :revisions, :id => 1, :repository_id => 'foo'
159 159 assert_response 404
160 160 end
161 161
162 162 def test_revision
163 163 get :revision, :id => 1, :rev => 1
164 164 assert_response :success
165 165 assert_not_nil assigns(:changeset)
166 166 assert_equal "1", assigns(:changeset).revision
167 167 end
168 168
169 169 def test_revision_should_show_add_related_issue_form
170 170 Role.find(1).add_permission! :manage_related_issues
171 171 @request.session[:user_id] = 2
172 172
173 173 get :revision, :id => 1, :rev => 1
174 174 assert_response :success
175 175
176 176 assert_select 'form[action=?]', '/projects/ecookbook/repository/revisions/1/issues' do
177 177 assert_select 'input[name=?]', 'issue_id'
178 178 end
179 179 end
180 180
181 181 def test_revision_should_not_change_the_project_menu_link
182 182 get :revision, :id => 1, :rev => 1
183 183 assert_response :success
184 184
185 185 assert_select '#main-menu a.repository[href=?]', '/projects/ecookbook/repository'
186 186 end
187 187
188 188 def test_revision_with_before_nil_and_afer_normal
189 189 get :revision, {:id => 1, :rev => 1}
190 190 assert_response :success
191 191 assert_template 'revision'
192 192
193 193 assert_select 'div.contextual' do
194 194 assert_select 'a[href=?]', '/projects/ecookbook/repository/revisions/0', 0
195 195 assert_select 'a[href=?]', '/projects/ecookbook/repository/revisions/2'
196 196 end
197 197 end
198 198
199 199 def test_add_related_issue
200 200 @request.session[:user_id] = 2
201 201 assert_difference 'Changeset.find(103).issues.size' do
202 202 xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => 2, :format => 'js'
203 203 assert_response :success
204 204 assert_template 'add_related_issue'
205 205 assert_equal 'text/javascript', response.content_type
206 206 end
207 207 assert_equal [2], Changeset.find(103).issue_ids
208 208 assert_include 'related-issues', response.body
209 209 assert_include 'Feature request #2', response.body
210 210 end
211 211
212 212 def test_add_related_issue_should_accept_issue_id_with_sharp
213 213 @request.session[:user_id] = 2
214 214 assert_difference 'Changeset.find(103).issues.size' do
215 215 xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => "#2", :format => 'js'
216 216 end
217 217 assert_equal [2], Changeset.find(103).issue_ids
218 218 end
219 219
220 220 def test_add_related_issue_with_invalid_issue_id
221 221 @request.session[:user_id] = 2
222 222 assert_no_difference 'Changeset.find(103).issues.size' do
223 223 xhr :post, :add_related_issue, :id => 1, :rev => 4, :issue_id => 9999, :format => 'js'
224 224 assert_response :success
225 225 assert_template 'add_related_issue'
226 226 assert_equal 'text/javascript', response.content_type
227 227 end
228 228 assert_include 'alert("Issue is invalid")', response.body
229 229 end
230 230
231 231 def test_remove_related_issue
232 232 Changeset.find(103).issues << Issue.find(1)
233 233 Changeset.find(103).issues << Issue.find(2)
234 234
235 235 @request.session[:user_id] = 2
236 236 assert_difference 'Changeset.find(103).issues.size', -1 do
237 237 xhr :delete, :remove_related_issue, :id => 1, :rev => 4, :issue_id => 2, :format => 'js'
238 238 assert_response :success
239 239 assert_template 'remove_related_issue'
240 240 assert_equal 'text/javascript', response.content_type
241 241 end
242 242 assert_equal [1], Changeset.find(103).issue_ids
243 243 assert_include 'related-issue-2', response.body
244 244 end
245 245
246 246 def test_graph_commits_per_month
247 247 # Make sure there's some data to display
248 248 latest = Project.find(1).repository.changesets.maximum(:commit_date)
249 249 assert_not_nil latest
250 250 Date.stubs(:today).returns(latest.to_date + 10)
251 251
252 252 get :graph, :id => 1, :graph => 'commits_per_month'
253 253 assert_response :success
254 254 assert_equal 'image/svg+xml', @response.content_type
255 255 end
256 256
257 257 def test_graph_commits_per_author
258 258 get :graph, :id => 1, :graph => 'commits_per_author'
259 259 assert_response :success
260 260 assert_equal 'image/svg+xml', @response.content_type
261 261 end
262 262
263 263 def test_get_committers
264 264 @request.session[:user_id] = 2
265 265 # add a commit with an unknown user
266 266 Changeset.create!(
267 267 :repository => Project.find(1).repository,
268 268 :committer => 'foo',
269 269 :committed_on => Time.now,
270 270 :revision => 100,
271 271 :comments => 'Committed by foo.'
272 272 )
273 273
274 274 get :committers, :id => 10
275 275 assert_response :success
276 276 assert_template 'committers'
277 277
278 278 assert_select 'input[value=dlopper] + select option[value="3"][selected=selected]', :text => 'Dave Lopper'
279 279 assert_select 'input[value=foo] + select option[selected=selected]', 0 # no option selected
280 280 end
281 281
282 def test_get_committers_without_changesets
283 Changeset.delete_all
284 @request.session[:user_id] = 2
285
286 get :committers, :id => 10
287 assert_response :success
288 assert_template 'committers'
289 end
290
282 291 def test_post_committers
283 292 @request.session[:user_id] = 2
284 293 # add a commit with an unknown user
285 294 c = Changeset.create!(
286 295 :repository => Project.find(1).repository,
287 296 :committer => 'foo',
288 297 :committed_on => Time.now,
289 298 :revision => 100,
290 299 :comments => 'Committed by foo.'
291 300 )
292 301 assert_no_difference "Changeset.where(:user_id => 3).count" do
293 302 post :committers, :id => 10, :committers => { '0' => ['foo', '2'], '1' => ['dlopper', '3']}
294 303 assert_response 302
295 304 assert_equal User.find(2), c.reload.user
296 305 end
297 306 end
298 307 end
General Comments 0
You need to be logged in to leave comments. Login now