issues_controller_test.rb 142 KB
Newer Older
1
# Redmine - project management software
2
# Copyright (C) 2006-2012  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

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

47 48
  include Redmine::I18n

49 50 51
  def setup
    User.current = nil
  end
52

53
  def test_index
54 55 56 57 58 59
    with_settings :default_language => "en" do
      get :index
      assert_response :success
      assert_template 'index'
      assert_not_nil assigns(:issues)
      assert_nil assigns(:project)
60 61 62 63

      # links to visible issues
      assert_select 'a[href=/issues/1]', :text => /Can&#x27;t print recipes/
      assert_select 'a[href=/issues/5]', :text => /Subproject issue/
64
      # private projects hidden
65 66
      assert_select 'a[href=/issues/6]', 0
      assert_select 'a[href=/issues/4]', 0
67
      # project column
68
      assert_select 'th', :text => /Project/
69
    end
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
tmaruyama's avatar
tmaruyama committed
76
    assert_template 'index'
77 78
    assert_not_nil assigns(:issues)
    assert_nil assigns(:project)
79 80 81

    assert_select 'a[href=/issues/1]', 0
    assert_select 'a[href=/issues/5]', :text => /Subproject issue/
82
  end
jplang's avatar
jplang committed
83 84 85 86 87 88 89

  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
90

91
  def test_index_with_project
92
    Setting.display_subprojects_issues = 0
93 94
    get :index, :project_id => 1
    assert_response :success
95
    assert_template 'index'
96
    assert_not_nil assigns(:issues)
97 98 99

    assert_select 'a[href=/issues/1]', :text => /Can&#x27;t print recipes/
    assert_select 'a[href=/issues/5]', 0
100
  end
101

102 103 104 105
  def test_index_with_project_and_subprojects
    Setting.display_subprojects_issues = 1
    get :index, :project_id => 1
    assert_response :success
106
    assert_template 'index'
107
    assert_not_nil assigns(:issues)
108 109 110 111

    assert_select 'a[href=/issues/1]', :text => /Can&#x27;t print recipes/
    assert_select 'a[href=/issues/5]', :text => /Subproject issue/
    assert_select 'a[href=/issues/6]', 0
112
  end
113

114
  def test_index_with_project_and_subprojects_should_show_private_subprojects_with_permission
115 116 117 118
    @request.session[:user_id] = 2
    Setting.display_subprojects_issues = 1
    get :index, :project_id => 1
    assert_response :success
tmaruyama's avatar
tmaruyama committed
119
    assert_template 'index'
120
    assert_not_nil assigns(:issues)
121 122 123 124

    assert_select 'a[href=/issues/1]', :text => /Can&#x27;t print recipes/
    assert_select 'a[href=/issues/5]', :text => /Subproject issue/
    assert_select 'a[href=/issues/6]', :text => /Issue of a private subproject/
125
  end
126

127
  def test_index_with_project_and_default_filter
128 129
    get :index, :project_id => 1, :set_filter => 1
    assert_response :success
130
    assert_template 'index'
131
    assert_not_nil assigns(:issues)
132

133 134 135 136 137
    query = assigns(:query)
    assert_not_nil query
    # default filter
    assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
  end
138

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

148 149 150 151
    query = assigns(:query)
    assert_not_nil query
    assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
  end
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  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'] }},
168 169
      'tracker_id' => {
        '3' => { :op => '=', :values => ['3'] },
emassip's avatar
emassip committed
170
        '=3' => { :op => '=', :values => ['3'] }},
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
      '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'] },
187 188 189
        '<t-2' => { :op => '<t-', :values => ['2'] },
        '>t-2' => { :op => '>t-', :values => ['2'] },
        't-2' => { :op => 't-', :values => ['2'] }},
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      '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
208

