summaryrefslogtreecommitdiff
path: root/vendor/plugins/will_paginate/test
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-02-17 12:47:49 +0100
committerhukl <contact@smyck.org>2009-02-17 12:47:49 +0100
commit3cdcb5ca02a94b2b342c30903a27d47d35d46e55 (patch)
treeef3e1154fe262561b3955c07edcaee3824a21f47 /vendor/plugins/will_paginate/test
parent7a282f2605f6fb3689940e5c7072b4801653deb2 (diff)
added will_paginate plugin
Diffstat (limited to 'vendor/plugins/will_paginate/test')
-rw-r--r--vendor/plugins/will_paginate/test/boot.rb21
-rw-r--r--vendor/plugins/will_paginate/test/collection_test.rb143
-rwxr-xr-xvendor/plugins/will_paginate/test/console8
-rw-r--r--vendor/plugins/will_paginate/test/finder_test.rb476
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/admin.rb3
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/developer.rb14
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/developers_projects.yml13
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/project.rb15
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/projects.yml6
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/replies.yml29
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/reply.rb7
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/topic.rb10
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/topics.yml30
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/user.rb2
-rw-r--r--vendor/plugins/will_paginate/test/fixtures/users.yml35
-rw-r--r--vendor/plugins/will_paginate/test/helper.rb37
-rw-r--r--vendor/plugins/will_paginate/test/lib/activerecord_test_case.rb36
-rw-r--r--vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb75
-rw-r--r--vendor/plugins/will_paginate/test/lib/load_fixtures.rb11
-rw-r--r--vendor/plugins/will_paginate/test/lib/view_test_process.rb171
-rw-r--r--vendor/plugins/will_paginate/test/tasks.rake59
-rw-r--r--vendor/plugins/will_paginate/test/view_test.rb365
22 files changed, 1566 insertions, 0 deletions
diff --git a/vendor/plugins/will_paginate/test/boot.rb b/vendor/plugins/will_paginate/test/boot.rb
new file mode 100644
index 0000000..622fc93
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/boot.rb
@@ -0,0 +1,21 @@
1plugin_root = File.join(File.dirname(__FILE__), '..')
2version = ENV['RAILS_VERSION']
3version = nil if version and version == ""
4
5# first look for a symlink to a copy of the framework
6if !version and framework_root = ["#{plugin_root}/rails", "#{plugin_root}/../../rails"].find { |p| File.directory? p }
7 puts "found framework root: #{framework_root}"
8 # this allows for a plugin to be tested outside of an app and without Rails gems
9 $:.unshift "#{framework_root}/activesupport/lib", "#{framework_root}/activerecord/lib", "#{framework_root}/actionpack/lib"
10else
11 # simply use installed gems if available
12 puts "using Rails#{version ? ' ' + version : nil} gems"
13 require 'rubygems'
14
15 if version
16 gem 'rails', version
17 else
18 gem 'actionpack'
19 gem 'activerecord'
20 end
21end
diff --git a/vendor/plugins/will_paginate/test/collection_test.rb b/vendor/plugins/will_paginate/test/collection_test.rb
new file mode 100644
index 0000000..a9336bb
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/collection_test.rb
@@ -0,0 +1,143 @@
1require 'helper'
2require 'will_paginate/array'
3
4class ArrayPaginationTest < Test::Unit::TestCase
5
6 def setup ; end
7
8 def test_simple
9 collection = ('a'..'e').to_a
10
11 [{ :page => 1, :per_page => 3, :expected => %w( a b c ) },
12 { :page => 2, :per_page => 3, :expected => %w( d e ) },
13 { :page => 1, :per_page => 5, :expected => %w( a b c d e ) },
14 { :page => 3, :per_page => 5, :expected => [] },
15 ].
16 each do |conditions|
17 expected = conditions.delete :expected
18 assert_equal expected, collection.paginate(conditions)
19 end
20 end
21
22 def test_defaults
23 result = (1..50).to_a.paginate
24 assert_equal 1, result.current_page
25 assert_equal 30, result.size
26 end
27
28 def test_deprecated_api
29 assert_raise(ArgumentError) { [].paginate(2) }
30 assert_raise(ArgumentError) { [].paginate(2, 10) }
31 end
32
33 def test_total_entries_has_precedence
34 result = %w(a b c).paginate :total_entries => 5
35 assert_equal 5, result.total_entries
36 end
37
38 def test_argument_error_with_params_and_another_argument
39 assert_raise ArgumentError do
40 [].paginate({}, 5)
41 end
42 end
43
44 def test_paginated_collection
45 entries = %w(a b c)
46 collection = create(2, 3, 10) do |pager|
47 assert_equal entries, pager.replace(entries)
48 end
49
50 assert_equal entries, collection
51 assert_respond_to_all collection, %w(total_pages each offset size current_page per_page total_entries)
52 assert_kind_of Array, collection
53 assert_instance_of Array, collection.entries
54 assert_equal 3, collection.offset
55 assert_equal 4, collection.total_pages
56 assert !collection.out_of_bounds?
57 end
58
59 def test_previous_next_pages
60 collection = create(1, 1, 3)
61 assert_nil collection.previous_page
62 assert_equal 2, collection.next_page
63
64 collection = create(2, 1, 3)
65 assert_equal 1, collection.previous_page
66 assert_equal 3, collection.next_page
67
68 collection = create(3, 1, 3)
69 assert_equal 2, collection.previous_page
70 assert_nil collection.next_page
71 end
72
73 def test_out_of_bounds
74 entries = create(2, 3, 2){}
75 assert entries.out_of_bounds?
76
77 entries = create(1, 3, 2){}
78 assert !entries.out_of_bounds?
79 end
80
81 def test_guessing_total_count
82 entries = create do |pager|
83 # collection is shorter than limit
84 pager.replace array
85 end
86 assert_equal 8, entries.total_entries
87
88 entries = create(2, 5, 10) do |pager|
89 # collection is shorter than limit, but we have an explicit count
90 pager.replace array
91 end
92 assert_equal 10, entries.total_entries
93
94 entries = create do |pager|
95 # collection is the same as limit; we can't guess
96 pager.replace array(5)
97 end
98 assert_equal nil, entries.total_entries
99
100 entries = create do |pager|
101 # collection is empty; we can't guess
102 pager.replace array(0)
103 end
104 assert_equal nil, entries.total_entries
105
106 entries = create(1) do |pager|
107 # collection is empty and we're on page 1,
108 # so the whole thing must be empty, too
109 pager.replace array(0)
110 end
111 assert_equal 0, entries.total_entries
112 end
113
114 def test_invalid_page
115 bad_inputs = [0, -1, nil, '', 'Schnitzel']
116
117 bad_inputs.each do |bad|
118 assert_raise(WillPaginate::InvalidPage) { create bad }
119 end
120 end
121
122 def test_invalid_per_page_setting
123 assert_raise(ArgumentError) { create(1, -1) }
124 end
125
126 def test_page_count_was_removed
127 assert_raise(NoMethodError) { create.page_count }
128 # It's `total_pages` now.
129 end
130
131 private
132 def create(page = 2, limit = 5, total = nil, &block)
133 if block_given?
134 WillPaginate::Collection.create(page, limit, total, &block)
135 else
136 WillPaginate::Collection.new(page, limit, total)
137 end
138 end
139
140 def array(size = 3)
141 Array.new(size)
142 end
143end
diff --git a/vendor/plugins/will_paginate/test/console b/vendor/plugins/will_paginate/test/console
new file mode 100755
index 0000000..3f282f1
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/console
@@ -0,0 +1,8 @@
1#!/usr/bin/env ruby
2irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
3libs = []
4
5libs << 'irb/completion'
6libs << File.join('lib', 'load_fixtures')
7
8exec "#{irb} -Ilib:test#{libs.map{ |l| " -r #{l}" }.join} --simple-prompt"
diff --git a/vendor/plugins/will_paginate/test/finder_test.rb b/vendor/plugins/will_paginate/test/finder_test.rb
new file mode 100644
index 0000000..38ef565
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/finder_test.rb
@@ -0,0 +1,476 @@
1require 'helper'
2require 'lib/activerecord_test_case'
3
4require 'will_paginate'
5WillPaginate.enable_activerecord
6WillPaginate.enable_named_scope
7
8class FinderTest < ActiveRecordTestCase
9 fixtures :topics, :replies, :users, :projects, :developers_projects
10
11 def test_new_methods_presence
12 assert_respond_to_all Topic, %w(per_page paginate paginate_by_sql)
13 end
14
15 def test_simple_paginate
16 assert_queries(1) do
17 entries = Topic.paginate :page => nil
18 assert_equal 1, entries.current_page
19 assert_equal 1, entries.total_pages
20 assert_equal 4, entries.size
21 end
22
23 assert_queries(2) do
24 entries = Topic.paginate :page => 2
25 assert_equal 1, entries.total_pages
26 assert entries.empty?
27 end
28 end
29
30 def test_parameter_api
31 # :page parameter in options is required!
32 assert_raise(ArgumentError){ Topic.paginate }
33 assert_raise(ArgumentError){ Topic.paginate({}) }
34
35 # explicit :all should not break anything
36 assert_equal Topic.paginate(:page => nil), Topic.paginate(:all, :page => 1)
37
38 # :count could be nil and we should still not cry
39 assert_nothing_raised { Topic.paginate :page => 1, :count => nil }
40 end
41
42 def test_paginate_with_per_page
43 entries = Topic.paginate :page => 1, :per_page => 1
44 assert_equal 1, entries.size
45 assert_equal 4, entries.total_pages
46
47 # Developer class has explicit per_page at 10
48 entries = Developer.paginate :page => 1
49 assert_equal 10, entries.size
50 assert_equal 2, entries.total_pages
51
52 entries = Developer.paginate :page => 1, :per_page => 5
53 assert_equal 11, entries.total_entries
54 assert_equal 5, entries.size
55 assert_equal 3, entries.total_pages
56 end
57
58 def test_paginate_with_order
59 entries = Topic.paginate :page => 1, :order => 'created_at desc'
60 expected = [topics(:futurama), topics(:harvey_birdman), topics(:rails), topics(:ar)].reverse
61 assert_equal expected, entries.to_a
62 assert_equal 1, entries.total_pages
63 end
64
65 def test_paginate_with_conditions
66 entries = Topic.paginate :page => 1, :conditions => ["created_at > ?", 30.minutes.ago]
67 expected = [topics(:rails), topics(:ar)]
68 assert_equal expected, entries.to_a
69 assert_equal 1, entries.total_pages
70 end
71
72 def test_paginate_with_include_and_conditions
73 entries = Topic.paginate \
74 :page => 1,
75 :include => :replies,
76 :conditions => "replies.content LIKE 'Bird%' ",
77 :per_page => 10
78
79 expected = Topic.find :all,
80 :include => 'replies',
81 :conditions => "replies.content LIKE 'Bird%' ",
82 :limit => 10
83
84 assert_equal expected, entries.to_a
85 assert_equal 1, entries.total_entries
86 end
87
88 def test_paginate_with_include_and_order
89 entries = nil
90 assert_queries(2) do
91 entries = Topic.paginate \
92 :page => 1,
93 :include => :replies,
94 :order => 'replies.created_at asc, topics.created_at asc',
95 :per_page => 10
96 end
97
98 expected = Topic.find :all,
99 :include => 'replies',
100 :order => 'replies.created_at asc, topics.created_at asc',
101 :limit => 10
102
103 assert_equal expected, entries.to_a
104 assert_equal 4, entries.total_entries
105 end
106
107 def test_paginate_associations_with_include
108 entries, project = nil, projects(:active_record)
109
110 assert_nothing_raised "THIS IS A BUG in Rails 1.2.3 that was fixed in [7326]. " +
111 "Please upgrade to a newer version of Rails." do
112 entries = project.topics.paginate \
113 :page => 1,
114 :include => :replies,
115 :conditions => "replies.content LIKE 'Nice%' ",
116 :per_page => 10
117 end
118
119 expected = Topic.find :all,
120 :include => 'replies',
121 :conditions => "project_id = #{project.id} AND replies.content LIKE 'Nice%' ",
122 :limit => 10
123
124 assert_equal expected, entries.to_a
125 end
126
127 def test_paginate_associations
128 dhh = users :david
129 expected_name_ordered = [projects(:action_controller), projects(:active_record)]
130 expected_id_ordered = [projects(:active_record), projects(:action_controller)]
131
132 assert_queries(2) do
133 # with association-specified order
134 entries = dhh.projects.paginate(:page => 1)
135 assert_equal expected_name_ordered, entries
136 assert_equal 2, entries.total_entries
137 end
138
139 # with explicit order
140 entries = dhh.projects.paginate(:page => 1, :order => 'projects.id')
141 assert_equal expected_id_ordered, entries
142 assert_equal 2, entries.total_entries
143
144 assert_nothing_raised { dhh.projects.find(:all, :order => 'projects.id', :limit => 4) }
145 entries = dhh.projects.paginate(:page => 1, :order => 'projects.id', :per_page => 4)
146 assert_equal expected_id_ordered, entries
147
148 # has_many with implicit order
149 topic = Topic.find(1)
150 expected = [replies(:spam), replies(:witty_retort)]
151 assert_equal expected.map(&:id).sort, topic.replies.paginate(:page => 1).map(&:id).sort
152 assert_equal expected.reverse, topic.replies.paginate(:page => 1, :order => 'replies.id ASC')
153 end
154
155 def test_paginate_association_extension
156 project = Project.find(:first)
157
158 assert_queries(2) do
159 entries = project.replies.paginate_recent :page => 1
160 assert_equal [replies(:brave)], entries
161 end
162 end
163
164 def test_paginate_with_joins
165 entries = nil
166
167 assert_queries(1) do
168 entries = Developer.paginate :page => 1,
169 :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
170 :conditions => 'project_id = 1'
171 assert_equal 2, entries.size
172 developer_names = entries.map &:name
173 assert developer_names.include?('David')
174 assert developer_names.include?('Jamis')
175 end
176
177 assert_queries(1) do
178 expected = entries.to_a
179 entries = Developer.paginate :page => 1,
180 :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
181 :conditions => 'project_id = 1', :count => { :select => "users.id" }
182 assert_equal expected, entries.to_a
183 assert_equal 2, entries.total_entries
184 end
185 end
186
187 def test_paginate_with_group
188 entries = nil
189 assert_queries(1) do
190 entries = Developer.paginate :page => 1, :per_page => 10,
191 :group => 'salary', :select => 'salary', :order => 'salary'
192 end
193
194 expected = [ users(:david), users(:jamis), users(:dev_10), users(:poor_jamis) ].map(&:salary).sort
195 assert_equal expected, entries.map(&:salary)
196 end
197
198 def test_paginate_with_dynamic_finder
199 expected = [replies(:witty_retort), replies(:spam)]
200 assert_equal expected, Reply.paginate_by_topic_id(1, :page => 1)
201
202 entries = Developer.paginate :conditions => { :salary => 100000 }, :page => 1, :per_page => 5
203 assert_equal 8, entries.total_entries
204 assert_equal entries, Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)
205
206 # dynamic finder + conditions
207 entries = Developer.paginate_by_salary(100000, :page => 1,
208 :conditions => ['id > ?', 6])
209 assert_equal 4, entries.total_entries
210 assert_equal (7..10).to_a, entries.map(&:id)
211
212 assert_raises NoMethodError do
213 Developer.paginate_by_inexistent_attribute 100000, :page => 1
214 end
215 end
216
217 def test_scoped_paginate
218 entries = Developer.with_poor_ones { Developer.paginate :page => 1 }
219
220 assert_equal 2, entries.size
221 assert_equal 2, entries.total_entries
222 end
223
224 ## named_scope ##
225
226 def test_paginate_in_named_scope
227 entries = Developer.poor.paginate :page => 1, :per_page => 1
228
229 assert_equal 1, entries.size
230 assert_equal 2, entries.total_entries
231 end
232
233 def test_paginate_in_named_scope_on_habtm_association
234 project = projects(:active_record)
235 assert_queries(2) do
236 entries = project.developers.poor.paginate :page => 1, :per_page => 1
237
238 assert_equal 1, entries.size, 'one developer should be found'
239 assert_equal 1, entries.total_entries, 'only one developer should be found'
240 end
241 end
242
243 def test_paginate_in_named_scope_on_hmt_association
244 project = projects(:active_record)
245 expected = [replies(:brave)]
246
247 assert_queries(2) do
248 entries = project.replies.recent.paginate :page => 1, :per_page => 1
249 assert_equal expected, entries
250 assert_equal 1, entries.total_entries, 'only one reply should be found'
251 end
252 end
253
254 def test_paginate_in_named_scope_on_has_many_association
255 project = projects(:active_record)
256 expected = [topics(:ar)]
257
258 assert_queries(2) do
259 entries = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
260 assert_equal expected, entries
261 assert_equal 1, entries.total_entries, 'only one topic should be found'
262 end
263 end
264
265 def test_named_scope_with_include
266 project = projects(:active_record)
267 entries = project.topics.with_replies_starting_with('AR ').paginate(:page => 1, :per_page => 1)
268 assert_equal 1, entries.size
269 end
270
271 ## misc ##
272
273 def test_count_and_total_entries_options_are_mutually_exclusive
274 e = assert_raise ArgumentError do
275 Developer.paginate :page => 1, :count => {}, :total_entries => 1
276 end
277 assert_match /exclusive/, e.to_s
278 end
279
280 def test_readonly
281 assert_nothing_raised { Developer.paginate :readonly => true, :page => 1 }
282 end
283
284 # this functionality is temporarily removed
285 def xtest_pagination_defines_method
286 pager = "paginate_by_created_at"
287 assert !User.methods.include?(pager), "User methods should not include `#{pager}` method"
288 # paginate!
289 assert 0, User.send(pager, nil, :page => 1).total_entries
290 # the paging finder should now be defined
291 assert User.methods.include?(pager), "`#{pager}` method should be defined on User"
292 end
293
294 # Is this Rails 2.0? Find out by testing find_all which was removed in [6998]
295 unless ActiveRecord::Base.respond_to? :find_all
296 def test_paginate_array_of_ids
297 # AR finders also accept arrays of IDs
298 # (this was broken in Rails before [6912])
299 assert_queries(1) do
300 entries = Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
301 assert_equal (4..6).to_a, entries.map(&:id)
302 assert_equal 8, entries.total_entries
303 end
304 end
305 end
306
307 uses_mocha 'internals' do
308 def test_implicit_all_with_dynamic_finders
309 Topic.expects(:find_all_by_foo).returns([])
310 Topic.expects(:count).returns(0)
311 Topic.paginate_by_foo :page => 2
312 end
313
314 def test_guessing_the_total_count
315 Topic.expects(:find).returns(Array.new(2))
316 Topic.expects(:count).never
317
318 entries = Topic.paginate :page => 2, :per_page => 4
319 assert_equal 6, entries.total_entries
320 end
321
322 def test_guessing_that_there_are_no_records
323 Topic.expects(:find).returns([])
324 Topic.expects(:count).never
325
326 entries = Topic.paginate :page => 1, :per_page => 4
327 assert_equal 0, entries.total_entries
328 end
329
330 def test_extra_parameters_stay_untouched
331 Topic.expects(:find).with(:all, {:foo => 'bar', :limit => 4, :offset => 0 }).returns(Array.new(5))
332 Topic.expects(:count).with({:foo => 'bar'}).returns(1)
333
334 Topic.paginate :foo => 'bar', :page => 1, :per_page => 4
335 end
336
337 def test_count_skips_select
338 Developer.stubs(:find).returns([])
339 Developer.expects(:count).with({}).returns(0)
340 Developer.paginate :select => 'salary', :page => 2
341 end
342
343 def test_count_select_when_distinct
344 Developer.stubs(:find).returns([])
345 Developer.expects(:count).with(:select => 'DISTINCT salary').returns(0)
346 Developer.paginate :select => 'DISTINCT salary', :page => 2
347 end
348
349 def test_count_with_scoped_select_when_distinct
350 Developer.stubs(:find).returns([])
351 Developer.expects(:count).with(:select => 'DISTINCT users.id').returns(0)
352 Developer.distinct.paginate :page => 2
353 end
354
355 def test_should_use_scoped_finders_if_present
356 # scope-out compatibility
357 Topic.expects(:find_best).returns(Array.new(5))
358 Topic.expects(:with_best).returns(1)
359
360 Topic.paginate_best :page => 1, :per_page => 4
361 end
362
363 def test_paginate_by_sql
364 assert_respond_to Developer, :paginate_by_sql
365 Developer.expects(:find_by_sql).with(regexp_matches(/sql LIMIT 3(,| OFFSET) 3/)).returns([])
366 Developer.expects(:count_by_sql).with('SELECT COUNT(*) FROM (sql) AS count_table').returns(0)
367
368 entries = Developer.paginate_by_sql 'sql', :page => 2, :per_page => 3
369 end
370
371 def test_paginate_by_sql_respects_total_entries_setting
372 Developer.expects(:find_by_sql).returns([])
373 Developer.expects(:count_by_sql).never
374
375 entries = Developer.paginate_by_sql 'sql', :page => 1, :total_entries => 999
376 assert_equal 999, entries.total_entries
377 end
378
379 def test_paginate_by_sql_strips_order_by_when_counting
380 Developer.expects(:find_by_sql).returns([])
381 Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n ) AS count_table").returns(0)
382
383 Developer.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 2
384 end
385
386 # TODO: counts are still wrong
387 def test_ability_to_use_with_custom_finders
388 # acts_as_taggable defines find_tagged_with(tag, options)
389 Topic.expects(:find_tagged_with).with('will_paginate', :offset => 5, :limit => 5).returns([])
390 Topic.expects(:count).with({}).returns(0)
391
392 Topic.paginate_tagged_with 'will_paginate', :page => 2, :per_page => 5
393 end
394
395 def test_array_argument_doesnt_eliminate_count
396 ids = (1..8).to_a
397 Developer.expects(:find_all_by_id).returns([])
398 Developer.expects(:count).returns(0)
399
400 Developer.paginate_by_id(ids, :per_page => 3, :page => 2, :order => 'id')
401 end
402
403 def test_paginating_finder_doesnt_mangle_options
404 Developer.expects(:find).returns([])
405 options = { :page => 1, :per_page => 2, :foo => 'bar' }
406 options_before = options.dup
407
408 Developer.paginate(options)
409 assert_equal options_before, options
410 end
411
412 def test_paginate_by_sql_doesnt_change_original_query
413 query = 'SQL QUERY'
414 original_query = query.dup
415 Developer.expects(:find_by_sql).returns([])
416
417 Developer.paginate_by_sql query, :page => 1
418 assert_equal original_query, query
419 end
420
421 def test_paginated_each
422 collection = stub('collection', :size => 5, :empty? => false, :per_page => 5)
423 collection.expects(:each).times(2).returns(collection)
424 last_collection = stub('collection', :size => 4, :empty? => false, :per_page => 5)
425 last_collection.expects(:each).returns(last_collection)
426
427 params = { :order => 'id', :total_entries => 0 }
428
429 Developer.expects(:paginate).with(params.merge(:page => 2)).returns(collection)
430 Developer.expects(:paginate).with(params.merge(:page => 3)).returns(collection)
431 Developer.expects(:paginate).with(params.merge(:page => 4)).returns(last_collection)
432
433 assert_equal 14, Developer.paginated_each(:page => '2') { }
434 end
435
436 def test_paginated_each_with_named_scope
437 assert_equal 2, Developer.poor.paginated_each(:per_page => 1) {
438 assert_equal 11, Developer.count
439 }
440 end
441
442 # detect ActiveRecord 2.1
443 if ActiveRecord::Base.private_methods.include?('references_eager_loaded_tables?')
444 def test_removes_irrelevant_includes_in_count
445 Developer.expects(:find).returns([1])
446 Developer.expects(:count).with({}).returns(0)
447
448 Developer.paginate :page => 1, :per_page => 1, :include => :projects
449 end
450
451 def test_doesnt_remove_referenced_includes_in_count
452 Developer.expects(:find).returns([1])
453 Developer.expects(:count).with({ :include => :projects, :conditions => 'projects.id > 2' }).returns(0)
454
455 Developer.paginate :page => 1, :per_page => 1,
456 :include => :projects, :conditions => 'projects.id > 2'
457 end
458 end
459
460 def test_paginate_from
461 result = Developer.paginate(:from => 'users', :page => 1, :per_page => 1)
462 assert_equal 1, result.size
463 end
464
465 def test_hmt_with_include
466 # ticket #220
467 reply = projects(:active_record).replies.find(:first, :order => 'replies.id')
468 assert_equal replies(:decisive), reply
469
470 # ticket #223
471 Project.find(1, :include => :replies)
472
473 # I cannot reproduce any of the failures from those reports :(
474 end
475 end
476end
diff --git a/vendor/plugins/will_paginate/test/fixtures/admin.rb b/vendor/plugins/will_paginate/test/fixtures/admin.rb
new file mode 100644
index 0000000..1d5e7f3
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/admin.rb
@@ -0,0 +1,3 @@
1class Admin < User
2 has_many :companies, :finder_sql => 'SELECT * FROM companies'
3end
diff --git a/vendor/plugins/will_paginate/test/fixtures/developer.rb b/vendor/plugins/will_paginate/test/fixtures/developer.rb
new file mode 100644
index 0000000..0224f4b
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/developer.rb
@@ -0,0 +1,14 @@
1class Developer < User
2 has_and_belongs_to_many :projects, :include => :topics, :order => 'projects.name'
3
4 def self.with_poor_ones(&block)
5 with_scope :find => { :conditions => ['salary <= ?', 80000], :order => 'salary' } do
6 yield
7 end
8 end
9
10 named_scope :distinct, :select => 'DISTINCT `users`.*'
11 named_scope :poor, :conditions => ['salary <= ?', 80000], :order => 'salary'
12
13 def self.per_page() 10 end
14end
diff --git a/vendor/plugins/will_paginate/test/fixtures/developers_projects.yml b/vendor/plugins/will_paginate/test/fixtures/developers_projects.yml
new file mode 100644
index 0000000..cee359c
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/developers_projects.yml
@@ -0,0 +1,13 @@
1david_action_controller:
2 developer_id: 1
3 project_id: 2
4 joined_on: 2004-10-10
5
6david_active_record:
7 developer_id: 1
8 project_id: 1
9 joined_on: 2004-10-10
10
11jamis_active_record:
12 developer_id: 2
13 project_id: 1 \ No newline at end of file
diff --git a/vendor/plugins/will_paginate/test/fixtures/project.rb b/vendor/plugins/will_paginate/test/fixtures/project.rb
new file mode 100644
index 0000000..0f85ef5
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/project.rb
@@ -0,0 +1,15 @@
1class Project < ActiveRecord::Base
2 has_and_belongs_to_many :developers, :uniq => true
3
4 has_many :topics
5 # :finder_sql => 'SELECT * FROM topics WHERE (topics.project_id = #{id})',
6 # :counter_sql => 'SELECT COUNT(*) FROM topics WHERE (topics.project_id = #{id})'
7
8 has_many :replies, :through => :topics do
9 def find_recent(params = {})
10 with_scope :find => { :conditions => ['replies.created_at > ?', 15.minutes.ago] } do
11 find :all, params
12 end
13 end
14 end
15end
diff --git a/vendor/plugins/will_paginate/test/fixtures/projects.yml b/vendor/plugins/will_paginate/test/fixtures/projects.yml
new file mode 100644
index 0000000..74f3c32
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/projects.yml
@@ -0,0 +1,6 @@
1active_record:
2 id: 1
3 name: Active Record
4action_controller:
5 id: 2
6 name: Active Controller
diff --git a/vendor/plugins/will_paginate/test/fixtures/replies.yml b/vendor/plugins/will_paginate/test/fixtures/replies.yml
new file mode 100644
index 0000000..9a83c00
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/replies.yml
@@ -0,0 +1,29 @@
1witty_retort:
2 id: 1
3 topic_id: 1
4 content: Birdman is better!
5 created_at: <%= 6.hours.ago.to_s(:db) %>
6
7another:
8 id: 2
9 topic_id: 2
10 content: Nuh uh!
11 created_at: <%= 1.hour.ago.to_s(:db) %>
12
13spam:
14 id: 3
15 topic_id: 1
16 content: Nice site!
17 created_at: <%= 1.hour.ago.to_s(:db) %>
18
19decisive:
20 id: 4
21 topic_id: 4
22 content: "I'm getting to the bottom of this"
23 created_at: <%= 30.minutes.ago.to_s(:db) %>
24
25brave:
26 id: 5
27 topic_id: 4
28 content: "AR doesn't scare me a bit"
29 created_at: <%= 10.minutes.ago.to_s(:db) %>
diff --git a/vendor/plugins/will_paginate/test/fixtures/reply.rb b/vendor/plugins/will_paginate/test/fixtures/reply.rb
new file mode 100644
index 0000000..ecaf3c1
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/reply.rb
@@ -0,0 +1,7 @@
1class Reply < ActiveRecord::Base
2 belongs_to :topic, :include => [:replies]
3
4 named_scope :recent, :conditions => ['replies.created_at > ?', 15.minutes.ago]
5
6 validates_presence_of :content
7end
diff --git a/vendor/plugins/will_paginate/test/fixtures/topic.rb b/vendor/plugins/will_paginate/test/fixtures/topic.rb
new file mode 100644
index 0000000..2c2ce72
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/topic.rb
@@ -0,0 +1,10 @@
1class Topic < ActiveRecord::Base
2 has_many :replies, :dependent => :destroy, :order => 'replies.created_at DESC'
3 belongs_to :project
4
5 named_scope :mentions_activerecord, :conditions => ['topics.title LIKE ?', '%ActiveRecord%']
6
7 named_scope :with_replies_starting_with, lambda { |text|
8 { :conditions => "replies.content LIKE '#{text}%' ", :include => :replies }
9 }
10end
diff --git a/vendor/plugins/will_paginate/test/fixtures/topics.yml b/vendor/plugins/will_paginate/test/fixtures/topics.yml
new file mode 100644
index 0000000..0a26904
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/topics.yml
@@ -0,0 +1,30 @@
1futurama:
2 id: 1
3 title: Isnt futurama awesome?
4 subtitle: It really is, isnt it.
5 content: I like futurama
6 created_at: <%= 1.day.ago.to_s(:db) %>
7 updated_at:
8
9harvey_birdman:
10 id: 2
11 title: Harvey Birdman is the king of all men
12 subtitle: yup
13 content: He really is
14 created_at: <%= 2.hours.ago.to_s(:db) %>
15 updated_at:
16
17rails:
18 id: 3
19 project_id: 1
20 title: Rails is nice
21 subtitle: It makes me happy
22 content: except when I have to hack internals to fix pagination. even then really.
23 created_at: <%= 20.minutes.ago.to_s(:db) %>
24
25ar:
26 id: 4
27 project_id: 1
28 title: ActiveRecord sometimes freaks me out
29 content: "I mean, what's the deal with eager loading?"
30 created_at: <%= 15.minutes.ago.to_s(:db) %>
diff --git a/vendor/plugins/will_paginate/test/fixtures/user.rb b/vendor/plugins/will_paginate/test/fixtures/user.rb
new file mode 100644
index 0000000..4a57cf0
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/user.rb
@@ -0,0 +1,2 @@
1class User < ActiveRecord::Base
2end
diff --git a/vendor/plugins/will_paginate/test/fixtures/users.yml b/vendor/plugins/will_paginate/test/fixtures/users.yml
new file mode 100644
index 0000000..ed2c03a
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/fixtures/users.yml
@@ -0,0 +1,35 @@
1david:
2 id: 1
3 name: David
4 salary: 80000
5 type: Developer
6
7jamis:
8 id: 2
9 name: Jamis
10 salary: 150000
11 type: Developer
12
13<% for digit in 3..10 %>
14dev_<%= digit %>:
15 id: <%= digit %>
16 name: fixture_<%= digit %>
17 salary: 100000
18 type: Developer
19<% end %>
20
21poor_jamis:
22 id: 11
23 name: Jamis
24 salary: 9000
25 type: Developer
26
27admin:
28 id: 12
29 name: admin
30 type: Admin
31
32goofy:
33 id: 13
34 name: Goofy
35 type: Admin
diff --git a/vendor/plugins/will_paginate/test/helper.rb b/vendor/plugins/will_paginate/test/helper.rb
new file mode 100644
index 0000000..ad52b1b
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/helper.rb
@@ -0,0 +1,37 @@
1require 'test/unit'
2require 'rubygems'
3
4# gem install redgreen for colored test output
5begin require 'redgreen'; rescue LoadError; end
6
7require 'boot' unless defined?(ActiveRecord)
8
9class Test::Unit::TestCase
10 protected
11 def assert_respond_to_all object, methods
12 methods.each do |method|
13 [method.to_s, method.to_sym].each { |m| assert_respond_to object, m }
14 end
15 end
16
17 def collect_deprecations
18 old_behavior = WillPaginate::Deprecation.behavior
19 deprecations = []
20 WillPaginate::Deprecation.behavior = Proc.new do |message, callstack|
21 deprecations << message
22 end
23 result = yield
24 [result, deprecations]
25 ensure
26 WillPaginate::Deprecation.behavior = old_behavior
27 end
28end
29
30# Wrap tests that use Mocha and skip if unavailable.
31def uses_mocha(test_name)
32 require 'mocha' unless Object.const_defined?(:Mocha)
33rescue LoadError => load_error
34 $stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
35else
36 yield
37end
diff --git a/vendor/plugins/will_paginate/test/lib/activerecord_test_case.rb b/vendor/plugins/will_paginate/test/lib/activerecord_test_case.rb
new file mode 100644
index 0000000..8f66ebe
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/lib/activerecord_test_case.rb
@@ -0,0 +1,36 @@
1require 'lib/activerecord_test_connector'
2
3class ActiveRecordTestCase < Test::Unit::TestCase
4 # Set our fixture path
5 if ActiveRecordTestConnector.able_to_connect
6 self.fixture_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
7 self.use_transactional_fixtures = true
8 end
9
10 def self.fixtures(*args)
11 super if ActiveRecordTestConnector.connected
12 end
13
14 def run(*args)
15 super if ActiveRecordTestConnector.connected
16 end
17
18 # Default so Test::Unit::TestCase doesn't complain
19 def test_truth
20 end
21
22 protected
23
24 def assert_queries(num = 1)
25 $query_count = 0
26 yield
27 ensure
28 assert_equal num, $query_count, "#{$query_count} instead of #{num} queries were executed."
29 end
30
31 def assert_no_queries(&block)
32 assert_queries(0, &block)
33 end
34end
35
36ActiveRecordTestConnector.setup
diff --git a/vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb b/vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb
new file mode 100644
index 0000000..1130cf5
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb
@@ -0,0 +1,75 @@
1require 'active_record'
2require 'active_record/version'
3require 'active_record/fixtures'
4
5class ActiveRecordTestConnector
6 cattr_accessor :able_to_connect
7 cattr_accessor :connected
8
9 FIXTURES_PATH = File.join(File.dirname(__FILE__), '..', 'fixtures')
10
11 # Set our defaults
12 self.connected = false
13 self.able_to_connect = true
14
15 def self.setup
16 unless self.connected || !self.able_to_connect
17 setup_connection
18 load_schema
19 add_load_path FIXTURES_PATH
20 self.connected = true
21 end
22 rescue Exception => e # errors from ActiveRecord setup
23 $stderr.puts "\nSkipping ActiveRecord tests: #{e}\n\n"
24 self.able_to_connect = false
25 end
26
27 private
28
29 def self.add_load_path(path)
30 dep = defined?(ActiveSupport::Dependencies) ? ActiveSupport::Dependencies : ::Dependencies
31 dep.load_paths.unshift path
32 end
33
34 def self.setup_connection
35 db = ENV['DB'].blank?? 'sqlite3' : ENV['DB']
36
37 configurations = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'database.yml'))
38 raise "no configuration for '#{db}'" unless configurations.key? db
39 configuration = configurations[db]
40
41 ActiveRecord::Base.logger = Logger.new(STDOUT) if $0 == 'irb'
42 puts "using #{configuration['adapter']} adapter" unless ENV['DB'].blank?
43
44 gem 'sqlite3-ruby' if 'sqlite3' == db
45
46 ActiveRecord::Base.establish_connection(configuration)
47 ActiveRecord::Base.configurations = { db => configuration }
48 prepare ActiveRecord::Base.connection
49
50 unless Object.const_defined?(:QUOTED_TYPE)
51 Object.send :const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')
52 end
53 end
54
55 def self.load_schema
56 ActiveRecord::Base.silence do
57 ActiveRecord::Migration.verbose = false
58 load File.join(FIXTURES_PATH, 'schema.rb')
59 end
60 end
61
62 def self.prepare(conn)
63 class << conn
64 IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SHOW FIELDS /]
65
66 def execute_with_counting(sql, name = nil, &block)
67 $query_count ||= 0
68 $query_count += 1 unless IGNORED_SQL.any? { |r| sql =~ r }
69 execute_without_counting(sql, name, &block)
70 end
71
72 alias_method_chain :execute, :counting
73 end
74 end
75end
diff --git a/vendor/plugins/will_paginate/test/lib/load_fixtures.rb b/vendor/plugins/will_paginate/test/lib/load_fixtures.rb
new file mode 100644
index 0000000..10d6f42
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/lib/load_fixtures.rb
@@ -0,0 +1,11 @@
1require 'boot'
2require 'lib/activerecord_test_connector'
3
4# setup the connection
5ActiveRecordTestConnector.setup
6
7# load all fixtures
8Fixtures.create_fixtures(ActiveRecordTestConnector::FIXTURES_PATH, ActiveRecord::Base.connection.tables)
9
10require 'will_paginate'
11WillPaginate.enable_activerecord
diff --git a/vendor/plugins/will_paginate/test/lib/view_test_process.rb b/vendor/plugins/will_paginate/test/lib/view_test_process.rb
new file mode 100644
index 0000000..508411e
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/lib/view_test_process.rb
@@ -0,0 +1,171 @@
1require 'action_controller'
2require 'action_controller/test_process'
3
4require 'will_paginate'
5WillPaginate.enable_actionpack
6
7ActionController::Routing::Routes.draw do |map|
8 map.connect 'dummy/page/:page', :controller => 'dummy'
9 map.connect 'dummy/dots/page.:page', :controller => 'dummy', :action => 'dots'
10 map.connect 'ibocorp/:page', :controller => 'ibocorp',
11 :requirements => { :page => /\d+/ },
12 :defaults => { :page => 1 }
13
14 map.connect ':controller/:action/:id'
15end
16
17ActionController::Base.perform_caching = false
18
19class WillPaginate::ViewTestCase < Test::Unit::TestCase
20 def setup
21 super
22 @controller = DummyController.new
23 @request = @controller.request
24 @html_result = nil
25 @template = '<%= will_paginate collection, options %>'
26
27 @view = ActionView::Base.new
28 @view.assigns['controller'] = @controller
29 @view.assigns['_request'] = @request
30 @view.assigns['_params'] = @request.params
31 end
32
33 def test_no_complain; end
34
35 protected
36
37 def paginate(collection = {}, options = {}, &block)
38 if collection.instance_of? Hash
39 page_options = { :page => 1, :total_entries => 11, :per_page => 4 }.merge(collection)
40 collection = [1].paginate(page_options)
41 end
42
43 locals = { :collection => collection, :options => options }
44
45 unless @view.respond_to? :render_template
46 # Rails 2.2
47 @html_result = ActionView::InlineTemplate.new(@template).render(@view, locals)
48 else
49 if defined? ActionView::InlineTemplate
50 # Rails 2.1
51 args = [ ActionView::InlineTemplate.new(@view, @template, locals) ]
52 else
53 # older Rails versions
54 args = [nil, @template, nil, locals]
55 end
56
57 @html_result = @view.render_template(*args)
58 end
59
60 @html_document = HTML::Document.new(@html_result, true, false)
61
62 if block_given?
63 classname = options[:class] || WillPaginate::ViewHelpers.pagination_options[:class]
64 assert_select("div.#{classname}", 1, 'no main DIV', &block)
65 end
66 end
67
68 def response_from_page_or_rjs
69 @html_document.root
70 end
71
72 def validate_page_numbers expected, links, param_name = :page
73 param_pattern = /\W#{CGI.escape(param_name.to_s)}=([^&]*)/
74
75 assert_equal(expected, links.map { |e|
76 e['href'] =~ param_pattern
77 $1 ? $1.to_i : $1
78 })
79 end
80
81 def assert_links_match pattern, links = nil, numbers = nil
82 links ||= assert_select 'div.pagination a[href]' do |elements|
83 elements
84 end
85
86 pages = [] if numbers
87
88 links.each do |el|
89 assert_match pattern, el['href']
90 if numbers
91 el['href'] =~ pattern
92 pages << ($1.nil?? nil : $1.to_i)
93 end
94 end
95
96 assert_equal numbers, pages, "page numbers don't match" if numbers
97 end
98
99 def assert_no_links_match pattern
100 assert_select 'div.pagination a[href]' do |elements|
101 elements.each do |el|
102 assert_no_match pattern, el['href']
103 end
104 end
105 end
106end
107
108class DummyRequest
109 attr_accessor :symbolized_path_parameters
110
111 def initialize
112 @get = true
113 @params = {}
114 @symbolized_path_parameters = { :controller => 'foo', :action => 'bar' }
115 end
116
117 def get?
118 @get
119 end
120
121 def post
122 @get = false
123 end
124
125 def relative_url_root
126 ''
127 end
128
129 def params(more = nil)
130 @params.update(more) if more
131 @params
132 end
133end
134
135class DummyController
136 attr_reader :request
137 attr_accessor :controller_name
138
139 def initialize
140 @request = DummyRequest.new
141 @url = ActionController::UrlRewriter.new(@request, @request.params)
142 end
143
144 def params
145 @request.params
146 end
147
148 def url_for(params)
149 @url.rewrite(params)
150 end
151end
152
153module HTML
154 Node.class_eval do
155 def inner_text
156 children.map(&:inner_text).join('')
157 end
158 end
159
160 Text.class_eval do
161 def inner_text
162 self.to_s
163 end
164 end
165
166 Tag.class_eval do
167 def inner_text
168 childless?? '' : super
169 end
170 end
171end
diff --git a/vendor/plugins/will_paginate/test/tasks.rake b/vendor/plugins/will_paginate/test/tasks.rake
new file mode 100644
index 0000000..59f2f94
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/tasks.rake
@@ -0,0 +1,59 @@
1require 'rake/testtask'
2
3desc 'Test the will_paginate plugin.'
4Rake::TestTask.new(:test) do |t|
5 t.pattern = 'test/**/*_test.rb'
6 t.verbose = true
7 t.libs << 'test'
8end
9
10# I want to specify environment variables at call time
11class EnvTestTask < Rake::TestTask
12 attr_accessor :env
13
14 def ruby(*args)
15 env.each { |key, value| ENV[key] = value } if env
16 super
17 env.keys.each { |key| ENV.delete key } if env
18 end
19end
20
21for configuration in %w( sqlite3 mysql postgres )
22 EnvTestTask.new("test_#{configuration}") do |t|
23 t.pattern = 'test/finder_test.rb'
24 t.verbose = true
25 t.env = { 'DB' => configuration }
26 t.libs << 'test'
27 end
28end
29
30task :test_databases => %w(test_mysql test_sqlite3 test_postgres)
31
32desc %{Test everything on SQLite3, MySQL and PostgreSQL}
33task :test_full => %w(test test_mysql test_postgres)
34
35desc %{Test everything with Rails 2.1.x, 2.0.x & 1.2.x gems}
36task :test_all do
37 all = Rake::Task['test_full']
38 versions = %w(2.1.0 2.0.4 1.2.6)
39 versions.each do |version|
40 ENV['RAILS_VERSION'] = "~> #{version}"
41 all.invoke
42 reset_invoked unless version == versions.last
43 end
44end
45
46def reset_invoked
47 %w( test_full test test_mysql test_postgres ).each do |name|
48 Rake::Task[name].instance_variable_set '@already_invoked', false
49 end
50end
51
52task :rcov do
53 excludes = %w( lib/will_paginate/named_scope*
54 lib/will_paginate/core_ext.rb
55 lib/will_paginate.rb
56 rails* )
57
58 system %[rcov -Itest:lib test/*.rb -x #{excludes.join(',')}]
59end
diff --git a/vendor/plugins/will_paginate/test/view_test.rb b/vendor/plugins/will_paginate/test/view_test.rb
new file mode 100644
index 0000000..3ada457
--- /dev/null
+++ b/vendor/plugins/will_paginate/test/view_test.rb
@@ -0,0 +1,365 @@
1require 'helper'
2require 'lib/view_test_process'
3
4class AdditionalLinkAttributesRenderer < WillPaginate::LinkRenderer
5 def initialize(link_attributes = nil)
6 super()
7 @additional_link_attributes = link_attributes || { :default => 'true' }
8 end
9
10 def page_link(page, text, attributes = {})
11 @template.link_to text, url_for(page), attributes.merge(@additional_link_attributes)
12 end
13end
14
15class ViewTest < WillPaginate::ViewTestCase
16
17 ## basic pagination ##
18
19 def test_will_paginate
20 paginate do |pagination|
21 assert_select 'a[href]', 3 do |elements|
22 validate_page_numbers [2,3,2], elements
23 assert_select elements.last, ':last-child', "Next &raquo;"
24 end
25 assert_select 'span', 2
26 assert_select 'span.disabled:first-child', '&laquo; Previous'
27 assert_select 'span.current', '1'
28 assert_equal '&laquo; Previous 1 2 3 Next &raquo;', pagination.first.inner_text
29 end
30 end
31
32 def test_no_pagination_when_page_count_is_one
33 paginate :per_page => 30
34 assert_equal '', @html_result
35 end
36
37 def test_will_paginate_with_options
38 paginate({ :page => 2 },
39 :class => 'will_paginate', :previous_label => 'Prev', :next_label => 'Next') do
40 assert_select 'a[href]', 4 do |elements|
41 validate_page_numbers [1,1,3,3], elements
42 # test rel attribute values:
43 assert_select elements[1], 'a', '1' do |link|
44 assert_equal 'prev start', link.first['rel']
45 end
46 assert_select elements.first, 'a', "Prev" do |link|
47 assert_equal 'prev start', link.first['rel']
48 end
49 assert_select elements.last, 'a', "Next" do |link|
50 assert_equal 'next', link.first['rel']
51 end
52 end
53 assert_select 'span.current', '2'
54 end
55 end
56
57 def test_will_paginate_using_renderer_class
58 paginate({}, :renderer => AdditionalLinkAttributesRenderer) do
59 assert_select 'a[default=true]', 3
60 end
61 end
62
63 def test_will_paginate_using_renderer_instance
64 renderer = WillPaginate::LinkRenderer.new
65 renderer.gap_marker = '<span class="my-gap">~~</span>'
66
67 paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
68 assert_select 'span.my-gap', '~~'
69 end
70
71 renderer = AdditionalLinkAttributesRenderer.new(:title => 'rendered')
72 paginate({}, :renderer => renderer) do
73 assert_select 'a[title=rendered]', 3
74 end
75 end
76
77 def test_prev_next_links_have_classnames
78 paginate do |pagination|
79 assert_select 'span.disabled.prev_page:first-child'
80 assert_select 'a.next_page[href]:last-child'
81 end
82 end
83
84 def test_prev_label_deprecated
85 assert_deprecated ':previous_label' do
86 paginate({ :page => 2 }, :prev_label => 'Deprecated') do
87 assert_select 'a[href]:first-child', 'Deprecated'
88 end
89 end
90 end
91
92 def test_full_output
93 paginate
94 expected = <<-HTML
95 <div class="pagination"><span class="disabled prev_page">&laquo; Previous</span>
96 <span class="current">1</span>
97 <a href="/foo/bar?page=2" rel="next">2</a>
98 <a href="/foo/bar?page=3">3</a>
99 <a href="/foo/bar?page=2" class="next_page" rel="next">Next &raquo;</a></div>
100 HTML
101 expected.strip!.gsub!(/\s{2,}/, ' ')
102
103 assert_dom_equal expected, @html_result
104 end
105
106 def test_escaping_of_urls
107 paginate({:page => 1, :per_page => 1, :total_entries => 2},
108 :page_links => false, :params => { :tag => '<br>' })
109
110 assert_select 'a[href]', 1 do |links|
111 query = links.first['href'].split('?', 2)[1]
112 assert_equal %w(page=2 tag=%3Cbr%3E), query.split('&amp;').sort
113 end
114 end
115
116 ## advanced options for pagination ##
117
118 def test_will_paginate_without_container
119 paginate({}, :container => false)
120 assert_select 'div.pagination', 0, 'main DIV present when it shouldn\'t'
121 assert_select 'a[href]', 3
122 end
123
124 def test_will_paginate_without_page_links
125 paginate({ :page => 2 }, :page_links => false) do
126 assert_select 'a[href]', 2 do |elements|
127 validate_page_numbers [1,3], elements
128 end
129 end
130 end
131
132 def test_will_paginate_windows
133 paginate({ :page => 6, :per_page => 1 }, :inner_window => 1) do |pagination|
134 assert_select 'a[href]', 8 do |elements|
135 validate_page_numbers [5,1,2,5,7,10,11,7], elements
136 assert_select elements.first, 'a', '&laquo; Previous'
137 assert_select elements.last, 'a', 'Next &raquo;'
138 end
139 assert_select 'span.current', '6'
140 assert_equal '&laquo; Previous 1 2 &hellip; 5 6 7 &hellip; 10 11 Next &raquo;', pagination.first.inner_text
141 end
142 end
143
144 def test_will_paginate_eliminates_small_gaps
145 paginate({ :page => 6, :per_page => 1 }, :inner_window => 2) do
146 assert_select 'a[href]', 12 do |elements|
147 validate_page_numbers [5,1,2,3,4,5,7,8,9,10,11,7], elements
148 end
149 end
150 end
151
152 def test_container_id
153 paginate do |div|
154 assert_nil div.first['id']
155 end
156
157 # magic ID
158 paginate({}, :id => true) do |div|
159 assert_equal 'fixnums_pagination', div.first['id']
160 end
161
162 # explicit ID
163 paginate({}, :id => 'custom_id') do |div|
164 assert_equal 'custom_id', div.first['id']
165 end
166 end
167
168 ## other helpers ##
169
170 def test_paginated_section
171 @template = <<-ERB
172 <% paginated_section collection, options do %>
173 <%= content_tag :div, '', :id => "developers" %>
174 <% end %>
175 ERB
176
177 paginate
178 assert_select 'div.pagination', 2
179 assert_select 'div.pagination + div#developers', 1
180 end
181
182 def test_page_entries_info
183 @template = '<%= page_entries_info collection %>'
184 array = ('a'..'z').to_a
185
186 paginate array.paginate(:page => 2, :per_page => 5)
187 assert_equal %{Displaying strings <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total},
188 @html_result
189
190 paginate array.paginate(:page => 7, :per_page => 4)
191 assert_equal %{Displaying strings <b>25&nbsp;-&nbsp;26</b> of <b>26</b> in total},
192 @html_result
193 end
194
195 uses_mocha 'class name' do
196 def test_page_entries_info_with_longer_class_name
197 @template = '<%= page_entries_info collection %>'
198 collection = ('a'..'z').to_a.paginate
199 collection.first.stubs(:class).returns(mock('class', :name => 'ProjectType'))
200
201 paginate collection
202 assert @html_result.index('project types'), "expected <#{@html_result.inspect}> to mention 'project types'"
203 end
204 end
205
206 def test_page_entries_info_with_single_page_collection
207 @template = '<%= page_entries_info collection %>'
208
209 paginate(('a'..'d').to_a.paginate(:page => 1, :per_page => 5))
210 assert_equal %{Displaying <b>all 4</b> strings}, @html_result
211
212 paginate(['a'].paginate(:page => 1, :per_page => 5))
213 assert_equal %{Displaying <b>1</b> string}, @html_result
214
215 paginate([].paginate(:page => 1, :per_page => 5))
216 assert_equal %{No entries found}, @html_result
217 end
218
219 def test_page_entries_info_with_custom_entry_name
220 @template = '<%= page_entries_info collection, :entry_name => "author" %>'
221
222 entries = (1..20).to_a
223
224 paginate(entries.paginate(:page => 1, :per_page => 5))
225 assert_equal %{Displaying authors <b>1&nbsp;-&nbsp;5</b> of <b>20</b> in total}, @html_result
226
227 paginate(entries.paginate(:page => 1, :per_page => 20))
228 assert_equal %{Displaying <b>all 20</b> authors}, @html_result
229
230 paginate(['a'].paginate(:page => 1, :per_page => 5))
231 assert_equal %{Displaying <b>1</b> author}, @html_result
232
233 paginate([].paginate(:page => 1, :per_page => 5))
234 assert_equal %{No authors found}, @html_result
235 end
236
237 ## parameter handling in page links ##
238
239 def test_will_paginate_preserves_parameters_on_get
240 @request.params :foo => { :bar => 'baz' }
241 paginate
242 assert_links_match /foo%5Bbar%5D=baz/
243 end
244
245 def test_will_paginate_doesnt_preserve_parameters_on_post
246 @request.post
247 @request.params :foo => 'bar'
248 paginate
249 assert_no_links_match /foo=bar/
250 end
251
252 def test_adding_additional_parameters
253 paginate({}, :params => { :foo => 'bar' })
254 assert_links_match /foo=bar/
255 end
256
257 def test_adding_anchor_parameter
258 paginate({}, :params => { :anchor => 'anchor' })
259 assert_links_match /#anchor$/
260 end
261
262 def test_removing_arbitrary_parameters
263 @request.params :foo => 'bar'
264 paginate({}, :params => { :foo => nil })
265 assert_no_links_match /foo=bar/
266 end
267
268 def test_adding_additional_route_parameters
269 paginate({}, :params => { :controller => 'baz', :action => 'list' })
270 assert_links_match %r{\Wbaz/list\W}
271 end
272
273 def test_will_paginate_with_custom_page_param
274 paginate({ :page => 2 }, :param_name => :developers_page) do
275 assert_select 'a[href]', 4 do |elements|
276 validate_page_numbers [1,1,3,3], elements, :developers_page
277 end
278 end
279 end
280
281 def test_complex_custom_page_param
282 @request.params :developers => { :page => 2 }
283
284 paginate({ :page => 2 }, :param_name => 'developers[page]') do
285 assert_select 'a[href]', 4 do |links|
286 assert_links_match /\?developers%5Bpage%5D=\d+$/, links
287 validate_page_numbers [1,1,3,3], links, 'developers[page]'
288 end
289 end
290 end
291
292 def test_custom_routing_page_param
293 @request.symbolized_path_parameters.update :controller => 'dummy', :action => nil
294 paginate :per_page => 2 do
295 assert_select 'a[href]', 6 do |links|
296 assert_links_match %r{/page/(\d+)$}, links, [2, 3, 4, 5, 6, 2]
297 end
298 end
299 end
300
301 def test_custom_routing_page_param_with_dot_separator
302 @request.symbolized_path_parameters.update :controller => 'dummy', :action => 'dots'
303 paginate :per_page => 2 do
304 assert_select 'a[href]', 6 do |links|
305 assert_links_match %r{/page\.(\d+)$}, links, [2, 3, 4, 5, 6, 2]
306 end
307 end
308 end
309
310 def test_custom_routing_with_first_page_hidden
311 @request.symbolized_path_parameters.update :controller => 'ibocorp', :action => nil
312 paginate :page => 2, :per_page => 2 do
313 assert_select 'a[href]', 7 do |links|
314 assert_links_match %r{/ibocorp(?:/(\d+))?$}, links, [nil, nil, 3, 4, 5, 6, 3]
315 end
316 end
317 end
318
319 ## internal hardcore stuff ##
320
321 class LegacyCollection < WillPaginate::Collection
322 alias :page_count :total_pages
323 undef :total_pages
324 end
325
326 def test_deprecation_notices_with_page_count
327 collection = LegacyCollection.new(1, 1, 2)
328
329 assert_deprecated collection.class.name do
330 paginate collection
331 end
332 end
333
334 uses_mocha 'view internals' do
335 def test_collection_name_can_be_guessed
336 collection = mock
337 collection.expects(:total_pages).returns(1)
338
339 @template = '<%= will_paginate options %>'
340 @controller.controller_name = 'developers'
341 @view.assigns['developers'] = collection
342
343 paginate(nil)
344 end
345 end
346
347 def test_inferred_collection_name_raises_error_when_nil
348 @template = '<%= will_paginate options %>'
349 @controller.controller_name = 'developers'
350
351 e = assert_raise ArgumentError do
352 paginate(nil)
353 end
354 assert e.message.include?('@developers')
355 end
356
357 if ActionController::Base.respond_to? :rescue_responses
358 # only on Rails 2
359 def test_rescue_response_hook_presence
360 assert_equal :not_found,
361 ActionController::Base.rescue_responses['WillPaginate::InvalidPage']
362 end
363 end
364
365end