20191218-01-ajax-check.diff

Toshi MARUYAMA, 2019-12-19 03:01

Download (14.1 KB)

View differences:

config/locales/en.yml
1190 1190
  text_time_logged_by_changeset: "Applied in changeset %{value}."
1191 1191
  text_issues_destroy_confirmation: 'Are you sure you want to delete the selected issue(s)?'
1192 1192
  text_issues_destroy_descendants_confirmation: "This will also delete %{count} subtask(s)."
1193
  text_close_parent_issue_whose_subtasks_are_open_confirmation: Are you sure you want to close parent issue whose subtasks are open?
1193 1194
  text_time_entries_destroy_confirmation: 'Are you sure you want to delete the selected time entr(y/ies)?'
1194 1195
  text_select_project_modules: 'Select modules to enable for this project:'
1195 1196
  text_default_administrator_account_changed: Default administrator account changed
app/views/issues/_edit.html.erb
82 82
    <%= hidden_field_tag 'issue_position', @issue_position if @issue_position %>
83 83
    <%= hidden_field_tag 'issue_count', @issue_count if @issue_count %>
84 84
<% end %>
85
<%= javascript_tag do %>
86
  $('#issue-form').submit(function(){
87
    var closedIssueStatusIds =
88
          <%= @allowed_statuses.select{|i| i.is_closed?}.map{|i| i.id.to_s}.to_json.html_safe  %> ;
89
    var children = $("#issue_tree").find("tr.issue.child");
90
    if (($.inArray($("#issue_status_id").val(), closedIssueStatusIds) > -1) &&
91
        (children.length > 0) &&
92
        ((children.length - $("#issue_tree").find("tr.issue.child.closed").length) > 0)) {
93
      if (!confirm(
94
          "<%= l(:text_close_parent_issue_whose_subtasks_are_open_confirmation) %>"
95
      )){
96
        return false;
97
      }
98
    }
99
    $("#issue-form").off('submit');
100
    $("#issue-form").submit();
101
  });
102
<% end %>
test/system/issues_test.rb
234 234
    assert_equal 5, issue.reload.status.id
235 235
  end
236 236

  
237
  test "add confirm dialog to issue submit button" do
238
    parent = Issue.generate!(:project_id => 1)
239
    child = Issue.generate!(:project_id => 1, :parent_issue_id => parent.id)
240

  
241
    with_settings :close_parent_issue_whose_subtasks_are_open => 1 do
242
      log_user('dlopper', 'foo')
243

  
244
      visit "/issues/#{parent.id}"
245
      page.first(:link, 'Edit').click
246
      assert page.has_select?("issue_status_id", {:selected => "New"})
247
      page.find("#issue_status_id").select("Closed")
248
      assert_no_difference ['Issue.count', 'child.journals.count'] do
249
        assert_no_difference 'parent.journals.count' do
250
          page.dismiss_confirm /Are you sure/ do
251
            page.first(:button, 'Submit').click
252
          end
253
          assert_equal 1, parent.reload.status.id
254
        end
255
        assert_difference 'parent.journals.count' do
256
          page.accept_confirm /Are you sure/ do
257
            page.first(:button, 'Submit').click
258
          end
259
          assert page.has_css?('#flash_notice')
260
          assert_equal 5, parent.reload.status.id
261
        end
262
      end
263

  
264
      page.first(:link, 'Edit').click
265
      assert page.has_select?("issue_status_id", {:selected => "Closed"})
266
      fill_in 'Subject', :with => 'test of confirm dialog'
267
      assert_no_difference ['Issue.count', 'child.journals.count'] do
268
        assert_no_difference 'parent.journals.count' do
269
          page.dismiss_confirm /Are you sure/ do
270
            page.first(:button, 'Submit').click
271
          end
272
          assert_equal 5, parent.reload.status.id
273
        end
274
        assert_difference 'parent.journals.count' do
275
          page.accept_confirm /Are you sure/ do
276
            page.first(:button, 'Submit').click
277
          end
278
          assert page.has_css?('#flash_notice')
279
          assert_equal 5, parent.reload.status.id
280
          assert_equal 'test of confirm dialog', parent.reload.subject
281
        end
282
      end
283

  
284
      page.first(:link, 'Edit').click
285
      assert page.has_select?("issue_status_id", {:selected => "Closed"})
286
      page.find("#issue_status_id").select("New")
287
      assert_no_difference ['Issue.count', 'child.journals.count'] do
288
        assert_difference 'parent.journals.count' do
289
          page.first(:button, 'Submit').click
290
          assert page.has_css?('#flash_notice')
291
          assert_equal 1, parent.reload.status.id
292
        end
293
      end
294

  
295
      visit "/issues/#{child.id}"
