To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 25 / 252339adc97bdbe7fac1c5b16dba024f1d817a65.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (11.6 KB)

1 1295:622f24f53b42 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2013  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18
require File.expand_path('../../test_helper', __FILE__)
19
20
class QueriesControllerTest < ActionController::TestCase
21
  fixtures :projects, :users, :members, :member_roles, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :queries, :enabled_modules
22
23
  def setup
24
    User.current = nil
25
  end
26
27
  def test_index
28
    get :index
29
    # HTML response not implemented
30
    assert_response 406
31
  end
32
33
  def test_new_project_query
34
    @request.session[:user_id] = 2
35
    get :new, :project_id => 1
36
    assert_response :success
37
    assert_template 'new'
38
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
39
                                                 :name => 'query[is_public]',
40
                                                 :checked => nil }
41
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
42
                                                 :name => 'query_is_for_all',
43
                                                 :checked => nil,
44
                                                 :disabled => nil }
45
    assert_select 'select[name=?]', 'c[]' do
46
      assert_select 'option[value=tracker]'
47
      assert_select 'option[value=subject]'
48
    end
49
  end
50
51
  def test_new_global_query
52
    @request.session[:user_id] = 2
53
    get :new
54
    assert_response :success
55
    assert_template 'new'
56
    assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
57
                                                    :name => 'query[is_public]' }
58
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
59
                                                 :name => 'query_is_for_all',
60
                                                 :checked => 'checked',
61
                                                 :disabled => nil }
62
  end
63
64
  def test_new_on_invalid_project
65
    @request.session[:user_id] = 2
66
    get :new, :project_id => 'invalid'
67
    assert_response 404
68
  end
69
70
  def test_create_project_public_query
71
    @request.session[:user_id] = 2
72
    post :create,
73
         :project_id => 'ecookbook',
74
         :default_columns => '1',
75
         :f => ["status_id", "assigned_to_id"],
76
         :op => {"assigned_to_id" => "=", "status_id" => "o"},
77
         :v => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
78
         :query => {"name" => "test_new_project_public_query", "is_public" => "1"}
79
80
    q = Query.find_by_name('test_new_project_public_query')
81
    assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
82
    assert q.is_public?
83
    assert q.has_default_columns?
84
    assert q.valid?
85
  end
86
87
  def test_create_project_private_query
88
    @request.session[:user_id] = 3
89
    post :create,
90
         :project_id => 'ecookbook',
91
         :default_columns => '1',
92
         :fields => ["status_id", "assigned_to_id"],
93
         :operators => {"assigned_to_id" => "=", "status_id" => "o"},
94
         :values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
95
         :query => {"name" => "test_new_project_private_query", "is_public" => "1"}
96
97
    q = Query.find_by_name('test_new_project_private_query')
98
    assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
99
    assert !q.is_public?
100
    assert q.has_default_columns?
101
    assert q.valid?
102
  end
103
104
  def test_create_global_private_query_with_custom_columns
105
    @request.session[:user_id] = 3
106
    post :create,
107
         :fields => ["status_id", "assigned_to_id"],
108
         :operators => {"assigned_to_id" => "=", "status_id" => "o"},
109
         :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]},
110
         :query => {"name" => "test_new_global_private_query", "is_public" => "1"},
111
         :c => ["", "tracker", "subject", "priority", "category"]
112
113
    q = Query.find_by_name('test_new_global_private_query')
114
    assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q
115
    assert !q.is_public?
116
    assert !q.has_default_columns?
117
    assert_equal [:id, :tracker, :subject, :priority, :category], q.columns.collect {|c| c.name}
118
    assert q.valid?
119
  end
120
121
  def test_create_global_query_with_custom_filters
122
    @request.session[:user_id] = 3
123
    post :create,
124
         :fields => ["assigned_to_id"],
125
         :operators => {"assigned_to_id" => "="},
126
         :values => { "assigned_to_id" => ["me"]},
127
         :query => {"name" => "test_new_global_query"}
128
129
    q = Query.find_by_name('test_new_global_query')
130
    assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q
131
    assert !q.has_filter?(:status_id)
132
    assert_equal ['assigned_to_id'], q.filters.keys
133
    assert q.valid?
134
  end
135
136
  def test_create_with_sort
137
    @request.session[:user_id] = 1
138
    post :create,
139
         :default_columns => '1',
140
         :operators => {"status_id" => "o"},
141
         :values => {"status_id" => ["1"]},
142
         :query => {:name => "test_new_with_sort",
143
                    :is_public => "1",
144
                    :sort_criteria => {"0" => ["due_date", "desc"], "1" => ["tracker", ""]}}
145
146
    query = Query.find_by_name("test_new_with_sort")
147
    assert_not_nil query
148
    assert_equal [['due_date', 'desc'], ['tracker', 'asc']], query.sort_criteria
149
  end
