Cleaned up unit tests and began refactoring link generation

This commit is contained in:
Alexey Verkhovsky 2005-08-25 07:41:25 +00:00
parent 84d4e71d3e
commit 49fccc41c1
5 changed files with 89 additions and 83 deletions

View file

@ -73,24 +73,6 @@ class Web
wiki.file_yard(self).has_file?(name) wiki.file_yard(self).has_file?(name)
end end
def make_file_link(mode, name, text, base_url)
link = CGI.escape(name)
case mode
when :export
if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{link}.html\">#{text}</a>"
else "<span class=\"newWikiWord\">#{text}</span>" end
when :publish
if has_file?(name) then "<a class=\"existingWikiWord\" href=\"#{base_url}/published/#{link}\">#{text}</a>"
else "<span class=\"newWikiWord\">#{text}</span>" end
else
if has_file?(name)
"<a class=\"existingWikiWord\" href=\"#{base_url}/file/#{link}\">#{text}</a>"
else
"<span class=\"newWikiWord\">#{text}<a href=\"#{base_url}/file/#{link}\">?</a></span>"
end
end
end
# Create a link for the given page name and link text based # Create a link for the given page name and link text based
# on the render mode in options and whether the page exists # on the render mode in options and whether the page exists
# in the this web. # in the this web.
@ -103,49 +85,16 @@ class Web
link_type = options[:link_type] || :show link_type = options[:link_type] || :show
case link_type.to_sym case link_type.to_sym
when :show when :show
make_page_link(mode, name, text, base_url) UrlGenerator.new.make_page_link(mode, name, text, base_url, has_page?(name))
when :file when :file
make_file_link(mode, name, text, base_url) UrlGenerator.new.make_file_link(mode, name, text, base_url, has_file?(name))
when :pic when :pic
make_pic_link(mode, name, text, base_url) UrlGenerator.new.make_pic_link(mode, name, text, base_url, has_file?(name))
else else
raise "Unknown link type: #{link_type}" raise "Unknown link type: #{link_type}"
end end
end end
def make_page_link(mode, name, text, base_url)
link = CGI.escape(name)
case mode.to_sym
when :export
if has_page?(name) then %{<a class="existingWikiWord" href="#{link}.html">#{text}</a>}
else %{<span class="newWikiWord">#{text}</span>} end
when :publish
if has_page?(name) then %{<a class="existingWikiWord" href="#{base_url}/published/#{link}">#{text}</a>}
else %{<span class="newWikiWord">#{text}</span>} end
else
if has_page?(name)
%{<a class="existingWikiWord" href="#{base_url}/show/#{link}">#{text}</a>}
else
%{<span class="newWikiWord">#{text}<a href="#{base_url}/show/#{link}">?</a></span>}
end
end
end
def make_pic_link(mode, name, text, base_url)
link = CGI.escape(name)
case mode.to_sym
when :export
if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
else %{<img alt="#{text}" src="no image" />} end
when :publish
if has_file?(name) then %{<img alt="#{text}" src="#{link}" />}
else %{<span class="newWikiWord">#{text}</span>} end
else
if has_file?(name) then %{<img alt="#{text}" src="#{base_url}/pic/#{link}" />}
else %{<span class="newWikiWord">#{text}<a href="#{base_url}/pic/#{link}">?</a></span>} end
end
end
# Clears the display cache for all the pages with references to # Clears the display cache for all the pages with references to
def refresh_pages_with_references(page_name) def refresh_pages_with_references(page_name)
select.pages_that_reference(page_name).each { |page| select.pages_that_reference(page_name).each { |page|

61
lib/url_generator.rb Normal file
View file

@ -0,0 +1,61 @@
class UrlGenerator
def initialize(controller = nil)
@controller = controller or ControllerStub.new
end
def make_file_link(mode, name, text, base_url, known_file)
link = CGI.escape(name)
case mode
when :export
if known_file then "<a class=\"existingWikiWord\" href=\"#{link}.html\">#{text}</a>"
else "<span class=\"newWikiWord\">#{text}</span>" end
when :publish
if known_file then "<a class=\"existingWikiWord\" href=\"#{base_url}/published/#{link}\">#{text}</a>"
else "<span class=\"newWikiWord\">#{text}</span>" end
else
if known_file
"<a class=\"existingWikiWord\" href=\"#{base_url}/file/#{link}\">#{text}</a>"
else
"<span class=\"newWikiWord\">#{text}<a href=\"#{base_url}/file/#{link}\">?</a></span>"
end
end
end
def make_page_link(mode, name, text, base_url, known_page)
link = CGI.escape(name)
case mode.to_sym
when :export
if known_page then %{<a class="existingWikiWord" href="#{link}.html">#{text}</a>}
else %{<span class="newWikiWord">#{text}</span>} end
when :publish
if known_page then %{<a class="existingWikiWord" href="#{base_url}/published/#{link}">#{text}</a>}
else %{<span class="newWikiWord">#{text}</span>} end
else
if known_page
%{<a class="existingWikiWord" href="#{base_url}/show/#{link}">#{text}</a>}
else
%{<span class="newWikiWord">#{text}<a href="#{base_url}/show/#{link}">?</a></span>}
end
end
end
def make_pic_link(mode, name, text, base_url, known_pic)
link = CGI.escape(name)
case mode.to_sym
when :export
if known_pic then %{<img alt="#{text}" src="#{link}" />}
else %{<img alt="#{text}" src="no image" />} end
when :publish
if known_pic then %{<img alt="#{text}" src="#{link}" />}
else %{<span class="newWikiWord">#{text}</span>} end
else
if known_pic then %{<img alt="#{text}" src="#{base_url}/pic/#{link}" />}
else %{<span class="newWikiWord">#{text}<a href="#{base_url}/pic/#{link}">?</a></span>} end
end
end
end
class ControllerStub
end

View file

@ -34,8 +34,8 @@ class FileControllerTest < Test::Unit::TestCase
r = process 'file', 'web' => 'wiki1', 'id' => 'foo.txt' r = process 'file', 'web' => 'wiki1', 'id' => 'foo.txt'
assert_success assert_success(bypass_body_parsing = true)
assert_equal "aaa\nbbb\n", r.binary_content assert_equal "aaa\nbbb\n", r.body
assert_equal 'text/plain', r.headers['Content-Type'] assert_equal 'text/plain', r.headers['Content-Type']
end end
@ -44,8 +44,8 @@ class FileControllerTest < Test::Unit::TestCase
r = process 'file', 'web' => 'wiki1', 'id' => 'foo.pdf' r = process 'file', 'web' => 'wiki1', 'id' => 'foo.pdf'
assert_success assert_success(bypass_body_parsing = true)
assert_equal "aaa\nbbb\n", r.binary_content assert_equal "aaa\nbbb\n", r.body
assert_equal 'application/pdf', r.headers['Content-Type'] assert_equal 'application/pdf', r.headers['Content-Type']
end end
@ -54,8 +54,8 @@ class FileControllerTest < Test::Unit::TestCase
r = process 'pic', 'web' => 'wiki1', 'id' => 'rails.gif' r = process 'pic', 'web' => 'wiki1', 'id' => 'rails.gif'
assert_success assert_success(bypass_body_parsing = true)
assert_equal File.size("#{FILE_AREA}/rails.gif"), r.binary_content.size assert_equal File.size("#{FILE_AREA}/rails.gif"), r.body.size
end end
def test_pic_unknown_pic def test_pic_unknown_pic

View file

@ -111,18 +111,17 @@ class WikiControllerTest < Test::Unit::TestCase
r = process 'export_html', 'web' => 'wiki1' r = process 'export_html', 'web' => 'wiki1'
assert_success assert_success(bypass_body_parsing = true)
assert_equal 'application/zip', r.headers['Content-Type'] assert_equal 'application/zip', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-html-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/, assert_match /attachment; filename="wiki1-html-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/,
r.headers['Content-Disposition'] r.headers['Content-Disposition']
content = r.binary_content assert_equal 'PK', r.body[0..1], 'Content is not a zip file'
assert_equal 'PK', content[0..1], 'Content is not a zip file'
assert_equal :export, r.template_objects['link_mode'] assert_equal :export, r.template_objects['link_mode']
# Tempfile doesn't know how to open files with binary flag, hence the two-step process # Tempfile doesn't know how to open files with binary flag, hence the two-step process
Tempfile.open('instiki_export_file') { |f| @tempfile_path = f.path } Tempfile.open('instiki_export_file') { |f| @tempfile_path = f.path }
begin begin
File.open(@tempfile_path, 'wb') { |f| f.write(content); @exported_file = f.path } File.open(@tempfile_path, 'wb') { |f| f.write(r.body); @exported_file = f.path }
Zip::ZipFile.open(@exported_file) do |zip| Zip::ZipFile.open(@exported_file) do |zip|
assert_equal %w(Elephant.html HomePage.html Oak.html index.html), zip.dir.entries('.').sort assert_equal %w(Elephant.html HomePage.html Oak.html index.html), zip.dir.entries('.').sort
assert_match /.*<html .*All about elephants.*<\/html>/, assert_match /.*<html .*All about elephants.*<\/html>/,
@ -143,38 +142,35 @@ class WikiControllerTest < Test::Unit::TestCase
r = process 'export_html', 'web' => 'wiki1', 'layout' => 'no' r = process 'export_html', 'web' => 'wiki1', 'layout' => 'no'
assert_success assert_success(bypass_body_parsing = true)
assert_equal 'application/zip', r.headers['Content-Type'] assert_equal 'application/zip', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-html-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/, assert_match /attachment; filename="wiki1-html-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/,
r.headers['Content-Disposition'] r.headers['Content-Disposition']
content = r.binary_content assert_equal 'PK', r.body[0..1], 'Content is not a zip file'
assert_equal 'PK', content[0..1], 'Content is not a zip file'
assert_equal :export, r.template_objects['link_mode'] assert_equal :export, r.template_objects['link_mode']
end end
def test_export_markup def test_export_markup
r = process 'export_markup', 'web' => 'wiki1' r = process 'export_markup', 'web' => 'wiki1'
assert_success assert_success(bypass_body_parsing = true)
assert_equal 'application/zip', r.headers['Content-Type'] assert_equal 'application/zip', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-textile-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/, assert_match /attachment; filename="wiki1-textile-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/,
r.headers['Content-Disposition'] r.headers['Content-Disposition']
content = r.binary_content assert_equal 'PK', r.body[0..1], 'Content is not a zip file'
assert_equal 'PK', content[0..1], 'Content is not a zip file'
end end
if ENV['INSTIKI_TEST_LATEX'] or defined? $INSTIKI_TEST_PDFLATEX if ENV['INSTIKI_TEST_PDFLATEX'] or defined? $INSTIKI_TEST_PDFLATEX
def test_export_pdf def test_export_pdf
r = process 'export_pdf', 'web' => 'wiki1' r = process 'export_pdf', 'web' => 'wiki1'
assert_success assert_success(bypass_body_parsing = true)
assert_equal 'application/pdf', r.headers['Content-Type'] assert_equal 'application/pdf', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-tex-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.pdf"/, assert_match /attachment; filename="wiki1-tex-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.pdf"/,
r.headers['Content-Disposition'] r.headers['Content-Disposition']
content = r.binary_content assert_equal '%PDF', r.body[0..3]
assert_equal '%PDF', content[0..3] assert_equal "EOF\n", r.body[-4..-1]
assert_equal "EOF\n", content[-4..-1]
end end
else else
@ -189,12 +185,11 @@ class WikiControllerTest < Test::Unit::TestCase
r = process 'export_tex', 'web' => 'wiki1' r = process 'export_tex', 'web' => 'wiki1'
assert_success assert_success(bypass_body_parsing = true)
assert_equal 'application/octet-stream', r.headers['Content-Type'] assert_equal 'application/octet-stream', r.headers['Content-Type']
assert_match /attachment; filename="wiki1-tex-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.tex"/, assert_match(/attachment; filename="wiki1-tex-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.tex"/,
r.headers['Content-Disposition'] r.headers['Content-Disposition'])
content = r.binary_content assert_equal '\documentclass', r.body[0..13], 'Content is not a TeX file'
assert_equal '\documentclass', content[0..13], 'Content is not a TeX file'
end end
def test_feeds def test_feeds

View file

@ -118,10 +118,11 @@ if defined? $validate_xml_in_assert_success and $validate_xml_in_assert_success
unless method_defined? :__assert_success_before_ovverride_by_instiki unless method_defined? :__assert_success_before_ovverride_by_instiki
alias :__assert_success_before_ovverride_by_instiki :assert_success alias :__assert_success_before_ovverride_by_instiki :assert_success
end end
def assert_success def assert_success(bypass_body_parsing = false)
__assert_success_before_ovverride_by_instiki __assert_success_before_ovverride_by_instiki
if @response.body.kind_of?(Proc) then # it's a file download, not an HTML content unless bypass_body_parsing
else assert_nothing_raised(@response.body) { REXML::Document.new(@response.body) } end assert_nothing_raised(@response.body) { REXML::Document.new(@response.body) }
end
end end
end end
end end