209 210 211 212 213 214 215 216 217 218 219 220 221 222
        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

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

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

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  def test_index_with_project_custom_field_filter
    field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
    CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
    CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
    filter_name = "project.cf_#{field.id}"
    @request.session[:user_id] = 1

    get :index, :set_filter => 1,
      :f => [filter_name],
      :op => {filter_name => '='},
      :v => {filter_name => ['Foo']}
    assert_response :success
    assert_template 'index'
    assert_equal [3, 5], assigns(:issues).map(&:project_id).uniq.sort
  end

jplang's avatar
jplang committed
251 252 253
  def test_index_with_query
    get :index, :project_id => 1, :query_id => 5
    assert_response :success
254
    assert_template 'index'
jplang's avatar
jplang committed
255 256 257
    assert_not_nil assigns(:issues)
    assert_nil assigns(:issue_count_by_group)
  end
258

259
  def test_index_with_query_grouped_by_tracker
jplang's avatar
jplang committed
260 261
    get :index, :project_id => 1, :query_id => 6
    assert_response :success
262
    assert_template 'index'
jplang's avatar
jplang committed
263
    assert_not_nil assigns(:issues)
264
    assert_not_nil assigns(:issue_count_by_group)
265
  end
266

267 268 269
  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
270
    assert_template 'index'
271
    assert_not_nil assigns(:issues)
272
    assert_not_nil assigns(:issue_count_by_group)
jplang's avatar
jplang committed
273
  end
274

275 276 277 278 279 280 281 282 283 284 285 286 287
  def test_index_with_query_grouped_by_user_custom_field
    cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
    CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
    CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
    CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
    CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')

    get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
    assert_response :success

    assert_select 'tr.group', 3
    assert_select 'tr.group' do
      assert_select 'a', :text => 'John Smith'
jplang's avatar
jplang committed
288
      assert_select 'span.count', :text => '1'
289 290 291
    end
    assert_select 'tr.group' do
      assert_select 'a', :text => 'Dave Lopper'
jplang's avatar
jplang committed
292
      assert_select 'span.count', :text => '2'
293 294 295
    end
  end

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  def test_index_with_query_grouped_by_tracker
    3.times {|i| Issue.generate!(:tracker_id => (i + 1))}

    get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc'
    assert_response :success

    trackers = assigns(:issues).map(&:tracker).uniq
    assert_equal [1, 2, 3], trackers.map(&:id)
  end

  def test_index_with_query_grouped_by_tracker_in_reverse_order
    3.times {|i| Issue.generate!(:tracker_id => (i + 1))}

    get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc,tracker:desc'
    assert_response :success

    trackers = assigns(:issues).map(&:tracker).uniq
    assert_equal [3, 2, 1], trackers.map(&:id)
  end

316 317 318 319 320 321 322 323
  def test_index_with_query_id_and_project_id_should_set_session_query
    get :index, :project_id => 1, :query_id => 4
    assert_response :success
    assert_kind_of Hash, session[:query]
    assert_equal 4, session[:query][:id]
    assert_equal 1, session[:query][:project_id]
  end

324 325 326 327 328
  def test_index_with_invalid_query_id_should_respond_404
    get :index, :project_id => 1, :query_id => 999
    assert_response 404
  end

329
  def test_index_with_cross_project_query_in_session_should_show_project_issues
330
    q = IssueQuery.create!(:name => "test", :user_id => 2, :is_public => false, :project => nil)
331 332 333 334 335 336 337 338 339 340 341 342
    @request.session[:query] = {:id => q.id, :project_id => 1}

    with_settings :display_subprojects_issues => '0' do
      get :index, :project_id => 1
    end
    assert_response :success
    assert_not_nil assigns(:query)
    assert_equal q.id, assigns(:query).id
    assert_equal 1, assigns(:query).project_id
    assert_equal [1], assigns(:issues).map(&:project_id).uniq
  end

343
  def test_private_query_should_not_be_available_to_other_users
344
    q = IssueQuery.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
345
    @request.session[:user_id] = 3
346

