GitLab wurde erfolgreich aktualisiert. Durch regelmäßige Updates bleibt das THM GitLab sicher. Danke für Ihre Geduld.

issues_controller_test.rb 67 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
tmaruyama's avatar
tmaruyama committed
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
tmaruyama's avatar
tmaruyama committed
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'] },
emassip's avatar
emassip committed
165
        '=3' => { :op => '=', :values => ['3'] }},
166 167 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
      '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
203

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
        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

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

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

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

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

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

255 256 257
  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
258

259 260 261
    get :index, :query_id => q.id
    assert_response 403
  end
262

263 264 265
  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
266

267 268 269
    get :index, :query_id => q.id
    assert_response :success
  end
270

271 272 273
  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
274

275 276 277
    get :index, :query_id => q.id
    assert_response :success
  end
278

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

284
  def test_index_csv
285 286 287 288
    get :index, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'text/csv', @response.content_type
289
    assert @response.body.starts_with?("#,")
290 291 292
    lines = @response.body.chomp.split("\n")
    assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
  end
293

294
  def test_index_csv_with_project
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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  def test_index_csv_with_description
    get :index, :format => 'csv', :description => '1'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'text/csv', @response.content_type
    assert @response.body.starts_with?("#,")
    lines = @response.body.chomp.split("\n")
    assert_equal assigns(:query).columns.size + 2, lines[0].split(',').size
  end

  def test_index_csv_with_all_columns
    get :index, :format => 'csv', :columns => 'all'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'text/csv', @response.content_type
    assert @response.body.starts_with?("#,")
    lines = @response.body.chomp.split("\n")
    assert_equal assigns(:query).available_columns.size + 1, lines[0].split(',').size
  end

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  def test_index_csv_big_5
    with_settings :default_language => "zh-TW" do
      str_utf8  = "\xe4\xb8\x80\xe6\x9c\x88"
      str_big5  = "\xa4@\xa4\xeb"
      if str_utf8.respond_to?(:force_encoding)
        str_utf8.force_encoding('UTF-8')
        str_big5.force_encoding('Big5')
      end
      issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
                        :status_id => 1, :priority => IssuePriority.all.first,
                        :subject => str_utf8)
      assert issue.save

      get :index, :project_id => 1, 
                  :f => ['subject'], 
                  :op => '=', :values => [str_utf8],
                  :format => 'csv'
      assert_equal 'text/csv', @response.content_type
339
      lines = @response.body.chomp.split("\n")
340 341 342 343 344 345 346 347 348
      s1 = "\xaa\xac\xbaA"
      if str_utf8.respond_to?(:force_encoding)
        s1.force_encoding('Big5')
      end
      assert lines[0].include?(s1)
      assert lines[1].include?(str_big5)
    end
  end

349 350 351 352 353 354 355 356 357 358 359 360 361 362
  def test_index_csv_cannot_convert_should_be_replaced_big_5
    with_settings :default_language => "zh-TW" do
      str_utf8  = "\xe4\xbb\xa5\xe5\x86\x85"
      if str_utf8.respond_to?(:force_encoding)
        str_utf8.force_encoding('UTF-8')
      end
      issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
                        :status_id => 1, :priority => IssuePriority.all.first,
                        :subject => str_utf8)
      assert issue.save

      get :index, :project_id => 1, 
                  :f => ['subject'], 
                  :op => '=', :values => [str_utf8],
363 364 365
                  :c => ['status', 'subject'],
                  :format => 'csv',
                  :set_filter => 1
366
      assert_equal 'text/csv', @response.content_type
367 368
      lines = @response.body.chomp.split("\n")
      s1 = "\xaa\xac\xbaA" # status
369 370 371 372
      if str_utf8.respond_to?(:force_encoding)
        s1.force_encoding('Big5')
      end
      assert lines[0].include?(s1)
373
      s2 = lines[1].split(",")[2]
374
      if s1.respond_to?(:force_encoding)
375
        s3 = "\xa5H?" # subject
376 377 378 379 380 381 382 383 384 385
        s3.force_encoding('Big5')
        assert_equal s3, s2
      elsif RUBY_PLATFORM == 'java'
        assert_equal "??", s2
      else
        assert_equal "\xa5H???", s2
      end
    end
  end

386 387 388 389 390
  def test_index_pdf
    get :index, :format => 'pdf'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'application/pdf', @response.content_type
391

392 393 394 395
    get :index, :project_id => 1, :format => 'pdf'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'application/pdf', @response.content_type
396

jplang's avatar
jplang committed
397 398 399 400
    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
401
  end
402

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

411
  def test_index_sort
jplang's avatar
jplang committed
412
    get :index, :sort => 'tracker,id:desc'
413
    assert_response :success
414

