Project

General

Profile

Defect #28033 » 28033-v2.patch

Go MAEDA, 2023-04-09 06:17

View differences:

app/controllers/queries_controller.rb
20 20
class QueriesController < ApplicationController
21 21
  menu_item :issues
22 22
  before_action :find_query, :only => [:edit, :update, :destroy]
23
  before_action :find_optional_project, :only => [:new, :create]
23
  before_action :find_optional_project, :only => [:new, :create, :edit, :update]
24 24

  
25 25
  accept_api_auth :index
26 26

  
......
128 128
  end
129 129

  
130 130
  def update_query_from_params
131
    @query.project = params[:query_is_for_all] ? nil : @project
131
    @query.project = (params[:query] && params[:query][:project_id].present?) ? Project.find(params[:query][:project_id]) : nil
132 132
    @query.build_from_params(params)
133 133
    @query.column_names = nil if params[:default_columns]
134 134
    @query.sort_criteria = (params[:query] && params[:query][:sort_criteria]) || @query.sort_criteria
app/views/calendars/show.html.erb
33 33
                         :class => 'icon icon-save' %>
34 34
  <% end %>
35 35
  <% if !@query.new_record? && @query.editable_by?(User.current) %>
36
    <%= link_to l(:button_edit_object, object_name: l(:label_query).downcase), edit_query_path(@query, :calendar => 1), :class => 'icon icon-edit' %>
36
    <%= link_to l(:button_edit_object, object_name: l(:label_query).downcase), edit_query_path(@query, :calendar => 1, :project_id => @project&.identifier), :class => 'icon icon-edit' %>
37 37
    <%= delete_link query_path(@query, :calendar => 1), {}, l(:button_delete_object, object_name: l(:label_query).downcase) %>
38 38
  <% end %>
39 39
</p>
app/views/gantts/show.html.erb
96 96
                         :class => 'icon icon-save' %>
97 97
  <% end %>
98 98
<% if !@query.new_record? && @query.editable_by?(User.current) %>
99
  <%= link_to l(:button_edit_object, object_name: l(:label_query).downcase), edit_query_path(@query, :gantt => 1), :class => 'icon icon-edit' %>
99
  <%= link_to l(:button_edit_object, object_name: l(:label_query).downcase), edit_query_path(@query, :gantt => 1, :project_id => @project&.identifier), :class => 'icon icon-edit' %>
100 100
  <%= delete_link query_path(@query, :gantt => 1), {}, l(:button_delete_object, object_name: l(:label_query).downcase) %>
101 101
<% end %>
102 102
</p>
app/views/queries/_form.html.erb
25 25
<% end %>
26 26

  
27 27
<% unless @query.type == 'ProjectQuery' %>
28
  <p><label for="query_is_for_all"><%=l(:field_is_for_all)%></label>
29
  <%= check_box_tag 'query_is_for_all', 1, @query.project.nil?, :class => (User.current.admin? ? '' : 'disable-unless-private') %></p>
28
  <p><label><%=l(:field_project)%></label>
29
    <%= radio_button_tag 'query[project_id]', '', @query.project.nil?, id: nil, class: ('disable-unless-private' unless User.current.admin?) %> <%= l(:field_is_for_all) %>
30
  <% if @project %>
31
    <%= radio_button_tag 'query[project_id]', @project.identifier, !@query.project.nil?, id: nil %> <%= @project.name %>
32
  <% end%>
33
  </p>
30 34
<% end %>
31 35

  
32 36
<% unless params[:calendar] %>
......
108 112
    var roles_checked = $('#query_visibility_1').is(':checked');
109 113
    var private_checked = $('#query_visibility_0').is(':checked');
110 114
    $("input[name='query[role_ids][]'][type=checkbox]").attr('disabled', !roles_checked);
111
    if (!private_checked) $("input.disable-unless-private").attr('checked', false);
112
    $("input.disable-unless-private").attr('disabled', !private_checked);
115
    if($("input.disable-unless-private").length){
116
      if (!private_checked) $("input[name='query[project_id]']:not([value=''])").prop('checked', true);
117
      $("input.disable-unless-private").attr('disabled', !private_checked);
118
    }
