summaryrefslogtreecommitdiffstats
path: root/test/helpers/issues_helper_test.rb
blob: 991e7fe781bcc3661422ed55c717e4f1f76f59f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2019  Jean-Philippe Lang
#
# 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.
#
# 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.
#
# 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.

require File.expand_path('../../test_helper', __FILE__)

class IssuesHelperTest < Redmine::HelperTest
  include IssuesHelper
  include CustomFieldsHelper
  include ERB::Util
  include Rails.application.routes.url_helpers

  fixtures :projects, :trackers, :issue_statuses, :issues,
           :enumerations, :users, :issue_categories,
           :projects_trackers,
           :roles,
           :member_roles,
           :members,
           :enabled_modules,
           :custom_fields,
           :attachments,
           :versions

  def test_issue_heading
    assert_equal "Bug #1", issue_heading(Issue.find(1))
  end

  def test_issues_destroy_confirmation_message_with_one_root_issue
    assert_equal l(:text_issues_destroy_confirmation),
                 issues_destroy_confirmation_message(Issue.find(1))
  end

  def test_issues_destroy_confirmation_message_with_an_arrayt_of_root_issues
    assert_equal l(:text_issues_destroy_confirmation),
                 issues_destroy_confirmation_message(Issue.find([1, 2]))
  end

  def test_issues_destroy_confirmation_message_with_one_parent_issue
    Issue.find(2).update! :parent_issue_id => 1
    assert_equal l(:text_issues_destroy_confirmation) + "\n" +
                   l(:text_issues_destroy_descendants_confirmation, :count => 1),
                 issues_destroy_confirmation_message(Issue.find(1))
  end

  def test_issues_destroy_confirmation_message_with_one_parent_issue_and_its_child
    Issue.find(2).update! :parent_issue_id => 1
    assert_equal l(:text_issues_destroy_confirmation),
                 issues_destroy_confirmation_message(Issue.find([1, 2]))
  end

  def test_issues_destroy_confirmation_message_with_issues_that_share_descendants
    root = Issue.generate!
    child = Issue.generate!(:parent_issue_id => root.id)
    Issue.generate!(:parent_issue_id => child.id)

    assert_equal l(:text_issues_destroy_confirmation) + "\n" +
                   l(:text_issues_destroy_descendants_confirmation, :count => 1),
                 issues_destroy_confirmation_message([root.reload, child.reload])
  end

  test 'show_detail with no_html should show a changing attribute' do
    detail = JournalDetail.new(:property => 'attr', :old_value => '40',
                               :value => '100', :prop_key => 'done_ratio')
    assert_equal "% Done changed from 40 to 100", show_detail(detail, true)
  end

  test 'show_detail with no_html should show a new attribute' do
    detail = JournalDetail.new(:property => 'attr', :old_value => nil,
                               :value => '100', :prop_key => 'done_ratio')
    assert_equal "% Done set to 100", show_detail(detail, true)
  end

  test 'show_detail with no_html should show a deleted attribute' do
    detail = JournalDetail.new(:property => 'attr', :old_value => '50',
                               :value => nil, :prop_key => 'done_ratio')
    assert_equal "% Done deleted (50)", show_detail(detail, true)
  end

  test 'show_detail with html should show a changing attribute with HTML highlights' do
    detail = JournalDetail.new(:property => 'attr', :old_value => '40',
                               :value => '100', :prop_key => 'done_ratio')
    html = show_detail(detail, false)
    assert_include '<strong>% Done</strong>', html
    assert_include '<i>40</i>', html
    assert_include '<i>100</i>', html
  end

  test 'show_detail with html should show a new attribute with HTML highlights' do
    detail = JournalDetail.new(:property => 'attr', :old_value => nil,
                               :value => '100', :prop_key => 'done_ratio')
    html = show_detail(detail, false)
    assert_include '<strong>% Done</strong>', html
    assert_include '<i>100</i>', html
  end

  test 'show_detail with html should show a deleted attribute with HTML highlights' do
    detail = JournalDetail.new(:property => 'attr', :old_value => '50',
                               :value => nil, :prop_key => 'done_ratio')
    html = show_detail(detail, false)
    assert_include '<strong>% Done</strong>', html
    assert_include '<del><i>50</i></del>', html
  end

  test 'show_detail with a start_date attribute should format the dates' do
    detail = JournalDetail.new(
               :property  => 'attr',
               :old_value => '2010-01-01',
               :value     => '2010-01-31',
               :prop_key  => 'start_date'
            )
    with_settings :date_format => '%m/%d/%Y' do
      assert_match "01/31/2010", show_detail(detail, true)
      assert_match "01/01/2010", show_detail(detail, true)
    end
  end

  test 'show_detail with a due_date attribute should format the dates' do
    detail = JournalDetail.new(
              :property  => 'attr',
              :old_value => '2010-01-01',
              :value     => '2010-01-31',
              :prop_key  => 'due_date'
            )
    with_settings :date_format => '%m/%d/%Y' do
      assert_match "01/31/2010", show_detail(detail, true)
      assert_match "01/01/2010", show_detail(detail, true)
    end
  end

  test 'show_detail should show old and new values with a project attribute' do
    User.current = User.find(2)
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'project_id',
                               :old_value => 1, :value => 2)
    assert_match 'eCookbook', show_detail(detail, true)
    assert_match 'OnlineStore', show_detail(detail, true)
  end

  test 'show_detail with a project attribute should show project ID if project is not visible' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'project_id',
                               :old_value => 1, :value => 2)
    assert_match 'eCookbook', show_detail(detail, true)
    assert_match '2', show_detail(detail, true)
  end

  test 'show_detail should show old and new values with a issue status attribute' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'status_id',
                               :old_value => 1, :value => 2)
    assert_match 'New', show_detail(detail, true)
    assert_match 'Assigned', show_detail(detail, true)
  end

  test 'show_detail should show old and new values with a tracker attribute' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'tracker_id',
                               :old_value => 1, :value => 2)
    assert_match 'Bug', show_detail(detail, true)
    assert_match 'Feature request', show_detail(detail, true)
  end

  test 'show_detail should show old and new values with a assigned to attribute' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'assigned_to_id',
                               :old_value => 1, :value => 2)
    assert_match 'Redmine Admin', show_detail(detail, true)
    assert_match 'John Smith', show_detail(detail, true)
  end

  test 'show_detail should show old and new values with a priority attribute' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'priority_id',
                               :old_value => 4, :value => 5)
    assert_match 'Low', show_detail(detail, true)
    assert_match 'Normal', show_detail(detail, true)
  end

  test 'show_detail should show old and new values with a category attribute' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'category_id',
                               :old_value => 1, :value => 2)
    assert_match 'Printing', show_detail(detail, true)
    assert_match 'Recipes', show_detail(detail, true)
  end

  test 'show_detail should show old and new values with a fixed version attribute' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'fixed_version_id',
                               :old_value => 1, :value => 2)
    assert_match '0.1', show_detail(detail, true)
    assert_match '1.0', show_detail(detail, true)
  end

  test 'show_detail should show old and new values with a estimated hours attribute' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'estimated_hours',
                               :old_value => '5', :value => '6.3')
    assert_match '5.00', show_detail(detail, true)
    assert_match '6.30', show_detail(detail, true)
  end

  test 'show_detail should not show values with a description attribute' do
    detail = JournalDetail.new(:property => 'attr', :prop_key => 'description',
                               :old_value => 'Foo', :value => 'Bar')
    assert_equal 'Description updated', show_detail(detail, true)
  end

  test 'show_detail should show old and new values with a custom field' do
    detail = JournalDetail.new(:property => 'cf', :prop_key => '1',
                               :old_value => 'MySQL', :value => 'PostgreSQL')
    assert_equal 'Database changed from MySQL to PostgreSQL', show_detail(detail, true)
  end

  test 'show_detail should not show values with a long text custom field' do
    field = IssueCustomField.create!(:name => "Long field", :field_format => 'text')
    detail = JournalDetail.new(:property => 'cf', :prop_key => field.id,
                               :old_value => 'Foo', :value => 'Bar')
    assert_equal 'Long field updated', show_detail(detail, true)
  end

  test 'show_detail should show added file' do
    detail = JournalDetail.new(:property => 'attachment', :prop_key => '1',
                               :old_value => nil, :value => 'error281.txt')
    assert_match 'error281.txt', show_detail(detail, true)
  end

  test 'show_detail should show removed file' do
    detail = JournalDetail.new(:property => 'attachment', :prop_key => '1',
                               :old_value => 'error281.txt', :value => nil)
    assert_match 'error281.txt', show_detail(detail, true)
  end

  def test_show_detail_relation_added
    detail = JournalDetail.new(:property => 'relation',
                               :prop_key => 'precedes',
                               :value    => 1)
    assert_equal "Precedes Bug #1: Cannot print recipes added", show_detail(detail, true)
    str = link_to("Bug #1", "/issues/1", :class => Issue.find(1).css_classes)
    assert_equal "<strong>Precedes</strong> <i>#{str}: Cannot print recipes</i> added",
                 show_detail(detail, false)
  end

  def test_show_detail_relation_added_with_inexistant_issue
    inexistant_issue_number = 9999
    assert_nil  Issue.find_by_id(inexistant_issue_number)
    detail = JournalDetail.new(:property => 'relation',
                               :prop_key => 'precedes',
                               :value    => inexistant_issue_number)
    assert_equal "Precedes Issue ##{inexistant_issue_number} added", show_detail(detail, true)
    assert_equal "<strong>Precedes</strong> <i>Issue ##{inexistant_issue_number}</i> added", show_detail(detail, false)
  end

  def test_show_detail_relation_added_should_not_disclose_issue_that_is_not_visible
    issue = Issue.generate!(:is_private => true)
    detail = JournalDetail.new(:property => 'relation',
                               :prop_key => 'precedes',
                               :value    => issue.id)

    assert_equal "Precedes Issue ##{issue.id} added", show_detail(detail, true)
    assert_equal "<strong>Precedes</strong> <i>Issue ##{issue.id}</i> added", show_detail(detail, false)
  end

  def test_show_detail_relation_deleted
    detail = JournalDetail.new(:property  => 'relation',
                               :prop_key  => 'precedes',
                               :old_value => 1)
    assert_equal "Precedes deleted (Bug #1: Cannot print recipes)", show_detail(detail, true)
    str = link_to("Bug #1",
                  "/issues/1",
                  :class => Issue.find(1).css_classes)
    assert_equal "<strong>Precedes</strong> deleted (<i>#{str}: Cannot print recipes</i>)",
                 show_detail(detail, false)
  end

  def test_show_detail_relation_deleted_with_inexistant_issue
    inexistant_issue_number = 9999
    assert_nil  Issue.find_by_id(inexistant_issue_number)
    detail = JournalDetail.new(:property  => 'relation',
                               :prop_key  => 'precedes',
                               :old_value => inexistant_issue_number)
    assert_equal "Precedes deleted (Issue #9999)", show_detail(detail, true)
    assert_equal "<strong>Precedes</strong> deleted (<i>Issue #9999</i>)", show_detail(detail, false)
  end

  def test_show_detail_relation_deleted_should_not_disclose_issue_that_is_not_visible
    issue = Issue.generate!(:is_private => true)
    detail = JournalDetail.new(:property => 'relation',
                               :prop_key => 'precedes',
                               :old_value    => issue.id)

    assert_equal "Precedes deleted (Issue ##{issue.id})", show_detail(detail, true)
    assert_equal "<strong>Precedes</strong> deleted (<i>Issue ##{issue.id}</i>)", show_detail(detail, false)
  end

  def test_details_to_strings_with_multiple_values_removed_from_custom_field
    field = IssueCustomField.generate!(:name => 'User', :field_format => 'user', :multiple => true)
    details = []
    details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => '1', :value => nil)
    details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => '3', :value => nil)

    assert_equal ["User deleted (Dave Lopper, Redmine Admin)"], details_to_strings(details, true)
    assert_equal ["<strong>User</strong> deleted (<del><i>Dave Lopper, Redmine Admin</i></del>)"], details_to_strings(details, false)
  end

  def test_details_to_strings_with_multiple_values_added_to_custom_field
    field = IssueCustomField.generate!(:name => 'User', :field_format => 'user', :multiple => true)
    details = []
    details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => nil, :value => '1')
    details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => nil, :value => '3')

    assert_equal ["User Dave Lopper, Redmine Admin added"], details_to_strings(details, true)
    assert_equal ["<strong>User</strong> <i>Dave Lopper, Redmine Admin</i> added"], details_to_strings(details, false)
  end

  def test_details_to_strings_with_multiple_values_added_and_removed_from_custom_field
    field = IssueCustomField.generate!(:name => 'User', :field_format => 'user', :multiple => true)
    details = []
    details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => nil, :value => '1')
    details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => '2', :value => nil)
    details << JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s, :old_value => '3', :value => nil)

    assert_equal [
      "User Redmine Admin added",
      "User deleted (Dave Lopper, John Smith)"
      ], details_to_strings(details, true)
    assert_equal [
      "<strong>User</strong> <i>Redmine Admin</i> added",
      "<strong>User</strong> deleted (<del><i>Dave Lopper, John Smith</i></del>)"
      ], details_to_strings(details, false)
  end

  def test_find_name_by_reflection_should_return_nil_for_missing_record
    assert_nil find_name_by_reflection('status', 99)
  end

  def test_issue_due_date_details
    travel_to Time.parse('2019-06-01 23:00:00 UTC') do
      User.current = User.first
      User.current.pref.update_attribute :time_zone, 'UTC'
      issue = Issue.generate!

      # due date is not set
      assert_nil issue_due_date_details(issue)

      # due date is set
      issue.due_date = User.current.today + 5
      issue.save!
      assert_equal '06/06/2019 (Due in 5 days)', issue_due_date_details(issue)

      # Don't show "Due in X days" if the issue is closed
      issue.status = IssueStatus.find_by_is_closed(true)
      issue.save!
      assert_equal '06/06/2019', issue_due_date_details(issue)
    end
  end

  def test_issue_spent_hours_details_should_link_to_project_time_entries_depending_on_cross_project_setting
    %w(descendants).each do |setting|
      with_settings :cross_project_subtasks => setting do
        TimeEntry.generate!(:issue => Issue.generate!(:parent_issue_id => 1), :hours => 3)
        TimeEntry.generate!(:issue => Issue.generate!(:parent_issue_id => 1), :hours => 4)

        assert_match "href=\"/projects/ecookbook/time_entries?issue_id=~1\"", issue_spent_hours_details(Issue.find(1))
      end
    end
  end

  def test_issue_spent_hours_details_should_link_to_global_time_entries_depending_on_cross_project_setting
    %w(system tree hierarchy).each do |setting|
      with_settings :cross_project_subtasks => setting do
        TimeEntry.generate!(:issue => Issue.generate!(:parent_issue_id => 1), :hours => 3)
        TimeEntry.generate!(:issue => Issue.generate!(:parent_issue_id => 1), :hours => 4)

        assert_match "href=\"/time_entries?issue_id=~1\"", issue_spent_hours_details(Issue.find(1))
      end
    end
  end
end