issues_controller_test.rb 61.3 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
    get :index
    assert_response :success
58
    assert_template 'index'
59 60
    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
  def test_index_should_not_list_issues_when_module_disabled
    EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
72 73
    get :index
    assert_response :success
74
    assert_template 'index'
75 76 77 78 79
    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
80 81 82 83 84 85 86

  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
87

88
  def test_index_with_project
89
    Setting.display_subprojects_issues = 0
90 91
    get :index, :project_id => 1
    assert_response :success
92
    assert_template 'index'
93
    assert_not_nil assigns(:issues)
94 95 96
    assert_tag :tag => 'a', :content => /Can't print recipes/
    assert_no_tag :tag => 'a', :content => /Subproject issue/
  end
97

98 99 100 101
  def test_index_with_project_and_subprojects
    Setting.display_subprojects_issues = 1
    get :index, :project_id => 1
    assert_response :success
102
    assert_template 'index'
103 104 105 106 107
    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
108

109 110 111 112 113
  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
114
    assert_template 'index'
115 116 117 118
    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/
119
  end
120

121
  def test_index_with_project_and_default_filter
122 123
    get :index, :project_id => 1, :set_filter => 1
    assert_response :success
124
    assert_template 'index'
125
    assert_not_nil assigns(:issues)
126

127 128 129 130 131
    query = assigns(:query)
    assert_not_nil query
    # default filter
    assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
  end
132

133
  def test_index_with_project_and_filter
134
    get :index, :project_id => 1, :set_filter => 1,
jplang's avatar
jplang committed
135 136
      :f => ['tracker_id'],
      :op => {'tracker_id' => '='},
137
      :v => {'tracker_id' => ['1']}
138
    assert_response :success
139
    assert_template 'index'
140
    assert_not_nil assigns(:issues)
141

