From 49fccc41c1a69e699307e8a5c356386f0065c353 Mon Sep 17 00:00:00 2001 From: Alexey Verkhovsky Date: Thu, 25 Aug 2005 07:41:25 +0000 Subject: [PATCH] Cleaned up unit tests and began refactoring link generation --- app/models/web.rb | 57 ++--------------------- lib/url_generator.rb | 61 +++++++++++++++++++++++++ test/functional/file_controller_test.rb | 12 ++--- test/functional/wiki_controller_test.rb | 35 ++++++-------- test/test_helper.rb | 7 +-- 5 files changed, 89 insertions(+), 83 deletions(-) create mode 100644 lib/url_generator.rb diff --git a/app/models/web.rb b/app/models/web.rb index 0e21fc80..60e936ee 100644 --- a/app/models/web.rb +++ b/app/models/web.rb @@ -73,24 +73,6 @@ class Web wiki.file_yard(self).has_file?(name) end - def make_file_link(mode, name, text, base_url) - link = CGI.escape(name) - case mode - when :export - if has_file?(name) then "#{text}" - else "#{text}" end - when :publish - if has_file?(name) then "#{text}" - else "#{text}" end - else - if has_file?(name) - "#{text}" - else - "#{text}?" - end - end - end - # Create a link for the given page name and link text based # on the render mode in options and whether the page exists # in the this web. @@ -103,49 +85,16 @@ class Web link_type = options[:link_type] || :show case link_type.to_sym 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 - make_file_link(mode, name, text, base_url) + UrlGenerator.new.make_file_link(mode, name, text, base_url, has_file?(name)) when :pic - make_pic_link(mode, name, text, base_url) + UrlGenerator.new.make_pic_link(mode, name, text, base_url, has_file?(name)) else raise "Unknown link type: #{link_type}" 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 %{#{text}} - else %{#{text}} end - when :publish - if has_page?(name) then %{#{text}} - else %{#{text}} end - else - if has_page?(name) - %{#{text}} - else - %{#{text}?} - 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 %{#{text}} - else %{#{text}} end - when :publish - if has_file?(name) then %{#{text}} - else %{#{text}} end - else - if has_file?(name) then %{#{text}} - else %{#{text}?} end - end - end - # Clears the display cache for all the pages with references to def refresh_pages_with_references(page_name) select.pages_that_reference(page_name).each { |page| diff --git a/lib/url_generator.rb b/lib/url_generator.rb new file mode 100644 index 00000000..4fd1ff93 --- /dev/null +++ b/lib/url_generator.rb @@ -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 "#{text}" + else "#{text}" end + when :publish + if known_file then "#{text}" + else "#{text}" end + else + if known_file + "#{text}" + else + "#{text}?" + 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 %{#{text}} + else %{#{text}} end + when :publish + if known_page then %{#{text}} + else %{#{text}} end + else + if known_page + %{#{text}} + else + %{#{text}?} + 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 %{#{text}} + else %{#{text}} end + when :publish + if known_pic then %{#{text}} + else %{#{text}} end + else + if known_pic then %{#{text}} + else %{#{text}?} end + end + end + +end + +class ControllerStub +end \ No newline at end of file diff --git a/test/functional/file_controller_test.rb b/test/functional/file_controller_test.rb index 23f18c42..5cd1ef26 100755 --- a/test/functional/file_controller_test.rb +++ b/test/functional/file_controller_test.rb @@ -34,8 +34,8 @@ class FileControllerTest < Test::Unit::TestCase r = process 'file', 'web' => 'wiki1', 'id' => 'foo.txt' - assert_success - assert_equal "aaa\nbbb\n", r.binary_content + assert_success(bypass_body_parsing = true) + assert_equal "aaa\nbbb\n", r.body assert_equal 'text/plain', r.headers['Content-Type'] end @@ -44,8 +44,8 @@ class FileControllerTest < Test::Unit::TestCase r = process 'file', 'web' => 'wiki1', 'id' => 'foo.pdf' - assert_success - assert_equal "aaa\nbbb\n", r.binary_content + assert_success(bypass_body_parsing = true) + assert_equal "aaa\nbbb\n", r.body assert_equal 'application/pdf', r.headers['Content-Type'] end @@ -54,8 +54,8 @@ class FileControllerTest < Test::Unit::TestCase r = process 'pic', 'web' => 'wiki1', 'id' => 'rails.gif' - assert_success - assert_equal File.size("#{FILE_AREA}/rails.gif"), r.binary_content.size + assert_success(bypass_body_parsing = true) + assert_equal File.size("#{FILE_AREA}/rails.gif"), r.body.size end def test_pic_unknown_pic diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 1ec11afe..aa2bf238 100755 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -111,18 +111,17 @@ class WikiControllerTest < Test::Unit::TestCase r = process 'export_html', 'web' => 'wiki1' - assert_success + assert_success(bypass_body_parsing = true) 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"/, r.headers['Content-Disposition'] - content = r.binary_content - assert_equal 'PK', content[0..1], 'Content is not a zip file' + assert_equal 'PK', r.body[0..1], 'Content is not a zip file' 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.open('instiki_export_file') { |f| @tempfile_path = f.path } 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| assert_equal %w(Elephant.html HomePage.html Oak.html index.html), zip.dir.entries('.').sort assert_match /.*/, @@ -143,38 +142,35 @@ class WikiControllerTest < Test::Unit::TestCase 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_match /attachment; filename="wiki1-html-\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d.zip"/, r.headers['Content-Disposition'] - content = r.binary_content - assert_equal 'PK', content[0..1], 'Content is not a zip file' + assert_equal 'PK', r.body[0..1], 'Content is not a zip file' assert_equal :export, r.template_objects['link_mode'] end def test_export_markup r = process 'export_markup', 'web' => 'wiki1' - assert_success + assert_success(bypass_body_parsing = true) 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"/, r.headers['Content-Disposition'] - content = r.binary_content - assert_equal 'PK', content[0..1], 'Content is not a zip file' + assert_equal 'PK', r.body[0..1], 'Content is not a zip file' 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 r = process 'export_pdf', 'web' => 'wiki1' - assert_success + assert_success(bypass_body_parsing = true) 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"/, r.headers['Content-Disposition'] - content = r.binary_content - assert_equal '%PDF', content[0..3] - assert_equal "EOF\n", content[-4..-1] + assert_equal '%PDF', r.body[0..3] + assert_equal "EOF\n", r.body[-4..-1] end else @@ -189,12 +185,11 @@ class WikiControllerTest < Test::Unit::TestCase r = process 'export_tex', 'web' => 'wiki1' - assert_success + assert_success(bypass_body_parsing = true) 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"/, - r.headers['Content-Disposition'] - content = r.binary_content - assert_equal '\documentclass', content[0..13], 'Content is not a TeX file' + 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']) + assert_equal '\documentclass', r.body[0..13], 'Content is not a TeX file' end def test_feeds diff --git a/test/test_helper.rb b/test/test_helper.rb index fe8c1e29..ae799f18 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 alias :__assert_success_before_ovverride_by_instiki :assert_success end - def assert_success + def assert_success(bypass_body_parsing = false) __assert_success_before_ovverride_by_instiki - if @response.body.kind_of?(Proc) then # it's a file download, not an HTML content - else assert_nothing_raised(@response.body) { REXML::Document.new(@response.body) } end + unless bypass_body_parsing + assert_nothing_raised(@response.body) { REXML::Document.new(@response.body) } + end end end end