issues_controller_test.rb 72.5 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
  include Redmine::I18n

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

55
  def test_index
56
    Setting.default_language = 'en'
57

58 59
    get :index
    assert_response :success
60
    assert_template 'index'
61 62
    assert_not_nil assigns(:issues)
    assert_nil assigns(:project)
63 64 65 66 67
    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/
68 69
    # project column
    assert_tag :tag => 'th', :content => /Project/
70
  end
71

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

  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
89

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

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

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

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

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

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

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

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  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'] }},
165 166
      'tracker_id' => {
        '3' => { :op => '=', :values => ['3'] },
emassip's avatar
emassip committed
167
        '=3' => { :op => '=', :values => ['3'] }},
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

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

291
  def test_index_csv_with_project
292 293 294 295 296
    get :index, :project_id => 1, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
    assert_equal 'text/csv', @response.content_type
  end
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
  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

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
  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
336
      lines = @response.body.chomp.split("\n")
337 338 339 340 341 342 343 344 345
      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

346 347 348 349 350 351 352 353 354 355 356 357 358 359
  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],
360 361 362
                  :c => ['status', 'subject'],
                  :format => 'csv',
                  :set_filter => 1
363
      assert_equal 'text/csv', @response.content_type
364 365
      lines = @response.body.chomp.split("\n")
      s1 = "\xaa\xac\xbaA" # status
366 367 368 369
      if str_utf8.respond_to?(:force_encoding)
        s1.force_encoding('Big5')
      end
      assert lines[0].include?(s1)
370
      s2 = lines[1].split(",")[2]
371
      if s1.respond_to?(:force_encoding)
372
        s3 = "\xa5H?" # subject
373 374 375 376 377 378 379 380 381 382
        s3.force_encoding('Big5')
        assert_equal s3, s2
      elsif RUBY_PLATFORM == 'java'
        assert_equal "??", s2
      else
        assert_equal "\xa5H???", s2
      end
    end
  end

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
  def test_index_csv_tw
    with_settings :default_language => "zh-TW" do
      str1  = "test_index_csv_tw"
      issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
                        :status_id => 1, :priority => IssuePriority.all.first,
                        :subject => str1, :estimated_hours => '1234.5')
      assert issue.save
      assert_equal 1234.5, issue.estimated_hours

      get :index, :project_id => 1, 
                  :f => ['subject'], 
                  :op => '=', :values => [str1],
                  :c => ['estimated_hours', 'subject'],
                  :format => 'csv',
                  :set_filter => 1
      assert_equal 'text/csv', @response.content_type
      lines = @response.body.chomp.split("\n")
      assert_equal "#{issue.id},1234.5,#{str1}", lines[1]

      str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
      if str_tw.respond_to?(:force_encoding)
        str_tw.force_encoding('UTF-8')
      end
      assert_equal str_tw, l(:general_lang_name)
      assert_equal ',', l(:general_csv_separator)
      assert_equal '.', l(:general_csv_decimal_separator)
    end
  end

  def test_index_csv_fr
    with_settings :default_language => "fr" do
      str1  = "test_index_csv_fr"
      issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
                        :status_id => 1, :priority => IssuePriority.all.first,
                        :subject => str1, :estimated_hours => '1234.5')
      assert issue.save
      assert_equal 1234.5, issue.estimated_hours

      get :index, :project_id => 1, 
                  :f => ['subject'], 
                  :op => '=', :values => [str1],
                  :c => ['estimated_hours', 'subject'],
                  :format => 'csv',
                  :set_filter => 1
      assert_equal 'text/csv', @response.content_type
      lines = @response.body.chomp.split("\n")
      assert_equal "#{issue.id};1234,5;#{str1}", lines[1]

      str_fr = "Fran\xc3\xa7ais"
      if str_fr.respond_to?(:force_encoding)
        str_fr.force_encoding('UTF-8')
      end
      assert_equal str_fr, l(:general_lang_name)
      assert_equal ';', l(:general_csv_separator)
      assert_equal ',', l(:general_csv_decimal_separator)
    end
  end

