vendor/rails/actionpack/test/template/url_helper_test.rb in radiant-0.7.2 vs vendor/rails/actionpack/test/template/url_helper_test.rb in radiant-0.8.0
- old
+ new
@@ -1,5 +1,6 @@
+# encoding: utf-8
require 'abstract_unit'
RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port, :env)
class UrlHelperTest < ActionView::TestCase
@@ -26,10 +27,20 @@
def test_url_for_escapes_url_once
@controller.url = "http://www.example.com?a=b&c=d"
assert_equal "http://www.example.com?a=b&c=d", url_for("http://www.example.com?a=b&c=d")
end
+ def test_url_for_with_back
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'})
+ assert_equal 'http://www.example.com/referer', url_for(:back)
+ end
+
+ def test_url_for_with_back_and_no_referer
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {})
+ assert_equal 'javascript:history.back()', url_for(:back)
+ end
+
# todo: missing test cases
def test_button_to_with_straight_url
assert_dom_equal "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com")
end
@@ -78,11 +89,11 @@
end
def test_link_tag_with_straight_url
assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com")
end
-
+
def test_link_tag_without_host_option
ActionController::Base.class_eval { attr_accessor :url }
url = {:controller => 'weblog', :action => 'show'}
@controller = ActionController::Base.new
@controller.request = ActionController::TestRequest.new
@@ -119,11 +130,11 @@
def test_link_tag_with_back
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'})
assert_dom_equal "<a href=\"http://www.example.com/referer\">go back</a>", link_to('go back', :back)
end
-
+
def test_link_tag_with_back_and_no_referer
@controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {})
assert_dom_equal "<a href=\"javascript:history.back()\">go back</a>", link_to('go back', :back)
end
@@ -207,13 +218,21 @@
link_to("Hello", "http://www.example.com", :method => :post, :confirm => "Are you serious?")
)
end
def test_link_tag_using_post_javascript_and_popup
- assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
+ assert_raise(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
end
-
+
+ def test_link_tag_using_block_in_erb
+ __in_erb_template = ''
+
+ link_to("http://example.com") { concat("Example site") }
+
+ assert_equal '<a href="http://example.com">Example site</a>', output_buffer
+ end
+
def test_link_to_unless
assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")
assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1)
assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name, options, html_options|
@@ -231,16 +250,59 @@
assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog")
assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_if(true, "Listing", :action => "list", :controller => "weblog")
assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1)
end
+ def test_current_page_with_simple_url
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show")
+ @controller.url = "http://www.example.com/weblog/show"
+ assert current_page?({ :action => "show", :controller => "weblog" })
+ assert current_page?("http://www.example.com/weblog/show")
+ end
+
+ def test_current_page_ignoring_params
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
+ @controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
+ assert current_page?({ :action => "show", :controller => "weblog" })
+ assert current_page?("http://www.example.com/weblog/show")
+ end
+
+ def test_current_page_with_params_that_match
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
+ @controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
+ assert current_page?({ :action => "show", :controller => "weblog", :order => "desc", :page => "1" })
+ assert current_page?("http://www.example.com/weblog/show?order=desc&page=1")
+ end
+
def test_link_unless_current
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@controller.url = "http://www.example.com/weblog/show"
assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc")
+ @controller.url = "http://www.example.com/weblog/show"
+ assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
+ assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
+
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
+ @controller.url = "http://www.example.com/weblog/show?order=desc&page=1"
+ assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog", :order=>'desc', :page=>'1' })
+ assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1")
+ assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1")
+
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc")
+ @controller.url = "http://www.example.com/weblog/show?order=asc"
+ assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
+ assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=asc")
+
+ @controller.request = RequestMock.new("http://www.example.com/weblog/show?order=desc&page=1")
+ @controller.url = "http://www.example.com/weblog/show?order=desc&page=2"
+ assert_equal "<a href=\"http://www.example.com/weblog/show?order=desc&page=2\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
+ assert_equal "<a href=\"http://www.example.com/weblog/show?order=desc&page=2\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=2")
+
+
@controller.request = RequestMock.new("http://www.example.com/weblog/show")
@controller.url = "http://www.example.com/weblog/list"
assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
link_to_unless_current("Listing", :action => "list", :controller => "weblog")
assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
@@ -257,13 +319,17 @@
assert_equal mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin"),
mail_to("david@loudthinking.com", "David Heinemeier Hansson", :class => "admin")
end
def test_mail_to_with_javascript
- assert_dom_equal "<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript")
+ assert_dom_equal "<script type=\"text/javascript\">eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript")
end
+ def test_mail_to_with_javascript_unicode
+ assert_dom_equal "<script type=\"text/javascript\">eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%75%6e%69%63%6f%64%65%40%65%78%61%6d%70%6c%65%2e%63%6f%6d%22%3e%c3%ba%6e%69%63%6f%64%65%3c%2f%61%3e%27%29%3b'))</script>", mail_to("unicode@example.com", "Ășnicode", :encode => "javascript")
+ end
+
def test_mail_with_options
assert_dom_equal(
%(<a href="mailto:me@example.com?cc=ccaddress%40example.com&bcc=bccaddress%40example.com&body=This%20is%20the%20body%20of%20the%20message.&subject=This%20is%20an%20example%20email">My email</a>),
mail_to("me@example.com", "My email", :cc => "ccaddress@example.com", :bcc => "bccaddress@example.com", :subject => "This is an example email", :body => "This is the body of the message.")
)
@@ -281,23 +347,21 @@
def test_mail_to_with_replace_options
assert_dom_equal "<a href=\"mailto:wolfgang@stufenlos.net\">wolfgang(at)stufenlos(dot)net</a>", mail_to("wolfgang@stufenlos.net", nil, :replace_at => "(at)", :replace_dot => "(dot)")
assert_dom_equal "<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">me(at)domain.com</a>", mail_to("me@domain.com", nil, :encode => "hex", :replace_at => "(at)")
assert_dom_equal "<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>", mail_to("me@domain.com", "My email", :encode => "hex", :replace_at => "(at)")
assert_dom_equal "<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">me(at)domain(dot)com</a>", mail_to("me@domain.com", nil, :encode => "hex", :replace_at => "(at)", :replace_dot => "(dot)")
- assert_dom_equal "<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
- assert_dom_equal "<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%6d%65%28%61%74%29%64%6f%6d%61%69%6e%28%64%6f%74%29%63%6f%6d%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", nil, :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
+ assert_dom_equal "<script type=\"text/javascript\">eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
+ assert_dom_equal "<script type=\"text/javascript\">eval(decodeURIComponent('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%6d%65%28%61%74%29%64%6f%6d%61%69%6e%28%64%6f%74%29%63%6f%6d%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", nil, :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
end
def protect_against_forgery?
false
end
end
class UrlHelperWithControllerTest < ActionView::TestCase
class UrlHelperController < ActionController::Base
- self.view_paths = [ "#{File.dirname(__FILE__)}/../fixtures/" ]
-
def self.controller_path; 'url_helper_with_controller' end
def show_url_for
render :inline => "<%= url_for :controller => 'url_helper_with_controller', :action => 'show_url_for' %>"
end
@@ -343,10 +407,23 @@
def test_url_for_nil_returns_current_path
get :nil_url_for
assert_equal '/url_helper_with_controller/nil_url_for', @response.body
end
+ def test_named_route_should_show_host_and_path_using_controller_default_url_options
+ class << @controller
+ def default_url_options(options = nil)
+ {:host => 'testtwo.host'}
+ end
+ end
+
+ with_url_helper_routing do
+ get :show_named_route, :kind => 'url'
+ assert_equal 'http://testtwo.host/url_helper_with_controller/show_named_route', @response.body
+ end
+ end
+
protected
def with_url_helper_routing
with_routing do |set|
set.draw do |map|
map.show_named_route 'url_helper_with_controller/show_named_route', :controller => 'url_helper_with_controller', :action => 'show_named_route'
@@ -356,12 +433,10 @@
end
end
class LinkToUnlessCurrentWithControllerTest < ActionView::TestCase
class TasksController < ActionController::Base
- self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"]
-
def self.controller_path; 'tasks' end
def index
render_default
end
@@ -413,22 +488,21 @@
yield
end
end
end
-
class Workshop
attr_accessor :id, :new_record
def initialize(id, new_record)
@id, @new_record = id, new_record
end
-
+
def new_record?
@new_record
end
-
+
def to_s
id.to_s
end
end
@@ -448,12 +522,10 @@
end
end
class PolymorphicControllerTest < ActionView::TestCase
class WorkshopsController < ActionController::Base
- self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"]
-
def self.controller_path; 'workshops' end
def index
@workshop = Workshop.new(1, true)
render :inline => "<%= url_for(@workshop) %>\n<%= link_to('Workshop', @workshop) %>"
@@ -466,11 +538,9 @@
def rescue_action(e) raise e end
end
class SessionsController < ActionController::Base
- self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"]
-
def self.controller_path; 'sessions' end
def index
@workshop = Workshop.new(params[:workshop_id], false)
@session = Session.new(1, true)