jplang's avatar
jplang committed
415 416 417
    sort_params = @request.session['issues_index_sort']
    assert sort_params.is_a?(String)
    assert_equal 'tracker,id:desc', sort_params
418

jplang's avatar
jplang committed
419 420 421 422
    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)
423
  end
424 425 426 427 428 429 430 431 432 433
  
  def test_index_sort_by_author
    get :index, :sort => 'author'
    assert_response :success
  end
  
  def test_index_group_by_author
    get :index, :group_by => 'author', :sort => 'priority'
    assert_response :success
  end
434

435 436
  def test_index_with_columns
    columns = ['tracker', 'subject', 'assigned_to']
437
    get :index, :set_filter => 1, :c => columns
438
    assert_response :success
439

440 441 442 443
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
444

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

    # 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" } }
455
  end
456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
  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

478 479 480 481
  def test_index_with_custom_field_column
    columns = %w(tracker subject cf_2)
    get :index, :set_filter => 1, :c => columns
    assert_response :success
482

483 484 485 486
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
487

488 489 490 491
    assert_tag :td,
      :attributes => {:class => 'cf_2 string'},
      :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
  end
492

493 494 495 496 497 498
  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

499 500 501 502 503 504
  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

505
  def test_show_by_anonymous
jplang's avatar
jplang committed
506 507
    get :show, :id => 1
    assert_response :success
508
    assert_template 'show'
jplang's avatar
jplang committed
509
    assert_not_nil assigns(:issue)
510
    assert_equal Issue.find(1), assigns(:issue)
511

512 513 514
    # anonymous role is allowed to add a note
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
515
                                :child => { :tag => 'legend',
516
                                            :content => /Notes/ } }
517 518
    assert_tag :tag => 'title',
      :content => "Bug #1: Can't print recipes - eCookbook - Redmine"
jplang's avatar
jplang committed
519
  end
520

521 522 523 524
  def test_show_by_manager
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success
525

526 527
    assert_tag :tag => 'a',
      :content => /Quote/
528

529 530
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
531
                                :child => { :tag => 'legend',
532 533
                                            :content => /Change properties/ } },
               :descendant => { :tag => 'fieldset',
534
                                :child => { :tag => 'legend',
535 536
                                            :content => /Log time/ } },
               :descendant => { :tag => 'fieldset',
537
                                :child => { :tag => 'legend',
538 539
                                            :content => /Notes/ } }
  end
540

541 542 543 544 545 546 547 548 549
  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
550

551 552 553
  def test_update_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :show, :id => 1
554

555 556 557 558 559 560 561
    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
562

jplang's avatar
jplang committed
563 564 565 566 567
  def test_show_should_deny_anonymous_access_without_permission
    Role.anonymous.remove_permission!(:view_issues)
    get :show, :id => 1
    assert_response :redirect
  end
568

jplang's avatar
jplang committed
569 570 571 572 573
  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
574

jplang's avatar
jplang committed
575 576 577 578 579 580
  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
581

jplang's avatar
jplang committed
582 583 584 585 586 587
  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
588

jplang's avatar
jplang committed
589 590 591 592 593 594
  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
595

jplang's avatar
jplang committed
596 597 598 599 600 601
  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
602

jplang's avatar
jplang committed
603 604 605 606 607 608
  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
609

jplang's avatar
jplang committed
610 611 612 613 614 615
  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
616

jplang's avatar
jplang committed
617 618 619 620 621 622 623
  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
624

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

631 632
    get :show, :id => 1
    assert_response :success
633

