diff options
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/controller/mime_responds_test.rb | 13 | ||||
-rw-r--r-- | actionpack/test/fixtures/test/array_translation.erb | 1 | ||||
-rw-r--r-- | actionpack/test/fixtures/test/scoped_array_translation.erb | 1 | ||||
-rw-r--r-- | actionpack/test/fixtures/test/scoped_translation.erb | 1 | ||||
-rw-r--r-- | actionpack/test/template/form_tag_helper_test.rb | 6 | ||||
-rw-r--r-- | actionpack/test/template/test_case_test.rb | 279 | ||||
-rw-r--r-- | actionpack/test/template/text_helper_test.rb | 58 | ||||
-rw-r--r-- | actionpack/test/template/translation_helper_test.rb | 22 |
8 files changed, 223 insertions, 158 deletions
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_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/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 8b6c107a21..0b84c8f811 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -304,6 +304,7 @@ class TextHelperTest < ActionView::TestCase assert_equal %(<p>Link #{link_result_with_options}</p>), auto_link("<p>Link #{link_raw}</p>", :all, {:target => "_blank"}) assert_equal %(Go to #{link_result}.), auto_link(%(Go to #{link_raw}.)) assert_equal %(<p>Go to #{link_result}, then say hello to #{email_result}.</p>), auto_link(%(<p>Go to #{link_raw}, then say hello to #{email_raw}.</p>)) + assert_equal %(#{link_result} #{link_result}), auto_link(%(#{link_result} #{link_raw})) email2_raw = '+david@loudthinking.com' email2_result = %{<a href="mailto:#{email2_raw}">#{email2_raw}</a>} @@ -376,24 +377,38 @@ class TextHelperTest < ActionView::TestCase end def test_auto_link_other_protocols - silence_warnings do - begin - old_re_value = ActionView::Helpers::TextHelper::AUTO_LINK_RE - ActionView::Helpers::TextHelper.const_set :AUTO_LINK_RE, %r{(ftp://)[^\s<]+} - link_raw = 'ftp://example.com/file.txt' - link_result = generate_result(link_raw) - assert_equal %(Download #{link_result}), auto_link("Download #{link_raw}") - ensure - ActionView::Helpers::TextHelper.const_set :AUTO_LINK_RE, old_re_value - end - end + ftp_raw = 'ftp://example.com/file.txt' + assert_equal %(Download #{generate_result(ftp_raw)}), auto_link("Download #{ftp_raw}") + + file_scheme = 'file:///home/username/RomeoAndJuliet.pdf' + z39_scheme = 'z39.50r://host:696/db' + chrome_scheme = 'chrome://package/section/path' + view_source = 'view-source:http://en.wikipedia.org/wiki/URI_scheme' + assert_equal generate_result(z39_scheme), auto_link(z39_scheme) + assert_equal generate_result(chrome_scheme), auto_link(chrome_scheme) + assert_equal generate_result(view_source), auto_link(view_source) end def test_auto_link_already_linked linked1 = generate_result('Ruby On Rails', 'http://www.rubyonrails.com') - linked2 = generate_result('www.rubyonrails.com', 'http://www.rubyonrails.com') + linked2 = %('<a href="http://www.example.com">www.example.com</a>') + linked3 = %('<a href="http://www.example.com" rel="nofollow">www.example.com</a>') + linked4 = %('<a href="http://www.example.com"><b>www.example.com</b></a>') + linked5 = %('<a href="#close">close</a> <a href="http://www.example.com"><b>www.example.com</b></a>') assert_equal linked1, auto_link(linked1) assert_equal linked2, auto_link(linked2) + assert_equal linked3, auto_link(linked3) + assert_equal linked4, auto_link(linked4) + assert_equal linked5, auto_link(linked5) + + linked_email = %Q(<a href="mailto:david@loudthinking.com">Mail me</a>) + assert_equal linked_email, auto_link(linked_email) + end + + def test_auto_link_within_tags + link_raw = 'http://www.rubyonrails.org/images/rails.png' + link_result = %Q(<img src="#{link_raw}" />) + assert_equal link_result, auto_link(link_result) end def test_auto_link_with_brackets @@ -413,12 +428,6 @@ class TextHelperTest < ActionView::TestCase assert_equal "{link: #{link3_result}}", auto_link("{link: #{link3_raw}}") end - def test_auto_link_in_tags - link_raw = 'http://www.rubyonrails.org/images/rails.png' - link_result = %Q(<img src="#{link_raw}" />) - assert_equal link_result, auto_link(link_result) - end - def test_auto_link_at_eol url1 = "http://api.rubyonrails.com/Foo.html" url2 = "http://www.ruby-doc.org/core/Bar.html" @@ -432,6 +441,19 @@ class TextHelperTest < ActionView::TestCase assert_equal %(<p><a href="#{url}">#{url[0...7]}...</a><br /><a href="mailto:#{email}">#{email[0...7]}...</a><br /></p>), auto_link("<p>#{url}<br />#{email}<br /></p>") { |url| truncate(url, :length => 10) } end + + def test_auto_link_with_block_with_html + pic = "http://example.com/pic.png" + url = "http://example.com/album?a&b=c" + + assert_equal %(My pic: <a href="#{pic}"><img src="#{pic}" width="160px"></a> -- full album here #{generate_result(url)}), auto_link("My pic: #{pic} -- full album here #{url}") { |link| + if link =~ /\.(jpg|gif|png|bmp|tif)$/i + raw %(<img src="#{link}" width="160px">) + else + link + end + } + end def test_auto_link_with_options_hash assert_dom_equal 'Welcome to my new blog at <a href="http://www.myblog.com/" class="menu" target="_blank">http://www.myblog.com/</a>. Please e-mail me at <a href="mailto:me@email.com" class="menu" target="_blank">me@email.com</a>.', 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 |