441
  def test_index_pdf
442 443
    ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
      with_settings :default_language => lang do
444

445 446 447
        get :index
        assert_response :success
        assert_template 'index'
448

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
        if lang == "ja"
          if RUBY_PLATFORM != 'java'
            assert_equal "CP932", l(:general_pdf_encoding)
          end
          if RUBY_PLATFORM == 'java' && l(:general_pdf_encoding) == "CP932"
            next
          end
        end

        get :index, :format => 'pdf'
        assert_response :success
        assert_not_nil assigns(:issues)
        assert_equal 'application/pdf', @response.content_type

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

        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
      end
    end
474
  end
475

476 477 478 479 480 481 482
  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
483

484
  def test_index_sort
jplang's avatar
jplang committed
485
    get :index, :sort => 'tracker,id:desc'
486
    assert_response :success
487

jplang's avatar
jplang committed
488 489 490
    sort_params = @request.session['issues_index_sort']
    assert sort_params.is_a?(String)
    assert_equal 'tracker,id:desc', sort_params
491

jplang's avatar
jplang committed
492 493 494 495
    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)
496
  end
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

  def test_index_sort_by_field_not_included_in_columns
    Setting.issue_list_default_columns = %w(subject author)
    get :index, :sort => 'tracker'
  end
  
  def test_index_sort_by_assigned_to
    get :index, :sort => 'assigned_to'
    assert_response :success
    assignees = assigns(:issues).collect(&:assigned_to).compact
    assert_equal assignees.sort, assignees
  end
  
  def test_index_sort_by_assigned_to_desc
    get :index, :sort => 'assigned_to:desc'
    assert_response :success
    assignees = assigns(:issues).collect(&:assigned_to).compact
    assert_equal assignees.sort.reverse, assignees
  end
  
  def test_index_group_by_assigned_to
    get :index, :group_by => 'assigned_to', :sort => 'priority'
    assert_response :success
  end
521 522 523 524
  
  def test_index_sort_by_author
    get :index, :sort => 'author'
    assert_response :success
525 526 527 528 529 530 531 532 533
    authors = assigns(:issues).collect(&:author)
    assert_equal authors.sort, authors
  end
  
  def test_index_sort_by_author_desc
    get :index, :sort => 'author:desc'
    assert_response :success
    authors = assigns(:issues).collect(&:author)
    assert_equal authors.sort.reverse, authors
534 535 536 537 538 539
  end
  
  def test_index_group_by_author
    get :index, :group_by => 'author', :sort => 'priority'
    assert_response :success
  end
540

541 542
  def test_index_with_columns
    columns = ['tracker', 'subject', 'assigned_to']
543
    get :index, :set_filter => 1, :c => columns
544
    assert_response :success
545

546 547 548 549
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
550

551 552 553 554
    # 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)
555 556 557 558 559 560

    # 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" } }
561
  end
562

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
  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

584 585 586 587
  def test_index_with_custom_field_column
    columns = %w(tracker subject cf_2)
    get :index, :set_filter => 1, :c => columns
    assert_response :success
588

589 590 591 592
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
593

594 595 596 597
    assert_tag :td,
      :attributes => {:class => 'cf_2 string'},
      :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
  end
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  def test_index_with_date_column
    Issue.find(1).update_attribute :start_date, '1987-08-24'

    with_settings :date_format => '%d/%m/%Y' do
      get :index, :set_filter => 1, :c => %w(start_date)
      assert_tag 'td', :attributes => {:class => /start_date/}, :content => '24/08/1987'
    end
  end

  def test_index_with_done_ratio
    Issue.find(1).update_attribute :done_ratio, 40

    get :index, :set_filter => 1, :c => %w(done_ratio)
    assert_tag 'td', :attributes => {:class => /done_ratio/},
      :child => {:tag => 'table', :attributes => {:class => 'progress'},
        :descendant => {:tag => 'td', :attributes => {:class => 'closed', :style => 'width: 40%;'}}
      }
  end

  def test_index_with_fixed_version
    get :index, :set_filter => 1, :c => %w(fixed_version)
    assert_tag 'td', :attributes => {:class => /fixed_version/},
      :child => {:tag => 'a', :content => '1.0', :attributes => {:href => '/versions/2'}}
  end

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

