issues_controller_test.rb 143 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 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
           :journal_details,
44 45 46
           :queries,
           :repositories,
           :changesets
47

48 49
  include Redmine::I18n

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

57
  def test_index
58 59 60 61 62 63
    with_settings :default_language => "en" do
      get :index
      assert_response :success
      assert_template 'index'
      assert_not_nil assigns(:issues)
      assert_nil assigns(:project)
jplang's avatar
jplang committed
64
      assert_tag :tag => 'a', :content => /Can&#x27;t print recipes/
65 66 67 68 69 70 71
      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/
      # project column
      assert_tag :tag => 'th', :content => /Project/
    end
72
  end
73

74 75
  def test_index_should_not_list_issues_when_module_disabled
    EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
76 77
    get :index
    assert_response :success
78
    assert_template 'index'
79 80
    assert_not_nil assigns(:issues)
    assert_nil assigns(:project)
jplang's avatar
jplang committed
81
    assert_no_tag :tag => 'a', :content => /Can&#x27;t print recipes/
82 83
    assert_tag :tag => 'a', :content => /Subproject issue/
  end
jplang's avatar
jplang committed
84 85 86 87 88 89 90

  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
91

92
  def test_index_with_project
93
    Setting.display_subprojects_issues = 0
94 95
    get :index, :project_id => 1
    assert_response :success
96
    assert_template 'index'
97
    assert_not_nil assigns(:issues)
jplang's avatar
jplang committed
98
    assert_tag :tag => 'a', :content => /Can&#x27;t print recipes/
99 100
    assert_no_tag :tag => 'a', :content => /Subproject issue/
  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)
jplang's avatar
jplang committed
108
    assert_tag :tag => 'a', :content => /Can&#x27;t print recipes/
109 110 111
    assert_tag :tag => 'a', :content => /Subproject issue/
    assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
  end
112

113 114 115 116 117
  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
118
    assert_template 'index'
119
    assert_not_nil assigns(:issues)
jplang's avatar
jplang committed
120
    assert_tag :tag => 'a', :content => /Can&#x27;t print recipes/
121 122
    assert_tag :tag => 'a', :content => /Subproject issue/
    assert_tag :tag => 'a', :content => /Issue of a private subproject/
123
  end
124

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

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

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

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

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

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  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
249 250 251
  def test_index_with_query
    get :index, :project_id => 1, :query_id => 5
    assert_response :success
252
    assert_template 'index'
jplang's avatar
jplang committed
253 254 255
    assert_not_nil assigns(:issues)
    assert_nil assigns(:issue_count_by_group)
  end
256

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

265 266 267
  def test_index_with_query_grouped_by_list_custom_field
    get :index, :project_id => 1, :query_id => 9
    assert_response :success
268
    assert_template 'index'
269
    assert_not_nil assigns(:issues)
270
    assert_not_nil assigns(:issue_count_by_group)
jplang's avatar
jplang committed
271
  end
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  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'
      assert_select 'span.count', :text => '(1)'
    end
    assert_select 'tr.group' do
      assert_select 'a', :text => 'Dave Lopper'
      assert_select 'span.count', :text => '(2)'
    end
  end

294 295 296 297 298 299 300 301
  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

302 303 304 305 306
  def test_index_with_invalid_query_id_should_respond_404
    get :index, :project_id => 1, :query_id => 999
    assert_response 404
  end

307 308 309 310 311 312 313 314 315 316 317 318 319 320
  def test_index_with_cross_project_query_in_session_should_show_project_issues
    q = Query.create!(:name => "test", :user_id => 2, :is_public => false, :project => nil)
    @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

321 322 323
  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
324

325 326 327
    get :index, :query_id => q.id
    assert_response 403
  end
328

329 330 331
  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
332

333 334 335
    get :index, :query_id => q.id
    assert_response :success
  end
336

337 338 339
  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
340

341 342 343
    get :index, :query_id => q.id
    assert_response :success
  end