113 119
  }).trigger('change');
114 120
});
115 121

  
app/views/queries/_query_form.html.erb
64 64
  <% else %>
65 65
    <% if @query.editable_by?(User.current) %>
66 66
      <% redirect_params = (controller_name == 'admin' && action_name == 'projects') ? {:admin_projects => 1} : {} %>
67
      <%= link_to l(:button_edit_object, object_name: l(:label_query).downcase), edit_query_path(@query, redirect_params), :class => 'icon icon-edit' %>
67
      <%= link_to l(:button_edit_object, object_name: l(:label_query).downcase), edit_query_path(@query, redirect_params.merge(project_id: @project&.identifier)), :class => 'icon icon-edit' %>
68 68
      <%= delete_link query_path(@query, redirect_params), {}, l(:button_delete_object, object_name: l(:label_query).downcase) %>
69 69
    <% end %>
70 70
  <% end %>
app/views/queries/edit.html.erb
1 1
<h2><%= l(:label_query) %></h2>
2 2

  
3
<%= form_tag(query_path(@query), :method => :put, :id => "query-form") do %>
3
<%= form_tag(query_path(@query, project_id: @project&.identifier), :method => :put, :id => "query-form") do %>
4 4
  <%= render :partial => 'form', :locals => {:query => @query} %>
5 5
  <%= submit_tag l(:button_save) %>
6 6
<% end %>
test/functional/queries_controller_test.rb
43 43
    assert_response :success
44 44

  
45 45
    assert_select 'input[name=?][value="0"][checked=checked]', 'query[visibility]'
46
    assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked]):not([disabled])'
46
    assert_select 'input[name=?][type=radio]', 'query[project_id]', 2 do
47
      assert_select '[value=""][class="disable-unless-private"]:not([checked])'
48
      assert_select '[value="ecookbook"][checked]'
49
    end
47 50
    assert_select 'select[name=?]', 'c[]' do
48 51
      assert_select 'option[value=tracker]'
49 52
      assert_select 'option[value=subject]'
......
56 59
    assert_response :success
57 60

  
58 61
    assert_select 'input[name=?]', 'query[visibility]', 0
59
    assert_select 'input[name=query_is_for_all][type=checkbox][checked]:not([disabled])'
62
    assert_select 'input[name=?][type=radio]', 'query[project_id]', 1 do
63
      assert_select '[value=""][class="disable-unless-private"][checked]'
64
    end
60 65
  end
61 66

  
62 67
  def test_new_on_invalid_project
......
84 89
    get(:new, :params => {:project_id => 1, :type => 'TimeEntryQuery'})
85 90
    assert_response :success
86 91
    assert_select 'input[name=type][value=?]', 'TimeEntryQuery'
92
    assert_select 'input[name=?][type=radio]', 'query[project_id]', 2 do
93
      assert_select '[value=""][class="disable-unless-private"]:not([checked])'
94
      assert_select '[value="ecookbook"][checked]'
95
    end
87 96
    assert_select 'p[class=?]', 'totable_columns', 1
88 97
    assert_select 'p[class=?]', 'block_columns', 0
89 98
  end
......
93 102
    get(:new, :params => {:type => 'ProjectQuery'})
94 103
    assert_response :success
95 104
    assert_select 'input[name=type][value=?]', 'ProjectQuery'
105
    assert_select 'input[name=?]', 'query[project_id]', 0
96 106
  end
97 107

  
98 108
  def test_new_project_query_should_not_render_roles_visibility_options
......
104 114
    assert_select 'input[id=?]', 'query_visibility_1', 0
105 115
  end
106 116

  
107
  def test_new_project_query_should_not_render_for_all_projects_option
108
    @request.session[:user_id] = 1
109
    get(:new, :params => {:type => 'ProjectQuery'})
110
    assert_response :success
111
    assert_select 'input[name=?]', 'for_all_projects', 0
112
  end
113

  
114 117
  def test_new_time_entry_query_should_select_spent_time_from_main_menu
115 118
    @request.session[:user_id] = 2