347 348 349
    get :index, :query_id => q.id
    assert_response 403
  end
350

351
  def test_private_query_should_be_available_to_its_user
352
    q = IssueQuery.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
353
    @request.session[:user_id] = 2
354

355 356 357
    get :index, :query_id => q.id
    assert_response :success
  end
358

359
  def test_public_query_should_be_available_to_other_users
360
    q = IssueQuery.create!(:name => "private", :user => User.find(2), :is_public => true, :project => nil)
361
    @request.session[:user_id] = 3
362

363 364 365
    get :index, :query_id => q.id
    assert_response :success
  end
366

367 368 369 370 371 372 373 374 375
  def test_index_should_omit_page_param_in_export_links
    get :index, :page => 2
    assert_response :success
    assert_select 'a.atom[href=/issues.atom]'
    assert_select 'a.csv[href=/issues.csv]'
    assert_select 'a.pdf[href=/issues.pdf]'
    assert_select 'form#csv-export-form[action=/issues.csv]'
  end

376
  def test_index_csv
377 378 379
    get :index, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
jplang's avatar
jplang committed
380
    assert_equal 'text/csv; header=present', @response.content_type
381
    assert @response.body.starts_with?("#,")
382 383 384
    lines = @response.body.chomp.split("\n")
    assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
  end
385

386
  def test_index_csv_with_project
387 388 389
    get :index, :project_id => 1, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
jplang's avatar
jplang committed
390
    assert_equal 'text/csv; header=present', @response.content_type
391
  end
392

393 394 395 396
  def test_index_csv_with_description
    get :index, :format => 'csv', :description => '1'
    assert_response :success
    assert_not_nil assigns(:issues)
jplang's avatar
jplang committed
397
    assert_equal 'text/csv; header=present', @response.content_type
398 399 400 401 402
    assert @response.body.starts_with?("#,")
    lines = @response.body.chomp.split("\n")
    assert_equal assigns(:query).columns.size + 2, lines[0].split(',').size
  end

403
  def test_index_csv_with_spent_time_column
jplang's avatar
jplang committed
404 405
    issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column', :author_id => 2)
    TimeEntry.create!(:project => issue.project, :issue => issue, :hours => 7.33, :user => User.find(2), :spent_on => Date.today)
406 407 408

    get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
    assert_response :success
jplang's avatar
jplang committed
409
    assert_equal 'text/csv; header=present', @response.content_type
410 411 412 413
    lines = @response.body.chomp.split("\n")
    assert_include "#{issue.id},#{issue.subject},7.33", lines
  end

414 415 416 417
  def test_index_csv_with_all_columns
    get :index, :format => 'csv', :columns => 'all'
    assert_response :success
    assert_not_nil assigns(:issues)
jplang's avatar
jplang committed
418
    assert_equal 'text/csv; header=present', @response.content_type
419 420
    assert @response.body.starts_with?("#,")
    lines = @response.body.chomp.split("\n")
421
    assert_equal assigns(:query).available_inline_columns.size + 1, lines[0].split(',').size
422 423
  end

jplang's avatar
jplang committed
424 425 426 427 428 429 430 431 432 433 434 435
  def test_index_csv_with_multi_column_field
    CustomField.find(1).update_attribute :multiple, true
    issue = Issue.find(1)
    issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
    issue.save!

    get :index, :format => 'csv', :columns => 'all'
    assert_response :success
    lines = @response.body.chomp.split("\n")
    assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
  end

436 437 438 439 440 441 442 443
  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
jplang's avatar
jplang committed
444
      issue = Issue.generate!(:subject => str_utf8)
445 446 447 448 449

      get :index, :project_id => 1, 
                  :f => ['subject'], 
                  :op => '=', :values => [str_utf8],
                  :format => 'csv'
jplang's avatar
jplang committed
450
      assert_equal 'text/csv; header=present', @response.content_type
451
      lines = @response.body.chomp.split("\n")
