require 'test_helper'
require 'action_view/template/handlers/erb'
class DynamicFormTest < ActionView::TestCase
tests ActionView::Helpers::DynamicForm
def form_for(*)
@output_buffer = super
end
silence_warnings do
class Post < Struct.new(:title, :author_name, :body, :secret, :written_on)
extend ActiveModel::Naming
include ActiveModel::Conversion
end
class User < Struct.new(:email)
extend ActiveModel::Naming
include ActiveModel::Conversion
end
class Column < Struct.new(:type, :name, :human_name)
extend ActiveModel::Naming
include ActiveModel::Conversion
end
end
class DirtyPost
class Errors
def empty?
false
end
def count
1
end
def full_messages
["Author name can't be empty "]
end
def [](field)
["can't be empty "]
end
end
def errors
Errors.new
end
end
def setup_post
@post = Post.new
def @post.errors
Class.new {
def [](field)
case field.to_s
when "author_name"
["can't be empty"]
when "body"
['foo']
else
[]
end
end
def empty?() false end
def count() 1 end
def full_messages() [ "Author name can't be empty" ] end
}.new
end
def @post.persisted?() false end
def @post.to_param() nil end
def @post.column_for_attribute(attr_name)
Post.content_columns.select { |column| column.name == attr_name }.first
end
silence_warnings do
def Post.content_columns() [ Column.new(:string, "title", "Title"), Column.new(:text, "body", "Body") ] end
end
@post.title = "Hello World"
@post.author_name = ""
@post.body = "Back to the hill and over it again!"
@post.secret = 1
@post.written_on = Date.new(2004, 6, 15)
end
def setup_user
@user = User.new
def @user.errors
Class.new {
def [](field) field == "email" ? ['nonempty'] : [] end
def empty?() false end
def count() 1 end
def full_messages() [ "User email can't be empty" ] end
}.new
end
def @user.new_record?() true end
def @user.to_param() nil end
def @user.column_for_attribute(attr_name)
User.content_columns.select { |column| column.name == attr_name }.first
end
silence_warnings do
def User.content_columns() [ Column.new(:string, "email", "Email") ] end
end
@user.email = ""
end
def protect_against_forgery?
@protect_against_forgery ? true : false
end
attr_accessor :request_forgery_protection_token, :form_authenticity_token
def setup
super
setup_post
setup_user
@response = ActionController::TestResponse.new
end
def url_for(options)
options = options.symbolize_keys
[options[:action], options[:id].to_param].compact.join('/')
end
def test_generic_input_tag
assert_dom_equal(
%( ), input("post", "title")
)
end
def test_text_area_with_errors
assert_dom_equal(
%(
),
text_area("post", "body")
)
end
def test_text_field_with_errors
assert_dom_equal(
%(
),
text_field("post", "author_name")
)
end
def test_field_error_proc
old_proc = ActionView::Base.field_error_proc
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
%(#{html_tag} #{[instance.error_message].join(', ')}
).html_safe
end
assert_dom_equal(
%( can't be empty
),
text_field("post", "author_name")
)
ensure
ActionView::Base.field_error_proc = old_proc if old_proc
end
def test_form_with_string
assert_dom_equal(
%(),
form("post")
)
silence_warnings do
class << @post
def persisted?() true end
def to_param() id end
def id() 1 end
end
end
assert_dom_equal(
%(
Title
\nBody
Back to the hill and over it again!
),
form("post")
)
end
def test_form_with_protect_against_forgery
@protect_against_forgery = true
@request_forgery_protection_token = 'authenticity_token'
@form_authenticity_token = '123'
assert_dom_equal(
%(
Title
\nBody
Back to the hill and over it again!
),
form("post")
)
end
def test_form_with_method_option
assert_dom_equal(
%(
Title
\nBody
Back to the hill and over it again!
),
form("post", :method=>'get')
)
end
def test_form_with_action_option
output_buffer << form("post", :action => "sign")
assert_select "form[action=sign]" do |form|
assert_select "input[type=submit][value=Sign]"
end
end
def test_form_with_date
silence_warnings do
def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end
end
assert_dom_equal(
%(
Written on \n1999 \n2000 \n2001 \n2002 \n2003 \n2004 \n2005 \n2006 \n2007 \n2008 \n2009 \n \n\nJanuary \nFebruary \nMarch \nApril \nMay \nJune \nJuly \nAugust \nSeptember \nOctober \nNovember \nDecember \n \n\n1 \n2 \n3 \n4 \n5 \n6 \n7 \n8 \n9 \n10 \n11 \n12 \n13 \n14 \n15 \n16 \n17 \n18 \n19 \n20 \n21 \n22 \n23 \n24 \n25 \n26 \n27 \n28 \n29 \n30 \n31 \n \n
),
form("post")
)
end
def test_form_with_datetime
silence_warnings do
def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end
end
@post.written_on = Time.gm(2004, 6, 15, 16, 30)
assert_dom_equal(
%(
Written on \n1999 \n2000 \n2001 \n2002 \n2003 \n2004 \n2005 \n2006 \n2007 \n2008 \n2009 \n \n\nJanuary \nFebruary \nMarch \nApril \nMay \nJune \nJuly \nAugust \nSeptember \nOctober \nNovember \nDecember \n \n\n1 \n2 \n3 \n4 \n5 \n6 \n7 \n8 \n9 \n10 \n11 \n12 \n13 \n14 \n15 \n16 \n17 \n18 \n19 \n20 \n21 \n22 \n23 \n24 \n25 \n26 \n27 \n28 \n29 \n30 \n31 \n \n — \n00 \n01 \n02 \n03 \n04 \n05 \n06 \n07 \n08 \n09 \n10 \n11 \n12 \n13 \n14 \n15 \n16 \n17 \n18 \n19 \n20 \n21 \n22 \n23 \n \n : \n00 \n01 \n02 \n03 \n04 \n05 \n06 \n07 \n08 \n09 \n10 \n11 \n12 \n13 \n14 \n15 \n16 \n17 \n18 \n19 \n20 \n21 \n22 \n23 \n24 \n25 \n26 \n27 \n28 \n29 \n30 \n31 \n32 \n33 \n34 \n35 \n36 \n37 \n38 \n39 \n40 \n41 \n42 \n43 \n44 \n45 \n46 \n47 \n48 \n49 \n50 \n51 \n52 \n53 \n54 \n55 \n56 \n57 \n58 \n59 \n \n
),
form("post")
)
end
def test_error_for_block
assert_dom_equal %(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ), error_messages_for("post")
assert_equal %(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ), error_messages_for("post", :class => "errorDeathByClass", :id => "errorDeathById", :header_tag => "h1")
assert_equal %(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ), error_messages_for("post", :class => nil, :id => "errorDeathById", :header_tag => "h1")
assert_equal %(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ), error_messages_for("post", :class => "errorDeathByClass", :id => nil, :header_tag => "h1")
end
def test_error_messages_for_escapes_html
@dirty_post = DirtyPost.new
assert_dom_equal %(1 error prohibited this dirty post from being saved There were problems with the following fields:
Author name can't be <em>empty</em> ), error_messages_for("dirty_post")
end
def test_error_messages_for_handles_nil
assert_equal "", error_messages_for("notthere")
end
def test_error_message_on_escapes_html
@dirty_post = DirtyPost.new
assert_dom_equal "can't be <em>empty</em>
", error_message_on(:dirty_post, :author_name)
end
def test_error_message_on_handles_nil
assert_equal "", error_message_on("notthere", "notthere")
end
def test_error_message_on
assert_dom_equal "can't be empty
", error_message_on(:post, :author_name)
end
def test_error_message_on_no_instance_variable
other_post = @post
assert_dom_equal "can't be empty
", error_message_on(other_post, :author_name)
end
def test_error_message_on_with_options_hash
assert_dom_equal "beforecan't be emptyafter
", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
end
def test_error_message_on_with_tag_option_in_options_hash
assert_dom_equal "beforecan't be emptyafter ", error_message_on(:post, :author_name, :html_tag => "span", :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
end
def test_error_message_on_handles_empty_errors
assert_equal "", error_message_on(@post, :tag)
end
def test_error_messages_for_many_objects
assert_dom_equal %(2 errors prohibited this post from being saved There were problems with the following fields:
Author name can't be empty User email can't be empty ), error_messages_for("post", "user")
# reverse the order, error order changes and so does the title
assert_dom_equal %(2 errors prohibited this user from being saved There were problems with the following fields:
User email can't be empty Author name can't be empty ), error_messages_for("user", "post")
# add the default to put post back in the title
assert_dom_equal %(2 errors prohibited this post from being saved There were problems with the following fields:
User email can't be empty Author name can't be empty ), error_messages_for("user", "post", :object_name => "post")
# symbols work as well
assert_dom_equal %(2 errors prohibited this post from being saved There were problems with the following fields:
User email can't be empty Author name can't be empty ), error_messages_for(:user, :post, :object_name => :post)
# any default works too
assert_dom_equal %(2 errors prohibited this monkey from being saved There were problems with the following fields:
User email can't be empty Author name can't be empty ), error_messages_for(:user, :post, :object_name => "monkey")
# should space object name
assert_dom_equal %(2 errors prohibited this chunky bacon from being saved There were problems with the following fields:
User email can't be empty Author name can't be empty ), error_messages_for(:user, :post, :object_name => "chunky_bacon")
# hide header and explanation messages with nil or empty string
assert_dom_equal %(User email can't be empty Author name can't be empty ), error_messages_for(:user, :post, :header_message => nil, :message => "")
# override header and explanation messages
header_message = "Yikes! Some errors"
message = "Please fix the following fields and resubmit:"
assert_dom_equal %(#{header_message} #{message}
User email can't be empty Author name can't be empty ), error_messages_for(:user, :post, :header_message => header_message, :message => message)
end
def test_error_messages_for_non_instance_variable
actual_user = @user
actual_post = @post
@user = nil
@post = nil
#explicitly set object
assert_dom_equal %(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ), error_messages_for("post", :object => actual_post)
#multiple objects
assert_dom_equal %(2 errors prohibited this user from being saved There were problems with the following fields:
User email can't be empty Author name can't be empty ), error_messages_for("user", "post", :object => [actual_user, actual_post])
#nil object
assert_equal '', error_messages_for('user', :object => nil)
end
def test_error_messages_for_model_objects
error = error_messages_for(@post)
assert_dom_equal %(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ),
error
error = error_messages_for(@user, @post)
assert_dom_equal %(2 errors prohibited this user from being saved There were problems with the following fields:
User email can't be empty Author name can't be empty ),
error
end
def test_form_with_string_multipart
assert_dom_equal(
%(
Title
\nBody
Back to the hill and over it again!
),
form("post", :multipart => true)
)
end
def test_default_form_builder_with_dynamic_form_helpers
form_for(@post, :as => :post, :url => {}) do |f|
concat f.error_message_on('author_name')
concat f.error_messages
end
expected = %() +
%(
) +
%(can't be empty
) +
%(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ) +
%( )
assert_dom_equal expected, output_buffer
end
def test_default_form_builder_no_instance_variable
post = @post
@post = nil
form_for(post, :as => :post, :url => {}) do |f|
concat f.error_message_on('author_name')
concat f.error_messages
end
expected = %() +
%(
) +
%(can't be empty
) +
%(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ) +
%( )
assert_dom_equal expected, output_buffer
end
def test_error_messages_without_prefixed_attribute_name
error = error_messages_for(@post)
assert_dom_equal %(1 error prohibited this post from being saved There were problems with the following fields:
Author name can't be empty ),
error
end
end