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
@@ -8,17 +8,23 @@ module BaseTest
8
8
  include ActionView::Helpers::UrlHelper
9
9
  include ActionView::Helpers::TagHelper
10
10
  include ActionView::Helpers::TextHelper
11
+ include ActionView::Helpers::FormTagHelper
11
12
  include ActionView::Helpers::FormHelper
12
13
  include ActionView::Helpers::CaptureHelper
13
14
 
14
15
  def setup
16
+ @template = nil
15
17
  @controller = Class.new do
16
18
  def url_for(options, *parameters_for_method_reference)
17
- url = "http://www.example.com/"
18
- url << options[:action].to_s if options and options[:action]
19
- url << "?a=#{options[:a]}" if options && options[:a]
20
- url << "&b=#{options[:b]}" if options && options[:a] && options[:b]
21
- url
19
+ if options.is_a?(String)
20
+ options
21
+ else
22
+ url = "http://www.example.com/"
23
+ url << options[:action].to_s if options and options[:action]
24
+ url << "?a=#{options[:a]}" if options && options[:a]
25
+ url << "&b=#{options[:b]}" if options && options[:a] && options[:b]
26
+ url
27
+ end
22
28
  end
23
29
  end.new
24
30
  end
@@ -61,6 +67,17 @@ class PrototypeHelperTest < Test::Unit::TestCase
61
67
  assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer',failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
62
68
  form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast })
63
69
  end
70
+
71
+ def test_form_remote_tag_with_method
72
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"><div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>),
73
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put })
74
+ end
75
+
76
+ def test_form_remote_tag_with_block
77
+ _erbout = ''
78
+ form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { _erbout.concat "Hello world!" }
79
+ assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">Hello world!</form>), _erbout
80
+ end
64
81
 
65
82
  def test_on_callbacks
66
83
  callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure]
@@ -126,30 +143,6 @@ class PrototypeHelperTest < Test::Unit::TestCase
126
143
  assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {alert('Form changed')})\n//]]>\n</script>),
127
144
  observe_form("cart", :frequency => 2, :function => "alert('Form changed')")
128
145
  end
129
-
130
- def test_update_element_function
131
- assert_equal %($('myelement').innerHTML = 'blub';\n),
132
- update_element_function('myelement', :content => 'blub')
133
- assert_equal %($('myelement').innerHTML = 'blub';\n),
134
- update_element_function('myelement', :action => :update, :content => 'blub')
135
- assert_equal %($('myelement').innerHTML = '';\n),
136
- update_element_function('myelement', :action => :empty)
137
- assert_equal %(Element.remove('myelement');\n),
138
- update_element_function('myelement', :action => :remove)
139
-
140
- assert_equal %(new Insertion.Bottom('myelement','blub');\n),
141
- update_element_function('myelement', :position => 'bottom', :content => 'blub')
142
- assert_equal %(new Insertion.Bottom('myelement','blub');\n),
143
- update_element_function('myelement', :action => :update, :position => :bottom, :content => 'blub')
144
-
145
- _erbout = ""
146
- assert_equal %($('myelement').innerHTML = 'test';\n),
147
- update_element_function('myelement') { _erbout << "test" }
148
-
149
- _erbout = ""
150
- assert_equal %($('myelement').innerHTML = 'blockstuff';\n),
151
- update_element_function('myelement', :content => 'paramstuff') { _erbout << "blockstuff" }
152
- end
153
146
 
154
147
  def test_update_page
155
148
  block = Proc.new { |page| page.replace_html('foo', 'bar') }
@@ -160,6 +153,12 @@ class PrototypeHelperTest < Test::Unit::TestCase
160
153
  block = Proc.new { |page| page.replace_html('foo', 'bar') }
161
154
  assert_equal javascript_tag(create_generator(&block).to_s), update_page_tag(&block)
162
155
  end