452 453 454 455 456 457 458 459 460
      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

461 462 463 464 465 466
  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
jplang's avatar
jplang committed
467
      issue = Issue.generate!(:subject => str_utf8)
468 469 470 471

      get :index, :project_id => 1, 
                  :f => ['subject'], 
                  :op => '=', :values => [str_utf8],
472 473 474
                  :c => ['status', 'subject'],
                  :format => 'csv',
                  :set_filter => 1
jplang's avatar
jplang committed
475
      assert_equal 'text/csv; header=present', @response.content_type
476 477
      lines = @response.body.chomp.split("\n")
      s1 = "\xaa\xac\xbaA" # status
478 479 480 481
      if str_utf8.respond_to?(:force_encoding)
        s1.force_encoding('Big5')
      end
      assert lines[0].include?(s1)
482
      s2 = lines[1].split(",")[2]
483
      if s1.respond_to?(:force_encoding)
484
        s3 = "\xa5H?" # subject
485 486 487 488 489 490 491 492 493 494
        s3.force_encoding('Big5')
        assert_equal s3, s2
      elsif RUBY_PLATFORM == 'java'
        assert_equal "??", s2
      else
        assert_equal "\xa5H???", s2
      end
    end
  end

495 496 497
  def test_index_csv_tw
    with_settings :default_language => "zh-TW" do
      str1  = "test_index_csv_tw"
jplang's avatar
jplang committed
498
      issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
499 500 501 502 503 504 505

      get :index, :project_id => 1, 
                  :f => ['subject'], 
                  :op => '=', :values => [str1],
                  :c => ['estimated_hours', 'subject'],
                  :format => 'csv',
                  :set_filter => 1
jplang's avatar
jplang committed
506
      assert_equal 'text/csv; header=present', @response.content_type
507
      lines = @response.body.chomp.split("\n")
508
      assert_equal "#{issue.id},1234.50,#{str1}", lines[1]
509 510 511 512 513 514
    end
  end

  def test_index_csv_fr
    with_settings :default_language => "fr" do
      str1  = "test_index_csv_fr"
jplang's avatar
jplang committed
515
      issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
516 517 518 519 520 521 522

      get :index, :project_id => 1, 
                  :f => ['subject'], 
                  :op => '=', :values => [str1],
                  :c => ['estimated_hours', 'subject'],
                  :format => 'csv',
                  :set_filter => 1
jplang's avatar
jplang committed
523
      assert_equal 'text/csv; header=present', @response.content_type
524
      lines = @response.body.chomp.split("\n")
525
      assert_equal "#{issue.id};1234,50;#{str1}", lines[1]
526 527 528
    end
  end

529
  def test_index_pdf
530 531
    ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
      with_settings :default_language => lang do
532

533 534 535
        get :index
        assert_response :success
        assert_template 'index'
536

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
        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
562
  end
563

564 565 566 567 568 569 570
  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
571

572 573 574 575
  def test_index_atom
    get :index, :project_id => 'ecookbook', :format => 'atom'
    assert_response :success
    assert_template 'common/feed'
576
    assert_equal 'application/atom+xml', response.content_type
577

578 579 580 581 582
    assert_select 'feed' do
      assert_select 'link[rel=self][href=?]', 'http://test.host/projects/ecookbook/issues.atom'
      assert_select 'link[rel=alternate][href=?]', 'http://test.host/projects/ecookbook/issues'
      assert_select 'entry link[href=?]', 'http://test.host/issues/1'
    end
583 584
  end

585
  def test_index_sort
jplang's avatar
jplang committed
586
    get :index, :sort => 'tracker,id:desc'
587
    assert_response :success
588

jplang's avatar
jplang committed
589 590 591
    sort_params = @request.session['issues_index_sort']
    assert sort_params.is_a?(String)
    assert_equal 'tracker,id:desc', sort_params
592

jplang's avatar
jplang committed
593 594 595 596
    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)
597
  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

  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
