2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "rails/engine"
|
2008-12-15 12:47:39 -05:00
|
|
|
|
2009-09-28 14:31:30 -04:00
|
|
|
module ActionView
|
2010-05-26 00:12:09 -04:00
|
|
|
module ATestHelper
|
|
|
|
end
|
|
|
|
|
|
|
|
module AnotherTestHelper
|
|
|
|
def from_another_helper
|
2016-08-06 12:50:17 -04:00
|
|
|
"Howdy!"
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
module ASharedTestHelper
|
|
|
|
def from_shared_helper
|
2016-08-06 12:50:17 -04:00
|
|
|
"Holla!"
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class TestCase
|
2009-09-28 14:31:30 -04:00
|
|
|
helper ASharedTestHelper
|
2015-08-22 00:03:53 -04:00
|
|
|
DeveloperStruct = Struct.new(:name)
|
2009-09-28 14:31:30 -04:00
|
|
|
|
|
|
|
module SharedTests
|
2019-02-07 16:30:58 -05:00
|
|
|
def setup
|
|
|
|
ActionView::LookupContext::DetailsKey.clear
|
2019-02-08 18:17:02 -05:00
|
|
|
super
|
2019-02-07 16:30:58 -05:00
|
|
|
end
|
|
|
|
|
2009-09-28 14:31:30 -04:00
|
|
|
def self.included(test_case)
|
|
|
|
test_case.class_eval do
|
|
|
|
test "helpers defined on ActionView::TestCase are available" do
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes test_case.ancestors, ASharedTestHelper
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "Holla!", from_shared_helper
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
class GeneralViewTest < ActionView::TestCase
|
|
|
|
include SharedTests
|
|
|
|
test_case = self
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-06-23 11:19:13 -04:00
|
|
|
test "memoizes the view" do
|
|
|
|
assert_same view, view
|
|
|
|
end
|
|
|
|
|
2015-12-29 10:37:07 -05:00
|
|
|
test "exposes params" do
|
|
|
|
assert params.is_a? ActionController::Parameters
|
|
|
|
end
|
|
|
|
|
2010-06-23 11:19:13 -04:00
|
|
|
test "exposes view as _view for backwards compatibility" do
|
|
|
|
assert_same _view, view
|
2010-06-08 15:18:02 -04:00
|
|
|
end
|
|
|
|
|
2011-07-08 20:44:31 -04:00
|
|
|
test "retrieve non existing config values" do
|
2019-01-29 18:17:52 -05:00
|
|
|
assert_nil ActionView::Base.empty.config.something_odd
|
2011-07-08 20:44:31 -04:00
|
|
|
end
|
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "works without testing a helper module" do
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Eloy", render("developers/developer", developer: DeveloperStruct.new("Eloy"))
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "can render a layout with block" do
|
|
|
|
assert_equal "Before (ChrisCruft)\n!\nAfter",
|
2016-08-16 03:30:11 -04:00
|
|
|
render(layout: "test/layout_for_partial", locals: { name: "ChrisCruft" }) { "!" }
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-11-06 17:02:55 -05:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
helper AnotherTestHelper
|
|
|
|
test "additional helper classes can be specified as in a controller" do
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes test_case.ancestors, AnotherTestHelper
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "Howdy!", from_another_helper
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2010-05-04 10:00:46 -04:00
|
|
|
|
2012-09-24 17:37:45 -04:00
|
|
|
test "determine_default_helper_class returns nil if the test name constant resolves to a class" do
|
2010-05-26 00:12:09 -04:00
|
|
|
assert_nil self.class.determine_default_helper_class("String")
|
|
|
|
end
|
2010-05-04 10:00:46 -04:00
|
|
|
|
2012-07-06 14:34:56 -04:00
|
|
|
test "delegates notice to request.flash[:notice]" do
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_called_with(view.request.flash, :[], [:notice]) do
|
|
|
|
view.notice
|
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2010-05-04 10:00:46 -04:00
|
|
|
|
2012-07-06 14:34:56 -04:00
|
|
|
test "delegates alert to request.flash[:alert]" do
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_called_with(view.request.flash, :[], [:alert]) do
|
|
|
|
view.alert
|
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2011-05-04 05:26:02 -04:00
|
|
|
|
|
|
|
test "uses controller lookup context" do
|
2016-08-07 19:05:28 -04:00
|
|
|
assert_equal lookup_context, @controller.lookup_context
|
2011-05-04 05:26:02 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
class ClassMethodsTest < ActionView::TestCase
|
|
|
|
include SharedTests
|
|
|
|
test_case = self
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
tests ATestHelper
|
|
|
|
test "tests the specified helper module" do
|
|
|
|
assert_equal ATestHelper, test_case.helper_class
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes test_case.ancestors, ATestHelper
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
helper AnotherTestHelper
|
|
|
|
test "additional helper classes can be specified as in a controller" do
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes test_case.ancestors, AnotherTestHelper
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "Howdy!", from_another_helper
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test_case.helper_class.module_eval do
|
|
|
|
def render_from_helper
|
|
|
|
from_another_helper
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
|
|
|
end
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Howdy!", render(partial: "test/from_helper")
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
class HelperInclusionTest < ActionView::TestCase
|
|
|
|
module RenderHelper
|
|
|
|
def render_from_helper
|
2016-08-06 13:36:34 -04:00
|
|
|
render partial: "customer", collection: @customers
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
helper RenderHelper
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "helper class that is being tested is always included in view instance" do
|
2016-08-06 12:50:17 -04:00
|
|
|
@controller.controller_path = "test"
|
2010-03-04 18:07:26 -05:00
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
@customers = [DeveloperStruct.new("Eloy"), DeveloperStruct.new("Manfred")]
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_match(/Hello: EloyHello: Manfred/, render(partial: "test/from_helper"))
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2011-01-12 09:14:00 -05:00
|
|
|
class ControllerHelperMethod < ActionView::TestCase
|
|
|
|
module SomeHelper
|
|
|
|
def some_method
|
2016-08-06 13:36:34 -04:00
|
|
|
render partial: "test/from_helper"
|
2011-01-12 09:14:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helper SomeHelper
|
|
|
|
|
|
|
|
test "can call a helper method defined on the current controller from a helper" do
|
|
|
|
@controller.singleton_class.class_eval <<-EOF, __FILE__, __LINE__ + 1
|
|
|
|
def render_from_helper
|
|
|
|
'controller_helper_method'
|
|
|
|
end
|
|
|
|
EOF
|
|
|
|
@controller.class.helper_method :render_from_helper
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "controller_helper_method", some_method
|
2011-01-12 09:14:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-02 13:35:17 -04:00
|
|
|
class ViewAssignsTest < ActionView::TestCase
|
|
|
|
test "view_assigns returns a Hash of user defined ivars" do
|
2016-08-06 12:50:17 -04:00
|
|
|
@a = "b"
|
|
|
|
@c = "d"
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal({ a: "b", c: "d" }, view_assigns)
|
2010-10-02 13:35:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "view_assigns excludes internal ivars" do
|
|
|
|
INTERNAL_IVARS.each do |ivar|
|
|
|
|
assert defined?(ivar), "expected #{ivar} to be defined"
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes view_assigns.keys, ivar.to_s.tr("@", "").to_sym, "expected #{ivar} to be excluded from view_assigns"
|
2010-10-02 13:35:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-01-17 17:45:24 -05:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
class HelperExposureTest < ActionView::TestCase
|
|
|
|
helper(Module.new do
|
|
|
|
def render_from_helper
|
2020-02-27 14:46:05 -05:00
|
|
|
from_test_case(suffix: "!")
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end)
|
|
|
|
test "is able to make methods available to the view" do
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Word!", render(partial: "test/from_helper")
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2020-02-27 14:46:05 -05:00
|
|
|
def from_test_case(suffix: "?"); "Word#{suffix}"; end
|
2010-05-26 00:12:09 -04:00
|
|
|
helper_method :from_test_case
|
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:46:00 -04:00
|
|
|
class IgnoreProtectAgainstForgeryTest < ActionView::TestCase
|
|
|
|
module HelperThatInvokesProtectAgainstForgery
|
|
|
|
def help_me
|
|
|
|
protect_against_forgery?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
helper HelperThatInvokesProtectAgainstForgery
|
|
|
|
|
|
|
|
test "protect_from_forgery? in any helpers returns false" do
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not view.help_me
|
2010-05-26 00:46:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
class ATestHelperTest < ActionView::TestCase
|
|
|
|
include SharedTests
|
|
|
|
test_case = self
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "inflects the name of the helper module to test from the test case class" do
|
|
|
|
assert_equal ATestHelper, test_case.helper_class
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes test_case.ancestors, ATestHelper
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "a configured test controller is available" do
|
|
|
|
assert_kind_of ActionController::Base, controller
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "", controller.controller_path
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "no additional helpers should shared across test cases" do
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes test_case.ancestors, AnotherTestHelper
|
2010-05-26 00:12:09 -04:00
|
|
|
assert_raise(NoMethodError) { send :from_another_helper }
|
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "is able to use routes" do
|
2016-08-06 12:50:17 -04:00
|
|
|
controller.request.assign_parameters(@routes, "foo", "index", {}, "/foo", [])
|
2018-09-24 17:49:26 -04:00
|
|
|
with_routing do |set|
|
|
|
|
set.draw {
|
|
|
|
get :foo, to: "foo#index"
|
|
|
|
get :bar, to: "bar#index"
|
|
|
|
}
|
|
|
|
assert_equal "/foo", url_for
|
|
|
|
assert_equal "/bar", url_for(controller: "bar")
|
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "is able to use named routes" do
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw { resources :contents }
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "http://test.host/contents/new", new_content_url
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "http://test.host/contents/1", content_url(id: 1)
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2012-06-01 11:42:33 -04:00
|
|
|
test "is able to use mounted routes" do
|
|
|
|
with_routing do |set|
|
2014-07-17 00:48:26 -04:00
|
|
|
app = Class.new(Rails::Engine) do
|
2012-06-01 11:42:33 -04:00
|
|
|
def self.routes
|
|
|
|
@routes ||= ActionDispatch::Routing::RouteSet.new
|
|
|
|
end
|
|
|
|
|
2018-09-25 13:18:20 -04:00
|
|
|
routes.draw { get "bar", to: lambda { } }
|
2012-06-01 11:42:33 -04:00
|
|
|
|
|
|
|
def self.call(*)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
set.draw { mount app => "/foo", :as => "foo_app" }
|
|
|
|
|
2018-09-24 17:49:26 -04:00
|
|
|
singleton_class.include set.mounted_helpers
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "/foo/bar", foo_app.bar_path
|
2012-06-01 11:42:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "named routes can be used from helper included in view" do
|
|
|
|
with_routing do |set|
|
2010-08-05 09:44:23 -04:00
|
|
|
set.draw { resources :contents }
|
2010-05-26 00:12:09 -04:00
|
|
|
_helpers.module_eval do
|
|
|
|
def render_from_helper
|
|
|
|
new_content_url
|
2010-03-04 18:07:26 -05:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "http://test.host/contents/new", render(partial: "test/from_helper")
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "is able to render partials with local variables" do
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "Eloy", render("developers/developer", developer: DeveloperStruct.new("Eloy"))
|
|
|
|
assert_equal "Eloy", render(partial: "developers/developer",
|
|
|
|
locals: { developer: DeveloperStruct.new("Eloy") })
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "is able to render partials from templates and also use instance variables" do
|
|
|
|
@controller.controller_path = "test"
|
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
@customers = [DeveloperStruct.new("Eloy"), DeveloperStruct.new("Manfred")]
|
2019-03-19 14:56:53 -04:00
|
|
|
assert_match(/Hello: EloyHello: Manfred/, render(template: "test/list"))
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
|
|
|
|
2010-06-23 11:19:13 -04:00
|
|
|
test "is able to render partials from templates and also use instance variables after view has been referenced" do
|
2010-06-23 08:59:56 -04:00
|
|
|
@controller.controller_path = "test"
|
|
|
|
|
2010-06-23 11:19:13 -04:00
|
|
|
view
|
2010-06-23 08:59:56 -04:00
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
@customers = [DeveloperStruct.new("Eloy"), DeveloperStruct.new("Manfred")]
|
2019-03-19 14:56:53 -04:00
|
|
|
assert_match(/Hello: EloyHello: Manfred/, render(template: "test/list"))
|
2010-06-23 08:59:56 -04:00
|
|
|
end
|
2017-07-25 22:27:11 -04:00
|
|
|
|
|
|
|
test "is able to use helpers that depend on the view flow" do
|
|
|
|
assert_not content_for?(:foo)
|
|
|
|
|
|
|
|
content_for :foo, "bar"
|
|
|
|
assert content_for?(:foo)
|
|
|
|
assert_equal "bar", content_for(:foo)
|
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class AssertionsTest < ActionView::TestCase
|
|
|
|
def render_from_helper
|
2016-08-06 12:50:17 -04:00
|
|
|
form_tag("/foo") do
|
2016-05-21 08:49:38 -04:00
|
|
|
safe_concat render(plain: "<ul><li>foo</li></ul>")
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
2010-05-26 00:12:09 -04:00
|
|
|
end
|
|
|
|
helper_method :render_from_helper
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2010-05-26 00:12:09 -04:00
|
|
|
test "uses the output_buffer for assert_select" do
|
2016-08-06 13:36:34 -04:00
|
|
|
render(partial: "test/from_helper")
|
2009-09-28 14:31:30 -04:00
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_select "form" do
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_select "li", text: "foo"
|
2009-09-28 14:31:30 -04:00
|
|
|
end
|
|
|
|
end
|
2014-09-08 17:03:17 -04:00
|
|
|
|
|
|
|
test "do not memoize the document_root_element in view tests" do
|
2016-08-06 12:50:17 -04:00
|
|
|
concat form_tag("/foo")
|
2014-09-08 17:03:17 -04:00
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_select "form"
|
2014-09-08 17:03:17 -04:00
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
concat content_tag(:b, "Strong", class: "foo")
|
2014-09-08 17:03:17 -04:00
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_select "form"
|
|
|
|
assert_select "b.foo"
|
2014-09-08 17:03:17 -04:00
|
|
|
end
|
2008-12-15 12:47:39 -05:00
|
|
|
end
|
2010-06-18 23:20:10 -04:00
|
|
|
|
2010-09-13 15:51:42 -04:00
|
|
|
module AHelperWithInitialize
|
|
|
|
def initialize(*)
|
|
|
|
super
|
|
|
|
@called_initialize = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AHelperWithInitializeTest < ActionView::TestCase
|
|
|
|
test "the helper's initialize was actually called" do
|
|
|
|
assert @called_initialize
|
|
|
|
end
|
|
|
|
end
|
2008-12-15 12:47:39 -05:00
|
|
|
end
|