156
+
157
+ def test_update_page_tag_with_html_options
158
+ block = Proc.new { |page| page.replace_html('foo', 'bar') }
159
+ assert_equal javascript_tag(create_generator(&block).to_s, {:defer => 'true'}), update_page_tag({:defer => 'true'}, &block)
160
+ end
161
+
163
162
  end
164
163
 
165
164
  class JavaScriptGeneratorTest < Test::Unit::TestCase
@@ -192,7 +191,7 @@ class JavaScriptGeneratorTest < Test::Unit::TestCase
192
191
  end
193
192
 
194
193
  def test_remove
195
- assert_equal '["foo"].each(Element.remove);',
194
+ assert_equal 'Element.remove("foo");',
196
195
  @generator.remove('foo')
197
196
  assert_equal '["foo", "bar", "baz"].each(Element.remove);',
198
197
  @generator.remove('foo', 'bar', 'baz')
@@ -201,17 +200,24 @@ class JavaScriptGeneratorTest < Test::Unit::TestCase
201
200
  def test_show
202
201
  assert_equal 'Element.show("foo");',
203
202
  @generator.show('foo')
204
- assert_equal 'Element.show("foo", "bar", "baz");',
205
- @generator.show('foo', 'bar', 'baz')
203
+ assert_equal '["foo", "bar", "baz"].each(Element.show);',
204
+ @generator.show('foo', 'bar', 'baz')
206
205
  end
207
206
 
208
207
  def test_hide
209
208
  assert_equal 'Element.hide("foo");',
210
209
  @generator.hide('foo')
211
- assert_equal 'Element.hide("foo", "bar", "baz");',
212
- @generator.hide('foo', 'bar', 'baz')
210
+ assert_equal '["foo", "bar", "baz"].each(Element.hide);',
211
+ @generator.hide('foo', 'bar', 'baz')
213
212
  end
214
213
 
214
+ def test_toggle
215
+ assert_equal 'Element.toggle("foo");',
216
+ @generator.toggle('foo')
217
+ assert_equal '["foo", "bar", "baz"].each(Element.toggle);',
218
+ @generator.toggle('foo', 'bar', 'baz')
219
+ end
220
+
215
221
  def test_alert
216
222
  assert_equal 'alert("hello");', @generator.alert('hello')
217
223
  end
@@ -251,6 +257,16 @@ Element.update("baz", "<p>This is a test</p>");
251
257
  @generator['hello'].hide
252
258
  assert_equal %($("hello").hide();), @generator.to_s
253
259
  end
260
+
261
+ def test_element_proxy_variable_access
262
+ @generator['hello']['style']
263
+ assert_equal %($("hello").style;), @generator.to_s
264
+ end
265
+
266
+ def test_element_proxy_variable_access_with_assignment
267
+ @generator['hello']['style']['color'] = 'red'
268
+ assert_equal %($("hello").style.color = "red";), @generator.to_s
269
+ end
254
270
 
255
271
  def test_element_proxy_assignment
256
272
  @generator['hello'].width = 400
@@ -414,10 +430,37 @@ return (value.className == "welcome");
414
430
  ensure
415
431
  ActionView::Base.debug_rjs = false
416
432
  end
433
+
434
+ def test_literal
435
+ literal = @generator.literal("function() {}")
436
+ assert_equal "function() {}", literal.to_json
437
+ assert_equal "", @generator.to_s
438
+ end
417
439
 
418
440
  def test_class_proxy
419
441
  @generator.form.focus('my_field')
420
442
  assert_equal "Form.focus(\"my_field\");", @generator.to_s
421
443
  end