622 623 624 625
  
  def test_index_sort_by_author
    get :index, :sort => 'author'
    assert_response :success
626 627 628 629 630 631 632 633 634
    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
635 636 637 638 639 640
  end
  
  def test_index_group_by_author
    get :index, :group_by => 'author', :sort => 'priority'
    assert_response :success
  end
641
  
jplang's avatar
jplang committed
642 643
  def test_index_sort_by_spent_hours
    get :index, :sort => 'spent_hours:desc'
644 645 646 647
    assert_response :success
    hours = assigns(:issues).collect(&:spent_hours)
    assert_equal hours.sort.reverse, hours
  end
648

649 650 651 652 653 654 655 656 657 658 659 660 661
  def test_index_sort_by_user_custom_field
    cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
    CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
    CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
    CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
    CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')

    get :index, :project_id => 1, :set_filter => 1, :sort => "cf_#{cf.id},id"
    assert_response :success

    assert_equal [2, 3, 1], assigns(:issues).select {|issue| issue.custom_field_value(cf).present?}.map(&:id)
  end

662 663
  def test_index_with_columns
    columns = ['tracker', 'subject', 'assigned_to']
664
    get :index, :set_filter => 1, :c => columns
665
    assert_response :success
666

667 668
    # query should use specified columns
    query = assigns(:query)
669
    assert_kind_of IssueQuery, query
670
    assert_equal columns, query.column_names.map(&:to_s)
671

672 673 674 675
    # 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)
676 677

    # ensure only these columns are kept in the selected columns list
678 679 680 681 682
    assert_select 'select#selected_columns option' do
      assert_select 'option', 3
      assert_select 'option[value=tracker]'
      assert_select 'option[value=project]', 0
    end
683
  end
684

685 686 687 688 689 690
  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)
691
    assert_kind_of IssueQuery, query
692 693 694 695 696 697 698 699 700 701
    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)
702
    assert_kind_of IssueQuery, query
703 704 705
    assert_equal columns.map(&:to_sym), query.columns.map(&:name)
  end

706 707 708 709
  def test_index_with_custom_field_column
    columns = %w(tracker subject cf_2)
    get :index, :set_filter => 1, :c => columns
    assert_response :success
710

711 712
    # query should use specified columns
    query = assigns(:query)
713
    assert_kind_of IssueQuery, query
714
    assert_equal columns, query.column_names.map(&:to_s)
715

716
    assert_select 'table.issues td.cf_2.string'
717
  end
718

719 720 721 722 723 724 725 726 727 728
  def test_index_with_multi_custom_field_column
    field = CustomField.find(1)
    field.update_attribute :multiple, true
    issue = Issue.find(1)
    issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
    issue.save!

    get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
    assert_response :success

729
    assert_select 'table.issues td.cf_1', :text => 'MySQL, Oracle'
730 731 732 733 734 735 736 737 738 739 740 741
  end

  def test_index_with_multi_user_custom_field_column
    field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
      :tracker_ids => [1], :is_for_all => true)
    issue = Issue.find(1)
    issue.custom_field_values = {field.id => ['2', '3']}
    issue.save!

    get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
    assert_response :success

742 743 744 745 746
    assert_select "table.issues td.cf_#{field.id}" do
      assert_select 'a', 2
      assert_select 'a[href=?]', '/users/2', :text => 'John Smith'
      assert_select 'a[href=?]', '/users/3', :text => 'Dave Lopper'
    end
747 748
  end

749 750
  def test_index_with_date_column
    with_settings :date_format => '%d/%m/%Y' do
751 752
      Issue.find(1).update_attribute :start_date, '1987-08-24'

753
      get :index, :set_filter => 1, :c => %w(start_date)
754 755

      assert_select "table.issues td.start_date", :text => '24/08/1987'
756 757 758
    end
  end

759
  def test_index_with_done_ratio_column