344

345 346 347 348 349 350 351 352 353
  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

354
  def test_index_csv
355 356 357
    get :index, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
jplang's avatar
jplang committed
358
    assert_equal 'text/csv; header=present', @response.content_type
359
    assert @response.body.starts_with?("#,")
360 361 362
    lines = @response.body.chomp.split("\n")
    assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
  end
363

364
  def test_index_csv_with_project
365 366 367
    get :index, :project_id => 1, :format => 'csv'
    assert_response :success
    assert_not_nil assigns(:issues)
jplang's avatar
jplang committed
368
    assert_equal 'text/csv; header=present', @response.content_type
369
  end
370

371 372 373 374
  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
375
    assert_equal 'text/csv; header=present', @response.content_type
376 377 378 379 380
    assert @response.body.starts_with?("#,")
    lines = @response.body.chomp.split("\n")
    assert_equal assigns(:query).columns.size + 2, lines[0].split(',').size
  end

381
  def test_index_csv_with_spent_time_column
jplang's avatar
jplang committed
382 383
    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)
384 385 386

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

392 393 394 395
  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
396
    assert_equal 'text/csv; header=present', @response.content_type
397 398 399 400 401
    assert @response.body.starts_with?("#,")
    lines = @response.body.chomp.split("\n")
    assert_equal assigns(:query).available_columns.size + 1, lines[0].split(',').size
  end

jplang's avatar
jplang committed
402 403 404 405 406 407 408 409 410 411 412 413
  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

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  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'
jplang's avatar
jplang committed
431
      assert_equal 'text/csv; header=present', @response.content_type
432
      lines = @response.body.chomp.split("\n")
433 434 435 436 437 438 439 440 441
      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

442 443 444 445 446 447 448 449 450 451 452 453 454 455
  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],
456 457 458
                  :c => ['status', 'subject'],
                  :format => 'csv',
                  :set_filter => 1
jplang's avatar
jplang committed
459
      assert_equal 'text/csv; header=present', @response.content_type
460 461
      lines = @response.body.chomp.split("\n")
      s1 = "\xaa\xac\xbaA" # status
462 463 464 465
      if str_utf8.respond_to?(:force_encoding)
        s1.force_encoding('Big5')
      end
      assert lines[0].include?(s1)
466
      s2 = lines[1].split(",")[2]
467
      if s1.respond_to?(:force_encoding)
468
        s3 = "\xa5H?" # subject
469 470 471 472 473 474 475 476 477 478
        s3.force_encoding('Big5')
        assert_equal s3, s2
      elsif RUBY_PLATFORM == 'java'
        assert_equal "??", s2
      else
        assert_equal "\xa5H???", s2
      end
    end
  end

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
  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
jplang's avatar
jplang committed
494
      assert_equal 'text/csv; header=present', @response.content_type
495
      lines = @response.body.chomp.split("\n")
496
      assert_equal "#{issue.id},1234.50,#{str1}", lines[1]
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

      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
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 529 530 531 532 533 534 535 536

      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

537
  def test_index_pdf
538 539
    ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
      with_settings :default_language => lang do
540

541 542 543
        get :index
        assert_response :success
        assert_template 'index'
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
        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
570
  end
571

572 573 574 575 576 577 578
  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
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
  def test_index_atom
    get :index, :project_id => 'ecookbook', :format => 'atom'
    assert_response :success
    assert_template 'common/feed'

    assert_tag :tag => 'link', :parent =>  {:tag => 'feed', :parent => nil },
        :attributes => {:rel => 'self', :href => 'http://test.host/projects/ecookbook/issues.atom'}
    assert_tag :tag => 'link', :parent =>  {:tag => 'feed', :parent => nil },
        :attributes => {:rel => 'alternate', :href => 'http://test.host/projects/ecookbook/issues'}

    assert_tag :tag => 'entry', :child => {
      :tag => 'link',
      :attributes => {:href => 'http://test.host/issues/1'}}
  end

