<commit_before>require 'abstract_unit'

class FormTagHelperTest < ActionView::TestCase
  include RenderERBUtils

  tests ActionView::Helpers::FormTagHelper

  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 << %{<input name="utf8" type="hidden" value="&#x2713;" />}
      end

      if method && !%w(get post).include?(method.to_s)
        txt << %{<input name="_method" type="hidden" value="#{method}" />}
      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 =  %{<form accept-charset="UTF-8" action="#{action}"}
    txt << %{ enctype="multipart/form-data"} if enctype
    txt << %{ data-remote="true"} if remote
    txt << %{ class="#{html_class}"} if html_class
    txt << %{ id="#{id}"} if id
    txt << %{ method="#{method}">}
  end

  def whole_form(action = "http://www.example.com", options = {})
    out = form_text(action, options) + hidden_fields(options)

    if block_given?
      out << yield << "</form>"
    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 = %(<input id="admin" name="admin" type="checkbox" value="1" />)
    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<commit_msg>Add test cases for checkbox_tag<commit_after>require 'abstract_unit'

class FormTagHelperTest < ActionView::TestCase
  include RenderERBUtils

  tests ActionView::Helpers::FormTagHelper

  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 << %{<input name="utf8" type="hidden" value="&#x2713;" />}
      end

      if method && !%w(get post).include?(method.to_s)
        txt << %{<input name="_method" type="hidden" value="#{method}" />}
      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 =  %{<form accept-charset="UTF-8" action="#{action}"}
    txt << %{ enctype="multipart/form-data"} if enctype
    txt << %{ data-remote="true"} if remote
    txt << %{ class="#{html_class}"} if html_class
    txt << %{ id="#{id}"} if id
    txt << %{ method="#{method}">}
  end

  def whole_form(action = "http://www.example.com", options = {})
    out = form_text(action, options) + hidden_fields(options)

    if block_given?
      out << yield << "</form>"
    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 = %(<input id="admin" name="admin" type="checkbox" value="1" />)
    assert_dom_equal expected, actual
  end

  def test_check_box_tag_disabled
    actual = check_box_tag "admin","1", false, disabled: true
    expected = %(<input id="admin" disabled="disabled" name="admin" type="checkbox" value="1" />)
    assert_dom_equal expected, actual
  end

  def test_check_box_tag_default_checked
    actual = check_box_tag "admin","1", true
    expected = %(<input id="admin" checked="checked" name="admin" type="checkbox" value="1" />)
    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
{'fraction_non_alphanumeric': 0.14985994397759103, 'fraction_numerical': 0.005952380952380952, 'mean_word_length': 3.5233570863024544, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 14, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6881142', 'n_tokens_mistral': 1998, 'n_tokens_neox': 1926, 'n_words': 419}