760 761 762
    Issue.find(1).update_attribute :done_ratio, 40

    get :index, :set_filter => 1, :c => %w(done_ratio)
763 764 765 766 767 768

    assert_select 'table.issues td.done_ratio' do
      assert_select 'table.progress' do
        assert_select 'td.closed[style=?]', 'width: 40%;'
      end
    end
769 770
  end

771 772 773
  def test_index_with_spent_hours_column
    get :index, :set_filter => 1, :c => %w(subject spent_hours)

774
    assert_select 'table.issues tr#issue-3 td.spent_hours', :text => '1.00'
775 776 777 778 779 780
  end

  def test_index_should_not_show_spent_hours_column_without_permission
    Role.anonymous.remove_permission! :view_time_entries
    get :index, :set_filter => 1, :c => %w(subject spent_hours)

781
    assert_select 'td.spent_hours', 0
782 783
  end

784
  def test_index_with_fixed_version_column
785
    get :index, :set_filter => 1, :c => %w(fixed_version)
786 787 788 789

    assert_select 'table.issues td.fixed_version' do
      assert_select 'a[href=?]', '/versions/2', :text => '1.0'
    end
790 791
  end

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
  def test_index_with_relations_column
    IssueRelation.delete_all
    IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(7))
    IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(8), :issue_to => Issue.find(1))
    IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(1), :issue_to => Issue.find(11))
    IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(12), :issue_to => Issue.find(2))

    get :index, :set_filter => 1, :c => %w(subject relations)
    assert_response :success
    assert_select "tr#issue-1 td.relations" do
      assert_select "span", 3
      assert_select "span", :text => "Related to #7"
      assert_select "span", :text => "Related to #8"
      assert_select "span", :text => "Blocks #11"
    end
    assert_select "tr#issue-2 td.relations" do
      assert_select "span", 1
      assert_select "span", :text => "Blocked by #12"
    end
    assert_select "tr#issue-3 td.relations" do
      assert_select "span", 0
    end

    get :index, :set_filter => 1, :c => %w(relations), :format => 'csv'
    assert_response :success
    assert_equal 'text/csv; header=present', response.content_type
    lines = response.body.chomp.split("\n")
    assert_include '1,"Related to #7, Related to #8, Blocks #11"', lines
    assert_include '2,Blocked by #12', lines
    assert_include '3,""', lines

    get :index, :set_filter => 1, :c => %w(subject relations), :format => 'pdf'
    assert_response :success
    assert_equal 'application/pdf', response.content_type
  end

828 829 830 831 832 833 834 835 836 837 838
  def test_index_with_description_column
    get :index, :set_filter => 1, :c => %w(subject description)

    assert_select 'table.issues thead th', 3 # columns: chekbox + id + subject
    assert_select 'td.description[colspan=3]', :text => 'Unable to print recipes'

    get :index, :set_filter => 1, :c => %w(subject description), :format => 'pdf'
    assert_response :success
    assert_equal 'application/pdf', response.content_type
  end

839 840 841 842 843 844
  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

845 846 847 848 849 850
  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

851
  def test_show_by_anonymous
jplang's avatar
jplang committed
852 853
    get :show, :id => 1
    assert_response :success
854
    assert_template 'show'
855
    assert_equal Issue.find(1), assigns(:issue)
856

857 858
    assert_select 'div.issue div.description', :text => /Unable to print recipes/

859
    # anonymous role is allowed to add a note
860 861 862 863 864 865 866 867
    assert_select 'form#issue-form' do
      assert_select 'fieldset' do
        assert_select 'legend', :text => 'Notes'
        assert_select 'textarea[name=?]', 'issue[notes]'
      end
    end

    assert_select 'title', :text => "Bug #1: Can&#x27;t print recipes - eCookbook - Redmine"
jplang's avatar
jplang committed
868
  end
869

870 871 872 873
  def test_show_by_manager
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success
874

875
    assert_select 'a', :text => /Quote/
876