595
  def test_index_sort
jplang's avatar
jplang committed
596
    get :index, :sort => 'tracker,id:desc'
597
    assert_response :success
598

jplang's avatar
jplang committed
599 600 601
    sort_params = @request.session['issues_index_sort']
    assert sort_params.is_a?(String)
    assert_equal 'tracker,id:desc', sort_params
602

jplang's avatar
jplang committed
603 604 605 606
    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)
607
  end
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

  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
632 633 634 635
  
  def test_index_sort_by_author
    get :index, :sort => 'author'
    assert_response :success
636 637 638 639 640 641 642 643 644
    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
645 646 647 648 649 650
  end
  
  def test_index_group_by_author
    get :index, :group_by => 'author', :sort => 'priority'
    assert_response :success
  end
651
  
jplang's avatar
jplang committed
652 653
  def test_index_sort_by_spent_hours
    get :index, :sort => 'spent_hours:desc'
654 655 656 657
    assert_response :success
    hours = assigns(:issues).collect(&:spent_hours)
    assert_equal hours.sort.reverse, hours
  end
658

659 660 661 662 663 664 665 666 667 668 669 670 671
  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

672 673
  def test_index_with_columns
    columns = ['tracker', 'subject', 'assigned_to']
674
    get :index, :set_filter => 1, :c => columns
675
    assert_response :success
676

677 678 679 680
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
681

682 683 684 685
    # 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)
686 687 688 689 690 691

    # 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" } }
692
  end
693

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  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

715 716 717 718
  def test_index_with_custom_field_column
    columns = %w(tracker subject cf_2)
    get :index, :set_filter => 1, :c => columns
    assert_response :success
719

720 721 722 723
    # query should use specified columns
    query = assigns(:query)
    assert_kind_of Query, query
    assert_equal columns, query.column_names.map(&:to_s)
724

725 726 727 728
    assert_tag :td,
      :attributes => {:class => 'cf_2 string'},
      :ancestor => {:tag => 'table', :attributes => {:class => /issues/}}
  end
729

730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
  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

    assert_tag :td,
      :attributes => {:class => /cf_1/},
      :content => 'MySQL, Oracle'
  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

    assert_tag :td,
      :attributes => {:class => /cf_#{field.id}/},
      :child => {:tag => 'a', :content => 'John Smith'}
  end

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

769
  def test_index_with_done_ratio_column
770 771 772 773 774 775 776 777 778
    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

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  def test_index_with_spent_hours_column
    get :index, :set_filter => 1, :c => %w(subject spent_hours)

    assert_tag 'tr', :attributes => {:id => 'issue-3'},
      :child => {
        :tag => 'td', :attributes => {:class => /spent_hours/}, :content => '1.00'
      }
  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)

    assert_no_tag 'td', :attributes => {:class => /spent_hours/}
  end

795
  def test_index_with_fixed_version_column
796 797 798 799 800
    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

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 828 829 830 831 832 833 834 835 836
  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

837 838 839 840 841 842
  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

843 844 845 846 847 848
  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

849
  def test_show_by_anonymous
850 851
    get :show, :id => 1
    assert_response :success
852
    assert_template 'show'
853
    assert_not_nil assigns(:issue)
854
    assert_equal Issue.find(1), assigns(:issue)
855

856 857 858
    # anonymous role is allowed to add a note
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
859
                                :child => { :tag => 'legend',
860
                                            :content => /Notes/ } }
861
    assert_tag :tag => 'title',
jplang's avatar
jplang committed
862
      :content => "Bug #1: Can&#x27;t print recipes - eCookbook - Redmine"
863
  end
864

865 866 867 868
  def test_show_by_manager
    @request.session[:user_id] = 2
    get :show, :id => 1
    assert_response :success
869

870 871
    assert_tag :tag => 'a',
      :content => /Quote/
872