630 631 632 633 634 635
  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

636
  def test_show_by_anonymous
637 638
    get :show, :id => 1
    assert_response :success
639
    assert_template 'show'
640
    assert_not_nil assigns(:issue)
641
    assert_equal Issue.find(1), assigns(:issue)
642

643 644 645
    # anonymous role is allowed to add a note
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
646
                                :child => { :tag => 'legend',
647
                                            :content => /Notes/ } }
648 649
    assert_tag :tag => 'title',
      :content => "Bug #1: Can't print recipes - eCookbook - Redmine"
650
  end
651

652 653 654 655
  def test_show_by_manager
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success
656

657 658
    assert_tag :tag => 'a',
      :content => /Quote/
659

660 661
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
662
                                :child => { :tag => 'legend',
663 664
                                            :content => /Change properties/ } },
               :descendant => { :tag => 'fieldset',
665
                                :child => { :tag => 'legend',
666 667
                                            :content => /Log time/ } },
               :descendant => { :tag => 'fieldset',
668
                                :child => { :tag => 'legend',
669 670
                                            :content => /Notes/ } }
  end
671

672 673 674 675 676 677 678 679 680
  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
681

682 683 684
  def test_update_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :show, :id => 1
685

686 687 688 689 690 691 692
    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
693

jplang's avatar
jplang committed
694 695 696 697 698
  def test_show_should_deny_anonymous_access_without_permission
    Role.anonymous.remove_permission!(:view_issues)
    get :show, :id => 1
    assert_response :redirect
  end
699

jplang's avatar
jplang committed
700 701 702 703 704
  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
705

jplang's avatar
jplang committed
706 707 708 709 710 711
  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
712

jplang's avatar
jplang committed
713 714 715 716 717 718
  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
719

jplang's avatar
jplang committed
720 721 722 723 724 725
  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
726

jplang's avatar
jplang committed
727 728 729 730 731 732
  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
733

jplang's avatar
jplang committed
734 735 736 737 738 739
  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
740

jplang's avatar
jplang committed
741 742 743 744 745 746
  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
747

jplang's avatar
jplang committed
748 749 750 751 752 753 754
  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
755

756 757 758 759 760
  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')
761

762 763
    get :show, :id => 1
    assert_response :success
764

