@@ -1,249 +1,249 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | # |
|
2 | # | |
3 | # Redmine - project management software |
|
3 | # Redmine - project management software | |
4 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
4 | # Copyright (C) 2006-2016 Jean-Philippe Lang | |
5 | # |
|
5 | # | |
6 | # This program is free software; you can redistribute it and/or |
|
6 | # This program is free software; you can redistribute it and/or | |
7 | # modify it under the terms of the GNU General Public License |
|
7 | # modify it under the terms of the GNU General Public License | |
8 | # as published by the Free Software Foundation; either version 2 |
|
8 | # as published by the Free Software Foundation; either version 2 | |
9 | # of the License, or (at your option) any later version. |
|
9 | # of the License, or (at your option) any later version. | |
10 | # |
|
10 | # | |
11 | # This program is distributed in the hope that it will be useful, |
|
11 | # This program is distributed in the hope that it will be useful, | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. |
|
14 | # GNU General Public License for more details. | |
15 | # |
|
15 | # | |
16 | # You should have received a copy of the GNU General Public License |
|
16 | # You should have received a copy of the GNU General Public License | |
17 | # along with this program; if not, write to the Free Software |
|
17 | # along with this program; if not, write to the Free Software | |
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 |
|
19 | |||
20 | module Redmine |
|
20 | module Redmine | |
21 | module Pagination |
|
21 | module Pagination | |
22 | class Paginator |
|
22 | class Paginator | |
23 | attr_reader :item_count, :per_page, :page, :page_param |
|
23 | attr_reader :item_count, :per_page, :page, :page_param | |
24 |
|
24 | |||
25 | def initialize(*args) |
|
25 | def initialize(*args) | |
26 | if args.first.is_a?(ActionController::Base) |
|
26 | if args.first.is_a?(ActionController::Base) | |
27 | args.shift |
|
27 | args.shift | |
28 | ActiveSupport::Deprecation.warn "Paginator no longer takes a controller instance as the first argument. Remove it from #new arguments." |
|
28 | ActiveSupport::Deprecation.warn "Paginator no longer takes a controller instance as the first argument. Remove it from #new arguments." | |
29 | end |
|
29 | end | |
30 | item_count, per_page, page, page_param = *args |
|
30 | item_count, per_page, page, page_param = *args | |
31 |
|
31 | |||
32 | @item_count = item_count |
|
32 | @item_count = item_count | |
33 | @per_page = per_page |
|
33 | @per_page = per_page | |
34 | page = (page || 1).to_i |
|
34 | page = (page || 1).to_i | |
35 | if page < 1 |
|
35 | if page < 1 | |
36 | page = 1 |
|
36 | page = 1 | |
37 | end |
|
37 | end | |
38 | @page = page |
|
38 | @page = page | |
39 | @page_param = page_param || :page |
|
39 | @page_param = page_param || :page | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def offset |
|
42 | def offset | |
43 | (page - 1) * per_page |
|
43 | (page - 1) * per_page | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def first_page |
|
46 | def first_page | |
47 | if item_count > 0 |
|
47 | if item_count > 0 | |
48 | 1 |
|
48 | 1 | |
49 | end |
|
49 | end | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | def previous_page |
|
52 | def previous_page | |
53 | if page > 1 |
|
53 | if page > 1 | |
54 | page - 1 |
|
54 | page - 1 | |
55 | end |
|
55 | end | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | def next_page |
|
58 | def next_page | |
59 | if last_item < item_count |
|
59 | if last_item < item_count | |
60 | page + 1 |
|
60 | page + 1 | |
61 | end |
|
61 | end | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def last_page |
|
64 | def last_page | |
65 | if item_count > 0 |
|
65 | if item_count > 0 | |
66 | (item_count - 1) / per_page + 1 |
|
66 | (item_count - 1) / per_page + 1 | |
67 | end |
|
67 | end | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | def multiple_pages? |
|
70 | def multiple_pages? | |
71 | per_page < item_count |
|
71 | per_page < item_count | |
72 | end |
|
72 | end | |
73 |
|
73 | |||
74 | def first_item |
|
74 | def first_item | |
75 | item_count == 0 ? 0 : (offset + 1) |
|
75 | item_count == 0 ? 0 : (offset + 1) | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | def last_item |
|
78 | def last_item | |
79 | l = first_item + per_page - 1 |
|
79 | l = first_item + per_page - 1 | |
80 | l > item_count ? item_count : l |
|
80 | l > item_count ? item_count : l | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | def linked_pages |
|
83 | def linked_pages | |
84 | pages = [] |
|
84 | pages = [] | |
85 | if item_count > 0 |
|
85 | if item_count > 0 | |
86 | pages += [first_page, page, last_page] |
|
86 | pages += [first_page, page, last_page] | |
87 | pages += ((page-2)..(page+2)).to_a.select {|p| p > first_page && p < last_page} |
|
87 | pages += ((page-2)..(page+2)).to_a.select {|p| p > first_page && p < last_page} | |
88 | end |
|
88 | end | |
89 | pages = pages.compact.uniq.sort |
|
89 | pages = pages.compact.uniq.sort | |
90 | if pages.size > 1 |
|
90 | if pages.size > 1 | |
91 | pages |
|
91 | pages | |
92 | else |
|
92 | else | |
93 | [] |
|
93 | [] | |
94 | end |
|
94 | end | |
95 | end |
|
95 | end | |
96 |
|
96 | |||
97 | def items_per_page |
|
97 | def items_per_page | |
98 | ActiveSupport::Deprecation.warn "Paginator#items_per_page will be removed. Use #per_page instead." |
|
98 | ActiveSupport::Deprecation.warn "Paginator#items_per_page will be removed. Use #per_page instead." | |
99 | per_page |
|
99 | per_page | |
100 | end |
|
100 | end | |
101 |
|
101 | |||
102 | def current |
|
102 | def current | |
103 | ActiveSupport::Deprecation.warn "Paginator#current will be removed. Use .offset instead of .current.offset." |
|
103 | ActiveSupport::Deprecation.warn "Paginator#current will be removed. Use .offset instead of .current.offset." | |
104 | self |
|
104 | self | |
105 | end |
|
105 | end | |
106 | end |
|
106 | end | |
107 |
|
107 | |||
108 | # Paginates the given scope or model. Returns a Paginator instance and |
|
108 | # Paginates the given scope or model. Returns a Paginator instance and | |
109 | # the collection of objects for the current page. |
|
109 | # the collection of objects for the current page. | |
110 | # |
|
110 | # | |
111 | # Options: |
|
111 | # Options: | |
112 | # :parameter name of the page parameter |
|
112 | # :parameter name of the page parameter | |
113 | # |
|
113 | # | |
114 | # Examples: |
|
114 | # Examples: | |
115 | # @user_pages, @users = paginate User.where(:status => 1) |
|
115 | # @user_pages, @users = paginate User.where(:status => 1) | |
116 | # |
|
116 | # | |
117 | def paginate(scope, options={}) |
|
117 | def paginate(scope, options={}) | |
118 | options = options.dup |
|
118 | options = options.dup | |
119 |
|
119 | |||
120 | paginator = paginator(scope.count, options) |
|
120 | paginator = paginator(scope.count, options) | |
121 | collection = scope.limit(paginator.per_page).offset(paginator.offset).to_a |
|
121 | collection = scope.limit(paginator.per_page).offset(paginator.offset).to_a | |
122 |
|
122 | |||
123 | return paginator, collection |
|
123 | return paginator, collection | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | def paginator(item_count, options={}) |
|
126 | def paginator(item_count, options={}) | |
127 | options.assert_valid_keys :parameter, :per_page |
|
127 | options.assert_valid_keys :parameter, :per_page | |
128 |
|
128 | |||
129 | page_param = options[:parameter] || :page |
|
129 | page_param = options[:parameter] || :page | |
130 | page = (params[page_param] || 1).to_i |
|
130 | page = (params[page_param] || 1).to_i | |
131 | per_page = options[:per_page] || per_page_option |
|
131 | per_page = options[:per_page] || per_page_option | |
132 | Paginator.new(item_count, per_page, page, page_param) |
|
132 | Paginator.new(item_count, per_page, page, page_param) | |
133 | end |
|
133 | end | |
134 |
|
134 | |||
135 | module Helper |
|
135 | module Helper | |
136 | include Redmine::I18n |
|
136 | include Redmine::I18n | |
137 |
|
137 | |||
138 | # Renders the pagination links for the given paginator. |
|
138 | # Renders the pagination links for the given paginator. | |
139 | # |
|
139 | # | |
140 | # Options: |
|
140 | # Options: | |
141 | # :per_page_links if set to false, the "Per page" links are not rendered |
|
141 | # :per_page_links if set to false, the "Per page" links are not rendered | |
142 | # |
|
142 | # | |
143 | def pagination_links_full(*args) |
|
143 | def pagination_links_full(*args) | |
144 | pagination_links_each(*args) do |text, parameters, options| |
|
144 | pagination_links_each(*args) do |text, parameters, options| | |
145 | if block_given? |
|
145 | if block_given? | |
146 | yield text, parameters, options |
|
146 | yield text, parameters, options | |
147 | else |
|
147 | else | |
148 | link_to text, params.merge(parameters), options |
|
148 | link_to text, {:params => request.query_parameters.merge(parameters)}, options | |
149 | end |
|
149 | end | |
150 | end |
|
150 | end | |
151 | end |
|
151 | end | |
152 |
|
152 | |||
153 | # Yields the given block with the text and parameters |
|
153 | # Yields the given block with the text and parameters | |
154 | # for each pagination link and returns a string that represents the links |
|
154 | # for each pagination link and returns a string that represents the links | |
155 | def pagination_links_each(paginator, count=nil, options={}, &block) |
|
155 | def pagination_links_each(paginator, count=nil, options={}, &block) | |
156 | options.assert_valid_keys :per_page_links |
|
156 | options.assert_valid_keys :per_page_links | |
157 |
|
157 | |||
158 | per_page_links = options.delete(:per_page_links) |
|
158 | per_page_links = options.delete(:per_page_links) | |
159 | per_page_links = false if count.nil? |
|
159 | per_page_links = false if count.nil? | |
160 | page_param = paginator.page_param |
|
160 | page_param = paginator.page_param | |
161 |
|
161 | |||
162 | html = '<ul class="pages">' |
|
162 | html = '<ul class="pages">' | |
163 |
|
163 | |||
164 | if paginator.multiple_pages? |
|
164 | if paginator.multiple_pages? | |
165 | # \xc2\xab(utf-8) = « |
|
165 | # \xc2\xab(utf-8) = « | |
166 | text = "\xc2\xab " + l(:label_previous) |
|
166 | text = "\xc2\xab " + l(:label_previous) | |
167 | if paginator.previous_page |
|
167 | if paginator.previous_page | |
168 | html << content_tag('li', |
|
168 | html << content_tag('li', | |
169 | yield(text, {page_param => paginator.previous_page}, |
|
169 | yield(text, {page_param => paginator.previous_page}, | |
170 | :accesskey => accesskey(:previous)), |
|
170 | :accesskey => accesskey(:previous)), | |
171 | :class => 'previous page') |
|
171 | :class => 'previous page') | |
172 | else |
|
172 | else | |
173 | html << content_tag('li', content_tag('span', text), :class => 'previous') |
|
173 | html << content_tag('li', content_tag('span', text), :class => 'previous') | |
174 | end |
|
174 | end | |
175 | end |
|
175 | end | |
176 |
|
176 | |||
177 | previous = nil |
|
177 | previous = nil | |
178 | paginator.linked_pages.each do |page| |
|
178 | paginator.linked_pages.each do |page| | |
179 | if previous && previous != page - 1 |
|
179 | if previous && previous != page - 1 | |
180 | html << content_tag('li', content_tag('span', '…'.html_safe), :class => 'spacer') |
|
180 | html << content_tag('li', content_tag('span', '…'.html_safe), :class => 'spacer') | |
181 | end |
|
181 | end | |
182 | if page == paginator.page |
|
182 | if page == paginator.page | |
183 | html << content_tag('li', content_tag('span', page.to_s), :class => 'current') |
|
183 | html << content_tag('li', content_tag('span', page.to_s), :class => 'current') | |
184 | else |
|
184 | else | |
185 | html << content_tag('li', |
|
185 | html << content_tag('li', | |
186 | yield(page.to_s, {page_param => page}), |
|
186 | yield(page.to_s, {page_param => page}), | |
187 | :class => 'page') |
|
187 | :class => 'page') | |
188 | end |
|
188 | end | |
189 | previous = page |
|
189 | previous = page | |
190 | end |
|
190 | end | |
191 |
|
191 | |||
192 | if paginator.multiple_pages? |
|
192 | if paginator.multiple_pages? | |
193 | # \xc2\xbb(utf-8) = » |
|
193 | # \xc2\xbb(utf-8) = » | |
194 | text = l(:label_next) + " \xc2\xbb" |
|
194 | text = l(:label_next) + " \xc2\xbb" | |
195 | if paginator.next_page |
|
195 | if paginator.next_page | |
196 | html << content_tag('li', |
|
196 | html << content_tag('li', | |
197 | yield(text, {page_param => paginator.next_page}, |
|
197 | yield(text, {page_param => paginator.next_page}, | |
198 | :accesskey => accesskey(:next)), |
|
198 | :accesskey => accesskey(:next)), | |
199 | :class => 'next page') |
|
199 | :class => 'next page') | |
200 | else |
|
200 | else | |
201 | html << content_tag('li', content_tag('span', text), :class => 'next') |
|
201 | html << content_tag('li', content_tag('span', text), :class => 'next') | |
202 | end |
|
202 | end | |
203 | end |
|
203 | end | |
204 | html << '</ul>' |
|
204 | html << '</ul>' | |
205 |
|
205 | |||
206 | info = ''.html_safe |
|
206 | info = ''.html_safe | |
207 | info << content_tag('span', "(#{paginator.first_item}-#{paginator.last_item}/#{paginator.item_count})", :class => 'items') + ' ' |
|
207 | info << content_tag('span', "(#{paginator.first_item}-#{paginator.last_item}/#{paginator.item_count})", :class => 'items') + ' ' | |
208 | if per_page_links != false && links = per_page_links(paginator, &block) |
|
208 | if per_page_links != false && links = per_page_links(paginator, &block) | |
209 | info << content_tag('span', links.to_s, :class => 'per-page') |
|
209 | info << content_tag('span', links.to_s, :class => 'per-page') | |
210 | end |
|
210 | end | |
211 | html << content_tag('span', info) |
|
211 | html << content_tag('span', info) | |
212 |
|
212 | |||
213 | html.html_safe |
|
213 | html.html_safe | |
214 | end |
|
214 | end | |
215 |
|
215 | |||
216 | # Renders the "Per page" links. |
|
216 | # Renders the "Per page" links. | |
217 | def per_page_links(paginator, &block) |
|
217 | def per_page_links(paginator, &block) | |
218 | values = per_page_options(paginator.per_page, paginator.item_count) |
|
218 | values = per_page_options(paginator.per_page, paginator.item_count) | |
219 | if values.any? |
|
219 | if values.any? | |
220 | links = values.collect do |n| |
|
220 | links = values.collect do |n| | |
221 | if n == paginator.per_page |
|
221 | if n == paginator.per_page | |
222 | content_tag('span', n.to_s, :class => 'selected') |
|
222 | content_tag('span', n.to_s, :class => 'selected') | |
223 | else |
|
223 | else | |
224 | yield(n, :per_page => n, paginator.page_param => nil) |
|
224 | yield(n, :per_page => n, paginator.page_param => nil) | |
225 | end |
|
225 | end | |
226 | end |
|
226 | end | |
227 | l(:label_display_per_page, links.join(', ')).html_safe |
|
227 | l(:label_display_per_page, links.join(', ')).html_safe | |
228 | end |
|
228 | end | |
229 | end |
|
229 | end | |
230 |
|
230 | |||
231 | def per_page_options(selected=nil, item_count=nil) |
|
231 | def per_page_options(selected=nil, item_count=nil) | |
232 | options = Setting.per_page_options_array |
|
232 | options = Setting.per_page_options_array | |
233 | if item_count && options.any? |
|
233 | if item_count && options.any? | |
234 | if item_count > options.first |
|
234 | if item_count > options.first | |
235 | max = options.detect {|value| value >= item_count} || item_count |
|
235 | max = options.detect {|value| value >= item_count} || item_count | |
236 | else |
|
236 | else | |
237 | max = item_count |
|
237 | max = item_count | |
238 | end |
|
238 | end | |
239 | options = options.select {|value| value <= max || value == selected} |
|
239 | options = options.select {|value| value <= max || value == selected} | |
240 | end |
|
240 | end | |
241 | if options.empty? || (options.size == 1 && options.first == selected) |
|
241 | if options.empty? || (options.size == 1 && options.first == selected) | |
242 | [] |
|
242 | [] | |
243 | else |
|
243 | else | |
244 | options |
|
244 | options | |
245 | end |
|
245 | end | |
246 | end |
|
246 | end | |
247 | end |
|
247 | end | |
248 | end |
|
248 | end | |
249 | end |
|
249 | end |
@@ -1,218 +1,218 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class IssuesTest < Redmine::IntegrationTest |
|
20 | class IssuesTest < Redmine::IntegrationTest | |
21 | fixtures :projects, |
|
21 | fixtures :projects, | |
22 | :users, :email_addresses, |
|
22 | :users, :email_addresses, | |
23 | :roles, |
|
23 | :roles, | |
24 | :members, |
|
24 | :members, | |
25 | :member_roles, |
|
25 | :member_roles, | |
26 | :trackers, |
|
26 | :trackers, | |
27 | :projects_trackers, |
|
27 | :projects_trackers, | |
28 | :enabled_modules, |
|
28 | :enabled_modules, | |
29 | :issue_statuses, |
|
29 | :issue_statuses, | |
30 | :issues, |
|
30 | :issues, | |
31 | :enumerations, |
|
31 | :enumerations, | |
32 | :custom_fields, |
|
32 | :custom_fields, | |
33 | :custom_values, |
|
33 | :custom_values, | |
34 | :custom_fields_trackers, |
|
34 | :custom_fields_trackers, | |
35 | :attachments |
|
35 | :attachments | |
36 |
|
36 | |||
37 | # create an issue |
|
37 | # create an issue | |
38 | def test_add_issue |
|
38 | def test_add_issue | |
39 | log_user('jsmith', 'jsmith') |
|
39 | log_user('jsmith', 'jsmith') | |
40 |
|
40 | |||
41 | get '/projects/ecookbook/issues/new' |
|
41 | get '/projects/ecookbook/issues/new' | |
42 | assert_response :success |
|
42 | assert_response :success | |
43 | assert_template 'issues/new' |
|
43 | assert_template 'issues/new' | |
44 |
|
44 | |||
45 | issue = new_record(Issue) do |
|
45 | issue = new_record(Issue) do | |
46 | post '/projects/ecookbook/issues', |
|
46 | post '/projects/ecookbook/issues', | |
47 | :issue => { :tracker_id => "1", |
|
47 | :issue => { :tracker_id => "1", | |
48 | :start_date => "2006-12-26", |
|
48 | :start_date => "2006-12-26", | |
49 | :priority_id => "4", |
|
49 | :priority_id => "4", | |
50 | :subject => "new test issue", |
|
50 | :subject => "new test issue", | |
51 | :category_id => "", |
|
51 | :category_id => "", | |
52 | :description => "new issue", |
|
52 | :description => "new issue", | |
53 | :done_ratio => "0", |
|
53 | :done_ratio => "0", | |
54 | :due_date => "", |
|
54 | :due_date => "", | |
55 | :assigned_to_id => "" }, |
|
55 | :assigned_to_id => "" }, | |
56 | :custom_fields => {'2' => 'Value for field 2'} |
|
56 | :custom_fields => {'2' => 'Value for field 2'} | |
57 | end |
|
57 | end | |
58 | # check redirection |
|
58 | # check redirection | |
59 | assert_redirected_to :controller => 'issues', :action => 'show', :id => issue |
|
59 | assert_redirected_to :controller => 'issues', :action => 'show', :id => issue | |
60 | follow_redirect! |
|
60 | follow_redirect! | |
61 | assert_equal issue, assigns(:issue) |
|
61 | assert_equal issue, assigns(:issue) | |
62 |
|
62 | |||
63 | # check issue attributes |
|
63 | # check issue attributes | |
64 | assert_equal 'jsmith', issue.author.login |
|
64 | assert_equal 'jsmith', issue.author.login | |
65 | assert_equal 1, issue.project.id |
|
65 | assert_equal 1, issue.project.id | |
66 | assert_equal 1, issue.status.id |
|
66 | assert_equal 1, issue.status.id | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def test_create_issue_by_anonymous_without_permission_should_fail |
|
69 | def test_create_issue_by_anonymous_without_permission_should_fail | |
70 | Role.anonymous.remove_permission! :add_issues |
|
70 | Role.anonymous.remove_permission! :add_issues | |
71 |
|
71 | |||
72 | assert_no_difference 'Issue.count' do |
|
72 | assert_no_difference 'Issue.count' do | |
73 | post '/projects/1/issues', :tracker_id => "1", :issue => {:subject => "new test issue"} |
|
73 | post '/projects/1/issues', :tracker_id => "1", :issue => {:subject => "new test issue"} | |
74 | end |
|
74 | end | |
75 | assert_response 302 |
|
75 | assert_response 302 | |
76 | end |
|
76 | end | |
77 |
|
77 | |||
78 | def test_create_issue_by_anonymous_with_custom_permission_should_succeed |
|
78 | def test_create_issue_by_anonymous_with_custom_permission_should_succeed | |
79 | Role.anonymous.remove_permission! :add_issues |
|
79 | Role.anonymous.remove_permission! :add_issues | |
80 | Member.create!(:project_id => 1, :principal => Group.anonymous, :role_ids => [3]) |
|
80 | Member.create!(:project_id => 1, :principal => Group.anonymous, :role_ids => [3]) | |
81 |
|
81 | |||
82 | issue = new_record(Issue) do |
|
82 | issue = new_record(Issue) do | |
83 | post '/projects/1/issues', :tracker_id => "1", :issue => {:subject => "new test issue"} |
|
83 | post '/projects/1/issues', :tracker_id => "1", :issue => {:subject => "new test issue"} | |
84 | assert_response 302 |
|
84 | assert_response 302 | |
85 | end |
|
85 | end | |
86 | assert_equal User.anonymous, issue.author |
|
86 | assert_equal User.anonymous, issue.author | |
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | # add then remove 2 attachments to an issue |
|
89 | # add then remove 2 attachments to an issue | |
90 | def test_issue_attachments |
|
90 | def test_issue_attachments | |
91 | log_user('jsmith', 'jsmith') |
|
91 | log_user('jsmith', 'jsmith') | |
92 | set_tmp_attachments_directory |
|
92 | set_tmp_attachments_directory | |
93 |
|
93 | |||
94 | attachment = new_record(Attachment) do |
|
94 | attachment = new_record(Attachment) do | |
95 | put '/issues/1', |
|
95 | put '/issues/1', | |
96 | :notes => 'Some notes', |
|
96 | :notes => 'Some notes', | |
97 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'This is an attachment'}} |
|
97 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'This is an attachment'}} | |
98 | assert_redirected_to "/issues/1" |
|
98 | assert_redirected_to "/issues/1" | |
99 | end |
|
99 | end | |
100 |
|
100 | |||
101 | assert_equal Issue.find(1), attachment.container |
|
101 | assert_equal Issue.find(1), attachment.container | |
102 | assert_equal 'testfile.txt', attachment.filename |
|
102 | assert_equal 'testfile.txt', attachment.filename | |
103 | assert_equal 'This is an attachment', attachment.description |
|
103 | assert_equal 'This is an attachment', attachment.description | |
104 | # verify the size of the attachment stored in db |
|
104 | # verify the size of the attachment stored in db | |
105 | #assert_equal file_data_1.length, attachment.filesize |
|
105 | #assert_equal file_data_1.length, attachment.filesize | |
106 | # verify that the attachment was written to disk |
|
106 | # verify that the attachment was written to disk | |
107 | assert File.exist?(attachment.diskfile) |
|
107 | assert File.exist?(attachment.diskfile) | |
108 |
|
108 | |||
109 | # remove the attachments |
|
109 | # remove the attachments | |
110 | Issue.find(1).attachments.each(&:destroy) |
|
110 | Issue.find(1).attachments.each(&:destroy) | |
111 | assert_equal 0, Issue.find(1).attachments.length |
|
111 | assert_equal 0, Issue.find(1).attachments.length | |
112 | end |
|
112 | end | |
113 |
|
113 | |||
114 | def test_other_formats_links_on_index |
|
114 | def test_other_formats_links_on_index | |
115 | get '/projects/ecookbook/issues' |
|
115 | get '/projects/ecookbook/issues' | |
116 |
|
116 | |||
117 | %w(Atom PDF CSV).each do |format| |
|
117 | %w(Atom PDF CSV).each do |format| | |
118 | assert_select 'a[rel=nofollow][href=?]', "/projects/ecookbook/issues.#{format.downcase}", :text => format |
|
118 | assert_select 'a[rel=nofollow][href=?]', "/projects/ecookbook/issues.#{format.downcase}", :text => format | |
119 | end |
|
119 | end | |
120 | end |
|
120 | end | |
121 |
|
121 | |||
122 | def test_other_formats_links_on_index_without_project_id_in_url |
|
122 | def test_other_formats_links_on_index_without_project_id_in_url | |
123 | get '/issues', :project_id => 'ecookbook' |
|
123 | get '/issues', :project_id => 'ecookbook' | |
124 |
|
124 | |||
125 | %w(Atom PDF CSV).each do |format| |
|
125 | %w(Atom PDF CSV).each do |format| | |
126 | assert_select 'a[rel=nofollow][href=?]', "/projects/ecookbook/issues.#{format.downcase}", :text => format |
|
126 | assert_select 'a[rel=nofollow][href=?]', "/projects/ecookbook/issues.#{format.downcase}", :text => format | |
127 | end |
|
127 | end | |
128 | end |
|
128 | end | |
129 |
|
129 | |||
130 | def test_pagination_links_on_index |
|
130 | def test_pagination_links_on_index | |
131 | with_settings :per_page_options => '2' do |
|
131 | with_settings :per_page_options => '2' do | |
132 | get '/projects/ecookbook/issues' |
|
132 | get '/projects/ecookbook/issues' | |
133 |
|
133 | |||
134 | assert_select 'a[href=?]', '/projects/ecookbook/issues?page=2', :text => '2' |
|
134 | assert_select 'a[href=?]', '/projects/ecookbook/issues?page=2', :text => '2' | |
135 | end |
|
135 | end | |
136 | end |
|
136 | end | |
137 |
|
137 | |||
138 |
def test_pagination_links_ |
|
138 | def test_pagination_links_should_preserve_query_parameters | |
139 | with_settings :per_page_options => '2' do |
|
139 | with_settings :per_page_options => '2' do | |
140 |
get '/ |
|
140 | get '/projects/ecookbook/issues?foo=bar' | |
141 |
|
141 | |||
142 | assert_select 'a[href=?]', '/projects/ecookbook/issues?page=2', :text => '2' |
|
142 | assert_select 'a[href=?]', '/projects/ecookbook/issues?foo=bar&page=2', :text => '2' | |
143 | end |
|
143 | end | |
144 | end |
|
144 | end | |
145 |
|
145 | |||
146 | def test_issue_with_user_custom_field |
|
146 | def test_issue_with_user_custom_field | |
147 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :trackers => Tracker.all) |
|
147 | @field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :trackers => Tracker.all) | |
148 | Role.anonymous.add_permission! :add_issues, :edit_issues |
|
148 | Role.anonymous.add_permission! :add_issues, :edit_issues | |
149 | users = Project.find(1).users.uniq.sort |
|
149 | users = Project.find(1).users.uniq.sort | |
150 | tester = users.first |
|
150 | tester = users.first | |
151 |
|
151 | |||
152 | # Issue form |
|
152 | # Issue form | |
153 | get '/projects/ecookbook/issues/new' |
|
153 | get '/projects/ecookbook/issues/new' | |
154 | assert_response :success |
|
154 | assert_response :success | |
155 | assert_select 'select[name=?]', "issue[custom_field_values][#{@field.id}]" do |
|
155 | assert_select 'select[name=?]', "issue[custom_field_values][#{@field.id}]" do | |
156 | assert_select 'option', users.size + 1 # +1 for blank value |
|
156 | assert_select 'option', users.size + 1 # +1 for blank value | |
157 | assert_select 'option[value=?]', tester.id.to_s, :text => tester.name |
|
157 | assert_select 'option[value=?]', tester.id.to_s, :text => tester.name | |
158 | end |
|
158 | end | |
159 |
|
159 | |||
160 | # Create issue |
|
160 | # Create issue | |
161 | issue = new_record(Issue) do |
|
161 | issue = new_record(Issue) do | |
162 | post '/projects/ecookbook/issues', |
|
162 | post '/projects/ecookbook/issues', | |
163 | :issue => { |
|
163 | :issue => { | |
164 | :tracker_id => '1', |
|
164 | :tracker_id => '1', | |
165 | :priority_id => '4', |
|
165 | :priority_id => '4', | |
166 | :subject => 'Issue with user custom field', |
|
166 | :subject => 'Issue with user custom field', | |
167 | :custom_field_values => {@field.id.to_s => users.first.id.to_s} |
|
167 | :custom_field_values => {@field.id.to_s => users.first.id.to_s} | |
168 | } |
|
168 | } | |
169 | assert_response 302 |
|
169 | assert_response 302 | |
170 | end |
|
170 | end | |
171 |
|
171 | |||
172 | # Issue view |
|
172 | # Issue view | |
173 | follow_redirect! |
|
173 | follow_redirect! | |
174 | assert_select ".cf_#{@field.id}" do |
|
174 | assert_select ".cf_#{@field.id}" do | |
175 | assert_select '.label', :text => 'Tester:' |
|
175 | assert_select '.label', :text => 'Tester:' | |
176 | assert_select '.value', :text => tester.name |
|
176 | assert_select '.value', :text => tester.name | |
177 | end |
|
177 | end | |
178 | assert_select 'select[name=?]', "issue[custom_field_values][#{@field.id}]" do |
|
178 | assert_select 'select[name=?]', "issue[custom_field_values][#{@field.id}]" do | |
179 | assert_select 'option', users.size + 1 # +1 for blank value |
|
179 | assert_select 'option', users.size + 1 # +1 for blank value | |
180 | assert_select 'option[value=?][selected=selected]', tester.id.to_s, :text => tester.name |
|
180 | assert_select 'option[value=?][selected=selected]', tester.id.to_s, :text => tester.name | |
181 | end |
|
181 | end | |
182 |
|
182 | |||
183 | new_tester = users[1] |
|
183 | new_tester = users[1] | |
184 | with_settings :default_language => 'en' do |
|
184 | with_settings :default_language => 'en' do | |
185 | # Update issue |
|
185 | # Update issue | |
186 | assert_difference 'Journal.count' do |
|
186 | assert_difference 'Journal.count' do | |
187 | put "/issues/#{issue.id}", |
|
187 | put "/issues/#{issue.id}", | |
188 | :notes => 'Updating custom field', |
|
188 | :notes => 'Updating custom field', | |
189 | :issue => { |
|
189 | :issue => { | |
190 | :custom_field_values => {@field.id.to_s => new_tester.id.to_s} |
|
190 | :custom_field_values => {@field.id.to_s => new_tester.id.to_s} | |
191 | } |
|
191 | } | |
192 | assert_redirected_to "/issues/#{issue.id}" |
|
192 | assert_redirected_to "/issues/#{issue.id}" | |
193 | end |
|
193 | end | |
194 | # Issue view |
|
194 | # Issue view | |
195 | follow_redirect! |
|
195 | follow_redirect! | |
196 | assert_select 'ul.details li', :text => "Tester changed from #{tester} to #{new_tester}" |
|
196 | assert_select 'ul.details li', :text => "Tester changed from #{tester} to #{new_tester}" | |
197 | end |
|
197 | end | |
198 | end |
|
198 | end | |
199 |
|
199 | |||
200 | def test_update_using_invalid_http_verbs |
|
200 | def test_update_using_invalid_http_verbs | |
201 | subject = 'Updated by an invalid http verb' |
|
201 | subject = 'Updated by an invalid http verb' | |
202 |
|
202 | |||
203 | get '/issues/update/1', {:issue => {:subject => subject}}, credentials('jsmith') |
|
203 | get '/issues/update/1', {:issue => {:subject => subject}}, credentials('jsmith') | |
204 | assert_response 404 |
|
204 | assert_response 404 | |
205 | assert_not_equal subject, Issue.find(1).subject |
|
205 | assert_not_equal subject, Issue.find(1).subject | |
206 |
|
206 | |||
207 | post '/issues/1', {:issue => {:subject => subject}}, credentials('jsmith') |
|
207 | post '/issues/1', {:issue => {:subject => subject}}, credentials('jsmith') | |
208 | assert_response 404 |
|
208 | assert_response 404 | |
209 | assert_not_equal subject, Issue.find(1).subject |
|
209 | assert_not_equal subject, Issue.find(1).subject | |
210 | end |
|
210 | end | |
211 |
|
211 | |||
212 | def test_get_watch_should_be_invalid |
|
212 | def test_get_watch_should_be_invalid | |
213 | assert_no_difference 'Watcher.count' do |
|
213 | assert_no_difference 'Watcher.count' do | |
214 | get '/watchers/watch?object_type=issue&object_id=1', {}, credentials('jsmith') |
|
214 | get '/watchers/watch?object_type=issue&object_id=1', {}, credentials('jsmith') | |
215 | assert_response 404 |
|
215 | assert_response 404 | |
216 | end |
|
216 | end | |
217 | end |
|
217 | end | |
218 | end |
|
218 | end |
General Comments 0
You need to be logged in to leave comments.
Login now