296
      page.first(:link, 'Edit').click
297
      assert page.has_select?("issue_status_id", {:selected => "New"})
298
      page.find("#issue_status_id").select("Closed")
299
      assert_no_difference ['Issue.count', 'parent.journals.count'] do
300
        assert_difference 'child.journals.count' do
301
          page.first(:button, 'Submit').click
302
          assert page.has_css?('#flash_notice')
303
          assert_equal 5, child.reload.status.id
304
        end
305
      end
306

  
307
      visit "/issues/#{parent.id}"
308
      page.first(:link, 'Edit').click
309
      assert page.has_select?("issue_status_id", {:selected => "New"})
310
      page.find("#issue_status_id").select("Closed")
311
      assert_no_difference ['Issue.count', 'child.journals.count'] do
312
        assert_difference 'parent.journals.count' do
313
          page.first(:button, 'Submit').click
314
          assert page.has_css?('#flash_notice')
315
          assert_equal 5, parent.reload.status.id
316
        end
317
      end
318
    end
319
  end
320

  
237 321
  test "removing issue shows confirm dialog" do
238 322
    log_user('jsmith', 'jsmith')
239 323
    visit '/issues/1'
app/controllers/issues_controller.rb
181 181
  end
182 182

  
183 183
  def update
184
    if request.xhr?
185
      if params[:check_go_to_close_confirm]
186
        result = true
187
        status_id = params[:status_id].to_i
188
        if status_id <= 0
189
          result = false
190
        else
191
          status = IssueStatus.find(status_id)
192
          if !status.is_closed
193
            result = false
194
          else
195
            result = !@issue.descendants.open.empty?
196
          end
197
        end
198
        render :json => {:result => result}
199
        return
200
      end
201

  
202
      return
203
    end
204

  
184 205
    return unless update_issue_from_params