765 766 767 768 769
    assert_tag :div, :attributes => { :id => 'relations' },
                     :descendant => { :tag => 'a', :content => /#2$/ }
    assert_no_tag :div, :attributes => { :id => 'relations' },
                        :descendant => { :tag => 'a', :content => /#4$/ }
  end
770

jplang's avatar
jplang committed
771 772 773 774 775 776
  def test_show_should_list_subtasks
    Issue.generate!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')

    get :show, :id => 1
    assert_response :success
    assert_tag 'div', :attributes => {:id => 'issue_tree'},
jplang's avatar
jplang committed
777
      :descendant => {:tag => 'td', :content => /Child Issue/, :attributes => {:class => /subject/}}
jplang's avatar
jplang committed
778 779
  end

780 781 782 783 784 785 786 787 788 789 790
  def test_show_should_list_parents
    issue = Issue.generate!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')

    get :show, :id => issue.id
    assert_response :success
    assert_tag 'div', :attributes => {:class => 'subject'},
      :descendant => {:tag => 'h3', :content => 'Child Issue'}
    assert_tag 'div', :attributes => {:class => 'subject'},
      :descendant => {:tag => 'a', :attributes => {:href => '/issues/1'}}
  end

791 792 793
  def test_show_atom
    get :show, :id => 2, :format => 'atom'
    assert_response :success
794
    assert_template 'journals/index'
795
    # Inline image
796
    assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
797
  end
798

jplang's avatar
jplang committed
799
  def test_show_export_to_pdf
800
    get :show, :id => 3, :format => 'pdf'
jplang's avatar
jplang committed
801 802 803 804 805 806
    assert_response :success
    assert_equal 'application/pdf', @response.content_type
    assert @response.body.starts_with?('%PDF')
    assert_not_nil assigns(:issue)
  end

807 808 809 810 811
  def test_get_new
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :tracker_id => 1
    assert_response :success
    assert_template 'new'
812

813
    assert_tag :tag => 'input', :attributes => { :name => 'issue[custom_field_values][2]',
814
                                                 :value => 'Default string' }
815 816 817 818 819

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

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
  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

846 847 848
  def test_get_new_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :new, :project_id => 1, :tracker_id => 1
849

850 851 852 853 854 855 856
    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
857 858 859 860 861 862

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

864 865 866 867
    issue = assigns(:issue)
    assert_not_nil issue
    assert_equal Project.find(1).trackers.first, issue.tracker
  end
868

869 870 871
  def test_get_new_with_no_default_status_should_display_an_error
    @request.session[:user_id] = 2
    IssueStatus.delete_all
872

873 874
    get :new, :project_id => 1
    assert_response 500
875
    assert_error_tag :content => /No default issue/
876
  end
877

878 879 880
  def test_get_new_with_no_tracker_should_display_an_error
    @request.session[:user_id] = 2
    Tracker.delete_all
881

882 883
    get :new, :project_id => 1
    assert_response 500
884
    assert_error_tag :content => /No tracker/
885
  end
886

887 888
  def test_update_new_form
    @request.session[:user_id] = 2
889
    xhr :post, :new, :project_id => 1,
890
                     :issue => {:tracker_id => 2,
891 892 893 894
                                :subject => 'This is the test_new issue',
                                :description => 'This is the description',
                                :priority_id => 5}
    assert_response :success
895
    assert_template 'attributes'
896

897 898 899 900 901 902
    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
903

904
  def test_post_create
905
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
906
    assert_difference 'Issue.count' do
907
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
908
                 :issue => {:tracker_id => 3,
909
                            :status_id => 2,
edavis10's avatar
edavis10 committed
910 911 912
                            :subject => 'This is the test_new issue',
                            :description => 'This is the description',
                            :priority_id => 5,
913
                            :start_date => '2010-11-07',
edavis10's avatar
edavis10 committed
914 915 916 917
                            :estimated_hours => '',
                            :custom_field_values => {'2' => 'Value for field 2'}}
    end
    assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
918

919 920 921
    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_equal 2, issue.author_id
922
    assert_equal 3, issue.tracker_id
923
    assert_equal 2, issue.status_id
924
    assert_equal Date.parse('2010-11-07'), issue.start_date
925
    assert_nil issue.estimated_hours
926
    v = issue.custom_values.find(:first, :conditions => {:custom_field_id => 2})
927 928
    assert_not_nil v
    assert_equal 'Value for field 2', v.value
929
  end
930

931 932 933 934 935 936 937 938
  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
939
        post :create, :project_id => project.id,
940 941 942 943 944 945 946
                      :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
947

948 949 950 951
    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
952

953 954 955
  def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
    Setting.default_issue_start_date_to_creation_date = 0

956 957
    @request.session[:user_id] = 2
    assert_difference 'Issue.count' do
958
      post :create, :project_id => 1,
959 960 961 962 963 964 965 966 967
                 :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
968

969 970 971 972
    issue = Issue.find_by_subject('This is the test_new issue')
    assert_not_nil issue
    assert_nil issue.start_date
  end
973

974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
  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

995
  def test_post_create_and_continue
996
    @request.session[:user_id] = 2
997 998 999 1000 1001
    assert_difference 'Issue.count' do
      post :create, :project_id => 1,
        :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
        :continue => ''
    end
1002

1003 1004 1005 1006
    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]}"
