diff options
author | Xavier Noria <fxn@hashref.com> | 2010-06-20 23:13:19 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-06-20 23:13:19 +0200 |
commit | 207fa59675cb624dde0e01c1d57597f752cdf095 (patch) | |
tree | 472d03af5bc3e2df1b759717f723ca914f2a77aa /actionpack/test/template | |
parent | 31cadc730a40281950a265ff6982dd76cc326828 (diff) | |
parent | 50d37a76064239a731f81a4ba68b80946c4dfae2 (diff) | |
download | rails-207fa59675cb624dde0e01c1d57597f752cdf095.tar.gz rails-207fa59675cb624dde0e01c1d57597f752cdf095.tar.bz2 rails-207fa59675cb624dde0e01c1d57597f752cdf095.zip |
Merge remote branch 'rails/master'
Conflicts:
actionpack/lib/abstract_controller/base.rb
Diffstat (limited to 'actionpack/test/template')
-rw-r--r-- | actionpack/test/template/date_helper_test.rb | 27 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 20 | ||||
-rw-r--r-- | actionpack/test/template/form_options_helper_test.rb | 30 | ||||
-rw-r--r-- | actionpack/test/template/test_case_test.rb | 8 | ||||
-rw-r--r-- | actionpack/test/template/text_helper_test.rb | 89 |
5 files changed, 103 insertions, 71 deletions
diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 053fcc4d24..a1db49d4d0 100644 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -882,6 +882,33 @@ class DateHelperTest < ActionView::TestCase assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { :date_separator => " / ", :start_year => 2003, :end_year => 2005, :prefix => "date[first]"}) end + def test_select_date_with_separator_and_discard_day + expected = %(<select id="date_first_year" name="date[first][year]">\n) + expected << %(<option value="2003" selected="selected">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n) + expected << "</select>\n" + + expected << " / " + + expected << %(<select id="date_first_month" name="date[first][month]">\n) + expected << %(<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6">June</option>\n<option value="7">July</option>\n<option value="8" selected="selected">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n) + expected << "</select>\n" + + expected << %(<input type="hidden" id="date_first_day" name="date[first][day]" value="1" />\n) + + assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { :date_separator => " / ", :discard_day => true, :start_year => 2003, :end_year => 2005, :prefix => "date[first]"}) + end + + def test_select_date_with_separator_discard_month_and_day + expected = %(<select id="date_first_year" name="date[first][year]">\n) + expected << %(<option value="2003" selected="selected">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n) + expected << "</select>\n" + + expected << %(<input type="hidden" id="date_first_month" name="date[first][month]" value="8" />\n) + expected << %(<input type="hidden" id="date_first_day" name="date[first][day]" value="16" />\n) + + assert_dom_equal expected, select_date(Time.mktime(2003, 8, 16), { :date_separator => " / ", :discard_month => true, :discard_day => true, :start_year => 2003, :end_year => 2005, :prefix => "date[first]"}) + end + def test_select_datetime expected = %(<select id="date_first_year" name="date[first][year]">\n) expected << %(<option value="2003" selected="selected">2003</option>\n<option value="2004">2004</option>\n<option value="2005">2005</option>\n) diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 2f3869994c..8de1e782c0 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -644,6 +644,26 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_with_remote_without_html + assert_deprecated do + form_for(:post, @post, :remote => true) do |f| + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + end + end + + expected = + "<form action='http://www.example.com' method='post' data-remote='true'>" + + "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" + + "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" + + "<input name='post[secret]' type='hidden' value='0' />" + + "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" + + "</form>" + + assert_dom_equal expected, output_buffer + end + def test_form_for_without_object form_for(:post, :html => { :id => 'create-post' }) do |f| concat f.text_field(:title) diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 19b73aa810..65b5f5ccc1 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -177,17 +177,16 @@ class FormOptionsHelperTest < ActionView::TestCase end def test_option_groups_from_collection_for_select - @continents = [ - Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ), - Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) - ] - assert_dom_equal( "<optgroup label=\"<Africa>\"><option value=\"<sa>\"><South Africa></option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option value=\"dk\" selected=\"selected\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>", - option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk") + option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk") ) end + def test_option_groups_from_collection_for_select_returns_html_safe_string + assert option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk").html_safe? + end + def test_grouped_options_for_select_with_array assert_dom_equal( "<optgroup label=\"North America\"><option value=\"US\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup><optgroup label=\"Europe\"><option value=\"GB\">Great Britain</option>\n<option value=\"Germany\">Germany</option></optgroup>", @@ -824,31 +823,21 @@ class FormOptionsHelperTest < ActionView::TestCase end def test_grouped_collection_select - @continents = [ - Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ), - Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) - ] - @post = Post.new @post.origin = 'dk' assert_dom_equal( %Q{<select id="post_origin" name="post[origin]"><optgroup label="<Africa>"><option value="<sa>"><South Africa></option>\n<option value="so">Somalia</option></optgroup><optgroup label="Europe"><option value="dk" selected="selected">Denmark</option>\n<option value="ie">Ireland</option></optgroup></select>}, - grouped_collection_select("post", "origin", @continents, :countries, :continent_name, :country_id, :country_name) + grouped_collection_select("post", "origin", dummy_continents, :countries, :continent_name, :country_id, :country_name) ) end def test_grouped_collection_select_under_fields_for - @continents = [ - Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ), - Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) - ] - @post = Post.new @post.origin = 'dk' output_buffer = fields_for :post, @post do |f| - concat f.grouped_collection_select("origin", @continents, :countries, :continent_name, :country_id, :country_name) + concat f.grouped_collection_select("origin", dummy_continents, :countries, :continent_name, :country_id, :country_name) end assert_dom_equal( @@ -864,4 +853,9 @@ class FormOptionsHelperTest < ActionView::TestCase Post.new("Babe went home", "Babe", "To a little house", "shh!"), Post.new("Cabe went home", "Cabe", "To a little house", "shh!") ] end + + def dummy_continents + [ Continent.new("<Africa>", [Country.new("<sa>", "<South Africa>"), Country.new("so", "Somalia")] ), + Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) ] + end end diff --git a/actionpack/test/template/test_case_test.rb b/actionpack/test/template/test_case_test.rb index 9b50ea8a42..c365aec841 100644 --- a/actionpack/test/template/test_case_test.rb +++ b/actionpack/test/template/test_case_test.rb @@ -218,4 +218,12 @@ module ActionView end end end + + class RenderTemplateTest < ActionView::TestCase + test "render template" do + controller.controller_path = "test" + render(:template => "test/calling_partial_with_layout") + assert_template "partial_for_use_in_layout" + end + end end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index b0a4c2a9cc..d22b9fe406 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -19,6 +19,10 @@ class TextHelperTest < ActionView::TestCase assert_equal 'foobar', output_buffer end + def test_simple_format_should_be_html_safe + assert simple_format("<b> test with html tags </b>").html_safe? + end + def test_simple_format assert_equal "<p></p>", simple_format(nil) @@ -36,43 +40,25 @@ class TextHelperTest < ActionView::TestCase assert_equal %Q(<p class="test">para 1</p>\n\n<p class="test">para 2</p>), simple_format("para 1\n\npara 2", :class => 'test') end - def test_simple_format_should_be_html_safe - assert simple_format("<b> test with html tags </b>").html_safe? + def test_simple_format_should_sanitize_input_when_sanitize_option_is_not_false + assert_equal "<p><b> test with unsafe string </b></p>", simple_format("<b> test with unsafe string </b><script>code!</script>") end - def test_simple_format_should_escape_unsafe_input - assert_equal "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>") + def test_simple_format_should_not_sanitize_input_when_sanitize_option_is_false + assert_equal "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>", {}, :sanitize => false) end - def test_simple_format_should_not_escape_input_if_safe_option - assert_equal "<p><b> test with unsafe string </b><script>code!</script></p>", simple_format("<b> test with unsafe string </b><script>code!</script>", {}, :safe => true) + def test_truncate_should_not_be_html_safe + assert !truncate("Hello World!", :length => 12).html_safe? end - def test_simple_format_should_not_escape_safe_input - assert_equal "<p><b> test with safe string </b></p>", simple_format("<b> test with safe string </b>".html_safe) - end - - def test_truncate_should_be_html_safe - assert truncate("Hello World!", :length => 12).html_safe? - end - def test_truncate assert_equal "Hello World!", truncate("Hello World!", :length => 12) assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12) end - def test_truncate_should_escape_unsafe_input - assert_equal "Hello <...", truncate("Hello <script>code!</script>World!!", :length => 12) - end - - def test_truncate_should_not_escape_input_if_safe_option - assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!", :length => 12, :safe => true) - assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!!", :length => 12, :safe => true) - end - - def test_truncate_should_not_escape_safe_input - assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!".html_safe, :length => 12) - assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!!".html_safe, :length => 12) + def test_truncate_should_not_escape_input + assert_equal "Hello <sc...", truncate("Hello <script>code!</script>World!!", :length => 12) end def test_truncate_should_use_default_length_of_30 @@ -138,24 +124,17 @@ class TextHelperTest < ActionView::TestCase assert_equal ' ', highlight(' ', 'blank text is returned verbatim') end - def test_highlight_should_escape_unsafe_input + def test_highlight_should_sanitize_input assert_equal( - "This is a <strong class=\"highlight\">beautiful</strong> morning<script>code!</script>", + "This is a <strong class=\"highlight\">beautiful</strong> morning", highlight("This is a beautiful morning<script>code!</script>", "beautiful") ) end - def test_highlight_should_not_escape_input_if_safe_option - assert_equal( - "This is a <strong class=\"highlight\">beautiful</strong> morning<script>code!</script>", - highlight("This is a beautiful morning<script>code!</script>", "beautiful", :safe => true) - ) - end - - def test_highlight_should_not_escape_safe_input + def test_highlight_should_not_sanitize_if_sanitize_option_if_false assert_equal( "This is a <strong class=\"highlight\">beautiful</strong> morning<script>code!</script>", - highlight("This is a beautiful morning<script>code!</script>".html_safe, "beautiful") + highlight("This is a beautiful morning<script>code!</script>", "beautiful", :sanitize => false) ) end @@ -189,23 +168,23 @@ class TextHelperTest < ActionView::TestCase def test_highlight_with_html assert_equal( - "<p>This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", + "<p>This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", highlight("<p>This is a beautiful morning, but also a beautiful day</p>", "beautiful") ) assert_equal( - "<p>This is a <em><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", + "<p>This is a <em><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", highlight("<p>This is a <em>beautiful</em> morning, but also a beautiful day</p>", "beautiful") ) assert_equal( - "<p>This is a <em class="error"><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> <span class="last">day</span></p>", + "<p>This is a <em class=\"error\"><strong class=\"highlight\">beautiful</strong></em> morning, but also a <strong class=\"highlight\">beautiful</strong> <span class=\"last\">day</span></p>", highlight("<p>This is a <em class=\"error\">beautiful</em> morning, but also a beautiful <span class=\"last\">day</span></p>", "beautiful") ) assert_equal( - "<p class="<strong class=\"highlight\">beautiful</strong>">This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", + "<p class=\"beautiful\">This is a <strong class=\"highlight\">beautiful</strong> morning, but also a <strong class=\"highlight\">beautiful</strong> day</p>", highlight("<p class=\"beautiful\">This is a beautiful morning, but also a beautiful day</p>", "beautiful") ) assert_equal( - "<p>This is a <strong class=\"highlight\">beautiful</strong> <a href="http://example.com/<strong class=\"highlight\">beautiful</strong>#top?what=<strong class=\"highlight\">beautiful</strong>%20morning&when=now+then">morning</a>, but also a <strong class=\"highlight\">beautiful</strong> day</p>", + "<p>This is a <strong class=\"highlight\">beautiful</strong> <a href=\"http://example.com/beautiful#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a <strong class=\"highlight\">beautiful</strong> day</p>", highlight("<p>This is a beautiful <a href=\"http://example.com/beautiful\#top?what=beautiful%20morning&when=now+then\">morning</a>, but also a beautiful day</p>", "beautiful") ) end @@ -217,6 +196,10 @@ class TextHelperTest < ActionView::TestCase assert_nil excerpt("This is a beautiful morning", "day") end + def test_excerpt_should_not_be_html_safe + assert !excerpt('This is a beautiful! morning', 'beautiful', 5).html_safe? + end + def test_excerpt_in_borderline_cases assert_equal("", excerpt("", "", 0)) assert_equal("a", excerpt("a", "a", 0)) @@ -323,9 +306,13 @@ class TextHelperTest < ActionView::TestCase end end - def generate_result(link_text, href = nil) + def generate_result(link_text, href = nil, escape = false) href ||= link_text - %{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>} + if escape + %{<a href="#{CGI::escapeHTML href}">#{CGI::escapeHTML link_text}</a>} + else + %{<a href="#{href}">#{link_text}</a>} + end end def test_auto_link_should_be_html_safe @@ -430,19 +417,14 @@ class TextHelperTest < ActionView::TestCase assert_equal %(<p>#{link10_result} Link</p>), auto_link("<p>#{link10_raw} Link</p>") end - def test_auto_link_should_sanitize_unsafe_input - link_raw = %{http://www.rubyonrails.com?id=1&num=2} - assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw) - end - - def test_auto_link_should_sanitize_unsafe_input + def test_auto_link_should_sanitize_input_when_sanitize_option_is_not_false link_raw = %{http://www.rubyonrails.com?id=1&num=2} - assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw, :safe => true) + assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw) end - def test_auto_link_should_not_sanitize_safe_input + def test_auto_link_should_not_sanitize_input_when_sanitize_option_is_false link_raw = %{http://www.rubyonrails.com?id=1&num=2} - assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw.html_safe) + assert_equal %{<a href="http://www.rubyonrails.com?id=1&num=2">http://www.rubyonrails.com?id=1&num=2</a>}, auto_link(link_raw, :sanitize => false) end def test_auto_link_other_protocols @@ -453,6 +435,7 @@ class TextHelperTest < ActionView::TestCase 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(file_scheme), auto_link(file_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) |