issues_controller_test.rb 57.8 KB
Newer Older
1
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 4 5 6 7
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
14 15 16 17
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

18
require File.expand_path('../../test_helper', __FILE__)
19 20
require 'issues_controller'

edavis10's avatar
edavis10 committed
21
class IssuesControllerTest < ActionController::TestCase
22 23 24 25
  fixtures :projects,
           :users,
           :roles,
           :members,
26
           :member_roles,
27 28
           :issues,
           :issue_statuses,
29
           :versions,
30
           :trackers,
31
           :projects_trackers,
32 33 34
           :issue_categories,
           :enabled_modules,
           :enumerations,
35
           :attachments,
36 37 38
           :workflows,
           :custom_fields,
           :custom_values,
jplang's avatar
jplang committed
39
           :custom_fields_projects,
40
           :custom_fields_trackers,
41 42
           :time_entries,
           :journals,
edavis10's avatar
edavis10 committed
43 44
           :journal_details,
           :queries
45

46 47 48 49 50 51
  def setup
    @controller = IssuesController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    User.current = nil
  end
52

53
  def test_index
54
    Setting.default_language = 'en'
55

56 57 58 59 60
    get :index
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
    assert_nil assigns(:project)
61 62 63 64 65
    assert_tag :tag => 'a', :content => /Can't print recipes/
    assert_tag :tag => 'a', :content => /Subproject issue/
    # private projects hidden
    assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
    assert_no_tag :tag => 'a', :content => /Issue on project 2/
66 67
    # project column
    assert_tag :tag => 'th', :content => /Project/
68
  end
69

70 71 72 73 74 75 76 77 78 79
  def test_index_should_not_list_issues_when_module_disabled
    EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
    get :index
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
    assert_nil assigns(:project)
    assert_no_tag :tag => 'a', :content => /Can't print recipes/
    assert_tag :tag => 'a', :content => /Subproject issue/
  end
80

81 82 83 84 85 86 87 88 89 90
  def test_index_should_not_list_issues_when_module_disabled
    EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
    get :index
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
    assert_nil assigns(:project)
    assert_no_tag :tag => 'a', :content => /Can't print recipes/
    assert_tag :tag => 'a', :content => /Subproject issue/
  end
jplang's avatar
jplang committed
91 92 93 94 95 96 97

  def test_index_should_list_visible_issues_only
    get :index, :per_page => 100
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_nil assigns(:issues).detect {|issue| !issue.visible?}
  end
98

99
  def test_index_with_project
100
    Setting.display_subprojects_issues = 0
101 102 103 104
    get :index, :project_id => 1
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
105 106 107
    assert_tag :tag => 'a', :content => /Can't print recipes/
    assert_no_tag :tag => 'a', :content => /Subproject issue/
  end
108

109 110 111 112 113 114 115 116 117 118
  def test_index_with_project_and_subprojects
    Setting.display_subprojects_issues = 1
    get :index, :project_id => 1
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
    assert_tag :tag => 'a', :content => /Can't print recipes/
    assert_tag :tag => 'a', :content => /Subproject issue/
    assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
  end
119

120 121 122 123 124 125 126 127 128 129
  def test_index_with_project_and_subprojects_should_show_private_subprojects
    @request.session[:user_id] = 2
    Setting.display_subprojects_issues = 1
    get :index, :project_id => 1
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
    assert_tag :tag => 'a', :content => /Can't print recipes/
    assert_tag :tag => 'a', :content => /Subproject issue/
    assert_tag :tag => 'a', :content => /Issue of a private subproject/
130
  end
131

132
  def test_index_with_project_and_default_filter
133 134
    get :index, :project_id => 1, :set_filter => 1
    assert_response :success
135
    assert_template 'index'
136
    assert_not_nil assigns(:issues)
137

138 139 140 141 142
    query = assigns(:query)
    assert_not_nil query
    # default filter
    assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
  end
143

144
  def test_index_with_project_and_filter