1007
  end
1008

1009
  def test_post_create_without_custom_fields_param
1010
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
1011
    assert_difference 'Issue.count' do
1012
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
1013 1014 1015 1016 1017 1018
                 :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
1019
  end
1020

1021
  def test_post_create_with_required_custom_field_and_without_custom_fields_param
1022 1023 1024 1025
    field = IssueCustomField.find_by_name('Database')
    field.update_attribute(:is_required, true)

    @request.session[:user_id] = 2
1026
    post :create, :project_id => 1,
1027 1028 1029 1030 1031 1032 1033 1034
               :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
1035
    assert_equal I18n.translate('activerecord.errors.messages.invalid'), issue.errors.on(:custom_values)
1036
  end
1037

1038
  def test_post_create_with_watchers
1039 1040
    @request.session[:user_id] = 2
    ActionMailer::Base.deliveries.clear
1041

1042
    assert_difference 'Watcher.count', 2 do
1043
      post :create, :project_id => 1,
1044 1045 1046 1047 1048 1049 1050
                 :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
1051 1052
    assert_not_nil issue
    assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1053

1054 1055 1056 1057 1058 1059 1060 1061
    # 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
1062

1063
  def test_post_create_subissue
jplang's avatar
jplang committed
1064
    @request.session[:user_id] = 2
1065

jplang's avatar
jplang committed
1066
    assert_difference 'Issue.count' do
1067
      post :create, :project_id => 1,
jplang's avatar
jplang committed
1068 1069 1070 1071 1072 1073 1074 1075
                 :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
1076 1077 1078

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

1080
    assert_difference 'Issue.count' do
1081
      post :create, :project_id => 1,
1082 1083 1084 1085 1086 1087 1088 1089
                 :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
1090

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
  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
1103

1104 1105 1106 1107
  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
1108

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
    @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
1120

1121
  def test_post_create_should_send_a_notification
1122 1123
    ActionMailer::Base.deliveries.clear
    @request.session[:user_id] = 2
edavis10's avatar
edavis10 committed
1124
    assert_difference 'Issue.count' do
1125
      post :create, :project_id => 1,
edavis10's avatar
edavis10 committed
1126 1127 1128 1129 1130 1131 1132 1133
                 :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
1134

1135 1136
    assert_equal 1, ActionMailer::Base.deliveries.size
  end
1137

1138
  def test_post_create_should_preserve_fields_values_on_validation_failure
1139
    @request.session[:user_id] = 2
1140
    post :create, :project_id => 1,
1141
               :issue => {:tracker_id => 1,
1142 1143 1144
                          # empty subject
                          :subject => '',
                          :description => 'This is a description',
1145 1146 1147 1148
                          :priority_id => 6,
                          :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
    assert_response :success
    assert_template 'new'
1149

1150 1151
    assert_tag :textarea, :attributes => { :name => 'issue[description]' },
                          :content => 'This is a description'
1152 1153 1154
    assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
                        :child => { :tag => 'option', :attributes => { :selected => 'selected',
                                                                       :value => '6' },
1155
                                                      :content => 'High' }
1156 1157 1158 1159
    # Custom fields
    assert_tag :select, :attributes => { :name => 'issue[custom_field_values][1]' },
                        :child => { :tag => 'option', :attributes => { :selected => 'selected',
                                                                       :value => 'Oracle' },
1160
                                                      :content => 'Oracle' }
1161 1162 1163
    assert_tag :input, :attributes => { :name => 'issue[custom_field_values][2]',
                                        :value => 'Value for field 2'}
  end
1164