877 878 879 880 881 882 883 884 885 886 887 888 889 890
    assert_select 'form#issue-form' do
      assert_select 'fieldset' do
        assert_select 'legend', :text => 'Change properties'
        assert_select 'input[name=?]', 'issue[subject]'
      end
      assert_select 'fieldset' do
        assert_select 'legend', :text => 'Log time'
        assert_select 'input[name=?]', 'time_entry[hours]'
      end
      assert_select 'fieldset' do
        assert_select 'legend', :text => 'Notes'
        assert_select 'textarea[name=?]', 'issue[notes]'
      end
    end
891
  end
892

893 894 895 896 897 898 899
  def test_show_should_display_update_form
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success

    assert_tag 'form', :attributes => {:id => 'issue-form'}
    assert_tag 'input', :attributes => {:name => 'issue[is_private]'}
900
    assert_tag 'select', :attributes => {:name => 'issue[project_id]'}
901 902 903 904 905 906 907 908 909 910 911 912 913 914
    assert_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
    assert_tag 'input', :attributes => {:name => 'issue[subject]'}
    assert_tag 'textarea', :attributes => {:name => 'issue[description]'}
    assert_tag 'select', :attributes => {:name => 'issue[status_id]'}
    assert_tag 'select', :attributes => {:name => 'issue[priority_id]'}
    assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
    assert_tag 'select', :attributes => {:name => 'issue[category_id]'}
    assert_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
    assert_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
    assert_tag 'input', :attributes => {:name => 'issue[start_date]'}
    assert_tag 'input', :attributes => {:name => 'issue[due_date]'}
    assert_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
    assert_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]' }
    assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
jplang's avatar
jplang committed
915
    assert_tag 'textarea', :attributes => {:name => 'issue[notes]'}
916 917 918 919
  end

  def test_show_should_display_update_form_with_minimal_permissions
    Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
920
    WorkflowTransition.delete_all :role_id => 1
921 922 923 924 925 926 927

    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success

    assert_tag 'form', :attributes => {:id => 'issue-form'}
    assert_no_tag 'input', :attributes => {:name => 'issue[is_private]'}
928
    assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
929 930 931 932 933 934 935 936 937 938 939 940 941 942
    assert_no_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
    assert_no_tag 'input', :attributes => {:name => 'issue[subject]'}
    assert_no_tag 'textarea', :attributes => {:name => 'issue[description]'}
    assert_no_tag 'select', :attributes => {:name => 'issue[status_id]'}
    assert_no_tag 'select', :attributes => {:name => 'issue[priority_id]'}
    assert_no_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
    assert_no_tag 'select', :attributes => {:name => 'issue[category_id]'}
    assert_no_tag 'select', :attributes => {:name => 'issue[fixed_version_id]'}
    assert_no_tag 'input', :attributes => {:name => 'issue[parent_issue_id]'}
    assert_no_tag 'input', :attributes => {:name => 'issue[start_date]'}
    assert_no_tag 'input', :attributes => {:name => 'issue[due_date]'}
    assert_no_tag 'select', :attributes => {:name => 'issue[done_ratio]'}
    assert_no_tag 'input', :attributes => { :name => 'issue[custom_field_values][2]' }
    assert_no_tag 'input', :attributes => {:name => 'issue[watcher_user_ids][]'}
jplang's avatar
jplang committed
943
    assert_tag 'textarea', :attributes => {:name => 'issue[notes]'}
944 945 946 947 948 949 950 951 952 953 954
  end

  def test_show_should_display_update_form_with_workflow_permissions
    Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]

    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success

    assert_tag 'form', :attributes => {:id => 'issue-form'}
    assert_no_tag 'input', :attributes => {:name => 'issue[is_private]'}
955
    assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
956 957 958 959 960 961 962 963 964 965 966 967 968 969
    assert_no_tag 'select', :attributes => {:name => 'issue[tracker_id]'}
    assert_no_tag 'input', :attributes => {:name =>