444
+
445
+ def test_call_with_block
446
+ @generator.call(:before)
447
+ @generator.call(:my_method) do |p|
448
+ p[:one].show
449
+ p[:two].hide
450
+ end
451
+ @generator.call(:in_between)
452
+ @generator.call(:my_method_with_arguments, true, "hello") do |p|
453
+ p[:three].visual_effect(:highlight)
454
+ end
455
+ assert_equal "before();\nmy_method(function() { $(\"one\").show();\n$(\"two\").hide(); });\nin_between();\nmy_method_with_arguments(true, \"hello\", function() { $(\"three\").visualEffect(\"highlight\"); });", @generator.to_s
456
+ end
457
+
458
+ def test_class_proxy_call_with_block
459
+ @generator.my_object.my_method do |p|
460
+ p[:one].show
461
+ p[:two].hide
462
+ end
463
+ assert_equal "MyObject.myMethod(function() { $(\"one\").show();\n$(\"two\").hide(); });", @generator.to_s
464
+ end
422
465
  end
423
466
 
@@ -1,19 +1,19 @@
1
1
  require File.dirname(__FILE__) + '/../abstract_unit'
2
2
 
3
- require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper'
4
- require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper'
5
-
6
3
  class TagHelperTest < Test::Unit::TestCase
7
4
  include ActionView::Helpers::TagHelper
8
5
  include ActionView::Helpers::UrlHelper
6
+ include ActionView::Helpers::TextHelper
7
+ include ActionView::Helpers::CaptureHelper
9
8
 
10
9
  def test_tag
11
- assert_equal "<p class=\"show\" />", tag("p", "class" => "show")
12
- assert_equal tag("p", "class" => "show"), tag("p", :class => "show")
10
+ assert_equal "<br />", tag("br")
11
+ assert_equal "<br clear=\"left\" />", tag(:br, :clear => "left")
12
+ assert_equal "<br>", tag("br", nil, true)
13
13
  end
14
14
 
15
15
  def test_tag_options
16
- assert_equal "<p class=\"elsewhere\" />", tag("p", "class" => "show", :class => "elsewhere")
16
+ assert_match /\A<p class="(show|elsewhere)" \/>\z/, tag("p", "class" => "show", :class => "elsewhere")
17
17
  end
18
18
 
19
19
  def test_tag_options_rejects_nil_option
@@ -35,7 +35,35 @@ class TagHelperTest < Test::Unit::TestCase
35
35
  content_tag("a", "Create", :href => "create")
36
36
  end
37
37
 
38
+ def test_content_tag_with_block
39
+ _erbout = ''
40
+ content_tag(:div) { _erbout.concat "Hello world!" }
41
+ assert_dom_equal "<div>Hello world!</div>", _erbout
42
+ end
43
+
44
+ def test_content_tag_with_block_and_options
45
+ _erbout = ''
46
+ content_tag(:div, :class => "green") { _erbout.concat "Hello world!" }
47
+ assert_dom_equal %(<div class="green">Hello world!</div>), _erbout
48
+ end
49
+
38
50
  def test_cdata_section
39
51
  assert_equal "<![CDATA[<hello world>]]>", cdata_section("<hello world>")
40
52
  end
53
+
54
+ def test_escape_once
55
+ assert_equal '1 &lt; 2 &amp; 3', escape_once('1 < 2 &amp; 3')
56
+ end
57
+
58
+ def test_double_escaping_attributes
59
+ ['1&amp;2', '1 &lt; 2', '&#8220;test&#8220;'].each do |escaped|
60
+ assert_equal %(<a href="#{escaped}" />), tag('a', :href => escaped)
61
+ end
62
+ end
63
+
64
+ def test_skip_invalid_escaped_attributes
65
+ ['&1;', '&#1dfa3;', '& #123;'].each do |escaped|
66
+ assert_equal %(<a href="#{escaped.gsub /&/, '&amp;'}" />), tag('a', :href => escaped)
67
+ end
68
+ end
41
69
  end
@@ -11,11 +11,19 @@ class TextHelperTest < Test::Unit::TestCase
11
11
  # a view is rendered. The cycle helper depends on this behavior.
12
12
  @_cycles = nil if (defined? @_cycles)
13
13
  end
14
-
14
+
15
15
  def test_simple_format