142 143 144 145
    query = assigns(:query)
    assert_not_nil query
    assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
  end
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  def test_index_with_short_filters

    to_test = {
      'status_id' => {
        'o' => { :op => 'o', :values => [''] },
        'c' => { :op => 'c', :values => [''] },
        '7' => { :op => '=', :values => ['7'] },
        '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
        '=7' => { :op => '=', :values => ['7'] },
        '!3' => { :op => '!', :values => ['3'] },
        '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
      'subject' => {
        'This is a subject' => { :op => '=', :values => ['This is a subject'] },
        'o' => { :op => '=', :values => ['o'] },
        '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
        '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
163 164
      'tracker_id' => {
        '3' => { :op => '=', :values => ['3'] },
165 166 167
        '=3' => { :op => '=', :values => ['3'] },
        '*' => { :op => '=', :values => ['*'] },
        '!*' => { :op => '!', :values => ['*'] }},
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
      'start_date' => {
        '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
        '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
        '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
        '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
        '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
        '<t+2' => { :op => '<t+', :values => ['2'] },
        '>t+2' => { :op => '>t+', :values => ['2'] },
        't+2' => { :op => 't+', :values => ['2'] },
        't' => { :op => 't', :values => [''] },
        'w' => { :op => 'w', :values => [''] },
        '>t-2' => { :op => '>t-', :values => ['2'] },
        '<t-2' => { :op => '<t-', :values => ['2'] },
        't-2' => { :op => 't-', :values => ['2'] }},
      'created_on' => {
        '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
        '<t+2' => { :op => '=', :values => ['<t+2'] },
        '>t+2' => { :op => '=', :values => ['>t+2'] },
        't+2' => { :op => 't', :values => ['+2'] }},
      'cf_1' => {
        'c' => { :op => '=', :values => ['c'] },
        '!c' => { :op => '!', :values => ['c'] },
        '!*' => { :op => '!*', :values => [''] },
        '*' => { :op => '*', :values => [''] }},
      'estimated_hours' => {
        '=13.4' => { :op => '=', :values => ['13.4'] },
        '>=45' => { :op => '>=', :values => ['45'] },
        '<=125' => { :op => '<=', :values => ['125'] },
        '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
        '!*' => { :op => '!*', :values => [''] },
        '*' => { :op => '*', :values => [''] }}
    }

    default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}

    to_test.each do |field, expression_and_expected|
      expression_and_expected.each do |filter_expression, expected|
emassip's avatar
emassip committed
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        get :index, :set_filter => 1, field => filter_expression

        assert_response :success
        assert_template 'index'
        assert_not_nil assigns(:issues)

        query = assigns(:query)
        assert_not_nil query
        assert query.has_filter?(field)
        assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
      end
    end

  end

221 222 223
  def test_index_with_project_and_empty_filters
    get :index, :project_id => 1, :set_filter => 1, :fields => ['']
    assert_response :success
224
    assert_template 'index'
225
    assert_not_nil assigns(:issues)
226

227 228 229 230
    query = assigns(:query)
    assert_not_nil query
    # no filter
    assert_equal({}, query.filters)
231
  end
232

jplang's avatar
jplang committed
233 234 235
  def test_index_with_query
    get :index, :project_id => 1, :query_id => 5
    assert_response :success
236
    assert_template 'index'
jplang's avatar
jplang committed
237 238 239
    assert_not_nil assigns(:issues)
    assert_nil assigns(:issue_count_by_group)
  end
240

241
  def test_index_with_query_grouped_by_tracker
jplang's avatar
jplang committed
242 243
    get :index, :project_id => 1, :query_id => 6
    assert_response :success
244
    assert_template 'index'
jplang's avatar
jplang committed
245
    assert_not_nil assigns(:issues)
246
    assert_not_nil assigns(:issue_count_by_group)
247
  end
248

249 250 251
  def test_index_with_query_grouped_by_list_custom_field
    get :index, :project_id => 1, :query_id => 9
    assert_response :success
252
    assert_template 'index'
253
    assert_not_nil assigns(:issues)
254
    assert_not_nil assigns(:issue_count_by_group)
jplang's avatar
jplang committed
255
  end
256

257 258 259
  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
260

261 262 263
    get :index, :query_id => q.id
    assert_response 403
  end
264

265 266 267
  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
268

269 270 271
    get :index, :query_id => q.id
    assert_response :success
  end
272

273 274 275
  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
276

277 278 279
    get :index, :query_id => q.id
    assert_response :success
  end
280

jplang's avatar
jplang committed
281 282 283 284
  def test_index_sort_by_field_not_included_in_columns
    Setting.issue_list_default_columns = %w(subject author)
    get :index, :sort => 'tracker'
  end
285

286
  def test_index_csv_with_project
287
    Setting.default_language = 'en'
288

289 290 291 292
    get :index, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'text/csv', @response.content_type
293
    assert @response.body.starts_with?("#,")
294 295 296 297 298 299

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

301 302 303 304 305
  def test_index_pdf
    get :index, :format => 'pdf'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'application/pdf', @response.content_type
306

307 308 309 310
    get :index, :project_id => 1, :format => 'pdf'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'application/pdf', @response.content_type
311

jplang's avatar
jplang committed
312 313 314 315
    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
316
  end
317

318 319 320 321 322 323 324
  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
325

326
  def test_index_sort
jplang's avatar
jplang committed
327
    get :index, :sort => 'tracker,id:desc'
328
    assert_response :success
329

jplang's avatar
jplang committed
330 331 332
    sort_params = @request.session['issues_index_sort']
    assert sort_params.is_a?(String)
    assert_equal 'tracker,id:desc', sort_params
333

jplang's avatar
jplang committed
334 335 336 337
    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)
338
  end
339

340 341
  def test_index_with_columns
    columns = ['tracker', 'subject', 'assigned_to']
342
    get :index, :set_filter => 1, :c => columns
343
    assert_response :success
344

345 346 347 348
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
349

350 351 352 353
    # 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)
354 355 356 357 358 359

    # 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" } }
360
  end
361

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
  def test_index_without_project_should_implicitly_add_project_column_to_default_columns
    Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
    get :index, :set_filter => 1

    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal [:project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
  end

  def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
    Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
    columns = ['tracker', 'subject', 'assigned_to']
    get :index, :set_filter => 1, :c => columns

    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns.map(&:to_sym), query.columns.map(&:name)
  end

383 384 385 386
  def test_index_with_custom_field_column
    columns = %w(tracker subject cf_2)
    get :index, :set_filter => 1, :c => columns
    assert_response :success
387

388 389 390 391
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
392

393 394 395 396
    assert_tag :td,
      :attributes => {:class => 'cf_2 string'},
      :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
  end
397

398 399 400 401 402 403
  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

404 405 406 407 408 409
  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

410
  def test_show_by_anonymous
411 412
    get :show, :id => 1
    assert_response :success
413
    assert_template 'show'
414
    assert_not_nil assigns(:issue)
415
    assert_equal Issue.find(1), assigns(:issue)
416

417 418 419
    # anonymous role is allowed to add a note
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
420
                                :child => { :tag => 'legend',
421
                                            :content => /Notes/ } }