634 635 636 637 638
    assert_tag :div, :attributes => { :id => 'relations' },
                     :descendant => { :tag => 'a', :content => /#2$/ }
    assert_no_tag :div, :attributes => { :id => 'relations' },
                        :descendant => { :tag => 'a', :content => /#4$/ }
  end
639

640 641 642
  def test_show_atom
    get :show, :id => 2, :format => 'atom'
    assert_response :success
643
    assert_template 'journals/index'
644
    # Inline image
645
    assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
646
  end
647

jplang's avatar
jplang committed
648
  def test_show_export_to_pdf
649
    get :show, :id => 3, :format => 'pdf'
jplang's avatar
jplang committed
650 651 652 653 654 655
    assert_response :success
    assert_equal 'application/pdf', @response.content_type
    assert @response.body.starts_with?('%PDF')
    assert_not_nil assigns(:issue)
  end

656 657 658 659 660
  def test_get_new
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :tracker_id => 1
    assert_response :success
    assert_template 'new'
661

662
    assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
663
                                                 :value => 'Default string' }
664 665 666 667 668

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

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  def test_get_new_without_default_start_date_is_creation_date
    Setting.default_issue_start_date_to_creation_date = 0

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

    assert_tag :tag => 'input', :attributes => { :name => 'issue[start_date]',
                                                 :value => nil }
  end

  def test_get_new_with_default_start_date_is_creation_date
    Setting.default_issue_start_date_to_creation_date = 1

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

    assert_tag :tag => 'input', :attributes => { :name => 'issue[start_date]',
                                                 :value => Date.today.to_s }
  end

695 696 697
  def test_get_new_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :tracker_id => 1
698

699 700 701 702 703 704 705
    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
706 707 708 709 710 711

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

713 714 715 716
    issue = assigns(:issue)
    assert_not_nil issue
    assert_equal Project.find(1).trackers.first, issue.tracker
  end
717

718 719 720
  def test_get_new_with_no_default_status_should_display_an_error
    @request.session[:user_id] = 2
    IssueStatus.delete_all
721

722 723
    get :new, :project_id => 1
    assert_response 500
724
    assert_error_tag :content => /No default issue/
725
  end
726

727 728 729
  def test_get_new_with_no_tracker_should_display_an_error
    @request.session[:user_id] = 2
    Tracker.delete_all
730

731 732
    get :new, :project_id => 1
    assert_response 500
733
    assert_error_tag :content => /No tracker/
734
  end
735

736 737
  def test_update_new_form
    @request.session[:user_id] = 2
738
    xhr :post, :new, :project_id => 1,
739
                     :issue => {:tracker_id => 2,
740 741 742 743
                                :subject => 'This is the test_new issue',
                                :description => 'This is the description',
                                :priority_id => 5}
    assert_response :success
744
    assert_template 'attributes'
745

746 747 748 749 750 751
    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
752

753
  def test_post_create
754
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
755
    assert_difference 'Issue.count' do
756
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
757
                 :issue => {:tracker_id => 3,
758
                            :status_id => 2,
edavis10's avatar
edavis10 committed
759 760 761
                            :subject => 'This is the test_new issue',
                            :description => 'This is the description',
                            :priority_id => 5,
762
                            :start_date => '2010-11-07',
edavis10's avatar
edavis10 committed
763 764 765 766
                            :estimated_hours => '',
                            :custom_field_values => {'2' => 'Value for field 2'}}
    end
    assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
767

768 769 770
    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_equal 2, issue.author_id
771
    assert_equal 3, issue.tracker_id
772
    assert_equal 2, issue.status_id
773
    assert_equal Date.parse('2010-11-07'), issue.start_date
774
    assert_nil issue.estimated_hours
775
    v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
776 777
    assert_not_nil v
    assert_equal 'Value for field 2', v.value
778
  end
779

780 781 782 783 784 785 786 787
  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
788
        post :create, :project_id => project.id,
789 790 791 792 793 794 795
                      :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
796

797 798 799 800
    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
801

802 803 804
  def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
    Setting.default_issue_start_date_to_creation_date = 0

805 806
    @request.session[:user_id] = 2
    assert_difference 'Issue.count' do
807
      post :create, :project_id => 1,
808 809 810 811 812 813 814 815 816
                 :issue => {:tracker_id => 3,
                            :status_id => 2,
                            :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
817

818 819 820 821
    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_nil issue.start_date
  end
822

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
  def test_post_create_without_start_date_and_default_start_date_is_creation_date
    Setting.default_issue_start_date_to_creation_date = 1

    @request.session[:user_id] = 2
    assert_difference 'Issue.count' do
      post :create, :project_id => 1,
                 :issue => {:tracker_id => 3,
                            :status_id => 2,
                            :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

    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_equal Date.today, issue.start_date
  end

844
  def test_post_create_and_continue
845
    @request.session[:user_id] = 2
846 847 848 849 850
    assert_difference 'Issue.count' do
      post :create, :project_id => 1,
        :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
        :continue => ''
    end
851

852 853 854 855
    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]}"
856
  end
857

858
  def test_post_create_without_custom_fields_param
859
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
860
    assert_difference 'Issue.count' do
861
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
862 863 864 865 866 867
                 :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
868
  end
869

870
  def test_post_create_with_required_custom_field_and_without_custom_fields_param
871 872 873 874
    field = IssueCustomField.find_by_name('Database')
    field.update_attribute(:is_required, true)

    @request.session[:user_id] = 2
875
    post :create, :project_id => 1,
876 877 878 879 880 881 882 883
               :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
884
    assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
885
  end
886

887
  def test_post_create_with_watchers
888 889
    @request.session[:user_id] = 2
    ActionMailer::Base.deliveries.clear
890

891
    assert_difference 'Watcher.count', 2 do
892
      post :create, :project_id => 1,
893 894 895 896 897 898 899
                 :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
900 901
    assert_not_nil issue
    assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
902

903 904 905 906 907 908