summaryrefslogtreecommitdiffstats
path: root/test/unit/project_query_test.rb
blob: 6c925e6fbdda757d971306319c4548fb617f1e1c (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
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2023  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 ProjectQueryTest < ActiveSupport::TestCase
  fixtures :projects, :users,
           :members, :roles, :member_roles,
           :issue_categories, :enumerations,
           :groups_users,
           :enabled_modules,
           :custom_fields, :custom_values,
           :queries

  include Redmine::I18n

  def test_filter_values_be_arrays
    q = ProjectQuery.new
    assert_nil q.project

    q.available_filters.each do |name, filter|
      values = filter.values
      assert (values.nil? || values.is_a?(Array)),
             "#values for #{name} filter returned a #{values.class.name}"
    end
  end

  def test_project_statuses_filter_should_return_project_statuses
    set_language_if_valid 'en'
    query = ProjectQuery.new(:name => '_')
    query.filters = {'status' => {:operator => '=', :values => []}}
    values = query.available_filters['status'][:values]
    assert_equal ['active', 'closed'], values.map(&:first)
    assert_equal ['1', '5'], values.map(&:second)
  end

  def test_default_columns
    q = ProjectQuery.new
    assert q.columns.any?
    assert q.inline_columns.any?
    assert q.block_columns.empty?
  end

  def test_available_columns_should_include_project_custom_fields
    query = ProjectQuery.new
    assert_include :cf_3, query.available_columns.map(&:name)
  end

  def test_available_display_types_should_returns_bord_and_list
    query = ProjectQuery.new
    query.admin_projects = nil
    assert_equal ['board', 'list'], query.available_display_types
  end

  def test_available_display_types_should_always_returns_list_when_admin_projects_is_set
    query = ProjectQuery.new
    query.admin_projects = 1
    assert_equal ['list'], query.available_display_types
  end

  def test_display_type_default_should_equal_with_setting_project_list_display_type
    ProjectQuery.new.available_display_types.each do |t|
      with_settings :project_list_display_type => t do
        q = ProjectQuery.new
        assert_equal t, q.display_type
      end
    end
  end

  def test_should_determine_default_project_query
    user = User.find(1)
    query = ProjectQuery.find(11)
    user_query = ProjectQuery.find(12)
    user_query.update(visibility: Query::VISIBILITY_PUBLIC)

    [nil, user, User.anonymous].each do |u|
      assert_nil IssueQuery.default(user: u)
    end

    # only global default is set
    with_settings :default_project_query => query.id do
      [nil, user, User.anonymous].each do |u|
        assert_equal query, ProjectQuery.default(user: u)
      end
    end

    # user default, overrides global default
    user.pref.default_project_query = user_query.id
    user.pref.save

    with_settings :default_project_query => query.id do
      assert_equal user_query, ProjectQuery.default(user: user)
    end
  end

  def test_project_query_default_should_return_nil_if_default_query_destroyed
    query = ProjectQuery.find(11)

    Setting.default_project_query = query.id
    query.destroy

    assert_nil ProjectQuery.default
  end

  def test_display_type_should_returns_list_when_admin_projects_is_set
    q = ProjectQuery.new
    q.admin_projects = 1
    assert_equal 'list', q.display_type
  end

  def test_project_statuses_values_should_equal_ancestors_return
    ancestor = Query.new
    q = ProjectQuery.new
    assert_equal ancestor.project_statuses_values, q.project_statuses_values
  end

  def test_project_statuses_values_should_includes_project_status_archeved_when_admin_projects_is_set
    q = ProjectQuery.new
    q.admin_projects = 1
    assert_includes q.project_statuses_values, [l(:project_status_archived), Project::STATUS_ARCHIVED.to_s]
    Query.new.project_statuses_values.each do |status|
      assert_includes q.project_statuses_values, status
    end
  end

  def test_base_scope_should_return_visible_projects
    q = ProjectQuery.new
    assert_equal Project.visible, q.base_scope
  end

  def test_base_scope_should_return_all_projects_when_admin_projects_is_set
    q = ProjectQuery.new
    q.admin_projects = 1
    assert_equal Project.all, q.base_scope
  end
end