16
+ assert_equal "<p></p>", simple_format(nil)
17
+
16
18
  assert_equal "<p>crazy\n<br /> cross\n<br /> platform linebreaks</p>", simple_format("crazy\r\n cross\r platform linebreaks")
17
19
  assert_equal "<p>A paragraph</p>\n\n<p>and another one!</p>", simple_format("A paragraph\n\nand another one!")
18
20
  assert_equal "<p>A paragraph\n<br /> With a newline</p>", simple_format("A paragraph\n With a newline")
21
+
22
+ text = "A\nB\nC\nD".freeze
23
+ assert_equal "<p>A\n<br />B\n<br />C\n<br />D</p>", simple_format(text)
24
+
25
+ text = "A\r\n \nB\n\n\r\n\t\nC\nD".freeze
26
+ assert_equal "<p>A\n<br /> \n<br />B</p>\n\n<p>\t\n<br />C\n<br />D</p>", simple_format(text)
19
27
  end
20
28
 
21
29
  def test_truncate
@@ -23,33 +31,18 @@ class TextHelperTest < Test::Unit::TestCase
23
31
  assert_equal "Hello Wor...", truncate("Hello World!!", 12)
24
32
  end
25
33
 
26
- def test_truncate_multibyte_without_kcode
27
- result = execute_in_sandbox(<<-'CODE')
28
- require File.dirname(__FILE__) + '/../../activesupport/lib/active_support/core_ext/kernel'
29
- require "#{File.dirname(__FILE__)}/../lib/action_view/helpers/text_helper"
30
- include ActionView::Helpers::TextHelper
31
- truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", 10)
32
- CODE
33
-
34
- assert_equal "\354\225\210\353\205\225\355...", result
34
+ def test_truncate_multibyte
35
+ with_kcode 'none' do
36
+ assert_equal "\354\225\210\353\205\225\355...", truncate("\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224", 10)
37
+ end
38
+ with_kcode 'u' do
39
+ assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...",
40
+ truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244", 10)
41
+ end
35
42
  end
36
-
37
- def test_truncate_multibyte_with_kcode
38
- result = execute_in_sandbox(<<-'CODE')
39
- $KCODE = "u"
40
- require 'jcode'
41
-
42
- require File.dirname(__FILE__) + '/../../activesupport/lib/active_support/core_ext/kernel'
43
- require "#{File.dirname(__FILE__)}/../lib/action_view/helpers/text_helper"
44
- include ActionView::Helpers::TextHelper
45
- truncate("\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254\353\236 \354\225\204\353\235\274\353\246\254\354\230\244", 10)
46
- CODE
47
-
48
- assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254\353\236 ...", result
49
- end
50
-
43
+
51
44
  def test_strip_links
52
- assert_equal "on my mind", strip_links("<a href='almost'>on my mind</a>")
45
+ assert_equal "on my mind\nall day long", strip_links("<a href='almost'>on my mind</a>\n<A href='almost'>all day long</A>")
53
46
  end
54
47
 
55
48
  def test_highlighter
@@ -95,7 +88,6 @@ class TextHelperTest < Test::Unit::TestCase
95
88
  assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5))
96
89
  assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5))
97
90
  assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
98
- assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5))
99
91
  assert_nil excerpt("This is a beautiful morning", "day")
100
92
  end
101
93
 
@@ -103,7 +95,15 @@ class TextHelperTest < Test::Unit::TestCase
103
95
  assert_equal('...is a beautiful! morn...', excerpt('This is a beautiful! morning', 'beautiful', 5))
104
96
  assert_equal('...is a beautiful? morn...', excerpt('This is a beautiful? morning', 'beautiful', 5))
105
97
  end