116 119
    get(
......
147 150
    assert_select 'fieldset#filters'
148 151
    assert_select 'fieldset legend', {:text => 'Sort', :count => 0}
149 152
    assert_select 'fieldset#columns'
153
    assert_select 'input[name=?]', 'query[project_id]'
150 154
  end
151 155

  
152 156
  def test_new_with_calendar_params
......
159 163
    assert_select 'fieldset#filters'
160 164
    assert_select 'fieldset legend', {:text => 'Sort', :count => 0}
161 165
    assert_select 'fieldset#columns', :count => 0
166
    assert_select 'input[name=?]', 'query[project_id]'
162 167
  end
163 168

  
164 169
  def test_new_without_gantt_and_calendar_params
......
187 192
          "assigned_to_id" => ["1"], "status_id" => ["1"]
188 193
        },
189 194
        :query => {
190
          "name" => "test_new_project_public_query", "visibility" => "2"
195
          "name" => "test_new_project_public_query", "visibility" => "2", "project_id" => "ecookbook"
191 196
        }
192 197
      }
193 198
    )
......
241 246
        :query => {
242 247
          "name" => "test_create_project_roles_query",
243 248
          "visibility" => "1",
244
          "role_ids" => ["1", "2", ""]
249
          "role_ids" => ["1", "2", ""],
250
          "project_id" => "ecookbook"
245 251
        }
246 252
      }
247 253
    )
......
441 447
        :params => {
442 448
          :project_id => 'ecookbook',
443 449
          :query => {
444
            "name" => "name", "visibility" => "2"
450
            "name" => "name", "visibility" => "2", "project_id" => "ecookbook"
445 451
          }
446 452
        }
447 453
      )
......
458 464
        :create,
459 465
        :params => {
460 466
          :project_id => 'ecookbook',
461
          :query_is_for_all => '1',
462 467
          :query => {
463
            "name" => "name", "visibility" => "2"
468
            "name" => "name", "visibility" => "2", "project_id" => ""
464 469
          }
465 470
        }
466 471
      )
......
478 483
        :params => {
479 484
          :project_id => 'ecookbook',
480 485
          :query => {
481
            "name" => "name", "visibility" => "2"
486
            "name" => "name", "visibility" => "2", "project_id" => "ecookbook"
482 487
          }
483 488
        }
484 489
      )
......
495 500
        :create,
496 501
        :params => {
497 502
          :project_id => 'ecookbook',
498
          :query_is_for_all => '1',
499 503
          :query => {
500
            "name" => "name", "visibility" => "2"
504
            "name" => "name", "visibility" => "2", "project_id" => ""
501 505
          }
502 506
        }
503 507
      )
......
514 518
        :create,
515 519
        :params => {
516 520
          :project_id => 'ecookbook',
517
          :query_is_for_all => '1',
518 521
          :query => {
519
            "name" => "name", "visibility" => "2"
522
            "name" => "name", "visibility" => "2", "project_id" => ""
520 523
          }
521 524
        }
522 525
      )
......
544 547
            "spent_on" => ["2016-07-14"]
545 548
          },
546 549
          :query => {
547
            "name" => "test_new_project_public_query", "visibility" => "2"
550
            "name" => "test_new_project_public_query", "visibility" => "2", "project_id" => "ecookbook"
548 551
          }
549 552
        }
550 553
      )
......
618 621
    assert_response :success
619 622

  
620 623
    assert_select 'input[name=?][value="2"][checked=checked]', 'query[visibility]'
621
    assert_select 'input[name=query_is_for_all][type=checkbox][checked=checked]'
624
    assert_select 'input[name=?][type=radio]', 'query[project_id]', 1 do
625
      assert_select '[value=""][checked]:not([class="disable-unless-private"])'
626
    end
622 627
  end
623 628

  
624 629
  def test_edit_global_private_query
......
627 632
    assert_response :success
628 633

  
629 634
    assert_select 'input[name=?]', 'query[visibility]', 0
630
    assert_select 'input[name=query_is_for_all][type=checkbox][checked=checked]'
635
    assert_select 'input[name=?][type=radio]', 'query[project_id]', 1 do
636
      assert_select '[value=""][class="disable-unless-private"][checked]'
