actionpack 1.12.5 → 1.13.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionpack might be problematic. Click here for more details.

Files changed (179) hide show
  1. data/CHANGELOG +517 -15
  2. data/MIT-LICENSE +1 -1
  3. data/README +18 -20
  4. data/Rakefile +7 -4
  5. data/examples/address_book_controller.rb +3 -3
  6. data/examples/blog_controller.cgi +3 -3
  7. data/examples/debate_controller.cgi +5 -5
  8. data/lib/action_controller.rb +2 -2
  9. data/lib/action_controller/assertions.rb +73 -311
  10. data/lib/action_controller/{deprecated_assertions.rb → assertions/deprecated_assertions.rb} +32 -8
  11. data/lib/action_controller/assertions/dom_assertions.rb +25 -0
  12. data/lib/action_controller/assertions/model_assertions.rb +12 -0
  13. data/lib/action_controller/assertions/response_assertions.rb +140 -0
  14. data/lib/action_controller/assertions/routing_assertions.rb +82 -0
  15. data/lib/action_controller/assertions/selector_assertions.rb +571 -0
  16. data/lib/action_controller/assertions/tag_assertions.rb +117 -0
  17. data/lib/action_controller/base.rb +334 -163
  18. data/lib/action_controller/benchmarking.rb +3 -6
  19. data/lib/action_controller/caching.rb +83 -22
  20. data/lib/action_controller/cgi_ext/cgi_ext.rb +0 -7
  21. data/lib/action_controller/cgi_ext/cgi_methods.rb +167 -173
  22. data/lib/action_controller/cgi_ext/raw_post_data_fix.rb +43 -22
  23. data/lib/action_controller/cgi_process.rb +50 -27
  24. data/lib/action_controller/components.rb +21 -25
  25. data/lib/action_controller/cookies.rb +10 -9
  26. data/lib/action_controller/{dependencies.rb → deprecated_dependencies.rb} +9 -27
  27. data/lib/action_controller/filters.rb +448 -225
  28. data/lib/action_controller/flash.rb +24 -20
  29. data/lib/action_controller/helpers.rb +2 -5
  30. data/lib/action_controller/integration.rb +40 -16
  31. data/lib/action_controller/layout.rb +11 -8
  32. data/lib/action_controller/macros/auto_complete.rb +3 -2
  33. data/lib/action_controller/macros/in_place_editing.rb +3 -2
  34. data/lib/action_controller/mime_responds.rb +41 -29
  35. data/lib/action_controller/mime_type.rb +68 -10
  36. data/lib/action_controller/pagination.rb +4 -3
  37. data/lib/action_controller/request.rb +22 -14
  38. data/lib/action_controller/rescue.rb +25 -22
  39. data/lib/action_controller/resources.rb +302 -0
  40. data/lib/action_controller/response.rb +20 -2
  41. data/lib/action_controller/response.rb.rej +17 -0
  42. data/lib/action_controller/routing.rb +1165 -567
  43. data/lib/action_controller/scaffolding.rb +30 -31
  44. data/lib/action_controller/session/active_record_store.rb +2 -0
  45. data/lib/action_controller/session/drb_store.rb +4 -0
  46. data/lib/action_controller/session/mem_cache_store.rb +4 -0
  47. data/lib/action_controller/session_management.rb +6 -9
  48. data/lib/action_controller/status_codes.rb +89 -0
  49. data/lib/action_controller/streaming.rb +6 -15
  50. data/lib/action_controller/templates/rescues/_request_and_response.rhtml +5 -5
  51. data/lib/action_controller/templates/rescues/diagnostics.rhtml +2 -2
  52. data/lib/action_controller/templates/rescues/routing_error.rhtml +4 -4
  53. data/lib/action_controller/templates/rescues/template_error.rhtml +1 -1
  54. data/lib/action_controller/templates/scaffolds/list.rhtml +1 -1
  55. data/lib/action_controller/test_process.rb +52 -30
  56. data/lib/action_controller/url_rewriter.rb +63 -29
  57. data/lib/action_controller/vendor/html-scanner/html/document.rb +1 -0
  58. data/lib/action_controller/vendor/html-scanner/html/node.rb +3 -4
  59. data/lib/action_controller/vendor/html-scanner/html/selector.rb +822 -0
  60. data/lib/action_controller/verification.rb +22 -11
  61. data/lib/action_pack.rb +1 -1
  62. data/lib/action_pack/version.rb +2 -2
  63. data/lib/action_view.rb +1 -1
  64. data/lib/action_view/base.rb +46 -43
  65. data/lib/action_view/compiled_templates.rb +1 -1
  66. data/lib/action_view/helpers/active_record_helper.rb +54 -17
  67. data/lib/action_view/helpers/asset_tag_helper.rb +97 -46
  68. data/lib/action_view/helpers/capture_helper.rb +1 -1
  69. data/lib/action_view/helpers/date_helper.rb +258 -136
  70. data/lib/action_view/helpers/debug_helper.rb +1 -1
  71. data/lib/action_view/helpers/deprecated_helper.rb +34 -0
  72. data/lib/action_view/helpers/form_helper.rb +75 -35
  73. data/lib/action_view/helpers/form_options_helper.rb +7 -5
  74. data/lib/action_view/helpers/form_tag_helper.rb +44 -6
  75. data/lib/action_view/helpers/java_script_macros_helper.rb +59 -46
  76. data/lib/action_view/helpers/javascript_helper.rb +71 -10
  77. data/lib/action_view/helpers/javascripts/controls.js +41 -23
  78. data/lib/action_view/helpers/javascripts/dragdrop.js +105 -76
  79. data/lib/action_view/helpers/javascripts/effects.js +293 -163
  80. data/lib/action_view/helpers/javascripts/prototype.js +897 -389
  81. data/lib/action_view/helpers/javascripts/prototype.js.rej +561 -0
  82. data/lib/action_view/helpers/number_helper.rb +111 -65
  83. data/lib/action_view/helpers/prototype_helper.rb +84 -109
  84. data/lib/action_view/helpers/scriptaculous_helper.rb +5 -0
  85. data/lib/action_view/helpers/tag_helper.rb +69 -16
  86. data/lib/action_view/helpers/text_helper.rb +149 -112
  87. data/lib/action_view/helpers/url_helper.rb +200 -107
  88. data/lib/action_view/template_error.rb +66 -42
  89. data/test/abstract_unit.rb +4 -2
  90. data/test/active_record_unit.rb +84 -56
  91. data/test/activerecord/active_record_assertions_test.rb +26 -18
  92. data/test/activerecord/active_record_store_test.rb +4 -36
  93. data/test/activerecord/pagination_test.rb +1 -6
  94. data/test/controller/action_pack_assertions_test.rb +230 -113
  95. data/test/controller/addresses_render_test.rb +2 -6
  96. data/test/controller/assert_select_test.rb +576 -0
  97. data/test/controller/base_test.rb +73 -3
  98. data/test/controller/caching_test.rb +228 -0
  99. data/test/controller/capture_test.rb +12 -10
  100. data/test/controller/cgi_test.rb +89 -12
  101. data/test/controller/components_test.rb +24 -2
  102. data/test/controller/content_type_test.rb +139 -0
  103. data/test/controller/controller_fixtures/app/controllers/admin/user_controller.rb +0 -0
  104. data/test/controller/controller_fixtures/app/controllers/user_controller.rb +0 -0
  105. data/test/controller/controller_fixtures/vendor/plugins/bad_plugin/lib/plugin_controller.rb +0 -0
  106. data/test/controller/cookie_test.rb +33 -25
  107. data/test/controller/deprecated_instance_variables_test.rb +48 -0
  108. data/test/controller/deprecation/deprecated_base_methods_test.rb +60 -0
  109. data/test/controller/fake_controllers.rb +0 -1
  110. data/test/controller/filters_test.rb +301 -16
  111. data/test/controller/flash_test.rb +19 -2
  112. data/test/controller/helper_test.rb +2 -2
  113. data/test/controller/integration_test.rb +154 -0
  114. data/test/controller/layout_test.rb +115 -1
  115. data/test/controller/mime_responds_test.rb +94 -0
  116. data/test/controller/mime_type_test.rb +9 -0
  117. data/test/controller/new_render_test.rb +161 -11
  118. data/test/controller/raw_post_test.rb +52 -15
  119. data/test/controller/redirect_test.rb +27 -14
  120. data/test/controller/render_test.rb +76 -29
  121. data/test/controller/request_test.rb +55 -4
  122. data/test/controller/resources_test.rb +274 -0
  123. data/test/controller/routing_test.rb +1533 -824
  124. data/test/controller/selector_test.rb +628 -0
  125. data/test/controller/send_file_test.rb +9 -1
  126. data/test/controller/session_management_test.rb +51 -0
  127. data/test/controller/test_test.rb +113 -29
  128. data/test/controller/url_rewriter_test.rb +86 -17
  129. data/test/controller/verification_test.rb +19 -17
  130. data/test/controller/webservice_test.rb +0 -7
  131. data/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml +1 -0
  132. data/test/fixtures/content_type/render_default_for_rhtml.rhtml +1 -0
  133. data/test/fixtures/content_type/render_default_for_rjs.rjs +1 -0
  134. data/test/fixtures/content_type/render_default_for_rxml.rxml +1 -0
  135. data/test/fixtures/deprecated_instance_variables/_cookies_ivar.rhtml +1 -0
  136. data/test/fixtures/deprecated_instance_variables/_cookies_method.rhtml +1 -0
  137. data/test/fixtures/deprecated_instance_variables/_flash_ivar.rhtml +1 -0
  138. data/test/fixtures/deprecated_instance_variables/_flash_method.rhtml +1 -0
  139. data/test/fixtures/deprecated_instance_variables/_headers_ivar.rhtml +1 -0
  140. data/test/fixtures/deprecated_instance_variables/_headers_method.rhtml +1 -0
  141. data/test/fixtures/deprecated_instance_variables/_params_ivar.rhtml +1 -0
  142. data/test/fixtures/deprecated_instance_variables/_params_method.rhtml +1 -0
  143. data/test/fixtures/deprecated_instance_variables/_request_ivar.rhtml +1 -0
  144. data/test/fixtures/deprecated_instance_variables/_request_method.rhtml +1 -0
  145. data/test/fixtures/deprecated_instance_variables/_response_ivar.rhtml +1 -0
  146. data/test/fixtures/deprecated_instance_variables/_response_method.rhtml +1 -0
  147. data/test/fixtures/deprecated_instance_variables/_session_ivar.rhtml +1 -0
  148. data/test/fixtures/deprecated_instance_variables/_session_method.rhtml +1 -0
  149. data/test/fixtures/multipart/binary_file +0 -0
  150. data/test/fixtures/public/javascripts/application.js +1 -0
  151. data/test/fixtures/test/_hello.rxml +1 -0
  152. data/test/fixtures/test/hello_world_container.rxml +3 -0
  153. data/test/fixtures/topic.rb +2 -2
  154. data/test/template/active_record_helper_test.rb +83 -12
  155. data/test/template/asset_tag_helper_test.rb +75 -95
  156. data/test/template/compiled_templates_test.rb +1 -0
  157. data/test/template/date_helper_test.rb +873 -181
  158. data/test/template/deprecated_helper_test.rb +36 -0
  159. data/test/template/deprecated_instance_variables_test.rb +43 -0
  160. data/test/template/form_helper_test.rb +77 -1
  161. data/test/template/form_options_helper_test.rb +4 -0
  162. data/test/template/form_tag_helper_test.rb +66 -2
  163. data/test/template/java_script_macros_helper_test.rb +4 -1
  164. data/test/template/javascript_helper_test.rb +29 -0
  165. data/test/template/number_helper_test.rb +63 -27
  166. data/test/template/prototype_helper_test.rb +77 -34
  167. data/test/template/tag_helper_test.rb +34 -6
  168. data/test/template/text_helper_test.rb +69 -34
  169. data/test/template/url_helper_test.rb +168 -16
  170. data/test/testing_sandbox.rb +7 -22
  171. metadata +66 -20
  172. data/filler.txt +0 -50
  173. data/lib/action_controller/code_generation.rb +0 -235
  174. data/lib/action_controller/vendor/xml_simple.rb +0 -1019
  175. data/test/controller/caching_filestore.rb +0 -74
  176. data/test/fixtures/application_root/app/controllers/a_class_that_contains_a_controller/poorly_placed_controller.rb +0 -7
  177. data/test/fixtures/application_root/app/controllers/module_that_holds_controllers/nested_controller.rb +0 -3
  178. data/test/fixtures/application_root/app/models/a_class_that_contains_a_controller.rb +0 -7
  179. data/test/fixtures/dont_load.rb +0 -3