873 874
    assert_tag :tag => 'form',
               :descendant => { :tag => 'fieldset',
875
                                :child => { :tag => 'legend',
876 877
                                            :content => /Change properties/ } },
               :descendant => { :tag => 'fieldset',
878
                                :child => { :tag => 'legend',
879 880
                                            :content => /Log time/ } },
               :descendant => { :tag => 'fieldset',
881
                                :child => { :tag => 'legend',
882 883
                                            :content => /Notes/ } }
  end
884

885 886 887 888 889 890 891
  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]'}
892
    assert_tag 'select', :attributes => {:name => 'issue[project_id]'}
893 894 895 896 897 898 899 900 901 902 903 904 905 906
    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
907
    assert_tag 'textarea', :attributes => {:name => 'issue[notes]'}
908 909 910 911
  end

  def test_show_should_display_update_form_with_minimal_permissions
    Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
912
    WorkflowTransition.delete_all :role_id => 1
913 914 915 916 917 918 919

    @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]'}
920
    assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
921 922 923 924 925 926 927 928 929 930 931 932 933 934
    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
935
    assert_tag 'textarea', :attributes => {:name => 'issue[notes]'}
936 937 938 939 940 941 942 943 944 945 946
  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]'}
947
    assert_no_tag 'select', :attributes => {:name => 'issue[project_id]'}
948 949 950 951 952 953 954 955 956 957 958 959 960 961
    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_tag 'select', :attributes => {:name => 'issue[status_id]'}
    assert_no_tag 'select', :attributes => {:name => 'issue[priority_id]'}
    assert_tag 'select', :attributes => {:name => 'issue[assigned_to_id]'}
    assert_no_tag 'select', :attributes => {:name => 'issue[category_id]'}
    assert_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_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
962
    assert_tag 'textarea', :attributes => {:name => 'issue[notes]'}
963 964 965 966 967 968 969 970 971 972 973 974
  end

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

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

    assert_no_tag 'form', :attributes => {:id => 'issue-form'}
  end

975 976 977 978 979 980 981 982 983
  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
984

985 986 987
  def test_update_form_should_allow_attachment_upload
    @request.session[:user_id] = 2
    get :show, :id => 1
988

989 990 991 992 993 994 995
    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
996

jplang's avatar
jplang committed
997 998 999 1000 1001
  def test_show_should_deny_anonymous_access_without_permission
    Role.anonymous.remove_permission!(:view_issues)
    get :show, :id => 1
    assert_response :redirect
  end
1002

jplang's avatar
jplang committed
1003 1004 1005 1006 1007
  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
1008

jplang's avatar
jplang committed
1009 1010 1011 1012 1013 1014
  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
1015

jplang's avatar
jplang committed
1016 1017 1018 1019 1020 1021
  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
1022

jplang's avatar
jplang committed
1023 1024 1025 1026 1027 1028
  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
1029

jplang's avatar
jplang committed
1030 1031 1032 1033 1034 1035
  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
1036

jplang's avatar
jplang committed
1037 1038 1039 1040 1041 1042
  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
1043

jplang's avatar
jplang committed
1044 1045 1046 1047 1048 1049
  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
1050

jplang's avatar
jplang committed
1051 1052 1053 1054 1055 1056 1057
  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
1058

1059 1060 1061 1062 1063
  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')
1064

1065 1066
    get :show, :id => 1
    assert_response :success
1067