150
151
  def test_create_with_failure
152
    @request.session[:user_id] = 2
153
    assert_no_difference '::Query.count' do
154
      post :create, :project_id => 'ecookbook', :query => {:name => ''}
155
    end
156
    assert_response :success
157
    assert_template 'new'
158
    assert_select 'input[name=?]', 'query[name]'
159
  end
160
161
  def test_edit_global_public_query
162
    @request.session[:user_id] = 1
163
    get :edit, :id => 4
164
    assert_response :success
165
    assert_template 'edit'
166
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
167
                                                 :name => 'query[is_public]',
168
                                                 :checked => 'checked' }
169
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
170
                                                 :name => 'query_is_for_all',
171
                                                 :checked => 'checked',
172
                                                 :disabled => 'disabled' }
173
  end
174
175
  def test_edit_global_private_query
176
    @request.session[:user_id] = 3
177
    get :edit, :id => 3
178
    assert_response :success
179
    assert_template 'edit'
180
    assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
181
                                                    :name => 'query[is_public]' }
182
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
183
                                                 :name => 'query_is_for_all',
184
                                                 :checked => 'checked',
185
                                                 :disabled => 'disabled' }
186
  end
187
188
  def test_edit_project_private_query
189
    @request.session[:user_id] = 3
190
    get :edit, :id => 2
191
    assert_response :success
192
    assert_template 'edit'
193
    assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
194
                                                    :name => 'query[is_public]' }
195
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
196
                                                 :name => 'query_is_for_all',
197
                                                 :checked => nil,
198
                                                 :disabled => nil }
199
  end
200
201
  def test_edit_project_public_query
202
    @request.session[:user_id] = 2
203
    get :edit, :id => 1
204
    assert_response :success
205
    assert_template 'edit'
206
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
207
                                                 :name => 'query[is_public]',
208
                                                 :checked => 'checked'
209
                                                  }
210
    assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
211
                                                 :name => 'query_is_for_all',
212
                                                 :checked => nil,
213
                                                 :disabled => 'disabled' }
214
  end
215
216
  def test_edit_sort_criteria
217
    @request.session[:user_id] = 1
218
    get :edit, :id => 5
219
    assert_response :success
220
    assert_template 'edit'
221
    assert_tag :tag => 'select', :attributes => { :name => 'query[sort_criteria][0][]' },
222
                                 :child => { :tag => 'option', :attributes => { :value => 'priority',
223
                                                                                :selected => 'selected' } }
224
    assert_tag :tag => 'select', :attributes => { :name => 'query[sort_criteria][0][]' },
225
                                 :child => { :tag => 'option', :attributes => { :value => 'desc',
226
                                                                                :selected => 'selected' } }
227
  end
228
229
  def test_edit_invalid_query
230
    @request.session[:user_id] = 2
231
    get :edit, :id => 99
232
    assert_response 404
233
  end
234
235
  def test_udpate_global_private_query
236
    @request.session[:user_id] = 3
237
    put :update,
238
         :id => 3,
239
         :default_columns => '1',
240
         :fields => ["status_id", "assigned_to_id"],
241
         :operators => {"assigned_to_id" => "=", "status_id" => "o"},
242
         :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]},
243
         :query => {"name" => "test_edit_global_private_query", "is_public" => "1"}
244
245
    assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 3
246
    q = Query.find_by_name('test_edit_global_private_query')
247
    assert !q.is_public?
248
    assert q.has_default_columns?
249
    assert q.valid?
250
  end
251
252
  def test_update_global_public_query
253
    @request.session[:user_id] = 1
254
    put :update,
255
         :id => 4,
256
         :default_columns => '1',
257
         :fields => ["status_id", "assigned_to_id"],
258
         :operators => {"assigned_to_id" => "=", "status_id" => "o"},
259
         :values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
260
         :query => {"name" => "test_edit_global_public_query", "is_public" => "1"}
261
262
    assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 4
263
    q = Query.find_by_name('test_edit_global_public_query')
264
    assert q.is_public?
265
    assert q.has_default_columns?
266
    assert q.valid?
267
  end
268
269
  def test_update_with_failure
270
    @request.session[:user_id] = 1
271
    put :update, :id => 4, :query => {:name => ''}
272
    assert_response :success
273
    assert_template 'edit'
274
  end
275
276
  def test_destroy
277
    @request.session[:user_id] = 2
278
    delete :destroy, :id => 1
279
    assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :set_filter => 1, :query_id => nil
280
    assert_nil Query.find_by_id(1)
281
  end
282
283
  def test_backslash_should_be_escaped_in_filters
284
    @request.session[:user_id] = 2
285
    get :new, :subject => 'foo/bar'
286
    assert_response :success
287
    assert_template 'new'
288
    assert_include 'addFilter("subject", "=", ["foo\/bar"]);', response.body
289
  end
290
end