require File.dirname(__FILE__) + '/../spec_helper'
require File.dirname(__FILE__) + '/../../test/test_helper'
require 'textfilter_controller'
require 'flickr_mock'
# Re-raise errors caught by the controller.
class TextfilterController; def rescue_action(e) raise e end; end
class ActionController::Base; def rescue_action(e) raise e end; end
describe TextfilterController do
before do
reset_whiteboard
get :test_action # set up @url; In Rails 1.0, we can't do url_for without it.
end
def blog
blogs(:default)
end
def filter_text(text, filters, filterparams={})
TextFilter.filter_text(blog, text, self, filters, filterparams)
end
def whiteboard
@whiteboard ||= Hash.new
end
def reset_whiteboard
@whiteboard = nil
end
def sparklines_available
begin
Plugins::Textfilters::SparklineController
rescue NameError
false
end
end
def test_unknown
text = filter_text('*foo*',[:unknowndoesnotexist])
assert_equal '*foo*', text
end
def test_smartypants
text = filter_text('"foo"',[:smartypants])
assert_equal '“foo”', text
end
def test_markdown
text = filter_text('*foo*', [:markdown])
assert_equal '
foo
', text
text = filter_text("foo\n\nbar",[:markdown])
assert_equal "foo
\n\nbar
", text
end
def test_filterchain
assert_equal '“foo”
',
filter_text('*"foo"*',[:markdown,:smartypants])
assert_equal '“foo”
',
filter_text('*"foo"*',[:doesntexist1,:markdown,"doesn't exist 2",:smartypants,:nopenotmeeither])
end
def test_flickr
assert_equal "This is Matz, Ruby's creator
",
filter_text('',
[:macropre,:macropost],
{'flickr-user' => 'scott@sigkill.org'})
# Test default image size
assert_equal "This is Matz, Ruby's creator
",
filter_text('',
[:macropre,:macropost],
{'flickr-user' => 'scott@sigkill.org'})
# Test with caption=""
assert_equal "",
filter_text('',
[:macropre,:macropost],
{'flickr-user' => 'scott@sigkill.org'})
end
def test_broken_flickr_link
assert_equal %{\`notaflickrid\' could not be displayed because:
Photo not found
},
filter_text('',
[:macropre, :macropost],
{ 'flickr-user' => 'scott@sigkill.org' })
end
def test_sparkline
return unless sparklines_available
tag = filter_text('',[:macropre,:macropost])
# url_for returns query params in hash order, which isn't stable, so we can't just compare
# with a static string. Yuck.
assert tag =~ %r{^$}
assert_equal "",
filter_text('',[:macropre,:macropost])
assert_equal "",
filter_text('',[:macropre,:macropost])
tag = filter_text('',[:macropre,:macropost])
assert_tag_in tag, :tag => 'img', :attributes => {
'alt' => 'ccc',
'src' => URI.parse('http://test.host/plugins/filters/sparkline/plot?data=')
}, :children => { :count => 0 }
tag = filter_text('',[:macropre,:macropost])
assert_tag_in tag, :tag => 'img', :attributes => {
'src' => URI.parse('http://test.host/plugins/filters/sparkline/plot?data=1%2C2%2C3%2C4&type=smooth')
}, :children => { :count => 0 }
assert_equal "",
filter_text('1 2 3 4 5 6',[:macropre,:macropost])
end
def test_sparkline_plot
return unless sparklines_available
get 'public_action', :filter => 'sparkline', :public_action => 'plot', :data => '1,2,3'
assert_response :success
get 'public_action', :filter => 'sparkline', :public_action => 'plot2', :data => '1,2,3'
assert_response :missing
get 'public_action', :filter => 'sparkline', :public_action => 'plot', :data => '1,2,3', :type => 'smooth'
assert_response :success
get 'public_action', :filter => 'sparkline', :public_action => 'plot', :data => '1,2,3', :type => 'instance_methods'
assert_response :error
end
def test_code
assert_equal %{},
filter_text('foo-code',[:macropre,:macropost])
assert_equal %{},
filter_text('foo-code',[:macropre,:macropost])
assert_equal %{ blah blah },
filter_text('foo-code blah blah zzz',[:macropre,:macropost])
end
def test_code_multiline
assert_equal %{\nclass Foo\n def bar\n @a = "zzz"\n end\nend
\n},
filter_text(%{
class Foo
def bar
@a = "zzz"
end
end
},[:macropre,:macropost])
end
def test_named_filter
assert_equal '“foo”
',
TextFilter.filter_text_by_name(blog, '*"foo"*', 'markdown smartypants')
end
def test_code_plus_markup_chain
text = <<-EOF
*header text here*
class test
def method
"foo"
end
end
_footer text here_
EOF
expects_markdown = <<-EOF
header text here
class test
def method
"foo"
end
end
footer text here
EOF
expects_textile = <<-EOF
header text here
class test
def method
"foo"
end
end
\tfooter text here
EOF
assert_equal expects_markdown.strip, TextFilter.filter_text_by_name(blog, text, 'markdown')
assert_equal expects_textile.strip, TextFilter.filter_text_by_name(blog, text, 'textile')
end
def test_lightbox
assert_equal "This is Matz, Ruby's creator
",
filter_text('',
[:macropre,:macropost],
{})
# Test default thumb image size
assert_equal "This is Matz, Ruby's creator
",
filter_text('',
[:macropre,:macropost],
{})
# Test default display image size
assert_equal "This is Matz, Ruby's creator
",
filter_text('',
[:macropre,:macropost],
{})
# Test with caption=""
assert_equal "",
filter_text('',
[:macropre,:macropost],
{})
end
end