637
    end
631 638
  end
632 639

  
633 640
  def test_edit_project_private_query
634 641
    @request.session[:user_id] = 3
635
    get(:edit, :params => {:id => 2})
642
    get(:edit, :params => {:id => 2, :project_id => 'ecookbook'})
636 643
    assert_response :success
637 644

  
638 645
    assert_select 'input[name=?]', 'query[visibility]', 0
639
    assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked])'
646
    assert_select 'input[name=?][type=radio]', 'query[project_id]', 2 do
647
      assert_select '[value=""][class="disable-unless-private"]:not([checked])'
648
      assert_select '[value="ecookbook"][checked]'
649
    end
640 650
  end
641 651

  
642 652
  def test_edit_project_public_query
643 653
    @request.session[:user_id] = 2
644
    get(:edit, :params => {:id => 1})
654
    get(:edit, :params => {:id => 1, :project_id => 'ecookbook'})
645 655
    assert_response :success
646 656

  
647 657
    assert_select 'input[name=?][value="2"][checked=checked]', 'query[visibility]'
648
    assert_select 'input[name=query_is_for_all][type=checkbox]:not([checked])'
658
    assert_select 'input[name=?][type=radio]', 'query[project_id]', 2 do
659
      assert_select '[value=""][class="disable-unless-private"]:not([checked])'
660
      assert_select '[value="ecookbook"][checked]'
661
    end
649 662
  end
650 663

  
651 664
  def test_edit_sort_criteria
......
680 693
          "assigned_to_id" => ["me"], "status_id" => ["1"]
681 694
        },
682 695
        :query => {
683
          "name" => "test_edit_global_private_query", "visibility" => "2"
696
          "name" => "test_edit_global_private_query", "visibility" => "2", "project_id" => ""
684 697
        }
685 698
      }
686 699
    )
......
688 701
    q = Query.find_by_name('test_edit_global_private_query')
689 702
    assert !q.is_public?
690 703
    assert q.has_default_columns?
704
    assert_nil q.project
691 705
    assert q.valid?
692 706
  end
693 707

  
......
706 720
          "assigned_to_id" => ["1"], "status_id" => ["1"]
707 721
        },
708 722
        :query => {
709
          "name" => "test_edit_global_public_query", "visibility" => "2"
723
          "name" => "test_edit_global_public_query", "visibility" => "2", "project_id" => ""
710 724
        }
711 725
      }
712 726
    )
......
714 728
    q = Query.find_by_name('test_edit_global_public_query')
715 729
    assert q.is_public?
716 730
    assert q.has_default_columns?
731
    assert_nil q.project
717 732
    assert q.valid?
718 733
  end
719 734

  
735
  def test_update_global_private_query_when_change_global_to_project_should_set_project
736
    assert_nil Query.find_by_name('test_edit_global_private_query')
737
    @request.session[:user_id] = 3
738
    put(
739
      :update,
740
      :params => {
741
        :id => 3,
742
        :project_id => 'ecookbook',
743
        :query => {
744
          "name" => "test_edit_global_private_query", "visibility" => "0", "project_id" => "ecookbook"
745
        }
746
      }
747
    )
748
    assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => 3
749
    q = Query.find_by_name('test_edit_global_private_query')
750
    assert !q.is_global?
751
    assert q.project
752
  end
753
  
754
  def test_update_project_private_query_when_change_project_to_global_should_not_set_project
755
    assert_nil Query.find_by_name('test_edit_project_private_query')
756
    @request.session[:user_id] = 3
757
    put(
758
      :update,
759
      :params => {
760
        :id => 2,
761
        :project_id => 'ecookbook',
762
        :query => {
763
          "name" => "test_edit_project_private_query", "visibility" => "0", "project_id" => ""
764
        }
765
      }
766
    )
767
    assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => 2
768
    q = Query.find_by_name('test_edit_project_private_query')
769
    assert q.is_global?
770
    assert_nil q.project
771
  end
772

  
720 773
  def test_update_admin_projects_query
721 774
    q = ProjectQuery.create(:name => 'project_query')
722 775
    @request.session[:user_id] = 1
(4-4/4)