145
    get :index, :project_id => 1, :set_filter => 1,
jplang's avatar
jplang committed
146 147
      :f => ['tracker_id'],
      :op => {'tracker_id' => '='},
148
      :v => {'tracker_id' => ['1']}
149 150 151
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
152

153 154 155 156
    query = assigns(:query)
    assert_not_nil query
    assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
  end
157

158 159 160
  def test_index_with_project_and_empty_filters
    get :index, :project_id => 1, :set_filter => 1, :fields => ['']
    assert_response :success
161
    assert_template 'index'
162
    assert_not_nil assigns(:issues)
163

164 165 166 167
    query = assigns(:query)
    assert_not_nil query
    # no filter
    assert_equal({}, query.filters)
168
  end
169

jplang's avatar
jplang committed
170 171 172
  def test_index_with_query
    get :index, :project_id => 1, :query_id => 5
    assert_response :success
173
    assert_template 'index'
jplang's avatar
jplang committed
174 175 176
    assert_not_nil assigns(:issues)
    assert_nil assigns(:issue_count_by_group)
  end
177

178
  def test_index_with_query_grouped_by_tracker
jplang's avatar
jplang committed
179 180 181 182
    get :index, :project_id => 1, :query_id => 6
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
183
    assert_not_nil assigns(:issue_count_by_group)
184
  end
185

186 187 188 189 190
  def test_index_with_query_grouped_by_list_custom_field
    get :index, :project_id => 1, :query_id => 9
    assert_response :success
    assert_template 'index.rhtml'
    assert_not_nil assigns(:issues)
191
    assert_not_nil assigns(:issue_count_by_group)
jplang's avatar
jplang committed
192
  end
193

194 195 196
  def test_private_query_should_not_be_available_to_other_users
    q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
    @request.session[:user_id] = 3
197

198 199 200
    get :index, :query_id => q.id
    assert_response 403
  end
201

202 203 204
  def test_private_query_should_be_available_to_its_user
    q = Query.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
    @request.session[:user_id] = 2
205

206 207 208
    get :index, :query_id => q.id
    assert_response :success
  end
209

210 211 212
  def test_public_query_should_be_available_to_other_users
    q = Query.create!(:name => "private", :user => User.find(2), :is_public => true, :project => nil)
    @request.session[:user_id] = 3
213

214 215 216
    get :index, :query_id => q.id
    assert_response :success
  end
217

jplang's avatar
jplang committed
218 219 220 221
  def test_index_sort_by_field_not_included_in_columns
    Setting.issue_list_default_columns = %w(subject author)
    get :index, :sort => 'tracker'
  end
222

223
  def test_index_csv_with_project
224
    Setting.default_language = 'en'
225

226 227 228 229
    get :index, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'text/csv', @response.content_type
230
    assert @response.body.starts_with?("#,")
231 232 233 234 235 236

    get :index, :project_id => 1, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'text/csv', @response.content_type
  end
237

238 239 240 241 242
  def test_index_pdf
    get :index, :format => 'pdf'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'application/pdf', @response.content_type
243

244 245 246 247
    get :index, :project_id => 1, :format => 'pdf'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'application/pdf', @response.content_type
248

jplang's avatar
jplang committed
249 250 251 252
    get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'application/pdf', @response.content_type
253
  end
254

255 256 257 258 259 260 261
  def test_index_pdf_with_query_grouped_by_list_custom_field
    get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_not_nil assigns(:issue_count_by_group)
    assert_equal 'application/pdf', @response.content_type
  end
262

263
  def test_index_sort
jplang's avatar
jplang committed
264
    get :index, :sort => 'tracker,id:desc'
265
    assert_response :success
266

jplang's avatar
jplang committed
267 268 269
    sort_params = @request.session['issues_index_sort']
    assert sort_params.is_a?(String)
    assert_equal 'tracker,id:desc', sort_params
270

jplang's avatar
jplang committed
271 272 273 274
    issues = assigns(:issues)
    assert_not_nil issues
    assert !issues.empty?
    assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
