PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/rails/actionpack/test/template/form_helper_test.rb

http://github.com/benburkert/cruisecontrolrb
Ruby | 499 lines | 417 code | 81 blank | 1 comment | 1 complexity | 7902de90ec6c2d9018f5027d72a80ae2 MD5 | raw file
Possible License(s): Apache-2.0
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. class FormHelperTest < Test::Unit::TestCase
  3. include ActionView::Helpers::FormHelper
  4. include ActionView::Helpers::FormTagHelper
  5. include ActionView::Helpers::UrlHelper
  6. include ActionView::Helpers::TagHelper
  7. include ActionView::Helpers::TextHelper
  8. silence_warnings do
  9. Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost)
  10. Post.class_eval do
  11. alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
  12. alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
  13. alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
  14. end
  15. end
  16. def setup
  17. @post = Post.new
  18. def @post.errors() Class.new{ def on(field) field == "author_name" end }.new end
  19. def @post.id; 123; end
  20. def @post.id_before_type_cast; 123; end
  21. @post.title = "Hello World"
  22. @post.author_name = ""
  23. @post.body = "Back to the hill and over it again!"
  24. @post.secret = 1
  25. @post.written_on = Date.new(2004, 6, 15)
  26. @controller = Class.new do
  27. attr_reader :url_for_options
  28. def url_for(options, *parameters_for_method_reference)
  29. @url_for_options = options
  30. "http://www.example.com"
  31. end
  32. end
  33. @controller = @controller.new
  34. end
  35. def test_text_field
  36. assert_dom_equal(
  37. '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
  38. )
  39. assert_dom_equal(
  40. '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
  41. )
  42. assert_dom_equal(
  43. '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
  44. )
  45. end
  46. def test_text_field_with_escapes
  47. @post.title = "<b>Hello World</b>"
  48. assert_dom_equal(
  49. '<input id="post_title" name="post[title]" size="30" type="text" value="&lt;b&gt;Hello World&lt;/b&gt;" />', text_field("post", "title")
  50. )
  51. end
  52. def test_text_field_with_options
  53. expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
  54. assert_dom_equal expected, text_field("post", "title", "size" => 35)
  55. assert_dom_equal expected, text_field("post", "title", :size => 35)
  56. end
  57. def test_text_field_assuming_size
  58. expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
  59. assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
  60. assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
  61. end
  62. def test_text_field_doesnt_change_param_values
  63. object_name = 'post[]'
  64. expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />'
  65. assert_equal expected, text_field(object_name, "title")
  66. assert_equal object_name, "post[]"
  67. end
  68. def test_check_box
  69. assert_dom_equal(
  70. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
  71. check_box("post", "secret")
  72. )
  73. @post.secret = 0
  74. assert_dom_equal(
  75. '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
  76. check_box("post", "secret")
  77. )
  78. assert_dom_equal(
  79. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
  80. check_box("post", "secret" ,{"checked"=>"checked"})
  81. )
  82. @post.secret = true
  83. assert_dom_equal(
  84. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
  85. check_box("post", "secret")
  86. )
  87. end
  88. def test_check_box_with_explicit_checked_and_unchecked_values
  89. @post.secret = "on"
  90. assert_dom_equal(
  91. '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" /><input name="post[secret]" type="hidden" value="off" />',
  92. check_box("post", "secret", {}, "on", "off")
  93. )
  94. end
  95. def test_radio_button
  96. assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
  97. radio_button("post", "title", "Hello World")
  98. )
  99. assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
  100. radio_button("post", "title", "Goodbye World")
  101. )
  102. end
  103. def test_radio_button_is_checked_with_integers
  104. assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
  105. radio_button("post", "secret", "1")
  106. )
  107. end
  108. def test_radio_button_respects_passed_in_id
  109. assert_dom_equal('<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
  110. radio_button("post", "secret", "1", :id=>"foo")
  111. )
  112. end
  113. def test_text_area
  114. assert_dom_equal(
  115. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
  116. text_area("post", "body")
  117. )
  118. end
  119. def test_text_area_with_escapes
  120. @post.body = "Back to <i>the</i> hill and over it again!"
  121. assert_dom_equal(
  122. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to &lt;i&gt;the&lt;/i&gt; hill and over it again!</textarea>',
  123. text_area("post", "body")
  124. )
  125. end
  126. def test_text_area_with_alternate_value
  127. assert_dom_equal(
  128. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
  129. text_area("post", "body", :value => 'Testing alternate values.')
  130. )
  131. end
  132. def test_text_area_with_size_option
  133. assert_dom_equal(
  134. '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
  135. text_area("post", "body", :size => "183x820")
  136. )
  137. end
  138. def test_date_selects
  139. assert_dom_equal(
  140. '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
  141. text_area("post", "body")
  142. )
  143. end
  144. def test_explicit_name
  145. assert_dom_equal(
  146. '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
  147. )
  148. assert_dom_equal(
  149. '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
  150. text_area("post", "body", "name" => "really!")
  151. )
  152. assert_dom_equal(
  153. '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
  154. check_box("post", "secret", "name" => "i mean it")
  155. )
  156. assert_dom_equal text_field("post", "title", "name" => "dont guess"),
  157. text_field("post", "title", :name => "dont guess")
  158. assert_dom_equal text_area("post", "body", "name" => "really!"),
  159. text_area("post", "body", :name => "really!")
  160. assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
  161. check_box("post", "secret", :name => "i mean it")
  162. end
  163. def test_explicit_id
  164. assert_dom_equal(
  165. '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
  166. )
  167. assert_dom_equal(
  168. '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
  169. text_area("post", "body", "id" => "really!")
  170. )
  171. assert_dom_equal(
  172. '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
  173. check_box("post", "secret", "id" => "i mean it")
  174. )
  175. assert_dom_equal text_field("post", "title", "id" => "dont guess"),
  176. text_field("post", "title", :id => "dont guess")
  177. assert_dom_equal text_area("post", "body", "id" => "really!"),
  178. text_area("post", "body", :id => "really!")
  179. assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
  180. check_box("post", "secret", :id => "i mean it")
  181. end
  182. def test_auto_index
  183. pid = @post.id
  184. assert_dom_equal(
  185. "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
  186. )
  187. assert_dom_equal(
  188. "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
  189. text_area("post[]", "body")
  190. )
  191. assert_dom_equal(
  192. "<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
  193. check_box("post[]", "secret")
  194. )
  195. assert_dom_equal(
  196. "<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
  197. radio_button("post[]", "title", "Hello World")
  198. )
  199. assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
  200. radio_button("post[]", "title", "Goodbye World")
  201. )
  202. end
  203. def test_form_for
  204. _erbout = ''
  205. form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
  206. _erbout.concat f.text_field(:title)
  207. _erbout.concat f.text_area(:body)
  208. _erbout.concat f.check_box(:secret)
  209. end
  210. expected =
  211. "<form action='http://www.example.com' id='create-post' method='post'>" +
  212. "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
  213. "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
  214. "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  215. "<input name='post[secret]' type='hidden' value='0' />" +
  216. "</form>"
  217. assert_dom_equal expected, _erbout
  218. end
  219. def test_form_for_with_method
  220. _erbout = ''
  221. form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
  222. _erbout.concat f.text_field(:title)
  223. _erbout.concat f.text_area(:body)
  224. _erbout.concat f.check_box(:secret)
  225. end
  226. expected =
  227. "<form action='http://www.example.com' id='create-post' method='post'>" +
  228. "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
  229. "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
  230. "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
  231. "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  232. "<input name='post[secret]' type='hidden' value='0' />" +
  233. "</form>"
  234. assert_dom_equal expected, _erbout
  235. end
  236. def test_form_for_without_object
  237. _erbout = ''
  238. form_for(:post, :html => { :id => 'create-post' }) do |f|
  239. _erbout.concat f.text_field(:title)
  240. _erbout.concat f.text_area(:body)
  241. _erbout.concat f.check_box(:secret)
  242. end
  243. expected =
  244. "<form action='http://www.example.com' id='create-post' method='post'>" +
  245. "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
  246. "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
  247. "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  248. "<input name='post[secret]' type='hidden' value='0' />" +
  249. "</form>"
  250. assert_dom_equal expected, _erbout
  251. end
  252. def test_form_for_with_index
  253. _erbout = ''
  254. form_for("post[]", @post) do |f|
  255. _erbout.concat f.text_field(:title)
  256. _erbout.concat f.text_area(:body)
  257. _erbout.concat f.check_box(:secret)
  258. end
  259. expected =
  260. "<form action='http://www.example.com' method='post'>" +
  261. "<input name='post[123][title]' size='30' type='text' id='post_title' value='Hello World' />" +
  262. "<textarea name='post[123][body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
  263. "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  264. "<input name='post[123][secret]' type='hidden' value='0' />" +
  265. "</form>"
  266. end
  267. def test_fields_for
  268. _erbout = ''
  269. fields_for(:post, @post) do |f|
  270. _erbout.concat f.text_field(:title)
  271. _erbout.concat f.text_area(:body)
  272. _erbout.concat f.check_box(:secret)
  273. end
  274. expected =
  275. "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
  276. "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
  277. "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  278. "<input name='post[secret]' type='hidden' value='0' />"
  279. assert_dom_equal expected, _erbout
  280. end
  281. def test_fields_for_without_object
  282. _erbout = ''
  283. fields_for(:post) do |f|
  284. _erbout.concat f.text_field(:title)
  285. _erbout.concat f.text_area(:body)
  286. _erbout.concat f.check_box(:secret)
  287. end
  288. expected =
  289. "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
  290. "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
  291. "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  292. "<input name='post[secret]' type='hidden' value='0' />"
  293. assert_dom_equal expected, _erbout
  294. end
  295. def test_form_builder_does_not_have_form_for_method
  296. assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
  297. end
  298. def test_form_for_and_fields_for
  299. _erbout = ''
  300. form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
  301. _erbout.concat post_form.text_field(:title)
  302. _erbout.concat post_form.text_area(:body)
  303. fields_for(:parent_post, @post) do |parent_fields|
  304. _erbout.concat parent_fields.check_box(:secret)
  305. end
  306. end
  307. expected =
  308. "<form action='http://www.example.com' id='create-post' method='post'>" +
  309. "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
  310. "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
  311. "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
  312. "<input name='parent_post[secret]' type='hidden' value='0' />" +
  313. "</form>"
  314. assert_dom_equal expected, _erbout
  315. end
  316. class LabelledFormBuilder < ActionView::Helpers::FormBuilder
  317. (field_helpers - %w(hidden_field)).each do |selector|
  318. src = <<-END_SRC
  319. def #{selector}(field, *args, &proc)
  320. "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
  321. end
  322. END_SRC
  323. class_eval src, __FILE__, __LINE__
  324. end
  325. end
  326. def test_form_for_with_labelled_builder
  327. _erbout = ''
  328. form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
  329. _erbout.concat f.text_field(:title)
  330. _erbout.concat f.text_area(:body)
  331. _erbout.concat f.check_box(:secret)
  332. end
  333. expected =
  334. "<form action='http://www.example.com' method='post'>" +
  335. "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
  336. "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
  337. "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  338. "<input name='post[secret]' type='hidden' value='0' /><br/>" +
  339. "</form>"
  340. assert_dom_equal expected, _erbout
  341. end
  342. def test_default_form_builder
  343. old_default_form_builder, ActionView::Base.default_form_builder =
  344. ActionView::Base.default_form_builder, LabelledFormBuilder
  345. _erbout = ''
  346. form_for(:post, @post) do |f|
  347. _erbout.concat f.text_field(:title)
  348. _erbout.concat f.text_area(:body)
  349. _erbout.concat f.check_box(:secret)
  350. end
  351. expected =
  352. "<form action='http://www.example.com' method='post'>" +
  353. "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
  354. "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
  355. "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  356. "<input name='post[secret]' type='hidden' value='0' /><br/>" +
  357. "</form>"
  358. assert_dom_equal expected, _erbout
  359. ensure
  360. ActionView::Base.default_form_builder = old_default_form_builder
  361. end
  362. # Perhaps this test should be moved to prototype helper tests.
  363. def test_remote_form_for_with_labelled_builder
  364. self.extend ActionView::Helpers::PrototypeHelper
  365. _erbout = ''
  366. remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
  367. _erbout.concat f.text_field(:title)
  368. _erbout.concat f.text_area(:body)
  369. _erbout.concat f.check_box(:secret)
  370. end
  371. expected =
  372. %(<form action="http://www.example.com" onsubmit="new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" method="post">) +
  373. "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
  374. "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
  375. "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  376. "<input name='post[secret]' type='hidden' value='0' /><br/>" +
  377. "</form>"
  378. assert_dom_equal expected, _erbout
  379. end
  380. def test_fields_for_with_labelled_builder
  381. _erbout = ''
  382. fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
  383. _erbout.concat f.text_field(:title)
  384. _erbout.concat f.text_area(:body)
  385. _erbout.concat f.check_box(:secret)
  386. end
  387. expected =
  388. "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
  389. "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
  390. "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
  391. "<input name='post[secret]' type='hidden' value='0' /><br/>"
  392. assert_dom_equal expected, _erbout
  393. end
  394. def test_form_for_with_html_options_adds_options_to_form_tag
  395. _erbout = ''
  396. form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
  397. expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
  398. assert_dom_equal expected, _erbout
  399. end
  400. def test_form_for_with_string_url_option
  401. _erbout = ''
  402. form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end
  403. assert_equal 'http://www.otherdomain.com', @controller.url_for_options
  404. end
  405. def test_form_for_with_hash_url_option
  406. _erbout = ''
  407. form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end
  408. assert_equal 'controller', @controller.url_for_options[:controller]
  409. assert_equal 'action', @controller.url_for_options[:action]
  410. end
  411. def test_remote_form_for_with_html_options_adds_options_to_form_tag
  412. self.extend ActionView::Helpers::PrototypeHelper
  413. _erbout = ''
  414. remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
  415. expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\" onsubmit=\"new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"></form>"
  416. assert_dom_equal expected, _erbout
  417. end
  418. end