26023_tests_visibility_fix.patch

Felix Schäfer, 2018-10-01 21:37

Download (7.31 KB)

View differences:

app/models/issue_category.rb
28 28
  safe_attributes 'name', 'assigned_to_id'
29 29

  
30 30
  scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
31
  scope :visible, lambda {|user = User.current|
32
    joins(:project).
33
      where(Project.allowed_to_condition(user, :view_issues))
34
  }
31 35

  
32 36
  alias :destroy_without_reassign :destroy
33 37

  
app/models/issue_query.rb
124 124

  
125 125
    add_available_filter "category_id",
126 126
      :type => :list_optional,
127
      :values => lambda { project.issue_categories.collect{|s| [s.name, s.id.to_s] } } if project
127
      :values => lambda { issue_category_values }
128 128

  
129 129
    add_available_filter "subject", :type => :text
130 130
    add_available_filter "description", :type => :text
app/models/project.rb
330 330
    @users = nil
331 331
    @shared_versions = nil
332 332
    @rolled_up_versions = nil
333
    @rolled_up_issue_categories = nil
333 334
    @rolled_up_trackers = nil
334 335
    @rolled_up_statuses = nil
335 336
    @rolled_up_custom_fields = nil
......
482 483
        where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED)
483 484
  end
484 485

  
486
  def rolled_up_issue_categories
487
    @rolled_up_issue_categories ||=
488
      IssueCategory.
489
        joins(:project).
490
        where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED)
491
  end
492

  
485 493
  # Returns a scope of the Versions used by the project
486 494
  def shared_versions
487 495
    if new_record?
app/models/query.rb
570 570
    watcher_values
571 571
  end
572 572

  
573
  def issue_category_values
574
    categories =
575
      if project
576
        project.rolled_up_issue_categories
577
      else
578
        IssueCategory
579
      end
580
    categories.visible.collect {|c| [c.name, c.id.to_s] }
581
  end
582

  
573 583
  # Returns a scope of issue custom fields that are available as columns or filters
574 584
  def issue_custom_fields
575 585
    if project
test/fixtures/issue_categories.yml
19 19
  project_id: 2
20 20
  assigned_to_id: 
21 21
  id: 4
22
issue_categories_005:
23
  name: Services
24
  project_id: 5
25
  assigned_to_id:
26
  id: 5
test/functional/queries_controller_test.rb
605 605
    assert_include ["eCookbook - 2.0", "3", "open"], json
606 606
  end
607 607

  
608
  def test_issue_categories_filter_should_not_return_issue_categories_from_invisible_subprojects
609
    # Remove "jsmith" user from "Private child of eCookbook" project
610
    Project.find(5).memberships.find_by(:user_id => 2).destroy
611

  
612
    @request.session[:user_id] = 2
613
    get :filter, :params =>
614
      {
615
        :project_id => 1,
616
        :name => 'category_id'
617
      }
618

  
619
    assert_response :success
620
    assert_equal 'application/json', response.content_type
621
    json = ActiveSupport::JSON.decode(response.body)
622

  
623
    assert_include ["Printing", "1"], json
624
    assert_include ["Recipes", "2"], json
625

  
626
    assert_not_include ["Services", "5"], json
627
  end
628

  
608 629
  def test_version_filter_without_project_id_should_return_all_visible_fixed_versions
609 630
    # Remove "jsmith" user from "Private child of eCookbook" project
610 631
    Project.find(5).memberships.find_by(:user_id => 2).destroy
test/unit/project_test.rb
555 555
      project.rolled_up_versions.sort
556 556
  end
557 557

  
558
  test "#rolled_up_issue_categories should include the categories for the current project" do
559
    project = Project.generate!
560
    current_category_1 = IssueCategory.create!(:name => 'Current Category 1' ,:project => project)
561
    current_category_2 = IssueCategory.create!(:name => 'Current Category 2' ,:project => project)
562
    expected_categories = [current_category_1, current_category_2].sort
563

  
564
    assert_equal expected_categories, project.rolled_up_issue_categories.sort
565
  end
566

  
567
  test "#rolled_up_issue_categories should include categories for a subproject" do
568
    project = Project.generate!
569
    parent_category_1 = IssueCategory.create!(:name => 'Parent Category 1' ,:project => project)
570
    parent_category_2 = IssueCategory.create!(:name => 'Parent Category 2' ,:project => project)
571
    subproject = Project.generate_with_parent!(project)
572
    subproject_category = IssueCategory.create!(:name => 'Subproject Category' ,:project => subproject)
573
    expected_categories = [parent_category_1, parent_category_2, subproject_category].sort
574

  
575
    assert_equal expected_categories, project.rolled_up_issue_categories.sort
576
  end
577

  
578
  test "#rolled_up_issue_categories should include categories for a sub-subproject" do
579
    project = Project.generate!
580
    parent_category_1 = IssueCategory.create!(:name => 'Parent Category 1' ,:project => project)
581
    parent_category_2 = IssueCategory.create!(:name => 'Parent Category 2' ,:project => project)
582
    subproject = Project.generate_with_parent!(project)
583
    sub_subproject = Project.generate_with_parent!(subproject)
584
    sub_subproject_category = IssueCategory.create!(:name => 'Sub Subproject Category' ,:project => sub_subproject)
585
    project.reload
586
    expected_categories = [parent_category_1, parent_category_2, sub_subproject_category].sort
587

  
588
    assert_equal expected_categories, project.rolled_up_issue_categories.sort
589
  end
590

  
591
  test "#rolled_up_issue_categories should only check active projects" do
592
    project = Project.generate!
593
    parent_category_1 = IssueCategory.create!(:name => 'Parent Category 1' ,:project => project)
594
    parent_category_2 = IssueCategory.create!(:name => 'Parent Category 2' ,:project => project)
595
    subproject = Project.generate_with_parent!(project)
596
    IssueCategory.create!(:name => 'Subproject Category' ,:project => subproject)
597
    assert subproject.archive
598
    project.reload
599
    expected_categories =  [parent_category_1, parent_category_2].sort
600

  
601
    assert !subproject.active?
602
    assert_equal expected_categories, project.rolled_up_issue_categories.sort
603
  end
604

  
558 605
  def test_shared_versions_none_sharing
559 606
    p = Project.find(5)
560 607
    v = Version.create!(:name => 'none_sharing', :project => p, :sharing => 'none')