106
-
98
+
99
+ def test_excerpt_with_utf8
100
+ with_kcode('u') do
101
+ assert_equal("...fficiency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8))
102
+ end
103
+ with_kcode('none') do
104
+ assert_equal("...\203ciency could not be h...", excerpt("That's why efficiency could not be helped", 'could', 8))
105
+ end
106
+ end
107
107
 
108
108
  def test_word_wrap
109
109
  assert_equal("my very very\nvery long\nstring", word_wrap("my very very very long string", 15))
@@ -112,6 +112,29 @@ class TextHelperTest < Test::Unit::TestCase
112
112
  def test_pluralization
113
113
  assert_equal("1 count", pluralize(1, "count"))
114
114
  assert_equal("2 counts", pluralize(2, "count"))
115
+ assert_equal("1 count", pluralize('1', "count"))
116
+ assert_equal("2 counts", pluralize('2', "count"))
117
+ assert_equal("1,066 counts", pluralize('1,066', "count"))
118
+ assert_equal("1.25 counts", pluralize('1.25', "count"))
119
+ assert_equal("2 counters", pluralize(2, "count", "counters"))
120
+ end
121
+
122
+ def test_auto_link_parsing
123
+ urls = %w(http://www.rubyonrails.com
124
+ http://www.rubyonrails.com:80
125
+ http://www.rubyonrails.com/~minam
126
+ https://www.rubyonrails.com/~minam
127
+ http://www.rubyonrails.com/~minam/url%20with%20spaces
128
+ http://www.rubyonrails.com/foo.cgi?something=here
129
+ http://www.rubyonrails.com/foo.cgi?something=here&and=here
130
+ http://www.rubyonrails.com/contact;new
131
+ http://www.rubyonrails.com/contact;new%20with%20spaces
132
+ http://www.rubyonrails.com/contact;new?with=query&string=params
133
+ http://www.rubyonrails.com/~minam/contact;new?with=query&string=params)
134
+
135
+ urls.each do |url|
136
+ assert_equal %(<a href="#{url}">#{url}</a>), auto_link(url)
137
+ end
115
138
  end
116
139
 
117
140
  def test_auto_linking
@@ -174,25 +197,37 @@ class TextHelperTest < Test::Unit::TestCase
174
197
  def test_sanitize_form
175
198
  raw = "<form action=\"/foo/bar\" method=\"post\"><input></form>"
176
199
  result = sanitize(raw)
177
- assert_equal "&lt;form action='/foo/bar' method='post'><input>&lt;/form>", result
200
+ assert_equal %(&lt;form action="/foo/bar" method="post"><input>&lt;/form>), result
201
+ end
202
+
203
+ def test_sanitize_plaintext
204
+ raw = "<plaintext><span>foo</span></plaintext>"
205
+ result = sanitize(raw)
206
+ assert_equal "&lt;plaintext><span>foo</span>&lt;/plaintext>", result
178
207
  end
179
208
 
180
209
  def test_sanitize_script
181
210
  raw = "<script language=\"Javascript\">blah blah blah</script>"
182
211
  result = sanitize(raw)
183
- assert_equal "&lt;script language='Javascript'>blah blah blah&lt;/script>", result
212
+ assert_equal %(&lt;script language="Javascript">blah blah blah&lt;/script>), result
184
213
  end
185
214
 
186
215
  def test_sanitize_js_handlers
187
216
  raw = %{onthis="do that" <a href="#" onclick="hello" name="foo" onbogus="remove me">hello</a>}
188
217
  result = sanitize(raw)
189
- assert_equal %{onthis="do that" <a name='foo' href='#'>hello</a>}, result
218
+ assert_equal %{onthis="do that" <a name="foo" href="#">hello</a>}, result
190
219
  end
191
220
 
192
221
  def test_sanitize_javascript_href
193
222
  raw = %{href="javascript:bang" <a href="javascript:bang" name="hello">foo</a>, <span href="javascript:bang">bar</span>}
194
223
  result = sanitize(raw)
195
- assert_equal %{href="javascript:bang" <a name='hello'>foo</a>, <span>bar</span>}, result
224
+ assert_equal %{href="javascript:bang" <a name="hello">foo</a>, <span>bar</span>}, result
225
+ end
226
+
227
+ def test_sanitize_image_src
228
+ raw = %{src="javascript:bang" <img src="javascript:bang" width="5">foo</img>, <span src="javascript:bang">bar</span>}
229
+ result = sanitize(raw)
230
+ assert_equal %{src="javascript:bang" <img width="5">foo</img>, <span>bar</span>}, result
196
231
  end
197
232
 
198
233
  def test_cycle_class
@@ -284,7 +319,7 @@ class TextHelperTest < Test::Unit::TestCase
284
319
  assert_equal(
285
320
  %{This is a test.\n\n\nIt no longer contains any HTML.\n}, strip_tags(
286
321
  %{<title>This is <b>a <a href="" target="_blank">test</a></b>.</title>\n\n<!-- it has a comment -->\n\n<p>It no <b>longer <strong>contains <em>any <strike>HTML</strike></em>.</strong></b></p>\n}))
287
- assert_equal("This has a here.", strip_tags("This has a <!-- comment --> here."))
322
+ assert_equal "This has a here.", strip_tags("This has a <!-- comment --> here.")
323
+ [nil, '', ' '].each { |blank| assert_equal blank, strip_tags(blank) }
288
324
  end
289
-
290
325
  end
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper'
4
4
  require File.dirname(__FILE__) + '/../../lib/action_view/helpers/asset_tag_helper'
5
5
  require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper'
6
6
 
7
- RequestMock = Struct.new("Request", :request_uri)
7
+ RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port)
8
8
 
9
9
  class UrlHelperTest < Test::Unit::TestCase
10
10
  include ActionView::Helpers::AssetTagHelper
@@ -13,7 +13,7 @@ class UrlHelperTest < Test::Unit::TestCase
13
13
 
14
14
  def setup
15
15
  @controller = Class.new do
16
- attr_accessor :url
16
+ attr_accessor :url, :request
17
17
  def url_for(options, *parameters_for_method_reference)
18
18
  url
19
19
  end
@@ -28,7 +28,7 @@ class UrlHelperTest < Test::Unit::TestCase
28
28
  assert_equal "http://www.example.com?a=b&amp;c=d", url_for(:a => 'b', :c => 'd', :escape => true)
29
29
  assert_equal "http://www.example.com?a=b&c=d", url_for(:a => 'b', :c => 'd', :escape => false)
30
30
  end
31
-
31
+
32
32
  # todo: missing test cases
33
33
  def test_button_to_with_straight_url
34
34
  assert_dom_equal "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com")
@@ -38,6 +38,10 @@ class UrlHelperTest < Test::Unit::TestCase
38
38
  assert_dom_equal "<form method=\"post\" action=\"http://www.example.com/q1=v1&amp;q2=v2\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com/q1=v1&q2=v2")
