summaryrefslogtreecommitdiffstats
path: root/test/functional/issues_controller_transaction_test.rb
blob: 7f7d94b55f2f21219041910c495c8d16faecbaf3 (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
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-  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__)
require 'issues_controller'

class IssuesControllerTransactionTest < Redmine::ControllerTest
  tests IssuesController
  fixtures :projects,
           :users,
           :roles,
           :members,
           :member_roles,
           :issues,
           :issue_statuses,
           :versions,
           :trackers,
           :projects_trackers,
           :issue_categories,
           :enabled_modules,
           :enumerations,
           :attachments,
           :workflows,
           :custom_fields,
           :custom_values,
           :custom_fields_projects,
           :custom_fields_trackers,
           :time_entries,
           :journals,
           :journal_details,
           :queries

  self.use_transactional_tests = false

  def setup
    User.current = nil
  end

  def test_update_stale_issue_should_not_update_the_issue
    issue = Issue.find(2)
    @request.session[:user_id] = 2

    assert_no_difference 'Journal.count' do
      assert_no_difference 'TimeEntry.count' do
        put(
          :update,
          :params => {
            :id => issue.id,
            :issue => {
              :fixed_version_id => 4,
              :notes => 'My notes',
              :lock_version => (issue.lock_version - 1)
            },
            :time_entry => {
              :hours => '2.5',
              :comments => '',
              :activity_id => TimeEntryActivity.first.id
            }
          }
        )
      end
    end

    assert_response :success

    assert_select 'div.conflict'
    assert_select 'input[name=?][value=?]', 'conflict_resolution', 'overwrite'
    assert_select 'input[name=?][value=?]', 'conflict_resolution', 'add_notes'
    assert_select 'label' do
      assert_select 'input[name=?][value=?]', 'conflict_resolution', 'cancel'
      assert_select 'a[href="/issues/2"]'
    end
  end

  def test_update_stale_issue_should_save_attachments
    set_tmp_attachments_directory
    issue = Issue.find(2)
    @request.session[:user_id] = 2

    assert_no_difference 'Journal.count' do
      assert_no_difference 'TimeEntry.count' do
        assert_difference 'Attachment.count' do
          put(
            :update,
            :params => {
              :id => issue.id,
              :issue => {
                :fixed_version_id => 4,
                :notes => 'My notes',
                :lock_version => (issue.lock_version - 1)
              },
              :attachments => {
                '1' => {
                  'file' => uploaded_test_file('testfile.txt', 'text/plain')
                }
              },
              :time_entry => {
                :hours => '2.5',
                :comments => '',
                :activity_id => TimeEntryActivity.first.id
              }
            }
          )
        end
      end
    end

    assert_response :success

    attachment = Attachment.order('id DESC').first
    assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
    assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
  end

  def test_update_stale_issue_without_notes_should_not_show_add_notes_option
    issue = Issue.find(2)
    @request.session[:user_id] = 2

    put(
      :update,
      :params => {
        :id => issue.id,
        :issue => {
          :fixed_version_id => 4,
          :notes => '',
          :lock_version => (issue.lock_version - 1)
        }
      }
    )
    assert_response :success

    assert_select 'div.conflict'
    assert_select 'input[name=conflict_resolution][value=overwrite]'
    assert_select 'input[name=conflict_resolution][value=add_notes]', 0
    assert_select 'input[name=conflict_resolution][value=cancel]'
  end

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

    put(
      :update,
      :params => {
        :id => 1,
        :issue => {
          :fixed_version_id => 4,
          :notes => '',
          :lock_version => 2
        },
        :last_journal_id => 1
      }
    )
    assert_response :success

    assert_select '.conflict-journal', 1
    assert_select 'div.conflict', :text => /Some notes with Redmine links/
  end

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

    put(
      :update,
      :params => {
        :id => 1,
        :issue => {
          :fixed_version_id => 4,
          :notes => '',
          :lock_version => 2
        },
        :last_journal_id => ''
      }
    )
    assert_response :success

    assert_select '.conflict-journal', 2
    assert_select 'div.conflict', :text => /Some notes with Redmine links/
    assert_select 'div.conflict', :text => /Journal notes/
  end

  def test_update_stale_issue_should_show_private_journals_with_permission_only
    journal = Journal.create!(:journalized => Issue.find(1), :notes => 'Privates notes', :private_notes => true, :user_id => 1)

    @request.session[:user_id] = 2
    put(
      :update,
      :params => {
        :id => 1,
        :issue => {
          :fixed_version_id => 4,
          :lock_version => 2
        },
        :last_journal_id => ''
      }
    )
    assert_response :success
    assert_select '.conflict-journal', :text => /Privates notes/

    Role.find(1).remove_permission! :view_private_notes
    put(
      :update,
      :params => {
        :id => 1,
        :issue => {
          :fixed_version_id => 4,
          :lock_version => 2
        },
        :last_journal_id => ''
      }
    )
    assert_response :success
    assert_select '.conflict-journal', :text => /Privates notes/, :count => 0
  end

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

    assert_difference 'Journal.count' do
      put(
        :update,
        :params => {
          :id => 1,
          :issue => {
            :fixed_version_id => 4,
            :notes => 'overwrite_conflict_resolution',
            :lock_version => 2
          },
          :conflict_resolution => 'overwrite'
        }
      )
    end

    assert_response 302
    issue = Issue.find(1)
    assert_equal 4, issue.fixed_version_id
    journal = Journal.order('id DESC').first
    assert_equal 'overwrite_conflict_resolution', journal.notes
    assert journal.details.any?
  end

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

    assert_difference 'Journal.count' do
      put(
        :update,
        :params => {
          :id => 1,
          :issue => {
            :fixed_version_id => 4,
            :notes => 'add_notes_conflict_resolution',
            :lock_version => 2
          },
          :conflict_resolution => 'add_notes'
        }
      )
    end

    assert_response 302
    issue = Issue.find(1)
    assert_nil issue.fixed_version_id
    journal = Journal.order('id DESC').first
    assert_equal 'add_notes_conflict_resolution', journal.notes
    assert_equal false, journal.private_notes
    assert journal.details.empty?
  end

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

    journal = new_record(Journal) do
      put(
        :update,
        :params => {
          :id => 1,
          :issue => {
            :fixed_version_id => 4,
            :notes => 'add_privates_notes_conflict_resolution',
            :private_notes => '1',
            :lock_version => 2
          },
          :conflict_resolution => 'add_notes'
        }
      )
    end

    assert_response 302
    assert_equal 'add_privates_notes_conflict_resolution', journal.notes
    assert_equal true, journal.private_notes
    assert journal.details.empty?
  end

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

    assert_no_difference 'Journal.count' do
      put(
        :update,
        :params => {
          :id => 1,
          :issue => {
            :fixed_version_id => 4,
            :notes => 'add_notes_conflict_resolution',
            :lock_version => 2
          },
          :conflict_resolution => 'cancel'
        }
      )
    end

    assert_redirected_to '/issues/1'
    issue = Issue.find(1)
    assert_nil issue.fixed_version_id
  end

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

    assert_no_difference('TimeEntry.count') do
      put(
        :update,
        :params => {
          :id => 1,
          :issue => {
            :subject => ''
          },
          :time_entry => {
            :hours => '2:30',
            :comments => 'should not be added',
            :activity_id => TimeEntryActivity.first.id
          }
        }
      )
      assert_response :success
    end

    assert_select 'input[name=?][value=?]', 'time_entry[hours]', '2:30'
    assert_select 'input[name=?][value=?]', 'time_entry[comments]', 'should not be added'
    assert_select 'select[name=?]', 'time_entry[activity_id]' do
      assert_select 'option[value=?][selected=selected]', TimeEntryActivity.first.id.to_s
    end
  end

  def test_index_should_rescue_invalid_sql_query
    IssueQuery.any_instance.stubs(:statement).returns("INVALID STATEMENT")

    get :index
    assert_response 500
    assert_select 'p', :text => /An error occurred/
    assert_nil session[:query]
    assert_nil session[:issues_index_sort]
  end
end