422
  end
423

424 425 426 427
  def test_show_by_manager
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success
428

429 430
    assert_tag :tag => 'a',
      :content => /Quote/
431

432 433
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
434
                                :child => { :tag => 'legend',
435 436
                                            :content => /Change properties/ } },
               :descendant => { :tag => 'fieldset',
437
                                :child => { :tag => 'legend',
438 439
                                            :content => /Log time/ } },
               :descendant => { :tag => 'fieldset',
440
                                :child => { :tag => 'legend',
441 442
                                            :content => /Notes/ } }
  end
443

444 445 446 447 448 449 450 451 452
  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
453

454 455 456
  def test_update_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :show, :id => 1
457

458 459 460 461 462 463 464
    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
465

jplang's avatar
jplang committed
466 467 468 469 470
  def test_show_should_deny_anonymous_access_without_permission
    Role.anonymous.remove_permission!(:view_issues)
    get :show, :id => 1
    assert_response :redirect
  end
471

jplang's avatar
jplang committed
472 473 474 475 476
  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
477

jplang's avatar
jplang committed
478 479 480 481 482 483
  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
484

jplang's avatar
jplang committed
485 486 487 488 489 490
  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
491

jplang's avatar
jplang committed
492 493 494 495 496 497
  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
498

jplang's avatar
jplang committed
499 500 501 502 503 504
  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
505

jplang's avatar
jplang committed
506 507 508 509 510 511
  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
512

jplang's avatar
jplang committed
513 514 515 516 517 518
  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
519

jplang's avatar
jplang committed
520 521 522 523 524 525 526
  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
527

528 529 530 531 532
  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')
533

534 535
    get :show, :id => 1
    assert_response :success
536

537 538 539 540 541
    assert_tag :div, :attributes => { :id => 'relations' },
                     :descendant => { :tag => 'a', :content => /#2$/ }
    assert_no_tag :div, :attributes => { :id => 'relations' },
                        :descendant => { :tag => 'a', :content => /#4$/ }
  end
542

543 544 545
  def test_show_atom
    get :show, :id => 2, :format => 'atom'
    assert_response :success
546
    assert_template 'journals/index'
547
    # Inline image
548
    assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
549
  end
550

jplang's avatar
jplang committed
551
  def test_show_export_to_pdf
552
    get :show, :id => 3, :format => 'pdf'
jplang's avatar
jplang committed
553 554 555 556 557 558
    assert_response :success
    assert_equal 'application/pdf', @response.content_type
    assert @response.body.starts_with?('%PDF')
    assert_not_nil assigns(:issue)
  end

559 560 561 562 563
  def test_get_new
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :tracker_id => 1
    assert_response :success
    assert_template 'new'
564

565
    assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
566
                                                 :value => 'Default string' }
567 568 569 570 571

    # 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'} }
572
  end
573

574 575 576
  def test_get_new_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :tracker_id => 1
577

578 579 580 581 582 583 584
    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
585 586 587 588 589 590

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

592 593 594 595
    issue = assigns(:issue)
    assert_not_nil issue
    assert_equal Project.find(1).trackers.first, issue.tracker
  end
596

597 598 599
  def test_get_new_with_no_default_status_should_display_an_error
    @request.session[:user_id] = 2
    IssueStatus.delete_all
600

601 602
    get :new, :project_id => 1
    assert_response 500
603
    assert_error_tag :content => /No default issue/
604
  end
605

606 607 608
  def test_get_new_with_no_tracker_should_display_an_error
    @request.session[:user_id] = 2
    Tracker.delete_all
609

610 611
    get :new, :project_id => 1
    assert_response 500
612
    assert_error_tag :content => /No tracker/
613
  end
614

615 616
  def test_update_new_form
    @request.session[:user_id] = 2
617
    xhr :post, :new, :project_id => 1,
618
                     :issue => {:tracker_id => 2,
619 620 621 622
                                :subject => 'This is the test_new issue',
                                :description => 'This is the description',
                                :priority_id => 5}
    assert_response :success
623
    assert_template 'attributes'
624

625 626 627 628 629 630
    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
631

632
  def test_post_create
633
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
634
    assert_difference 'Issue.count' do
635
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
636
                 :issue => {:tracker_id => 3,
637
                            :status_id => 2,
edavis10's avatar
edavis10 committed
638 639 640
                            :subject => 'This is the test_new issue',
                            :description => 'This is the description',
                            :priority_id => 5,
641
                            :start_date => '2010-11-07',
edavis10's avatar
edavis10 committed
642 643 644 645
                            :estimated_hours => '',
                            :custom_field_values => {'2' => 'Value for field 2'}}
    end
    assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
646

647 648 649
    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_equal 2, issue.author_id
650
    assert_equal 3, issue.tracker_id
651
    assert_equal 2, issue.status_id
652
    assert_equal Date.parse('2010-11-07'), issue.start_date
653
    assert_nil issue.estimated_hours
654
    v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
655 656
    assert_not_nil v
    assert_equal 'Value for field 2', v.value
657
  end
658

659 660 661 662 663 664 665 666
  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
667
        post :create, :project_id => project.id,
668 669 670 671 672 673 674
                      :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
675

676 677 678 679
    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
680

681 682 683
  def test_post_create_without_start_date
    @request.session[:user_id] = 2
    assert_difference 'Issue.count' do
684
      post :create, :project_id => 1,
685 686 687 688 689 690 691 692 693 694
                 :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
695

696 697 698 699
    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_nil issue.start_date
  end
700

701
  def test_post_create_and_continue
702
    @request.session[:user_id] = 2
703 704 705 706 707
    assert_difference 'Issue.count' do
      post :create, :project_id => 1,
        :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
        :continue => ''
    end
708

709 710 711 712
    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]}"