@@ -0,0 +1,571 @@
1
+ #--
2
+ # Copyright (c) 2006 Assaf Arkin (http://labnotes.org)
3
+ # Under MIT and/or CC By license.
4
+ #++
5
+
6
+ require 'rexml/document'
7
+ require File.dirname(__FILE__) + "/../vendor/html-scanner/html/document"
8
+
9
+ module ActionController
10
+ module Assertions
11
+ unless const_defined?(:NO_STRIP)
12
+ NO_STRIP = %w{pre script style textarea}
13
+ end
14
+
15
+ # Adds the #assert_select method for use in Rails functional
16
+ # test cases.
17
+ #
18
+ # Use #assert_select to make assertions on the response HTML of a controller
19
+ # action. You can also call #assert_select within another #assert_select to
20
+ # make assertions on elements selected by the enclosing assertion.
21
+ #
22
+ # Use #css_select to select elements without making an assertions, either
23
+ # from the response HTML or elements selected by the enclosing assertion.
24
+ #
25
+ # In addition to HTML responses, you can make the following assertions:
26
+ # * #assert_select_rjs -- Assertions on HTML content of RJS update and
27
+ # insertion operations.
28
+ # * #assert_select_encoded -- Assertions on HTML encoded inside XML,
29
+ # for example for dealing with feed item descriptions.
30
+ # * #assert_select_email -- Assertions on the HTML body of an e-mail.
31
+ #
32
+ # Also see HTML::Selector for learning how to use selectors.
33
+ module SelectorAssertions
34
+ # :call-seq:
35
+ # css_select(selector) => array
36
+ # css_select(element, selector) => array
37
+ #
38
+ # Select and return all matching elements.
39
+ #
40
+ # If called with a single argument, uses that argument as a selector
41
+ # to match all elements of the current page. Returns an empty array
42
+ # if no match is found.
43
+ #
44
+ # If called with two arguments, uses the first argument as the base
45
+ # element and the second argument as the selector. Attempts to match the
46
+ # base element and any of its children. Returns an empty array if no
47
+ # match is found.
48
+ #
49
+ # The selector may be a CSS selector expression (+String+), an expression
50
+ # with substitution values (+Array+) or an HTML::Selector object.
51
+ #
52
+ # For example:
53
+ # forms = css_select("form")
54
+ # forms.each do |form|
55
+ # inputs = css_select(form, "input")
56
+ # ...
57
+ # end
58
+ def css_select(*args)
59
+ # See assert_select to understand what's going on here.
60
+ arg = args.shift
61
+
62
+ if arg.is_a?(HTML::Node)
63
+ root = arg
64
+ arg = args.shift
65
+ elsif arg == nil
66
+ raise ArgumentError, "First argument is either selector or element to select, but nil found. Perhaps you called assert_select with an element that does not exist?"
67
+ elsif @selected
68
+ matches = []
69
+ @selected.each do |selected|
70
+ subset = css_select(selected, HTML::Selector.new(arg.dup, args.dup))
71
+ subset.each do |match|
72
+ matches << match unless matches.any? { |m| m.equal?(match) }
73
+ end
74
+ end
75
+
76
+ return matches
77
+ else
78
+ root = response_from_page_or_rjs
79
+ end
80
+
81
+ case arg
82
+ when String
83
+ selector = HTML::Selector.new(arg, args)
84
+ when Array
85
+ selector = HTML::Selector.new(*arg)
86
+ when HTML::Selector
87
+ selector = arg
88
+ else raise ArgumentError, "Expecting a selector as the first argument"
89
+ end
90
+
91
+ selector.select(root)
92
+ end
93
+
94
+ # :call-seq:
95
+ # assert_select(selector, equality?, message?)
96
+ # assert_select(element, selector, equality?, message?)
97
+ #
98
+ # An assertion that selects elements and makes one or more equality tests.
99
+ #
100
+ # If the first argument is an element, selects all matching elements
101
+ # starting from (and including) that element and all its children in
102
+ # depth-first order.
103
+ #
104
+ # If no element if specified, calling #assert_select will select from the
105
+ # response HTML. Calling #assert_select inside an #assert_select block will
106
+ # run the assertion for each element selected by the enclosing assertion.
107
+ #
108
+ # For example:
109
+ # assert_select "ol>li" do |elements|
110
+ # elements.each do |element|
111
+ # assert_select element, "li"
112
+ # end
113
+ # end
114
+ # Or for short:
115
+ # assert_select "ol>li" do
116
+ # assert_select "li"
117
+ # end
118
+ #
119
+ # The selector may be a CSS selector expression (+String+), an expression
120
+ # with substitution values, or an HTML::Selector object.
121
+ #
122
+ # === Equality Tests
123
+ #
124
+ # The equality test may be one of the following:
125
+ # * <tt>true</tt> -- Assertion is true if at least one element selected.
126
+ # * <tt>false</tt> -- Assertion is true if no element selected.
127
+ # * <tt>String/Regexp</tt> -- Assertion is true if the text value of at least
128
+ # one element matches the string or regular expression.
129
+ # * <tt>Integer</tt> -- Assertion is true if exactly that number of
130
+ # elements are selected.
131
+ # * <tt>Range</tt> -- Assertion is true if the number of selected
132
+ # elements fit the range.
133
+ # If no equality test specified, the assertion is true if at least one
134
+ # element selected.
135
+ #
136
+ # To perform more than one equality tests, use a hash with the following keys:
137
+ # * <tt>:text</tt> -- Narrow the selection to elements that have this text
138
+ # value (string or regexp).
139
+ # * <tt>:html</tt> -- Narrow the selection to elements that have this HTML
140
+ # content (string or regexp).
141
+ # * <tt>:count</tt> -- Assertion is true if the number of selected elements
142
+ # is equal to this value.
143
+ # * <tt>:minimum</tt> -- Assertion is true if the number of selected
144
+ # elements is at least this value.
145
+ # * <tt>:maximum</tt> -- Assertion is true if the number of selected
146
+ # elements is at most this value.
147
+ #
148
+ # If the method is called with a block, once all equality tests are
149
+ # evaluated the block is called with an array of all matched elements.
150
+ #
151
+ # === Examples
152
+ #
153
+ # # At least one form element
154
+ # assert_select "form"
155
+ #
156
+ # # Form element includes four input fields
157
+ # assert_select "form input", 4
158
+ #
159
+ # # Page title is "Welcome"
160
+ # assert_select "title", "Welcome"
161
+ #
162
+ # # Page title is "Welcome" and there is only one title element
163
+ # assert_select "title", {:count=>1, :text=>"Welcome"},
164
+ # "Wrong title or more than one title element"
165
+ #
166
+ # # Page contains no forms
167
+ # assert_select "form", false, "This page must contain no forms"
168
+ #
169
+ # # Test the content and style
170
+ # assert_select "body div.header ul.menu"
171
+ #
172
+ # # Use substitution values
173
+ # assert_select "ol>li#?", /item-\d+/
174
+ #
175
+ # # All input fields in the form have a name
176
+ # assert_select "form input" do
177
+ # assert_select "[name=?]", /.+/ # Not empty
178
+ # end
179
+ def assert_select(*args, &block)
180
+ # Start with optional element followed by mandatory selector.
181
+ arg = args.shift
182
+
183
+ if arg.is_a?(HTML::Node)
184
+ # First argument is a node (tag or text, but also HTML root),
185
+ # so we know what we're selecting from.
186
+ root = arg
187
+ arg = args.shift
188
+ elsif arg == nil
189
+ # This usually happens when passing a node/element that
190
+ # happens to be nil.
191
+ raise ArgumentError, "First argument is either selector or element to select, but nil found. Perhaps you called assert_select with an element that does not exist?"
192
+ elsif @selected
193
+ root = HTML::Node.new(nil)
194
+ root.children.concat @selected
195
+ else
196
+ # Otherwise just operate on the response document.
197
+ root = response_from_page_or_rjs
198
+ end
199
+
200
+ # First or second argument is the selector: string and we pass
201
+ # all remaining arguments. Array and we pass the argument. Also
202
+ # accepts selector itself.
203
+ case arg
204
+ when String
205
+ selector = HTML::Selector.new(arg, args)
206
+ when Array
207
+ selector = HTML::Selector.new(*arg)
208
+ when HTML::Selector
209
+ selector = arg
210
+ else raise ArgumentError, "Expecting a selector as the first argument"
211
+ end
212
+
213
+ # Next argument is used for equality tests.
214
+ equals = {}
215
+ case arg = args.shift
216
+ when Hash
217
+ equals = arg
218
+ when String, Regexp
219
+ equals[:text] = arg
220
+ when Integer
221
+ equals[:count] = arg
222
+ when Range
223
+ equals[:minimum] = arg.begin
224
+ equals[:maximum] = arg.end
225
+ when FalseClass
226
+ equals[:count] = 0
227
+ when NilClass, TrueClass
228
+ equals[:minimum] = 1
229
+ else raise ArgumentError, "I don't understand what you're trying to match"
230
+ end
231
+
232
+ # By default we're looking for at least one match.
233
+ if equals[:count]
234
+ equals[:minimum] = equals[:maximum] = equals[:count]
235
+ else
236
+ equals[:minimum] = 1 unless equals[:minimum]
237
+ end
238
+
239
+ # Last argument is the message we use if the assertion fails.
240
+ message = args.shift
241
+ #- message = "No match made with selector #{selector.inspect}" unless message
242
+ if args.shift
243
+ raise ArgumentError, "Not expecting that last argument, you either have too many arguments, or they're the wrong type"
244
+ end
245
+
246
+ matches = selector.select(root)
247
+ # If text/html, narrow down to those elements that match it.
248
+ content_mismatch = nil
249
+ if match_with = equals[:text]
250
+ matches.delete_if do |match|
251
+ text = ""
252
+ stack = match.children.reverse
253
+ while node = stack.pop
254
+ if node.tag?
255
+ stack.concat node.children.reverse
256
+ else
257
+ text << node.content
258
+ end
259
+ end
260
+ text.strip! unless NO_STRIP.include?(match.name)
261
+ unless match_with.is_a?(Regexp) ? (text =~ match_with) : (text == match_with.to_s)
262
+ content_mismatch ||= build_message(message, "<?> expected but was\n<?>.", match_with, text)
263
+ true
264
+ end
265
+ end
266
+ elsif match_with = equals[:html]
267
+ matches.delete_if do |match|
268
+ html = match.children.map(&:to_s).join
269
+ html.strip! unless NO_STRIP.include?(match.name)
270
+ unless match_with.is_a?(Regexp) ? (html =~ match_with) : (html == match_with.to_s)
271
+ content_mismatch ||= build_message(message, "<?> expected but was\n<?>.", match_with, html)
272
+ true
273
+ end
274
+ end
275
+ end
276
+ # Expecting foo found bar element only if found zero, not if
277
+ # found one but expecting two.
278
+ message ||= content_mismatch if matches.empty?
279
+ # Test minimum/maximum occurrence.
280
+ if equals[:minimum]
281
+ assert matches.size >= equals[:minimum], message ||
282
+ "Expected at least #{equals[:minimum]} elements, found #{matches.size}."
283
+ end
284
+ if equals[:maximum]
285
+ assert matches.size <= equals[:maximum], message ||
286
+ "Expected at most #{equals[:maximum]} elements, found #{matches.size}."
287
+ end
288
+
289
+ # If a block is given call that block. Set @selected to allow
290
+ # nested assert_select, which can be nested several levels deep.
291
+ if block_given? && !matches.empty?
292
+ begin
293
+ in_scope, @selected = @selected, matches
294
+ yield matches
295
+ ensure
296
+ @selected = in_scope
297
+ end
298
+ end
299
+
300
+ # Returns all matches elements.
301
+ matches
302
+ end
303
+
304
+ # :call-seq:
305
+ # assert_select_rjs(id?) { |elements| ... }
306
+ # assert_select_rjs(statement, id?) { |elements| ... }
307
+ # assert_select_rjs(:insert, position, id?) { |elements| ... }
308
+ #
309
+ # Selects content from the RJS response.
310
+ #
311
+ # === Narrowing down
312
+ #
313
+ # With no arguments, asserts that one or more elements are updated or
314
+ # inserted by RJS statements.
315
+ #
316
+ # Use the +id+ argument to narrow down the assertion to only statements
317
+ # that update or insert an element with that identifier.
318
+ #
319
+ # Use the first argument to narrow down assertions to only statements
320
+ # of that type. Possible values are +:replace+, +:replace_html+ and
321
+ # +:insert_html+.
322
+ #
323
+ # Use the argument +:insert+ followed by an insertion position to narrow
324
+ # down the assertion to only statements that insert elements in that
325
+ # position. Possible values are +:top+, +:bottom+, +:before+ and +:after+.
326
+ #
327
+ # === Using blocks
328
+ #
329
+ # Without a block, #assert_select_rjs merely asserts that the response
330
+ # contains one or more RJS statements that replace or update content.
331
+ #
332
+ # With a block, #assert_select_rjs also selects all elements used in
333
+ # these statements and passes them to the block. Nested assertions are
334
+ # supported.
335
+ #
336
+ # Calling #assert_select_rjs with no arguments and using nested asserts
337
+ # asserts that the HTML content is returned by one or more RJS statements.
338
+ # Using #assert_select directly makes the same assertion on the content,
339
+ # but without distinguishing whether the content is returned in an HTML
340
+ # or JavaScript.
341
+ #
342
+ # === Examples
343
+ #
344
+ # # Replacing the element foo.
345
+ # # page.replace 'foo', ...
346
+ # assert_select_rjs :replace, "foo"
347
+ #
348
+ # # Replacing with the chained RJS proxy.
349
+ # # page[:foo].replace ...
350
+ # assert_select_rjs :chained_replace, 'foo'
351
+ #
352
+ # # Inserting into the element bar, top position.
353
+ # assert_select_rjs :insert, :top, "bar"
354
+ #
355
+ # # Changing the element foo, with an image.
356
+ # assert_select_rjs "foo" do
357
+ # assert_select "img[src=/images/logo.gif""
358
+ # end
359
+ #
360
+ # # RJS inserts or updates a list with four items.
361
+ # assert_select_rjs do
362
+ # assert_select "ol>li", 4
363
+ # end
364
+ #
365
+ # # The same, but shorter.
366
+ # assert_select "ol>li", 4
367
+ def assert_select_rjs(*args, &block)
368
+ rjs_type = nil
369
+ arg = args.shift
370
+
371
+ # If the first argument is a symbol, it's the type of RJS statement we're looking
372
+ # for (update, replace, insertion, etc). Otherwise, we're looking for just about
373
+ # any RJS statement.
374
+ if arg.is_a?(Symbol)
375
+ rjs_type = arg
376
+ if rjs_type == :insert
377
+ arg = args.shift
378
+ insertion = "insert_#{arg}".to_sym
379
+ raise ArgumentError, "Unknown RJS insertion type #{arg}" unless RJS_STATEMENTS[insertion]
380
+ statement = "(#{RJS_STATEMENTS[insertion]})"
381
+ else
382
+ raise ArgumentError, "Unknown RJS statement type #{rjs_type}" unless RJS_STATEMENTS[rjs_type]
383
+ statement = "(#{RJS_STATEMENTS[rjs_type]})"
384
+ end
385
+ arg = args.shift
386
+ else
387
+ statement = "#{RJS_STATEMENTS[:any]}"
388
+ end
389
+
390
+ # Next argument we're looking for is the element identifier. If missing, we pick
391
+ # any element.
392
+ if arg.is_a?(String)
393
+ id = Regexp.quote(arg)
394
+ arg = args.shift
395
+ else
396
+ id = "[^\"]*"
397
+ end
398
+
399
+ pattern =
400
+ case rjs_type
401
+ when :chained_replace, :chained_replace_html
402
+ Regexp.new("\\$\\(\"#{id}\"\\)#{statement}\\(#{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)
403
+ else
404
+ Regexp.new("#{statement}\\(\"#{id}\", #{RJS_PATTERN_HTML}\\)", Regexp::MULTILINE)
405
+ end
406
+
407
+ # Duplicate the body since the next step involves destroying it.
408
+ matches = nil
409
+ @response.body.gsub(pattern) do |match|
410
+ html = unescape_rjs($2)
411
+ matches ||= []
412
+ matches.concat HTML::Document.new(html).root.children.select { |n| n.tag? }
413
+ ""
414
+ end
415
+ if matches
416
+ if block_given?
417
+ begin
418
+ in_scope, @selected = @selected, matches
419
+ yield matches
420
+ ensure
421
+ @selected = in_scope
422
+ end
423
+ end
424
+ matches
425
+ else
426
+ # RJS statement not found.
427
+ flunk args.shift || "No RJS statement that replaces or inserts HTML content."
428
+ end
429
+ end
430
+
431
+ # :call-seq:
432
+ # assert_select_encoded(element?) { |elements| ... }
433
+ #
434
+ # Extracts the content of an element, treats it as encoded HTML and runs
435
+ # nested assertion on it.
436
+ #
437
+ # You typically call this method within another assertion to operate on
438
+ # all currently selected elements. You can also pass an element or array
439
+ # of elements.
440
+ #
441
+ # The content of each element is un-encoded, and wrapped in the root
442
+ # element +encoded+. It then calls the block with all un-encoded elements.
443
+ #
444
+ # === Example
445
+ #
446
+ # assert_select_feed :rss, 2.0 do
447
+ # # Select description element of each feed item.
448
+ # assert_select "channel>item>description" do
449
+ # # Run assertions on the encoded elements.
450
+ # assert_select_encoded do
451
+ # assert_select "p"
452
+ # end
453
+ # end
454
+ # end
455
+ def assert_select_encoded(element = nil, &block)
456
+ case element
457
+ when Array
458
+ elements = element
459
+ when HTML::Node
460
+ elements = [element]
461
+ when nil
462
+ unless elements = @selected
463
+ raise ArgumentError, "First argument is optional, but must be called from a nested assert_select"
464
+ end
465
+ else
466
+ raise ArgumentError, "Argument is optional, and may be node or array of nodes"
467
+ end
468
+
469
+ fix_content = lambda do |node|
470
+ # Gets around a bug in the Rails 1.1 HTML parser.
471
+ node.content.gsub(/<!\[CDATA\[(.*)(\]\]>)?/m) { CGI.escapeHTML($1) }
472
+ end
473
+
474
+ selected = elements.map do |element|
475
+ text = element.children.select{ |c| not c.tag? }.map{ |c| fix_content[c] }.join
476
+ root = HTML::Document.new(CGI.unescapeHTML("<encoded>#{text}</encoded>")).root
477
+ css_select(root, "encoded:root", &block)[0]
478
+ end
479
+
480
+ begin
481
+ old_selected, @selected = @selected, selected
482
+ assert_select ":root", &block
483
+ ensure
484
+ @selected = old_selected
485
+ end
486
+ end
487
+
488
+ # :call-seq:
489
+ # assert_select_email { }
490
+ #
491
+ # Extracts the body of an email and runs nested assertions on it.
492
+ #
493
+ # You must enable deliveries for this assertion to work, use:
494
+ # ActionMailer::Base.perform_deliveries = true
495
+ #
496
+ # === Example
497
+ #
498
+ # assert_select_email do
499
+ # assert_select "h1", "Email alert"
500
+ # end
501
+ def assert_select_email(&block)
502
+ deliveries = ActionMailer::Base.deliveries
503
+ assert !deliveries.empty?, "No e-mail in delivery list"
504
+
505
+ for delivery in deliveries
506
+ for part in delivery.parts
507
+ if part["Content-Type"].to_s =~ /^text\/html\W/
508
+ root = HTML::Document.new(part.body).root
509
+ assert_select root, ":root", &block
510
+ end
511
+ end
512
+ end
513
+ end
514
+
515
+ protected
516
+ unless const_defined?(:RJS_STATEMENTS)
517
+ RJS_STATEMENTS = {
518
+ :replace => /Element\.replace/,
519
+ :replace_html => /Element\.update/,
520
+ :chained_replace => /\.replace/,
521
+ :chained_replace_html => /\.update/,
522
+ }
523
+ RJS_INSERTIONS = [:top, :bottom, :before, :after]
524
+ RJS_INSERTIONS.each do |insertion|
525
+ RJS_STATEMENTS["insert_#{insertion}".to_sym] = Regexp.new(Regexp.quote("new Insertion.#{insertion.to_s.camelize}"))
526
+ end
527
+ RJS_STATEMENTS[:any] = Regexp.new("(#{RJS_STATEMENTS.values.join('|')})")
528
+ RJS_STATEMENTS[:insert_html] = Regexp.new(RJS_INSERTIONS.collect do |insertion|
529
+ Regexp.quote("new Insertion.#{insertion.to_s.camelize}")
530
+ end.join('|'))
531
+ RJS_PATTERN_HTML = /"((\\"|[^"])*)"/
532
+ RJS_PATTERN_EVERYTHING = Regexp.new("#{RJS_STATEMENTS[:any]}\\(\"([^\"]*)\", #{RJS_PATTERN_HTML}\\)",
533
+ Regexp::MULTILINE)
534
+ RJS_PATTERN_UNICODE_ESCAPED_CHAR = /\\u([0-9a-zA-Z]{4})/
535
+ end
536
+
537
+ # #assert_select and #css_select call this to obtain the content in the HTML
538
+ # page, or from all the RJS statements, depending on the type of response.
539
+ def response_from_page_or_rjs()
540
+ content_type = @response.headers["Content-Type"]
541
+ if content_type && content_type =~ /text\/javascript/
542
+ body = @response.body.dup
543
+ root = HTML::Node.new(nil)
544
+ while true
545
+ next if body.sub!(RJS_PATTERN_EVERYTHING) do |match|
546
+ html = unescape_rjs($3)
547
+ matches = HTML::Document.new(html).root.children.select { |n| n.tag? }
548
+ root.children.concat matches
549
+ ""
550
+ end
551
+ break
552
+ end
553
+ root
554
+ else
555
+ html_document.root
556
+ end
557
+ end
558
+
559
+ # Unescapes a RJS string.
560
+ def unescape_rjs(rjs_string)
561
+ # RJS encodes double quotes and line breaks.
562
+ unescaped= rjs_string.gsub('\"', '"')
563
+ unescaped.gsub!('\n', "\n")
564
+ # RJS encodes non-ascii characters.
565
+ unescaped.gsub!(RJS_PATTERN_UNICODE_ESCAPED_CHAR) {|u| [$1.hex].pack('U*')}
566
+ unescaped
567
+ end
568
+
569
+ end
570
+ end
571
+ end