39
39
  end
40
40
 
41
+ def test_button_to_with_escaped_query
42
+ assert_dom_equal "<form method=\"post\" action=\"http://www.example.com/q1=v1&amp;q2=v2\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com/q1=v1&amp;q2=v2")
43
+ end
44
+
41
45
  def test_button_to_with_query_and_no_name
42
46
  assert_dom_equal "<form method=\"post\" action=\"http://www.example.com?q1=v1&amp;q2=v2\" class=\"button-to\"><div><input type=\"submit\" value=\"http://www.example.com?q1=v1&amp;q2=v2\" /></div></form>", button_to(nil, "http://www.example.com?q1=v1&q2=v2")
43
47
  end
@@ -59,6 +63,20 @@ class UrlHelperTest < Test::Unit::TestCase
59
63
  button_to("Hello", "http://www.example.com", :disabled => true)
60
64
  )
61
65
  end
66
+
67
+ def test_button_to_with_method_delete
68
+ assert_dom_equal(
69
+ "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"hidden\" name=\"_method\" value=\"delete\" /><input type=\"submit\" value=\"Hello\" /></div></form>",
70
+ button_to("Hello", "http://www.example.com", :method => :delete)
71
+ )
72
+ end
73
+
74
+ def test_button_to_with_method_get
75
+ assert_dom_equal(
76
+ "<form method=\"get\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
77
+ button_to("Hello", "http://www.example.com", :method => :get)
78
+ )
79
+ end
62
80
 