713
  end
714

715
  def test_post_create_without_custom_fields_param
716
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
717
    assert_difference 'Issue.count' do
718
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
719 720 721 722 723 724
                 :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
725
  end
726

727
  def test_post_create_with_required_custom_field_and_without_custom_fields_param
728 729 730 731
    field = IssueCustomField.find_by_name('Database')
    field.update_attribute(:is_required, true)

    @request.session[:user_id] = 2
732
    post :create, :project_id => 1,
733 734 735 736 737 738 739 740
               :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
741
    assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
742
  end
743

744
  def test_post_create_with_watchers
745 746
    @request.session[:user_id] = 2
    ActionMailer::Base.deliveries.clear
747

748
    assert_difference 'Watcher.count', 2 do
749
      post :create, :project_id => 1,
750 751 752 753 754 755 756
                 :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
757 758
    assert_not_nil issue
    assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
759

760 761 762 763 764 765 766 767
    # 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
768

769
  def test_post_create_subissue
jplang's avatar
jplang committed
770
    @request.session[:user_id] = 2
771

jplang's avatar
jplang committed
772
    assert_difference 'Issue.count' do
773
      post :create, :project_id => 1,
jplang's avatar
jplang committed
774 775 776 777 778 779 780 781
                 :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
782 783 784

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

786
    assert_difference 'Issue.count' do
787
      post :create, :project_id => 1,
788 789 790 791 792 793 794 795
                 :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
796

797 798 799 800 801 802 803 804 805 806 807 808
  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
809

810 811 812 813
  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
814

815 816 817 818 819 820 821 822 823 824 825
    @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
826

827
  def test_post_create_should_send_a_notification
828 829
    ActionMailer::Base.deliveries.clear
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
830
    assert_difference 'Issue.count' do
831
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
832 833 834 835 836 837 838 839
                 :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
840

841 842
    assert_equal 1, ActionMailer::Base.deliveries.size
  end
843

844
  def test_post_create_should_preserve_fields_values_on_validation_failure
845
    @request.session[:user_id] = 2
846
    post :create, :project_id => 1,
847
               :issue => {:tracker_id => 1,
848 849 850
                          # empty subject
                          :subject => '',
                          :description => 'This is a description',
851 852 853 854
                          :priority_id => 6,
                          :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
    assert_response :success
    assert_template 'new'
855

856 857
    assert_tag :textarea, :attributes => { :name => 'issue[description]' },
                          :content => 'This is a description'
858 859 860
    assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
                        :child => { :tag => 'option', :attributes => { :selected => 'selected',
                                                                       :value => '6' },
861
                                                      :content => 'High' }
862 863 864 865
    # Custom fields
    assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
                        :child => { :tag => 'option', :attributes => { :selected => 'selected',
                                                                       :value => 'Oracle' },
866
                                                      :content => 'Oracle' }
867 868 869
    assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
                                        :value => 'Value for field 2'}
  end
870

871
  def test_post_create_should_ignore_non_safe_attributes