275
  end
276

277 278
  def test_index_with_columns
    columns = ['tracker', 'subject', 'assigned_to']
279
    get :index, :set_filter => 1, :c => columns
280
    assert_response :success
281

282 283 284 285
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
286

287 288 289 290
    # columns should be stored in session
    assert_kind_of Hash, session[:query]
    assert_kind_of Array, session[:query][:column_names]
    assert_equal columns, session[:query][:column_names].map(&:to_s)
291 292 293 294 295 296

    # ensure only these columns are kept in the selected columns list
    assert_tag :tag => 'select', :attributes => { :id => 'selected_columns' },
                                 :children => { :count => 3 }
    assert_no_tag :tag => 'option', :attributes => { :value => 'project' },
                                    :parent => { :tag => 'select', :attributes => { :id => "selected_columns" } }
297
  end
298

299 300 301 302
  def test_index_with_custom_field_column
    columns = %w(tracker subject cf_2)
    get :index, :set_filter => 1, :c => columns
    assert_response :success
303

304 305 306 307
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
308

309 310 311 312
    assert_tag :td,
      :attributes => {:class => 'cf_2 string'},
      :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
  end
313

314 315 316 317 318 319
  def test_index_send_html_if_query_is_invalid
    get :index, :f => ['start_date'], :op => {:start_date => '='}
    assert_equal 'text/html', @response.content_type
    assert_template 'index'
  end

320 321 322 323 324 325
  def test_index_send_nothing_if_query_is_invalid
    get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
    assert_equal 'text/csv', @response.content_type
    assert @response.body.blank?
  end

326
  def test_show_by_anonymous
jplang's avatar
jplang committed
327 328
    get :show, :id => 1
    assert_response :success
329
    assert_template 'show'
jplang's avatar
jplang committed
330
    assert_not_nil assigns(:issue)
331
    assert_equal Issue.find(1), assigns(:issue)
332

333 334 335
    # anonymous role is allowed to add a note
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
336
                                :child => { :tag => 'legend',
337
                                            :content => /Notes/ } }
jplang's avatar
jplang committed
338
  end
339

340 341 342 343
  def test_show_by_manager
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success
344

345 346
    assert_tag :tag => 'a',
      :content => /Quote/
347

348 349
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
350
                                :child => { :tag => 'legend',
351 352
                                            :content => /Change properties/ } },
               :descendant => { :tag => 'fieldset',
353
                                :child => { :tag => 'legend',
354 355
                                            :content => /Log time/ } },
               :descendant => { :tag => 'fieldset',
356
                                :child => { :tag => 'legend',
357 358
                                            :content => /Notes/ } }
  end
359

360 361 362 363 364 365 366 367 368
  def test_update_form_should_not_display_inactive_enumerations
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success

    assert ! IssuePriority.find(15).active?
    assert_no_tag :option, :attributes => {:value => '15'},
                           :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
  end
369

370 371 372
  def test_update_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :show, :id => 1
373

374 375 376 377 378 379 380
    assert_tag :tag => 'form',
      :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
      :descendant => {
        :tag => 'input',
        :attributes => {:type => 'file', :name => 'attachments[1][file]'}
      }
  end
381

jplang's avatar
jplang committed
382 383 384 385 386
  def test_show_should_deny_anonymous_access_without_permission
    Role.anonymous.remove_permission!(:view_issues)
    get :show, :id => 1
    assert_response :redirect
  end
387

jplang's avatar
jplang committed
388 389 390 391 392
  def test_show_should_deny_anonymous_access_to_private_issue
    Issue.update_all(["is_private = ?", true], "id = 1")
    get :show, :id => 1
    assert_response :redirect
  end
393

jplang's avatar
jplang committed
394 395 396 397 398 399
  def test_show_should_deny_non_member_access_without_permission
    Role.non_member.remove_permission!(:view_issues)
    @request.session[:user_id] = 9
    get :show, :id => 1
    assert_response 403
  end
400

