# frozen_string_literal: true require "abstract_unit" class FormTagHelperTest < ActionView::TestCase include RenderERBUtils tests ActionView::Helpers::FormTagHelper class WithActiveStorageRoutesControllers < ActionController::Base test_routes do post "/rails/active_storage/direct_uploads" => "active_storage/direct_uploads#create", as: :rails_direct_uploads end def url_options { host: "testtwo.host" } end end def setup super @controller = BasicController.new end def hidden_fields(options = {}) method = options[:method] enforce_utf8 = options.fetch(:enforce_utf8, true) (+"").tap do |txt| if enforce_utf8 txt << %{} end if method && !%w(get post).include?(method.to_s) txt << %{} end end end def form_text(action = "http://www.example.com", options = {}) remote, enctype, html_class, id, method = options.values_at(:remote, :enctype, :html_class, :id, :method) method = method.to_s == "get" ? "get" : "post" txt = +%{
} end def whole_form(action = "http://www.example.com", options = {}) out = form_text(action, options) + hidden_fields(options) if block_given? out << yield << "
" end out end def url_for(options) if options.is_a?(Hash) "http://www.example.com" else super end end VALID_HTML_ID = /^[A-Za-z][-_:.A-Za-z0-9]*$/ # see http://www.w3.org/TR/html4/types.html#type-name def test_check_box_tag actual = check_box_tag "admin" expected = %() assert_dom_equal expected, actual end def test_check_box_tag_disabled actual = check_box_tag "admin", "1", false, disabled: true expected = %() assert_dom_equal expected, actual end def test_check_box_tag_default_checked actual = check_box_tag "admin", "1", true expected = %() assert_dom_equal expected, actual end def test_check_box_tag_id_sanitized label_elem = root_elem(check_box_tag("project[2][admin]")) assert_match VALID_HTML_ID, label_elem["id"] end def test_form_tag actual = form_tag expected = whole_form assert_dom_equal expected, actual end def test_form_tag_multipart actual = form_tag({}, { "multipart" => true }) expected = whole_form("http://www.example.com", enctype: true) assert_dom_equal expected, actual end def test_form_tag_with_method_patch actual = form_tag({}, { method: :patch }) expected = whole_form("http://www.example.com", method: :patch) assert_dom_equal expected, actual end def test_form_tag_with_method_put actual = form_tag({}, { method: :put }) expected = whole_form("http://www.example.com", method: :put) assert_dom_equal expected, actual end def test_form_tag_with_method_delete actual = form_tag({}, { method: :delete }) expected = whole_form("http://www.example.com", method: :delete) assert_dom_equal expected, actual end def test_form_tag_with_remote actual = form_tag({}, { remote: true }) expected = whole_form("http://www.example.com", remote: true) assert_dom_equal expected, actual end def test_form_tag_with_remote_false actual = form_tag({}, { remote: false }) expected = whole_form assert_dom_equal expected, actual end def test_form_tag_enforce_utf8_true actual = form_tag({}, { enforce_utf8: true }) expected = whole_form("http://www.example.com", enforce_utf8: true) assert_dom_equal expected, actual assert_predicate actual, :html_safe? end def test_form_tag_enforce_utf8_false actual = form_tag({}, { enforce_utf8: false }) expected = whole_form("http://www.example.com", enforce_utf8: false) assert_dom_equal expected, actual assert_predicate actual, :html_safe? end def test_form_tag_default_enforce_utf8_false with_default_enforce_utf8 false do actual = form_tag({}) expected = whole_form("http://www.example.com", enforce_utf8: false) assert_dom_equal expected, actual assert_predicate actual, :html_safe? end end def test_form_tag_default_enforce_utf8_true with_default_enforce_utf8 true do actual = form_tag({}) expected = whole_form("http://www.example.com", enforce_utf8: true) assert_dom_equal expected, actual assert_predicate actual, :html_safe? end end def test_form_tag_with_block_in_erb output_buffer = render_erb("<%= form_tag('http://www.example.com') do %>Hello world!<% end %>") expected = whole_form { "Hello world!" } assert_dom_equal expected, output_buffer end def test_form_tag_with_block_and_method_in_erb output_buffer = render_erb("<%= form_tag('http://www.example.com', :method => :put) do %>Hello world!<% end %>") expected = whole_form("http://www.example.com", method: "put") do "Hello world!" end assert_dom_equal expected, output_buffer end def test_hidden_field_tag actual = hidden_field_tag "id", 3 expected = %() assert_dom_equal expected, actual end def test_hidden_field_tag_id_sanitized input_elem = root_elem(hidden_field_tag("item[][title]")) assert_match VALID_HTML_ID, input_elem["id"] end def test_file_field_tag assert_dom_equal "", file_field_tag("picsplz") end def test_file_field_tag_with_options assert_dom_equal "", file_field_tag("picsplz", class: "pix") end def test_file_field_tag_with_direct_upload_when_rails_direct_uploads_url_is_not_defined assert_dom_equal( "", file_field_tag("picsplz", class: "pix", direct_upload: true) ) end def test_file_field_tag_with_direct_upload_when_rails_direct_uploads_url_is_defined @controller = WithActiveStorageRoutesControllers.new assert_dom_equal( "", file_field_tag("picsplz", class: "pix", direct_upload: true) ) end def test_file_field_tag_with_direct_upload_dont_mutate_arguments original_options = { class: "pix", direct_upload: true } assert_dom_equal( "", file_field_tag("picsplz", original_options) ) assert_equal({ class: "pix", direct_upload: true }, original_options) end def test_password_field_tag actual = password_field_tag expected = %() assert_dom_equal expected, actual end def test_multiple_field_tags_with_same_options options = { class: "important" } assert_dom_equal %(), file_field_tag("title", options) assert_dom_equal %(), password_field_tag("title", "Hello!", options) assert_dom_equal %(), text_field_tag("title", "Hello!", options) end def test_radio_button_tag actual = radio_button_tag "people", "david" expected = %() assert_dom_equal expected, actual actual = radio_button_tag("num_people", 5) expected = %() assert_dom_equal expected, actual actual = radio_button_tag("gender", "m") + radio_button_tag("gender", "f") expected = %() assert_dom_equal expected, actual actual = radio_button_tag("opinion", "-1") + radio_button_tag("opinion", "1") expected = %() assert_dom_equal expected, actual actual = radio_button_tag("person[gender]", "m") expected = %() assert_dom_equal expected, actual actual = radio_button_tag("ctrlname", "apache2.2") expected = %() assert_dom_equal expected, actual end def test_select_tag actual = select_tag "people", raw("") expected = %() assert_dom_equal expected, actual end def test_select_tag_with_multiple actual = select_tag "colors", raw(""), multiple: true expected = %() assert_dom_equal expected, actual end def test_select_tag_disabled actual = select_tag "places", raw(""), disabled: true expected = %() assert_dom_equal expected, actual end def test_select_tag_id_sanitized input_elem = root_elem(select_tag("project[1]people", "")) assert_match VALID_HTML_ID, input_elem["id"] end def test_select_tag_with_include_blank actual = select_tag "places", raw(""), include_blank: true expected = %() assert_dom_equal expected, actual end def test_select_tag_with_include_blank_doesnt_change_options options = { include_blank: true, prompt: "string" } expected_options = options.dup select_tag "places", raw(""), options expected_options.each { |k, v| assert_equal v, options[k] } end def test_select_tag_with_include_blank_false actual = select_tag "places", raw(""), include_blank: false expected = %() assert_dom_equal expected, actual end def test_select_tag_with_include_blank_string actual = select_tag "places", raw(""), include_blank: "Choose" expected = %() assert_dom_equal expected, actual end def test_select_tag_with_prompt actual = select_tag "places", raw(""), prompt: "string" expected = %() assert_dom_equal expected, actual end def test_select_tag_escapes_prompt actual = select_tag "places", raw(""), prompt: "" expected = %() assert_dom_equal expected, actual end def test_select_tag_with_prompt_and_include_blank actual = select_tag "places", raw(""), prompt: "string", include_blank: true expected = %() assert_dom_equal expected, actual end def test_select_tag_with_nil_option_tags_and_include_blank actual = select_tag "places", nil, include_blank: true expected = %() assert_dom_equal expected, actual end def test_select_tag_with_nil_option_tags_and_prompt actual = select_tag "places", nil, prompt: "string" expected = %() assert_dom_equal expected, actual end def test_text_area_tag_size_string actual = text_area_tag "body", "hello world", "size" => "20x40" expected = %() assert_dom_equal expected, actual end def test_text_area_tag_size_symbol actual = text_area_tag "body", "hello world", size: "20x40" expected = %() assert_dom_equal expected, actual end def test_text_area_tag_should_disregard_size_if_its_given_as_an_integer actual = text_area_tag "body", "hello world", size: 20 expected = %() assert_dom_equal expected, actual end def test_text_area_tag_id_sanitized input_elem = root_elem(text_area_tag("item[][description]")) assert_match VALID_HTML_ID, input_elem["id"] end def test_text_area_tag_escape_content actual = text_area_tag "body", "hello world", size: "20x40" expected = %() assert_dom_equal expected, actual end def test_text_area_tag_unescaped_content actual = text_area_tag "body", "hello world", size: "20x40", escape: false expected = %() assert_dom_equal expected, actual end def test_text_area_tag_unescaped_nil_content actual = text_area_tag "body", nil, escape: false expected = %() assert_dom_equal expected, actual end def test_text_field_tag actual = text_field_tag "title", "Hello!" expected = %() assert_dom_equal expected, actual end def test_text_field_tag_class_string actual = text_field_tag "title", "Hello!", "class" => "admin" expected = %() assert_dom_equal expected, actual end def test_text_field_tag_size_symbol actual = text_field_tag "title", "Hello!", size: 75 expected = %() assert_dom_equal expected, actual end def test_text_field_tag_with_ac_parameters actual = text_field_tag "title", ActionController::Parameters.new(key: "value") expected = %() assert_dom_equal expected, actual end def test_text_field_tag_size_string actual = text_field_tag "title", "Hello!", "size" => "75" expected = %() assert_dom_equal expected, actual end def test_text_field_tag_maxlength_symbol actual = text_field_tag "title", "Hello!", maxlength: 75 expected = %() assert_dom_equal expected, actual end def test_text_field_tag_maxlength_string actual = text_field_tag "title", "Hello!", "maxlength" => "75" expected = %() assert_dom_equal expected, actual end def test_text_field_tag_disabled actual = text_field_tag "title", "Hello!", disabled: true expected = %() assert_dom_equal expected, actual end def test_text_field_tag_with_placeholder_option actual = text_field_tag "title", "Hello!", placeholder: "Enter search term..." expected = %() assert_dom_equal expected, actual end def test_text_field_tag_with_multiple_options actual = text_field_tag "title", "Hello!", size: 70, maxlength: 80 expected = %() assert_dom_equal expected, actual end def test_text_field_tag_id_sanitized input_elem = root_elem(text_field_tag("item[][title]")) assert_match VALID_HTML_ID, input_elem["id"] end def test_label_tag_without_text actual = label_tag "title" expected = %() assert_dom_equal expected, actual end def test_label_tag_with_symbol actual = label_tag :title expected = %() assert_dom_equal expected, actual end def test_label_tag_with_text actual = label_tag "title", "My Title" expected = %() assert_dom_equal expected, actual end def test_label_tag_class_string actual = label_tag "title", "My Title", "class" => "small_label" expected = %() assert_dom_equal expected, actual end def test_label_tag_id_sanitized label_elem = root_elem(label_tag("item[title]")) assert_match VALID_HTML_ID, label_elem["for"] end def test_label_tag_with_block assert_dom_equal("", label_tag { "Blocked" }) end def test_label_tag_with_block_and_argument output = label_tag("clock") { "Grandfather" } assert_dom_equal('', output) end def test_label_tag_with_block_and_argument_and_options output = label_tag("clock", id: "label_clock") { "Grandfather" } assert_dom_equal('', output) end def test_boolean_options assert_dom_equal %(), check_box_tag("admin", 1, true, "disabled" => true, :readonly => "yes") assert_dom_equal %(), check_box_tag("admin", 1, true, disabled: false, readonly: nil) assert_dom_equal %(), tag(:input, type: "checkbox", checked: false) assert_dom_equal %(), select_tag("people", raw(""), multiple: true) assert_dom_equal %(), select_tag("people[]", raw(""), multiple: true) assert_dom_equal %(), select_tag("people", raw(""), multiple: nil) end def test_stringify_symbol_keys actual = text_field_tag "title", "Hello!", id: "admin" expected = %() assert_dom_equal expected, actual end def test_submit_tag assert_dom_equal( %(), submit_tag("Save", onclick: "alert('hello!')", data: { disable_with: "Saving..." }) ) end def test_empty_submit_tag assert_dom_equal( %(), submit_tag("Save") ) end def test_empty_submit_tag_with_opt_out ActionView::Base.automatically_disable_submit_tag = false assert_dom_equal( %(), submit_tag("Save") ) ensure ActionView::Base.automatically_disable_submit_tag = true end def test_submit_tag_having_data_disable_with_string assert_dom_equal( %(), submit_tag("Save", "data-disable-with" => "Processing...", "data-confirm" => "Are you sure?") ) end def test_submit_tag_having_data_disable_with_boolean assert_dom_equal( %(), submit_tag("Save", "data-disable-with" => false, "data-confirm" => "Are you sure?") ) end def test_submit_tag_having_data_hash_disable_with_boolean assert_dom_equal( %(), submit_tag("Save", data: { confirm: "Are you sure?", disable_with: false }) ) end def test_submit_tag_with_no_onclick_options assert_dom_equal( %(), submit_tag("Save", data: { disable_with: "Saving..." }) ) end def test_submit_tag_with_confirmation assert_dom_equal( %(), submit_tag("Save", data: { confirm: "Are you sure?" }) ) end def test_submit_tag_doesnt_have_data_disable_with_twice assert_equal( %(), submit_tag("Save", "data-disable-with" => "Processing...", "data-confirm" => "Are you sure?") ) end def test_submit_tag_doesnt_have_data_disable_with_twice_with_hash assert_equal( %(), submit_tag("Save", data: { disable_with: "Processing..." }) ) end def test_submit_tag_with_symbol_value assert_dom_equal( %(), submit_tag(:Save) ) end def test_button_tag assert_dom_equal( %(), button_tag ) end def test_button_tag_with_submit_type assert_dom_equal( %(), button_tag("Save", type: "submit") ) end def test_button_tag_with_button_type assert_dom_equal( %(), button_tag("Button", type: "button") ) end def test_button_tag_with_reset_type assert_dom_equal( %(), button_tag("Reset", type: "reset") ) end def test_button_tag_with_disabled_option assert_dom_equal( %(), button_tag("Reset", type: "reset", disabled: true) ) end def test_button_tag_escape_content assert_dom_equal( %(), button_tag("Reset", type: "reset", disabled: true) ) end def test_button_tag_with_block assert_dom_equal('', button_tag { "Content" }) end def test_button_tag_with_block_and_options output = button_tag(name: "temptation", type: "button") { content_tag(:strong, "Do not press me") } assert_dom_equal('', output) end def test_button_tag_defaults_with_block_and_options output = button_tag(name: "temptation", value: "within") { content_tag(:strong, "Do not press me") } assert_dom_equal('', output) end def test_button_tag_with_confirmation assert_dom_equal( %(), button_tag("Save", type: "submit", data: { confirm: "Are you sure?" }) ) end def test_button_tag_with_data_disable_with_option assert_dom_equal( %(), button_tag("Checkout", data: { disable_with: "Please wait..." }) ) end def test_image_submit_tag_with_confirmation assert_dom_equal( %(), image_submit_tag("save.gif", data: { confirm: "Are you sure?" }) ) end def test_color_field_tag expected = %{} assert_dom_equal(expected, color_field_tag("car")) end def test_search_field_tag expected = %{} assert_dom_equal(expected, search_field_tag("query")) end def test_telephone_field_tag expected = %{} assert_dom_equal(expected, telephone_field_tag("cell")) end def test_date_field_tag expected = %{} assert_dom_equal(expected, date_field_tag("cell")) end def test_time_field_tag expected = %{} assert_dom_equal(expected, time_field_tag("cell")) end def test_datetime_field_tag expected = %{} assert_dom_equal(expected, datetime_field_tag("appointment")) end def test_datetime_local_field_tag expected = %{} assert_dom_equal(expected, datetime_local_field_tag("appointment")) end def test_month_field_tag expected = %{} assert_dom_equal(expected, month_field_tag("birthday")) end def test_week_field_tag expected = %{} assert_dom_equal(expected, week_field_tag("birthday")) end def test_url_field_tag expected = %{} assert_dom_equal(expected, url_field_tag("homepage")) end def test_email_field_tag expected = %{} assert_dom_equal(expected, email_field_tag("address")) end def test_number_field_tag expected = %{} assert_dom_equal(expected, number_field_tag("quantity", nil, in: 1...10)) end def test_range_input_tag expected = %{} assert_dom_equal(expected, range_field_tag("volume", nil, in: 0..11, step: 0.1)) end def test_field_set_tag_in_erb output_buffer = render_erb("<%= field_set_tag('Your details') do %>Hello world!<% end %>") expected = %(
Your detailsHello world!
) assert_dom_equal expected, output_buffer output_buffer = render_erb("<%= field_set_tag do %>Hello world!<% end %>") expected = %(
Hello world!
) assert_dom_equal expected, output_buffer output_buffer = render_erb("<%= field_set_tag('') do %>Hello world!<% end %>") expected = %(
Hello world!
) assert_dom_equal expected, output_buffer output_buffer = render_erb("<%= field_set_tag('', :class => 'format') do %>Hello world!<% end %>") expected = %(
Hello world!
) assert_dom_equal expected, output_buffer output_buffer = render_erb("<%= field_set_tag %>") expected = %(
) assert_dom_equal expected, output_buffer output_buffer = render_erb("<%= field_set_tag('You legend!') %>") expected = %(
You legend!
) assert_dom_equal expected, output_buffer end def test_text_area_tag_options_symbolize_keys_side_effects options = { option: "random_option" } text_area_tag "body", "hello world", options assert_equal({ option: "random_option" }, options) end def test_submit_tag_options_symbolize_keys_side_effects options = { option: "random_option" } submit_tag "submit value", options assert_equal({ option: "random_option" }, options) end def test_button_tag_options_symbolize_keys_side_effects options = { option: "random_option" } button_tag "button value", options assert_equal({ option: "random_option" }, options) end def test_image_submit_tag_options_symbolize_keys_side_effects options = { option: "random_option" } image_submit_tag "submit source", options assert_equal({ option: "random_option" }, options) end def test_image_label_tag_options_symbolize_keys_side_effects options = { option: "random_option" } label_tag "submit source", "title", options assert_equal({ option: "random_option" }, options) end def protect_against_forgery? false end private def root_elem(rendered_content) Nokogiri::HTML::DocumentFragment.parse(rendered_content).children.first # extract from nodeset end def with_default_enforce_utf8(value) old_value = ActionView::Helpers::FormTagHelper.default_enforce_utf8 ActionView::Helpers::FormTagHelper.default_enforce_utf8 = value yield ensure ActionView::Helpers::FormTagHelper.default_enforce_utf8 = old_value end end