185 206

  
186 207
    @issue.save_attachments(params[:attachments] ||
app/views/issues/_edit.html.erb
84 84
<% end %>
85 85
<%= javascript_tag do %>
86 86
  $('#issue-form').submit(function(){
87
    var closedIssueStatusIds =
88
          <%= @allowed_statuses.select{|i| i.is_closed?}.map{|i| i.id.to_s}.to_json.html_safe  %> ;
89
    var children = $("#issue_tree").find("tr.issue.child");
90
    if (($.inArray($("#issue_status_id").val(), closedIssueStatusIds) > -1) &&
91
        (children.length > 0) &&
92
        ((children.length - $("#issue_tree").find("tr.issue.child.closed").length) > 0)) {
87
    var status_id = 0;
88
    var result = false;
89
    if ($("#issue_status_id").length > 0) {
90
      status_id = $("#issue_status_id").val();
91
    }
92
    $.ajax({
93
      url: $("#issue-form").attr('action'),
94
      type: 'patch',
95
      async: false,
96
      data: { 
97
        "check_go_to_close_confirm": "",
98
        "status_id": status_id
99
       },
100
    })
101
      .then(
102
        function(data){
103
          result = data["result"];
104
        }
105
      );
106
    if (result) {
93 107
      if (!confirm(
94 108
          "<%= l(:text_close_parent_issue_whose_subtasks_are_open_confirmation) %>"
95 109
      )){
test/functional/issues_controller_test.rb
6222 6222
    assert_equal 2, issue.reload.assigned_to_id
6223 6223
  end
6224 6224

  
6225
  test "check_go_to_close_confirm returns false if status_id is not close" do
6226
    issue = Issue.find(1)
6227
    user = User.find(2)
6228
    assert issue.visible?(user)
6229
    assert_not issue.closed?
6230
    @request.session[:user_id] = user.id
6231
    put(
6232
      :update,
6233
      :params => {
6234
        :id => issue.id,
6235
        :check_go_to_close_confirm => "",
6236
        :status_id => 1
6237
      },
6238
      :xhr => true
6239
    )
6240
    assert_response :success
6241
    assert_equal 'application/json', response.content_type
6242
    json = ActiveSupport::JSON.decode(response.body)
6243
    assert_equal({"result" => false}, json)
6244
  end
6245

  
6246
  test "check_go_to_close_confirm returns false if status_id is 0" do
6247
    issue = Issue.find(1)
6248
    user = User.find(2)
6249
    assert issue.visible?(user)
6250
    assert_not issue.closed?
6251
    @request.session[:user_id] = user.id
6252
    put(
6253
      :update,
6254
      :params => {
6255
        :id => issue.id,
6256
        :check_go_to_close_confirm => "",
6257
        :status_id => 0
6258
      },
6259
      :xhr => true
6260
    )
6261
    assert_response :success
6262
    assert_equal 'application/json', response.content_type
6263
    json = ActiveSupport::JSON.decode(response.body)
6264
    assert_equal({"result" => false}, json)
6265
  end
6266

  
6267
  test "check_go_to_close_confirm returns false if issue does not have child" do
6268
    issue = Issue.generate!
6269
    user = User.find(2)
6270
    assert issue.visible?(user)
6271
    assert_not issue.closed?
6272
    @request.session[:user_id] = user.id
6273
    put(
6274
      :update,
6275
      :params => {
6276
        :id => issue.id,
6277
        :check_go_to_close_confirm => "",
6278
        :status_id => 5
6279
      },
6280
      :xhr => true
6281
    )
6282
    assert_response :success
6283
    assert_equal 'application/json', response.content_type
6284
    json = ActiveSupport::JSON.decode(response.body)
6285

  
6286
    assert_equal 1, issue.reload.status.id
6287
    assert_equal({"result" => false}, json)
6288
  end
6289

  
6290
  test "check_go_to_close_confirm returns true if issue have open child" do
6291
    parent = Issue.generate!
6292
    child = Issue.generate!(:parent_issue_id => parent.id)
6293
    user = User.find(2)
6294
    assert parent.reload.visible?(user)
6295
    assert_not parent.closed?
6296
    assert child.reload.visible?(user)
6297
    assert_not child.closed?
6298

  
6299
    @request.session[:user_id] = user.id
6300
    put(
6301
      :update,
6302
      :params => {
6303
        :id => parent.id,
6304
        :check_go_to_close_confirm => "",
6305
        :status_id => 5
6306
      },
6307
      :xhr => true
6308
    )
6309
    assert_response :success
6310
    assert_equal 'application/json', response.content_type
6311
    json = ActiveSupport::JSON.decode(response.body)
6312

  
6313
    assert_equal 1, parent.reload.status.id
6314
    assert_equal({"result" => true}, json)
6315
  end
6316

  
6317
  test "check_go_to_close_confirm returns false if child is closed" do
6318
    parent = Issue.generate!
6319
    child = Issue.
6320
              generate!(
6321
                :parent_issue_id => parent.id,
6322
                :status_id => 5
6323
              )
6324
    user = User.find(2)
6325
    assert parent.reload.visible?(user)
6326
    assert_not parent.closed?
6327
    assert child.reload.visible?(user)
6328
    assert child.closed?
6329

  
6330
    @request.session[:user_id] = user.id
6331
    put(
6332
      :update,
6333
      :params => {
6334
        :id => parent.id,
6335
        :check_go_to_close_confirm => "",
6336
        :status_id => 5
6337
      },
6338
      :xhr => true
6339
    )
6340
    assert_response :success
6341
    assert_equal 'application/json', response.content_type
6342
    json = ActiveSupport::JSON.decode(response.body)
6343

  
6344
    assert_equal 1, parent.reload.status.id
6345
    assert_equal({"result" => false}, json)
6346
  end
6347

  
6348
  test "check_go_to_close_confirm returns true if child is open and not visible" do
6349
    user = User.generate!
6350
    project = Project.generate!
6351
    role = Role.generate!
6352
    role.add_permission! :view_issues, :edit_issues
6353
    role.set_permission_trackers :view_issues, [2]
6354
    role.set_permission_trackers :edit_issues, [2]
6355
    role.save!
6356
    User.add_to_project(user, project, role)
6357
    parent = Issue.
6358
              generate!(
6359
                :project => project,
6360
                :tracker_id => 2,
6361
                :status_id => 1
6362
              )
6363
    child = Issue.
6364
              generate!(
6365
                :project => project,
6366
                :tracker_id => 1,
6367
                :parent_issue_id => parent.id,
6368
                :status_id => 1
6369
              )
6370
    assert parent.reload.visible?(user)
6371
    assert_not parent.closed?
6372
    assert_not child.reload.visible?(user)
6373
    assert_not child.closed?
6374

  
6375
    @request.session[:user_id] = user.id
6376
    put(
6377
      :update,
6378
      :params => {
6379
        :id => parent.id,
6380
        :check_go_to_close_confirm => "",
6381
        :status_id => 5
6382
      },
6383
      :xhr => true
6384
    )
6385
    assert_response :success
6386
    assert_equal 'application/json', response.content_type
6387
    json = ActiveSupport::JSON.decode(response.body)
6388

  
6389
    assert_equal 1, parent.reload.status.id
6390
    assert_equal({"result" => true}, json)
6391
  end
6392

  
6225 6393
  def test_get_bulk_edit
6226 6394
    @request.session[:user_id] = 2
6227 6395
    get(:bulk_edit, :params => {:ids => [1, 3]})