jplang's avatar
jplang committed
401 402 403 404 405 406
  def test_show_should_deny_non_member_access_to_private_issue
    Issue.update_all(["is_private = ?", true], "id = 1")
    @request.session[:user_id] = 9
    get :show, :id => 1
    assert_response 403
  end
407

jplang's avatar
jplang committed
408 409 410 411 412 413
  def test_show_should_deny_member_access_without_permission
    Role.find(1).remove_permission!(:view_issues)
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response 403
  end
414

jplang's avatar
jplang committed
415 416 417 418 419 420
  def test_show_should_deny_member_access_to_private_issue_without_permission
    Issue.update_all(["is_private = ?", true], "id = 1")
    @request.session[:user_id] = 3
    get :show, :id => 1
    assert_response 403
  end
421

jplang's avatar
jplang committed
422 423 424 425 426 427
  def test_show_should_allow_author_access_to_private_issue
    Issue.update_all(["is_private = ?, author_id = 3", true], "id = 1")
    @request.session[:user_id] = 3
    get :show, :id => 1
    assert_response :success
  end
428

jplang's avatar
jplang committed
429 430 431 432 433 434
  def test_show_should_allow_assignee_access_to_private_issue
    Issue.update_all(["is_private = ?, assigned_to_id = 3", true], "id = 1")
    @request.session[:user_id] = 3
    get :show, :id => 1
    assert_response :success
  end
435

jplang's avatar
jplang committed
436 437 438 439 440 441 442
  def test_show_should_allow_member_access_to_private_issue_with_permission
    Issue.update_all(["is_private = ?", true], "id = 1")
    User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
    @request.session[:user_id] = 3
    get :show, :id => 1
    assert_response :success
  end
443

444 445 446 447 448
  def test_show_should_not_disclose_relations_to_invisible_issues
    Setting.cross_project_issue_relations = '1'
    IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
    # Relation to a private project issue
    IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
449

450 451
    get :show, :id => 1
    assert_response :success
452

