aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-05-27 10:57:05 +0200
committerXavier Noria <fxn@hashref.com>2010-05-27 10:57:05 +0200
commitddec74fb3ab66528ca98ff37ca9b5f4227fdd2e3 (patch)
treec58ac9162487bdca68342fb2d30730ccfd926378 /actionpack
parent96e2094b8b634e4af0d9d3c8a1db9bbb7023a4a7 (diff)
parente02db06ece7aeecec7c37f5b0e3de7d65c8684e6 (diff)
downloadrails-ddec74fb3ab66528ca98ff37ca9b5f4227fdd2e3.tar.gz
rails-ddec74fb3ab66528ca98ff37ca9b5f4227fdd2e3.tar.bz2
rails-ddec74fb3ab66528ca98ff37ca9b5f4227fdd2e3.zip
Merge remote branch 'rails/master'
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/abstract_controller/layouts.rb6
-rw-r--r--actionpack/lib/action_controller/metal/responder.rb2
-rw-r--r--actionpack/lib/action_view/helpers/active_model_helper.rb2
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb5
-rw-r--r--actionpack/lib/action_view/helpers/form_tag_helper.rb2
-rw-r--r--actionpack/lib/action_view/helpers/translation_helper.rb23
-rw-r--r--actionpack/lib/action_view/test_case.rb164
-rw-r--r--actionpack/test/controller/mime_responds_test.rb13
-rw-r--r--actionpack/test/fixtures/test/array_translation.erb1
-rw-r--r--actionpack/test/fixtures/test/scoped_array_translation.erb1
-rw-r--r--actionpack/test/fixtures/test/scoped_translation.erb1
-rw-r--r--actionpack/test/template/form_helper_test.rb5
-rw-r--r--actionpack/test/template/form_tag_helper_test.rb6
-rw-r--r--actionpack/test/template/test_case_test.rb279
-rw-r--r--actionpack/test/template/translation_helper_test.rb22
15 files changed, 299 insertions, 233 deletions
diff --git a/actionpack/lib/abstract_controller/layouts.rb b/actionpack/lib/abstract_controller/layouts.rb
index 319472c937..5cd7a90ab5 100644
--- a/actionpack/lib/abstract_controller/layouts.rb
+++ b/actionpack/lib/abstract_controller/layouts.rb
@@ -245,7 +245,7 @@ module AbstractController
case defined?(@_layout) ? @_layout : nil
when String
- self.class_eval %{def _layout; #{@_layout.inspect} end}
+ self.class_eval %{def _layout; #{@_layout.inspect} end}, __FILE__, __LINE__
when Symbol
self.class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
def _layout
@@ -259,9 +259,9 @@ module AbstractController
ruby_eval
when Proc
define_method :_layout_from_proc, &@_layout
- self.class_eval %{def _layout; _layout_from_proc(self) end}
+ self.class_eval %{def _layout; _layout_from_proc(self) end}, __FILE__, __LINE__
when false
- self.class_eval %{def _layout; end}
+ self.class_eval %{def _layout; end}, __FILE__, __LINE__
when true
raise ArgumentError, "Layouts must be specified as a String, Symbol, false, or nil"
when nil
diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb
index 6ad9a23542..22bdcd0f3c 100644
--- a/actionpack/lib/action_controller/metal/responder.rb
+++ b/actionpack/lib/action_controller/metal/responder.rb
@@ -217,7 +217,7 @@ module ActionController #:nodoc:
# the verb is POST.
#
def default_action
- @action ||= ACTIONS_FOR_VERBS[request.method_symbol]
+ @action ||= ACTIONS_FOR_VERBS[request.request_method_symbol]
end
end
end
diff --git a/actionpack/lib/action_view/helpers/active_model_helper.rb b/actionpack/lib/action_view/helpers/active_model_helper.rb
index d9f09b5dc5..0cddd09eb0 100644
--- a/actionpack/lib/action_view/helpers/active_model_helper.rb
+++ b/actionpack/lib/action_view/helpers/active_model_helper.rb
@@ -43,7 +43,7 @@ module ActionView
end
%w(tag content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
- module_eval "def #{meth}(*) error_wrapping(super) end"
+ module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
end
def error_wrapping(html_tag)
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 414a5d4cd9..b3db3151d3 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -1111,7 +1111,7 @@ module ActionView
end
(field_helpers - %w(label check_box radio_button fields_for hidden_field)).each do |selector|
- src, line = <<-end_src, __LINE__ + 1
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{selector}(method, options = {}) # def text_field(method, options = {})
@template.send( # @template.send(
#{selector.inspect}, # "text_field",
@@ -1119,8 +1119,7 @@ module ActionView
method, # method,
objectify_options(options)) # objectify_options(options))
end # end
- end_src
- class_eval src, __FILE__, line
+ RUBY_EVAL
end
def fields_for(record_or_name_or_array, *args, &block)
diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb
index 2a3f826c15..796268628a 100644
--- a/actionpack/lib/action_view/helpers/form_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb
@@ -293,7 +293,7 @@ module ActionView
escape = options.key?("escape") ? options.delete("escape") : true
content = html_escape(content) if escape
- content_tag :textarea, content.html_safe, { "name" => name, "id" => sanitize_to_id(name) }.update(options)
+ content_tag :textarea, content.to_s.html_safe, { "name" => name, "id" => sanitize_to_id(name) }.update(options)
end
# Creates a check box form input tag.
diff --git a/actionpack/lib/action_view/helpers/translation_helper.rb b/actionpack/lib/action_view/helpers/translation_helper.rb
index 086ad261c8..0d2b2aa7b1 100644
--- a/actionpack/lib/action_view/helpers/translation_helper.rb
+++ b/actionpack/lib/action_view/helpers/translation_helper.rb
@@ -3,7 +3,7 @@ require 'action_view/helpers/tag_helper'
module ActionView
module Helpers
module TranslationHelper
- # Delegates to I18n#translate but also performs three additional functions. First, it'll catch MissingTranslationData exceptions
+ # Delegates to I18n#translate but also performs three additional functions. First, it'll catch MissingTranslationData exceptions
# and turn them into inline spans that contains the missing key, such that you can see in a view what is missing where.
#
# Second, it'll scope the key by the current partial if the key starts with a period. So if you call translate(".foo") from the
@@ -11,16 +11,14 @@ module ActionView
# to translate many keys within the same partials and gives you a simple framework for scoping them consistently. If you don't
# prepend the key with a period, nothing is converted.
#
- # Third, it’ll mark the translation as safe HTML if the key has the suffix "_html" or the last element of the key is the word
- # "html". For example, calling translate("footer_html") or translate("footer.html") will return a safe HTML string that won’t
- # be escaped by other HTML helper methods. This naming convention helps to identify translations that include HTML tags so that
+ # Third, it’ll mark the translation as safe HTML if the key has the suffix "_html" or the last element of the key is the word
+ # "html". For example, calling translate("footer_html") or translate("footer.html") will return a safe HTML string that won’t
+ # be escaped by other HTML helper methods. This naming convention helps to identify translations that include HTML tags so that
# you know what kind of output to expect when you call translate in a template.
def translate(key, options = {})
- options[:raise] = true
- translation = I18n.translate(scope_key_by_partial(key), options)
- translation = (translation.respond_to?(:join) ? translation.join : translation)
- if html_safe_translation_key?(key)
+ translation = I18n.translate(scope_key_by_partial(key), options.merge!(:raise => true))
+ if html_safe_translation_key?(key) && translation.respond_to?(:html_safe)
translation.html_safe
else
translation
@@ -38,12 +36,10 @@ module ActionView
alias :l :localize
private
-
def scope_key_by_partial(key)
- strkey = key.respond_to?(:join) ? key.join : key.to_s
- if strkey.first == "."
+ if key.to_s.first == "."
if @_virtual_path
- @_virtual_path.gsub(%r{/_?}, ".") + strkey
+ @_virtual_path.gsub(%r{/_?}, ".") + key.to_s
else
raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
end
@@ -53,8 +49,7 @@ module ActionView
end
def html_safe_translation_key?(key)
- key = key.is_a?(Array) ? key.last : key.to_s
- key =~ /(\b|_|\.)html$/
+ key.to_s =~ /(\b|_|\.)html$/
end
end
end
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
index f6417d5c2c..4dbbd2eb6a 100644
--- a/actionpack/lib/action_view/test_case.rb
+++ b/actionpack/lib/action_view/test_case.rb
@@ -10,11 +10,16 @@ module ActionView
attr_accessor :request, :response, :params
- def self.controller_path
- ''
+ class << self
+ attr_writer :controller_path
+ end
+
+ def controller_path=(path)
+ self.class.controller_path=(path)
end
def initialize
+ self.class.controller_path = ""
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@@ -24,80 +29,99 @@ module ActionView
end
end
- include ActionDispatch::Assertions, ActionDispatch::TestProcess
- include ActionController::TemplateAssertions
- include ActionView::Context
+ module Behavior
+ extend ActiveSupport::Concern
- include ActionController::PolymorphicRoutes
- include ActionController::RecordIdentifier
+ include ActionDispatch::Assertions, ActionDispatch::TestProcess
+ include ActionController::TemplateAssertions
+ include ActionView::Context
- include AbstractController::Helpers
- include ActionView::Helpers
+ include ActionController::PolymorphicRoutes
+ include ActionController::RecordIdentifier
- class_inheritable_accessor :helper_class
- attr_accessor :controller, :output_buffer, :rendered
+ include AbstractController::Helpers
+ include ActionView::Helpers
- setup :setup_with_controller
- def setup_with_controller
- @controller = TestController.new
- @output_buffer = ActiveSupport::SafeBuffer.new
- @rendered = ''
+ attr_accessor :controller, :output_buffer, :rendered
+
+ module ClassMethods
+ def tests(helper_class)
+ self.helper_class = helper_class
+ end
- self.class.send(:include_helper_modules!)
- make_test_case_available_to_view!
- end
+ def determine_default_helper_class(name)
+ mod = name.sub(/Test$/, '').constantize
+ mod.is_a?(Class) ? nil : mod
+ rescue NameError
+ nil
+ end
- def config
- @controller.config if @controller.respond_to?(:config)
- end
+ def helper_method(*methods)
+ # Almost a duplicate from ActionController::Helpers
+ methods.flatten.each do |method|
+ _helpers.module_eval <<-end_eval
+ def #{method}(*args, &block) # def current_user(*args, &block)
+ _test_case.send(%(#{method}), *args, &block) # test_case.send(%(current_user), *args, &block)
+ end # end
+ end_eval
+ end
+ end
- def render(options = {}, local_assigns = {}, &block)
- @rendered << output = _view.render(options, local_assigns, &block)
- output
- end
+ attr_writer :helper_class
- def protect_against_forgery?
- false
- end
+ def helper_class
+ @helper_class ||= determine_default_helper_class(name)
+ end
- class << self
- def tests(helper_class)
- self.helper_class = helper_class
- end
+ private
- def helper_class
- if current_helper_class = read_inheritable_attribute(:helper_class)
- current_helper_class
- else
- self.helper_class = determine_default_helper_class(name)
+ def include_helper_modules!
+ helper(helper_class) if helper_class
+ include _helpers
end
+
end
- def determine_default_helper_class(name)
- name.sub(/Test$/, '').constantize
- rescue NameError
- nil
+ def setup_with_controller
+ @controller = ActionView::TestCase::TestController.new
+ @output_buffer = ActiveSupport::SafeBuffer.new
+ @rendered = ''
+
+ self.class.send(:include_helper_modules!)
+ make_test_case_available_to_view!
+ say_no_to_protect_against_forgery!
end
- def helper_method(*methods)
- # Almost a duplicate from ActionController::Helpers
- methods.flatten.each do |method|
- _helpers.module_eval <<-end_eval
- def #{method}(*args, &block) # def current_user(*args, &block)
- _test_case.send(%(#{method}), *args, &block) # test_case.send(%(current_user), *args, &block)
- end # end
- end_eval
- end
+ def config
+ @controller.config if @controller.respond_to?(:config)
end
- private
- def include_helper_modules!
- helper(helper_class) if helper_class
- include _helpers
- end
- end
+ def render(options = {}, local_assigns = {}, &block)
+ @rendered << output = _view.render(options, local_assigns, &block)
+ output
+ end
+
+ included do
+ setup :setup_with_controller
+ end
private
+
+ # Support the selector assertions
+ #
+ # Need to experiment if this priority is the best one: rendered => output_buffer
+ def response_from_page_or_rjs
+ HTML::Document.new(@rendered.blank? ? @output_buffer : @rendered).root
+ end
+
+ def say_no_to_protect_against_forgery!
+ _helpers.module_eval do
+ def protect_against_forgery?
+ false
+ end
+ end
+ end
+
def make_test_case_available_to_view!
test_case_instance = self
_helpers.module_eval do
@@ -110,29 +134,30 @@ module ActionView
view = ActionView::Base.new(ActionController::Base.view_paths, _assigns, @controller)
view.singleton_class.send :include, _helpers
view.singleton_class.send :include, @controller._router.url_helpers
+ view.singleton_class.send :delegate, :alert, :notice, :to => "request.flash"
view.output_buffer = self.output_buffer
view
end
- # Support the selector assertions
- #
- # Need to experiment if this priority is the best one: rendered => output_buffer
- def response_from_page_or_rjs
- HTML::Document.new(rendered.blank? ? output_buffer : rendered).root
- end
-
EXCLUDE_IVARS = %w{
+ @_result
@output_buffer
- @fixture_cache
+ @rendered
+ @templates
+ @view_context_class
+ @layouts
+ @partials
+ @controller
+
@method_name
- @_result
+ @fixture_cache
@loaded_fixtures
@test_passed
- @view
}
def _instance_variables
instance_variables - EXCLUDE_IVARS
+ instance_variables
end
def _assigns
@@ -155,5 +180,10 @@ module ActionView
super
end
end
+
+ end
+
+ include Behavior
+
end
end
diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb
index c8ba8bcaf3..d654338dba 100644
--- a/actionpack/test/controller/mime_responds_test.rb
+++ b/actionpack/test/controller/mime_responds_test.rb
@@ -668,6 +668,19 @@ class RespondWithControllerTest < ActionController::TestCase
end
end
+ def test_using_resource_for_put_with_html_rerender_on_failure_even_on_method_override
+ with_test_route_set do
+ errors = { :name => :invalid }
+ Customer.any_instance.stubs(:errors).returns(errors)
+ @request.env["rack.methodoverride.original_method"] = "POST"
+ put :using_resource
+ assert_equal "text/html", @response.content_type
+ assert_equal 200, @response.status
+ assert_equal "Edit world!\n", @response.body
+ assert_nil @response.location
+ end
+ end
+
def test_using_resource_for_put_with_xml_yields_ok_on_success
@request.accept = "application/xml"
put :using_resource
diff --git a/actionpack/test/fixtures/test/array_translation.erb b/actionpack/test/fixtures/test/array_translation.erb
deleted file mode 100644
index def3a1a0c1..0000000000
--- a/actionpack/test/fixtures/test/array_translation.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= t(['foo', 'bar', 'html']) %> \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/scoped_array_translation.erb b/actionpack/test/fixtures/test/scoped_array_translation.erb
deleted file mode 100644
index 0a0c79f717..0000000000
--- a/actionpack/test/fixtures/test/scoped_array_translation.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= t(['.foo', '.bar']) %> \ No newline at end of file
diff --git a/actionpack/test/fixtures/test/scoped_translation.erb b/actionpack/test/fixtures/test/scoped_translation.erb
new file mode 100644
index 0000000000..30baa61c77
--- /dev/null
+++ b/actionpack/test/fixtures/test/scoped_translation.erb
@@ -0,0 +1 @@
+<%= t('.foo.bar') %> \ No newline at end of file
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index d1e1338a17..2f3869994c 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -1425,12 +1425,11 @@ class FormHelperTest < ActionView::TestCase
class LabelledFormBuilder < ActionView::Helpers::FormBuilder
(field_helpers - %w(hidden_field)).each do |selector|
- src, line = <<-END_SRC, __LINE__ + 1
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{selector}(field, *args, &proc)
("<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>").html_safe
end
- END_SRC
- class_eval src, __FILE__, line
+ RUBY_EVAL
end
end
diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb
index 1c095b621e..b75863bb6a 100644
--- a/actionpack/test/template/form_tag_helper_test.rb
+++ b/actionpack/test/template/form_tag_helper_test.rb
@@ -206,6 +206,12 @@ class FormTagHelperTest < ActionView::TestCase
assert_dom_equal expected, actual
end
+ def test_text_area_tag_unescaped_nil_content
+ actual = text_area_tag "body", nil, :escape => false
+ expected = %(<textarea id="body" name="body"></textarea>)
+ assert_dom_equal expected, actual
+ end
+
def test_text_field_tag
actual = text_field_tag "title", "Hello!"
expected = %(<input id="title" name="title" type="text" value="Hello!" />)
diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb
index c1a38a25de..16e5ee4f72 100644
--- a/actionpack/test/template/test_case_test.rb
+++ b/actionpack/test/template/test_case_test.rb
@@ -2,21 +2,23 @@ require 'abstract_unit'
require 'controller/fake_controllers'
module ActionView
- class TestCase
- module ATestHelper
- end
- module AnotherTestHelper
- def from_another_helper
- 'Howdy!'
- end
+ module ATestHelper
+ end
+
+ module AnotherTestHelper
+ def from_another_helper
+ 'Howdy!'
end
+ end
- module ASharedTestHelper
- def from_shared_helper
- 'Holla!'
- end
+ module ASharedTestHelper
+ def from_shared_helper
+ 'Holla!'
end
+ end
+
+ class TestCase
helper ASharedTestHelper
module SharedTests
@@ -29,161 +31,186 @@ module ActionView
end
end
end
+ end
- class GeneralViewTest < ActionView::TestCase
- include SharedTests
- test_case = self
+ class GeneralViewTest < ActionView::TestCase
+ include SharedTests
+ test_case = self
- test "works without testing a helper module" do
- assert_equal 'Eloy', render('developers/developer', :developer => stub(:name => 'Eloy'))
- end
+ test "works without testing a helper module" do
+ assert_equal 'Eloy', render('developers/developer', :developer => stub(:name => 'Eloy'))
+ end
- test "can render a layout with block" do
- assert_equal "Before (ChrisCruft)\n!\nAfter",
- render(:layout => "test/layout_for_partial", :locals => {:name => "ChrisCruft"}) {"!"}
- end
+ test "can render a layout with block" do
+ assert_equal "Before (ChrisCruft)\n!\nAfter",
+ render(:layout => "test/layout_for_partial", :locals => {:name => "ChrisCruft"}) {"!"}
+ end
- helper AnotherTestHelper
- test "additional helper classes can be specified as in a controller" do
- assert test_case.ancestors.include?(AnotherTestHelper)
- assert_equal 'Howdy!', from_another_helper
- end
+ helper AnotherTestHelper
+ test "additional helper classes can be specified as in a controller" do
+ assert test_case.ancestors.include?(AnotherTestHelper)
+ assert_equal 'Howdy!', from_another_helper
+ end
+
+ test "determine_default_helper_class returns nil if name.sub(/Test$/, '').constantize resolves to a class" do
+ assert_nil self.class.determine_default_helper_class("String")
end
- class ClassMethodsTest < ActionView::TestCase
- include SharedTests
- test_case = self
+ test "delegates notice to request.flash" do
+ _view.request.flash.expects(:notice).with("this message")
+ _view.notice("this message")
+ end
- tests ATestHelper
- test "tests the specified helper module" do
- assert_equal ATestHelper, test_case.helper_class
- assert test_case.ancestors.include?(ATestHelper)
- end
+ test "delegates alert to request.flash" do
+ _view.request.flash.expects(:alert).with("this message")
+ _view.alert("this message")
+ end
+ end
- helper AnotherTestHelper
- test "additional helper classes can be specified as in a controller" do
- assert test_case.ancestors.include?(AnotherTestHelper)
- assert_equal 'Howdy!', from_another_helper
+ class ClassMethodsTest < ActionView::TestCase
+ include SharedTests
+ test_case = self
- test_case.helper_class.module_eval do
- def render_from_helper
- from_another_helper
- end
- end
- assert_equal 'Howdy!', render(:partial => 'test/from_helper')
- end
+ tests ATestHelper
+ test "tests the specified helper module" do
+ assert_equal ATestHelper, test_case.helper_class
+ assert test_case.ancestors.include?(ATestHelper)
end
- class ATestHelperTest < ActionView::TestCase
- include SharedTests
- test_case = self
+ helper AnotherTestHelper
+ test "additional helper classes can be specified as in a controller" do
+ assert test_case.ancestors.include?(AnotherTestHelper)
+ assert_equal 'Howdy!', from_another_helper
- test "inflects the name of the helper module to test from the test case class" do
- assert_equal ATestHelper, test_case.helper_class
- assert test_case.ancestors.include?(ATestHelper)
+ test_case.helper_class.module_eval do
+ def render_from_helper
+ from_another_helper
+ end
end
+ assert_equal 'Howdy!', render(:partial => 'test/from_helper')
+ end
+ end
- test "a configured test controller is available" do
- assert_kind_of ActionController::Base, controller
- assert_equal '', controller.controller_path
+ class HelperInclusionTest < ActionView::TestCase
+ module RenderHelper
+ def render_from_helper
+ render :partial => 'customer', :collection => @customers
end
+ end
- test "helper class that is being tested is always included in view instance" do
- # This ensure is a hidious hack to deal with these tests bleeding
- # methods between eachother
- begin
- self.class.helper_class.module_eval do
- def render_from_helper
- render :partial => 'customer', :collection => @customers
- end
- end
+ helper RenderHelper
- TestController.stubs(:controller_path).returns('test')
+ test "helper class that is being tested is always included in view instance" do
+ @controller.controller_path = 'test'
- @customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
- assert_match /Hello: EloyHello: Manfred/, render(:partial => 'test/from_helper')
+ @customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
+ assert_match /Hello: EloyHello: Manfred/, render(:partial => 'test/from_helper')
+ end
+ end
- ensure
- self.class.helper_class.send(:remove_method, :render_from_helper)
- end
+ class HelperExposureTest < ActionView::TestCase
+ helper(Module.new do
+ def render_from_helper
+ from_test_case
end
+ end)
+ test "is able to make methods available to the view" do
+ assert_equal 'Word!', render(:partial => 'test/from_helper')
+ end
- test "no additional helpers should shared across test cases" do
- assert !test_case.ancestors.include?(AnotherTestHelper)
- assert_raise(NoMethodError) { send :from_another_helper }
- end
+ def from_test_case; 'Word!'; end
+ helper_method :from_test_case
+ end
- test "is able to use routes" do
- controller.request.assign_parameters(@routes, 'foo', 'index')
- assert_equal '/foo', url_for
- assert_equal '/bar', url_for(:controller => 'bar')
+ class IgnoreProtectAgainstForgeryTest < ActionView::TestCase
+ module HelperThatInvokesProtectAgainstForgery
+ def help_me
+ protect_against_forgery?
end
+ end
- test "is able to use named routes" do
- with_routing do |set|
- set.draw { |map| resources :contents }
- assert_equal 'http://test.host/contents/new', new_content_url
- assert_equal 'http://test.host/contents/1', content_url(:id => 1)
- end
- end
+ helper HelperThatInvokesProtectAgainstForgery
- test "named routes can be used from helper included in view" do
- with_routing do |set|
- set.draw { |map| resources :contents }
- _helpers.module_eval do
- def render_from_helper
- new_content_url
- end
- end
+ test "protect_from_forgery? in any helpers returns false" do
+ assert !_view.help_me
+ end
- assert_equal 'http://test.host/contents/new', render(:partial => 'test/from_helper')
- end
- end
+ end
- test "is able to render partials with local variables" do
- assert_equal 'Eloy', render('developers/developer', :developer => stub(:name => 'Eloy'))
- assert_equal 'Eloy', render(:partial => 'developers/developer',
- :locals => { :developer => stub(:name => 'Eloy') })
- end
+ class ATestHelperTest < ActionView::TestCase
+ include SharedTests
+ test_case = self
+
+ test "inflects the name of the helper module to test from the test case class" do
+ assert_equal ATestHelper, test_case.helper_class
+ assert test_case.ancestors.include?(ATestHelper)
+ end
+
+ test "a configured test controller is available" do
+ assert_kind_of ActionController::Base, controller
+ assert_equal '', controller.controller_path
+ end
- test "is able to render partials from templates and also use instance variables" do
- TestController.stubs(:controller_path).returns('test')
+ test "no additional helpers should shared across test cases" do
+ assert !test_case.ancestors.include?(AnotherTestHelper)
+ assert_raise(NoMethodError) { send :from_another_helper }
+ end
- @customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
- assert_match /Hello: EloyHello: Manfred/, render(:file => 'test/list')
+ test "is able to use routes" do
+ controller.request.assign_parameters(@routes, 'foo', 'index')
+ assert_equal '/foo', url_for
+ assert_equal '/bar', url_for(:controller => 'bar')
+ end
+
+ test "is able to use named routes" do
+ with_routing do |set|
+ set.draw { |map| resources :contents }
+ assert_equal 'http://test.host/contents/new', new_content_url
+ assert_equal 'http://test.host/contents/1', content_url(:id => 1)
end
+ end
- test "is able to make methods available to the view" do
- # This ensure is a hidious hack to deal with these tests bleeding
- # methods between eachother
- begin
- _helpers.module_eval do
- def render_from_helper; from_test_case end
+ test "named routes can be used from helper included in view" do
+ with_routing do |set|
+ set.draw { |map| resources :contents }
+ _helpers.module_eval do
+ def render_from_helper
+ new_content_url
end
- assert_equal 'Word!', render(:partial => 'test/from_helper')
- ensure
- _helpers.send(:remove_method, :render_from_helper)
end
+
+ assert_equal 'http://test.host/contents/new', render(:partial => 'test/from_helper')
end
+ end
- def from_test_case; 'Word!'; end
- helper_method :from_test_case
+ test "is able to render partials with local variables" do
+ assert_equal 'Eloy', render('developers/developer', :developer => stub(:name => 'Eloy'))
+ assert_equal 'Eloy', render(:partial => 'developers/developer',
+ :locals => { :developer => stub(:name => 'Eloy') })
end
- class AssertionsTest < ActionView::TestCase
- def render_from_helper
- form_tag('/foo') do
- safe_concat render(:text => '<ul><li>foo</li></ul>')
- end
+ test "is able to render partials from templates and also use instance variables" do
+ @controller.controller_path = "test"
+
+ @customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
+ assert_match /Hello: EloyHello: Manfred/, render(:file => 'test/list')
+ end
+
+ end
+
+ class AssertionsTest < ActionView::TestCase
+ def render_from_helper
+ form_tag('/foo') do
+ safe_concat render(:text => '<ul><li>foo</li></ul>')
end
- helper_method :render_from_helper
+ end
+ helper_method :render_from_helper
- test "uses the output_buffer for assert_select" do
- render(:partial => 'test/from_helper')
+ test "uses the output_buffer for assert_select" do
+ render(:partial => 'test/from_helper')
- assert_select 'form' do
- assert_select 'li', :text => 'foo'
- end
+ assert_select 'form' do
+ assert_select 'li', :text => 'foo'
end
end
end
diff --git a/actionpack/test/template/translation_helper_test.rb b/actionpack/test/template/translation_helper_test.rb
index b382b5eb22..1be418a206 100644
--- a/actionpack/test/template/translation_helper_test.rb
+++ b/actionpack/test/template/translation_helper_test.rb
@@ -18,16 +18,9 @@ class TranslationHelperTest < ActiveSupport::TestCase
assert_equal expected, translate(:foo)
end
- def test_translation_of_an_array
- I18n.expects(:translate).with(["foo", "bar"], :raise => true).returns(["foo", "bar"])
- assert_equal "foobar", translate(["foo", "bar"])
- end
-
- def test_translation_of_an_array_with_html
- expected = '<a href="#">foo</a><a href="#">bar</a>'
- I18n.expects(:translate).with(["foo", "bar", "html"], :raise => true).returns(['<a href="#">foo</a>', '<a href="#">bar</a>'])
- @view = ActionView::Base.new(ActionController::Base.view_paths, {})
- assert_equal expected, @view.render(:file => "test/array_translation")
+ def test_translation_returning_an_array
+ I18n.expects(:translate).with(:foo, :raise => true).returns(["foo", "bar"])
+ assert_equal ["foo", "bar"], translate(:foo)
end
def test_delegates_localize_to_i18n
@@ -43,9 +36,9 @@ class TranslationHelperTest < ActiveSupport::TestCase
end
def test_scoping_by_partial_of_an_array
- I18n.expects(:translate).with("test.scoped_array_translation.foo.bar", :raise => true).returns(["foo", "bar"])
+ I18n.expects(:translate).with("test.scoped_translation.foo.bar", :raise => true).returns(["foo", "bar"])
@view = ActionView::Base.new(ActionController::Base.view_paths, {})
- assert_equal "foobar", @view.render(:file => "test/scoped_array_translation")
+ assert_equal "foobar", @view.render(:file => "test/scoped_translation")
end
def test_translate_does_not_mark_plain_text_as_safe_html
@@ -62,4 +55,9 @@ class TranslationHelperTest < ActiveSupport::TestCase
I18n.expects(:translate).with("hello_html", :raise => true).returns("<a>Hello World</a>")
assert translate("hello_html").html_safe?
end
+
+ def test_translation_returning_an_array_ignores_html_suffix
+ I18n.expects(:translate).with(:foo_html, :raise => true).returns(["foo", "bar"])
+ assert_equal ["foo", "bar"], translate(:foo_html)
+ end
end