63
81
  def test_link_tag_with_straight_url
64
82
  assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com")
@@ -125,22 +143,38 @@ class UrlHelperTest < Test::Unit::TestCase
125
143
  )
126
144
  end
127
145
 
146
+ def test_link_tag_with_post_is_deprecated
147
+ assert_deprecated 'post' do
148
+ assert_dom_equal(
149
+ "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();return false;\">Hello</a>",
150
+ link_to("Hello", "http://www.example.com", :post => true)
151
+ )
152
+ end
153
+ end
154
+
128
155
  def test_link_tag_using_post_javascript
129
156
  assert_dom_equal(
130
- "<a href=\"http://www.example.com\" onclick=\"var f = document.createElement('form'); this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();return false;\">Hello</a>",
131
- link_to("Hello", "http://www.example.com", :post => true)
157
+ "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();return false;\">Hello</a>",
158
+ link_to("Hello", "http://www.example.com", :method => :post)
159
+ )
160
+ end
161
+
162
+ def test_link_tag_using_delete_javascript
163
+ assert_dom_equal(
164
+ "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;\">Destroy</a>",
165
+ link_to("Destroy", "http://www.example.com", :method => :delete)
132
166
  )
133
167
  end
134
168
 
135
169
  def test_link_tag_using_post_javascript_and_confirm
136
170
  assert_dom_equal(
137
- "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit(); };return false;\">Hello</a>",
138
- link_to("Hello", "http://www.example.com", :post => true, :confirm => "Are you serious?")
171
+ "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit(); };return false;\">Hello</a>",
172
+ link_to("Hello", "http://www.example.com", :method => :post, :confirm => "Are you serious?")
139
173
  )
140
174
  end
141
175
 
142
176
  def test_link_tag_using_post_javascript_and_popup
143
- assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :post => true, :confirm => "Are you serious?") }
177
+ assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
144
178
  end
145
179
 
146
180
  def test_link_to_unless
@@ -164,16 +198,20 @@ class UrlHelperTest < Test::Unit::TestCase
164
198
  assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1)
165
199
  end
166
200
 
167
- def xtest_link_unless_current
168
- @request = RequestMock.new("http://www.example.com")
169
- assert_equal "Showing", link_to_unless_current("Showing", :action => "show", :controller => "weblog")
170
- @request = RequestMock.new("http://www.example.org")
171
- assert "<a href=\"http://www.example.com\">Listing</a>", link_to_unless_current("Listing", :action => "list", :controller => "weblog")
201
+ def test_link_unless_current
202
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show")
203
+ @controller.url = "http://www.example.com/weblog/show"
204
+ assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
205
+ assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
172
206
 
173
- @request = RequestMock.new("http://www.example.com")
174
- assert_equal "Showing", link_to_unless_current("Showing", :action => "show", :controller => "weblog", :id => 1)
207
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show")
208
+ @controller.url = "http://www.example.com/weblog/list"
209
+ assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
210
+ link_to_unless_current("Listing", :action => "list", :controller => "weblog")
211
+ assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
212
+ link_to_unless_current("Listing", "http://www.example.com/weblog/list")
175
213
  end
176
-
214
+
177
215
  def test_mail_to
178
216
  assert_dom_equal "<a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>", mail_to("david@loudthinking.com")
179
217
  assert_dom_equal "<a href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>", mail_to("david@loudthinking.com", "David Heinemeier Hansson")
@@ -212,3 +250,117 @@ class UrlHelperTest < Test::Unit::TestCase
212
250
  assert_dom_equal "<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