872 873
    @request.session[:user_id] = 2
    assert_nothing_raised do
874
      post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
875 876
    end
  end
877

878 879 880
  def test_post_create_with_attachment
    set_tmp_attachments_directory
    @request.session[:user_id] = 2
881

882 883
    assert_difference 'Issue.count' do
      assert_difference 'Attachment.count' do
884
        post :create, :project_id => 1,
885 886 887 888
          :issue => { :tracker_id => '1', :subject => 'With attachment' },
          :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
      end
    end
889

890 891
    issue = Issue.first(:order => 'id DESC')
    attachment = Attachment.first(:order => 'id DESC')
892

893 894 895 896 897 898 899 900 901 902
    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

903 904 905
  context "without workflow privilege" do
    setup do
      Workflow.delete_all(["role_id = ?", Role.anonymous.id])
906
      Role.anonymous.add_permission! :add_issues, :add_issue_notes
907
    end
908

909 910 911 912 913 914 915 916 917 918
    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
919

920 921
      should "accept default status" do
        assert_difference 'Issue.count' do
922
          post :create, :project_id => 1,
923 924 925 926 927 928 929
                     :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
930

931 932
      should "ignore unauthorized status" do
        assert_difference 'Issue.count' do
933
          post :create, :project_id => 1,
934 935 936 937 938 939 940 941
                     :issue => {:tracker_id => 1,
                                :subject => 'This is an issue',
                                :status_id => 3}
        end
        issue = Issue.last(:order => 'id')
        assert_equal IssueStatus.default, issue.status
      end
    end
942

943 944 945 946 947 948 949
    context "#update" do
      should "ignore status change" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
        end
        assert_equal 1, Issue.find(1).status_id
      end
950

951 952 953 954 955 956 957 958 959 960
      should "ignore attributes changes" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
        end
        issue = Issue.find(1)
        assert_equal "Can't print recipes", issue.subject
        assert_nil issue.assigned_to
      end
    end
  end
961

962 963 964 965 966 967 968
  context "with workflow privilege" do
    setup do
      Workflow.delete_all(["role_id = ?", Role.anonymous.id])
      Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
      Workflow.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
      Role.anonymous.add_permission! :add_issues, :add_issue_notes
    end
969

970 971 972 973 974 975 976
    context "#update" do
      should "accept authorized status" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
        end
        assert_equal 3, Issue.find(1).status_id
      end
977

978 979 980 981 982 983
      should "ignore unauthorized status" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
        end
        assert_equal 1, Issue.find(1).status_id
      end
984

985 986 987 988 989 990 991
      should "accept authorized attributes changes" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:assigned_to_id => 2}
        end
        issue = Issue.find(1)
        assert_equal 2, issue.assigned_to_id
      end
992

993 994 995 996 997 998 999 1000
      should "ignore unauthorized attributes changes" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed'}
        end
        issue = Issue.find(1)
        assert_equal "Can't print recipes", issue.subject
      end
    end
1001

1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
    context "and :edit_issues permission" do
      setup do
        Role.anonymous.add_permission! :add_issues, :edit_issues
      end

      should "accept authorized status" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 3}
        end
        assert_equal 3, Issue.find(1).status_id
      end
1013

1014 1015 1016 1017 1018 1019
      should "ignore unauthorized status" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:status_id => 2}
        end
        assert_equal 1, Issue.find(1).status_id
      end
1020

1021 1022 1023 1024 1025 1026 1027 1028 1029
      should "accept authorized attributes changes" do
        assert_difference 'Journal.count' do
          put :update, :id => 1, :notes => 'just trying', :issue => {:subject => 'changed', :assigned_to_id => 2}
        end
        issue = Issue.find(1)
        assert_equal "changed", issue.subject
        assert_equal 2, issue.assigned_to_id
      end
    end
1030
  end
1031

1032 1033 1034 1035 1036 1037 1038 1039
  def test_copy_issue
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :copy_from => 1
    assert_template 'new'
    assert_not_nil assigns(:issue)
    orig = Issue.find(1)
    assert_equal orig.subject, assigns(:issue).subject
  end
1040

1041 1042 1043 1044 1045 1046 1047
  def test_get_edit
    @request.session[:user_id] = 2
    get :edit, :id => 1
    assert_response :success
    assert_template 'edit'
    assert_not_nil assigns(:issue)
    assert_equal Issue.find(1), assigns(:issue)
1048 1049 1050 1051 1052

    # 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'} }
1053
  end