1068 1069 1070 1071 1072
    assert_tag :div, :attributes => { :id => 'relations' },
                     :descendant => { :tag => 'a', :content => /#2$/ }
    assert_no_tag :div, :attributes => { :id => 'relations' },
                        :descendant => { :tag => 'a', :content => /#4$/ }
  end
1073

jplang's avatar
jplang committed
1074
  def test_show_should_list_subtasks
jplang's avatar
jplang committed
1075
    Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
jplang's avatar
jplang committed
1076 1077 1078 1079

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

1083
  def test_show_should_list_parents
jplang's avatar
jplang committed
1084
    issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1085 1086 1087 1088 1089 1090 1091 1092 1093

    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

1094 1095 1096 1097 1098
  def test_show_should_not_display_prev_next_links_without_query_in_session
    get :show, :id => 1
    assert_response :success
    assert_nil assigns(:prev_issue_id)
    assert_nil assigns(:next_issue_id)
1099 1100

    assert_no_tag 'div', :attributes => {:class => /next-prev-links/}
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
  end

  def test_show_should_display_prev_next_links_with_query_in_session
    @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
    @request.session['issues_index_sort'] = 'id'

    with_settings :display_subprojects_issues => '0' do
      get :show, :id => 3
    end

    assert_response :success
    # Previous and next issues for all projects
    assert_equal 2, assigns(:prev_issue_id)
    assert_equal 5, assigns(:next_issue_id)

1116
    assert_tag 'div', :attributes => {:class => /next-prev-links/}
1117 1118
    assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Previous/
    assert_tag 'a', :attributes => {:href => '/issues/5'}, :content => /Next/
1119 1120

    count = Issue.open.visible.count
1121
    assert_tag 'span', :attributes => {:class => 'position'}, :content => "3 of #{count}"
1122 1123
  end

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
  def test_show_should_display_prev_next_links_with_saved_query_in_session
    query = Query.create!(:name => 'test', :is_public => true,  :user_id => 1,
      :filters => {'status_id' => {:values => ['5'], :operator => '='}},
      :sort_criteria => [['id', 'asc']])
    @request.session[:query] = {:id => query.id, :project_id => nil}

    get :show, :id => 11

    assert_response :success
    assert_equal query, assigns(:query)
    # Previous and next issues for all projects
    assert_equal 8, assigns(:prev_issue_id)
    assert_equal 12, assigns(:next_issue_id)

    assert_tag 'a', :attributes => {:href => '/issues/8'}, :content => /Previous/
    assert_tag 'a', :attributes => {:href => '/issues/12'}, :content => /Next/
  end

1142 1143 1144 1145 1146 1147 1148 1149 1150
  def test_show_should_display_prev_next_links_with_query_and_sort_on_association
    @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
    
    %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
      @request.session['issues_index_sort'] = assoc_sort

      get :show, :id => 3
      assert_response :success, "Wrong response status for #{assoc_sort} sort"

jplang's avatar
jplang committed
1151 1152
      assert_tag 'div', :attributes => {:class => /next-prev-links/}, :content => /Previous/
      assert_tag 'div', :attributes => {:class => /next-prev-links/}, :content => /Next/
1153 1154 1155
    end
  end

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
  def test_show_should_display_prev_next_links_with_project_query_in_session
    @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
    @request.session['issues_index_sort'] = 'id'

    with_settings :display_subprojects_issues => '0' do
      get :show, :id => 3
    end

    assert_response :success
    # Previous and next issues inside project
    assert_equal 2, assigns(:prev_issue_id)
    assert_equal 7, assigns(:next_issue_id)

    assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Previous/
    assert_tag 'a', :attributes => {:href => '/issues/7'}, :content => /Next/
  end

  def test_show_should_not_display_prev_link_for_first_issue
    @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
    @request.session['issues_index_sort'] = 'id'

    with_settings :display_subprojects_issues => '0' do
      get :show, :id => 1
    end

    assert_response :success
    assert_nil assigns(:prev_issue_id)
    assert_equal 2, assigns(:next_issue_id)

    assert_no_tag 'a', :content => /Previous/
    assert_tag 'a', :attributes => {:href => '/issues/2'}, :content => /Next/
  end

  def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
    @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
    @request.session['issues_index_sort'] = 'id'

    get :show, :id => 1

    assert_response :success
    assert_nil assigns(:prev_issue_id)
    assert_nil assigns(:next_issue_id)

    assert_no_tag 'a', :content => /Previous/
    assert_no_tag 'a', :content => /Next/
  end

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
  def test_show_show_should_display_prev_next_links_with_query_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 => '')

    query = Query.create!(:name => 'test', :is_public => true,  :user_id => 1, :filters => {},
      :sort_criteria => [["cf_#{cf.id}", 'asc'], ['id', 'asc']])
    @request.session[:query] = {:id => query.id, :project_id => nil}

    get :show, :id => 3
    assert_response :success

    assert_equal 2, assigns(:prev_issue_id)
    assert_equal 1, assigns(:next_issue_id)
  end

1221 1222 1223 1224 1225 1226 1227 1228
  def test_show_should_display_link_to_the_assignee
    get :show, :id => 2
    assert_response :success
    assert_select '.assigned-to' do
      assert_select 'a[href=/users/3]'
    end
  end

1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
  def test_show_should_display_visible_changesets_from_other_projects
    project = Project.find(2)
    issue = project.issues.first
    issue.changeset_ids = [102]
    issue.save!
    project.disable_module! :repository

    @request.session[:user_id] = 2
    get :show, :id => issue.id
    assert_tag 'a', :attributes => {:href => "/projects/ecookbook/repository/revisions/3"}
  end

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
  def test_show_should_display_watchers
    @request.session[:user_id] = 2
    Issue.find(1).add_watcher User.find(2)

    get :show, :id => 1
    assert_select 'div#watchers ul' do
      assert_select 'li' do
        assert_select 'a[href=/users/2]'
        assert_select 'a img[alt=Delete]'
      end
    end
  end

  def test_show_should_display_watchers_with_gravatars
    @request.session[:user_id] = 2
    Issue.find(1).add_watcher User.find(2)

    with_settings :gravatar_enabled => '1' do
      get :show, :id => 1
    end

    assert_select 'div#watchers ul' do
      assert_select 'li' do
        assert_select 'img.gravatar'
        assert_select 'a[href=/users/2]'
        assert_select 'a img[alt=Delete]'
      end
    end
  end
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296

  def test_show_with_thumbnails_enabled_should_display_thumbnails
    @request.session[:user_id] = 2

    with_settings :thumbnails_enabled => '1' do
      get :show, :id => 14
      assert_response :success
    end

    assert_select 'div.thumbnails' do
      assert_select 'a[href=/attachments/16/testfile.png]' do
        assert_select 'img[src=/attachments/thumbnail/16]'
      end
    end
  end

  def test_show_with_thumbnails_disabled_should_not_display_thumbnails
    @request.session[:user_id] = 2

    with_settings :thumbnails_enabled => '0' do
      get :show, :id => 14
      assert_response :success
    end

    assert_select 'div.thumbnails', 0
  end

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
  def test_show_with_multi_custom_field
    field = CustomField.find(1)
    field.update_attribute :multiple, true
    issue = Issue.find(1)
    issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
    issue.save!

    get :show, :id => 1
    assert_response :success

    assert_tag :td, :content => 'MySQL, Oracle'
  end

  def test_show_with_multi_user_custom_field
    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 :show, :id => 1
    assert_response :success

    # TODO: should display links
jplang's avatar
jplang committed
1321
    assert_tag :td, :content => 'Dave Lopper, John Smith'
1322 1323
  end

jplang's avatar
jplang committed
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
  def test_show_should_display_private_notes_with_permission_only
    journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
    @request.session[:user_id] = 2

    get :show, :id => 2
    assert_response :success
    assert_include journal, assigns(:journals)

    Role.find(1).remove_permission! :view_private_notes
    get :show, :id => 2
    assert_response :success
    assert_not_include journal, assigns(:journals)
  end

1338 1339 1340
  def test_show_atom
    get :show, :id => 2, :format => 'atom'
    assert_response :success
1341
    assert_template 'journals/index'
1342
    # Inline image
1343
    assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1344
  end
1345

jplang's avatar
jplang committed
1346
  def test_show_export_to_pdf
1347
    get :show, :id => 3, :format => 'pdf'