213
251
  end
214
252
  end
253
+
254
+ class UrlHelperWithControllerTest < Test::Unit::TestCase
255
+ class UrlHelperController < ActionController::Base
256
+ self.template_root = "#{File.dirname(__FILE__)}/../fixtures/"
257
+
258
+ def self.controller_path; 'url_helper_with_controller' end
259
+
260
+ def show_url_for
261
+ render :inline => "<%= url_for :controller => 'url_helper_with_controller', :action => 'show_url_for' %>"
262
+ end
263
+
264
+ def show_named_route
265
+ render :inline => "<%= show_named_route_#{params[:kind]} %>"
266
+ end
267
+
268
+ def rescue_action(e) raise e end
269
+ end
270
+
271
+ include ActionView::Helpers::UrlHelper
272
+
273
+ def setup
274
+ @request = ActionController::TestRequest.new
275
+ @response = ActionController::TestResponse.new
276
+ @controller = UrlHelperController.new
277
+ end
278
+
279
+ def test_url_for_shows_only_path
280
+ get :show_url_for
281
+ assert_equal '/url_helper_with_controller/show_url_for', @response.body
282
+ end
283
+
284
+ def test_named_route_shows_host_and_path
285
+ with_url_helper_routing do
286
+ get :show_named_route, :kind => 'url'
287
+ assert_equal 'http://test.host/url_helper_with_controller/show_named_route', @response.body
288
+ end
289
+ end
290
+
291
+ def test_named_route_path_shows_only_path
292
+ with_url_helper_routing do
293
+ get :show_named_route, :kind => 'path'
294
+ assert_equal '/url_helper_with_controller/show_named_route', @response.body
295
+ end
296
+ end
297
+
298
+ protected
299
+ def with_url_helper_routing
300
+ with_routing do |set|
301
+ set.draw do |map|
302
+ map.show_named_route 'url_helper_with_controller/show_named_route', :controller => 'url_helper_with_controller', :action => 'show_named_route'
303
+ end
304
+ yield
305
+ end
306
+ end
307
+ end
308
+
309
+ class LinkToUnlessCurrentWithControllerTest < Test::Unit::TestCase
310
+ class TasksController < ActionController::Base
311
+ self.template_root = "#{File.dirname(__FILE__)}/../fixtures/"
312
+
313
+ def self.controller_path; 'tasks' end
314
+
315
+ def index
316
+ render_default
317
+ end
318
+
319
+ def show
320
+ render_default
321
+ end
322
+
323
+ def rescue_action(e) raise e end
324
+
325
+ protected
326
+ def render_default
327
+ render :inline =>
328
+ "<%= link_to_unless_current(\"tasks\", tasks_path) %>\n" +
329
+ "<%= link_to_unless_current(\"tasks\", tasks_url) %>"
330
+ end
331
+ end
332
+
333
+ include ActionView::Helpers::UrlHelper
334
+
335
+ def setup
336
+ @request = ActionController::TestRequest.new
337
+ @response = ActionController::TestResponse.new
338
+ @controller = TasksController.new
339
+ end
340
+
341
+ def test_link_to_unless_current_to_current
342
+ with_restful_routing do
343
+ get :index
344
+ assert_equal "tasks\ntasks", @response.body
345
+ end
346
+ end
347
+
348
+ def test_link_to_unless_current_shows_link
349
+ with_restful_routing do
350
+ get :show, :id => 1
351
+ assert_equal "<a href=\"/tasks\">tasks</a>\n" +
352
+ "<a href=\"#{@request.protocol}#{@request.host_with_port}/tasks\">tasks</a>",
353
+ @response.body
354
+ end
355
+ end
356
+
357
+ protected
358
+ def with_restful_routing
359
+ with_routing do |set|
360
+ set.draw do |map|
361
+ map.resources :tasks
362
+ end
363
+ yield
364
+ end
365
+ end
366
+ end