453 454 455 456 457
    assert_tag :div, :attributes => { :id => 'relations' },
                     :descendant => { :tag => 'a', :content => /#2$/ }
    assert_no_tag :div, :attributes => { :id => 'relations' },
                        :descendant => { :tag => 'a', :content => /#4$/ }
  end
458

459 460 461
  def test_show_atom
    get :show, :id => 2, :format => 'atom'
    assert_response :success
462
    assert_template 'journals/index.rxml'
463
    # Inline image
464
    assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
465
  end
466

jplang's avatar
jplang committed
467
  def test_show_export_to_pdf
468
    get :show, :id => 3, :format => 'pdf'
jplang's avatar
jplang committed
469 470 471 472 473 474
    assert_response :success
    assert_equal 'application/pdf', @response.content_type
    assert @response.body.starts_with?('%PDF')
    assert_not_nil assigns(:issue)
  end

475 476 477 478 479
  def test_get_new
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :tracker_id => 1
    assert_response :success
    assert_template 'new'
480

481
    assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
482
                                                 :value => 'Default string' }
483 484 485 486 487

    # Be sure we don't display inactive IssuePriorities
    assert ! IssuePriority.find(15).active?
    assert_no_tag :option, :attributes => {:value => '15'},
                           :parent => {:tag => 'select', :attributes => {:id => 'issue_priority_id'} }
488
  end
489

490 491 492
  def test_get_new_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :tracker_id => 1
493

494 495 496 497 498 499 500
    assert_tag :tag => 'form',
      :attributes => {:id => 'issue-form', :method => 'post', :enctype => 'multipart/form-data'},
      :descendant => {
        :tag => 'input',
        :attributes => {:type => 'file', :name => 'attachments[1][file]'}
      }
  end
501 502 503 504 505 506

  def test_get_new_without_tracker_id
    @request.session[:user_id] = 2
    get :new, :project_id => 1
    assert_response :success
    assert_template 'new'
507

508 509 510 511
    issue = assigns(:issue)
    assert_not_nil issue
    assert_equal Project.find(1).trackers.first, issue.tracker
  end
512

513 514 515
  def test_get_new_with_no_default_status_should_display_an_error
    @request.session[:user_id] = 2
    IssueStatus.delete_all
516

517 518
    get :new, :project_id => 1
    assert_response 500
519
    assert_error_tag :content => /No default issue/
520
  end
521

522 523 524
  def test_get_new_with_no_tracker_should_display_an_error
    @request.session[:user_id] = 2
    Tracker.delete_all
525

526 527
    get :new, :project_id => 1
    assert_response 500
528
    assert_error_tag :content => /No tracker/
529
  end
530

531 532
  def test_update_new_form
    @request.session[:user_id] = 2
533
    xhr :post, :new, :project_id => 1,
534
                     :issue => {:tracker_id => 2,
535 536 537 538
                                :subject => 'This is the test_new issue',
                                :description => 'This is the description',
                                :priority_id => 5}
    assert_response :success
539
    assert_template 'attributes'
540

541 542 543 544 545 546
    issue = assigns(:issue)
    assert_kind_of Issue, issue
    assert_equal 1, issue.project_id
    assert_equal 2, issue.tracker_id
    assert_equal 'This is the test_new issue', issue.subject
  end
547

548
  def test_post_create
549
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
550
    assert_difference 'Issue.count' do
551
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
552
                 :issue => {:tracker_id => 3,
553
                            :status_id => 2,
edavis10's avatar
edavis10 committed
554 555 556
                            :subject => 'This is the test_new issue',
                            :description => 'This is the description',
                            :priority_id => 5,
557
                            :start_date => '2010-11-07',
edavis10's avatar
edavis10 committed
558 559 560 561
                            :estimated_hours => '',
                            :custom_field_values => {'2' => 'Value for field 2'}}
    end
    assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
562

563 564 565
    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_equal 2, issue.author_id
566
    assert_equal 3, issue.tracker_id
567
    assert_equal 2, issue.status_id
568
    assert_equal Date.parse('2010-11-07'), issue.start_date
569
    assert_nil issue.estimated_hours
570
    v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
571 572
    assert_not_nil v
    assert_equal 'Value for field 2', v.value
573
  end
574

575 576 577 578 579 580 581 582
  def test_post_new_with_group_assignment
    group = Group.find(11)
    project = Project.find(1)
    project.members << Member.new(:principal => group, :roles => [Role.first])

    with_settings :issue_group_assignment => '1' do
      @request.session[:user_id] = 2
      assert_difference 'Issue.count' do
583
        post :create, :project_id => project.id,
584 585 586 587 588 589 590
                      :issue => {:tracker_id => 3,
                                 :status_id => 1,
                                 :subject => 'This is the test_new_with_group_assignment issue',
                                 :assigned_to_id => group.id}
      end
    end
    assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
591

592 593 594 595
    issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
    assert_not_nil issue
    assert_equal group, issue.assigned_to
  end
596

597 598 599
  def test_post_create_without_start_date
    @request.session[:user_id] = 2
    assert_difference 'Issue.count' do
600
      post :create, :project_id => 1,
601 602 603 604 605 606 607 608 609 610
                 :issue => {:tracker_id => 3,
                            :status_id => 2,
                            :subject => 'This is the test_new issue',
                            :description => 'This is the description',
                            :priority_id => 5,
                            :start_date => '',
                            :estimated_hours => '',
                            :custom_field_values => {'2' => 'Value for field 2'}}
    end
    assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
611

612 613 614 615
    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_nil issue.start_date
  end
616

617
  def test_post_create_and_continue
618
    @request.session[:user_id] = 2
619 620 621 622 623
    assert_difference 'Issue.count' do
      post :create, :project_id => 1,
        :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
        :continue => ''
    end
624

625 626 627 628
    issue = Issue.first(:order => 'id DESC')
    assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
    assert_not_nil flash[:notice], "flash was not set"
    assert flash[:notice].include?("<a href='/issues/#{issue.id}'>##{issue.id}</a>"), "issue link not found in flash: #{flash[:notice]}"
629
  end
630

631
  def test_post_create_without_custom_fields_param
632
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
633
    assert_difference 'Issue.count' do
634
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
635 636 637 638 639 640
                 :issue => {:tracker_id => 1,
                            :subject => 'This is the test_new issue',
                            :description => 'This is the description',
                            :priority_id => 5}
    end
    assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
641
  end
642

643
  def test_post_create_with_required_custom_field_and_without_custom_fields_param
644 645 646 647
    field = IssueCustomField.find_by_name('Database')
    field.update_attribute(:is_required, true)

    @request.session[:user_id] = 2
648
    post :create, :project_id => 1,
649 650 651 652 653 654 655 656
               :issue => {:tracker_id => 1,
                          :subject => 'This is the test_new issue',
                          :description => 'This is the description',
                          :priority_id => 5}
    assert_response :success
    assert_template 'new'
    issue = assigns(:issue)
    assert_not_nil issue
657
    assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
658
  end
659

660
  def test_post_create_with_watchers
661 662
    @request.session[:user_id] = 2
    ActionMailer::Base.deliveries.clear
663

664
    assert_difference 'Watcher.count', 2 do
665
      post :create, :project_id => 1,
666 667 668 669 670 671 672
                 :issue => {:tracker_id => 1,
                            :subject => 'This is a new issue with watchers',
                            :description => 'This is the description',
                            :priority_id => 5,
                            :watcher_user_ids => ['2', '3']}
    end
    issue = Issue.find_by_subject('This is a new issue with watchers')
jplang's avatar
jplang committed
673 674
    assert_not_nil issue
    assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
675

676 677 678 679 680 681 682 683
    # Watchers added
    assert_equal [2, 3], issue.watcher_user_ids.sort
    assert issue.watched_by?(User.find(3))
    # Watchers notified
    mail = ActionMailer::Base.deliveries.last
    assert_kind_of TMail::Mail, mail
    assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
  end
684

685
  def test_post_create_subissue
jplang's avatar
jplang committed
686
    @request.session[:user_id] = 2
687

jplang's avatar
jplang committed
688
    assert_difference 'Issue.count' do
689
      post :create, :project_id => 1,
jplang's avatar
jplang committed
690 691 692 693 694 695 696 697
                 :issue => {:tracker_id => 1,
                            :subject => 'This is a child issue',
                            :parent_issue_id => 2}
    end
    issue = Issue.find_by_subject('This is a child issue')
    assert_not_nil issue
    assert_equal Issue.find(2), issue.parent
  end
698 699 700

  def test_post_create_subissue_with_non_numeric_parent_id
    @request.session[:user_id] = 2
701

702
    assert_difference 'Issue.count' do
703
      post :create, :project_id => 1,
704 705 706 707 708 709 710 711
                 :issue => {:tracker_id => 1,
                            :subject => 'This is a child issue',
                            :parent_issue_id => 'ABC'}
    end
    issue = Issue.find_by_subject('This is a child issue')
    assert_not_nil issue
    assert_nil issue.parent
  end
712

713 714 715 716 717 718 719 720 721 722 723 724
  def test_post_create_private
    @request.session[:user_id] = 2

    assert_difference 'Issue.count' do
      post :create, :project_id => 1,
                 :issue => {:tracker_id => 1,
                            :subject => 'This is a private issue',
                            :is_private => '1'}
    end
    issue = Issue.first(:order => 'id DESC')
    assert issue.is_private?
  end
725

726 727 728 729
  def test_post_create_private_with_set_own_issues_private_permission
    role = Role.find(1)
    role.remove_permission! :set_issues_private
    role.add_permission! :set_own_issues_private
730

731 732 733 734 735 736 737 738 739 740 741
    @request.session[:user_id] = 2

    assert_difference 'Issue.count' do
      post :create, :project_id => 1,
                 :issue => {:tracker_id => 1,
                            :subject => 'This is a private issue',
                            :is_private => '1'}
    end
    issue = Issue.first(:order => 'id DESC')
    assert issue.is_private?
  end
742

743
  def test_post_create_should_send_a_notification
744 745
    ActionMailer::Base.deliveries.clear
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
746
    assert_difference 'Issue.count' do
747
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
748 749 750 751 752 753 754 755
                 :issue => {:tracker_id => 3,
                            :subject => 'This is the test_new issue',
                            :description => 'This is the description',
                            :priority_id => 5,
                            :estimated_hours => '',
                            :custom_field_values => {'2' => 'Value for field 2'}}
    end
    assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
756

757 758
    assert_equal 1, ActionMailer::Base.deliveries.size
  end
759

760
  def test_post_create_should_preserve_fields_values_on_validation_failure
761
    @request.session[:user_id] = 2
762
    post :create, :project_id => 1,
763
               :issue => {:tracker_id => 1,
764 765 766
                          # empty subject
                          :subject => '',
                          :description => 'This is a description',
767 768 769 770
                          :priority_id => 6,
                          :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
    assert_response :success
    assert_template 'new'
771

772 773
    assert_tag :textarea, :attributes => { :name => 'issue[description]' },
                          :content => 'This is a description'
774 775 776
    assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
                        :child => { :tag => 'option', :attributes => { :selected => 'selected',
                                                                       :value => '6' },
777
                                                      :content => 'High' }
778 779 780 781
    # Custom fields
    assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
                        :child => { :tag => 'option', :attributes => { :selected => 'selected',
                                                                       :value => 'Oracle' },
782
                                                      :content => 'Oracle' }
783 784 785
    assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
                                        :value => 'Value for field 2'}
  end
786

787
  def test_post_create_should_ignore_non_safe_attributes
788 789
    @request.session[:user_id] = 2
    assert_nothing_raised do
790
      post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
791 792
    end
  end
793

794 795 796
  def test_post_create_with_attachment
    set_tmp_attachments_directory
    @request.session[:user_id] = 2
797

798 799
    assert_difference 'Issue.count' do
      assert_difference 'Attachment.count' do
800
        post :create, :project_id => 1,
801 802 803 804
          :issue => { :tracker_id => '1', :subject => 'With attachment' },
          :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
      end
    end
805

806 807
    issue = Issue.first(:order => 'id DESC')
    attachment = Attachment.first(:order => 'id DESC')
808

809 810 811 812 813 814 815 816 817 818
    assert_equal issue, attachment.container
    assert_equal 2, attachment.author_id
    assert_equal 'testfile.txt', attachment.filename
    assert_equal 'text/plain', attachment.content_type
    assert_equal 'test file', attachment.description
    assert_equal 59, attachment.filesize
    assert File.exists?(attachment.diskfile)
    assert_equal 59, File.size(attachment.diskfile)
  end

819 820 821
  context "without workflow privilege" do
    setup do
      Workflow.delete_all(["role_id = ?", Role.anonymous.id])
822
      Role.anonymous.add_permission! :add_issues, :add_issue_notes
823
    end
824

825 826 827 828 829 830 831 832 833 834
    context "#new" do
      should "propose default status only" do
        get :new, :project_id => 1
        assert_response :success
        assert_template 'new'
        assert_tag :tag => 'select',
          :attributes => {:name => 'issue[status_id]'},
          :children => {:count => 1},
          :child => {:tag => 'option', :attributes => {:value => IssueStatus.default.id.to_s}}
      end
835

836 837
      should "accept default status" do
        assert_difference 'Issue.count' do
838
          post :create, :project_id => 1,
839 840 841 842 843 844 845
                     :issue => {:tracker_id => 1,
                                :subject => 'This is an issue',
                                :status_id => 1}
        end
        issue = Issue.last(:order => 'id')
        assert_equal IssueStatus.default, issue.status
      end
846

jplang's avatar