#!/usr/bin/env ruby # encoding: UTF-8 require File.expand_path(File.dirname(__FILE__) + '/../test_helper') class PageRendererTest < ActiveSupport::TestCase fixtures :webs, :pages, :revisions, :system, :wiki_references def setup @wiki = Wiki.new @web = webs(:test_wiki) @page = pages(:home_page) @revision = revisions(:home_page_second_revision) end def test_wiki_word_linking @web.add_page('SecondPage', 'Yo, yo. Have you EverBeenHated', Time.now, 'DavidHeinemeierHansson', test_renderer) assert_equal("

Yo, yo. Have you Ever Been Hated" + "?

", rendered_content(@web.page("SecondPage"))) @web.add_page('EverBeenHated', 'Yo, yo. Have you EverBeenHated', Time.now, 'DavidHeinemeierHansson', test_renderer) assert_equal("

Yo, yo. Have you Ever Been Hated

", rendered_content(@web.page("SecondPage"))) end def test_wiki_words assert_equal %w( HisWay MyWay SmartEngine SmartEngineGUI ThatWay ), test_renderer(@revision).wiki_words.sort @wiki.write_page('wiki1', 'NoWikiWord', 'hey you!', Time.now, 'Me', test_renderer) assert_equal [], test_renderer(@wiki.read_page('wiki1', 'NoWikiWord').revisions.last).wiki_words end def test_existing_pages assert_equal %w( MyWay SmartEngine ThatWay ), test_renderer(@revision).existing_pages.sort end def test_unexisting_pages assert_equal %w( HisWay SmartEngineGUI ), test_renderer(@revision).unexisting_pages.sort end def test_content_with_wiki_links assert_equal "

His Way? " + "would be My Way " + "" + "sin(x)" + " in kinda " + "That Way in " + "His Way? " + %{though My Way OverThere \342\200\223 see } + "Smart Engine in that " + "Smart Engine GUI" + "?

", test_renderer(@revision).display_content end def test_markdown set_web_property :markup, :markdownMML assert_markup_parsed_as( %{

equation } + %{sin(x)

}, "equation $\\sin(x)$") re = Regexp.new('\\A

My Headline

\n\n

that Smart Engine GUI\?

\\Z') assert_match_markup_parsed_as(re, "My Headline\n===========\n\nthat SmartEngineGUI") assert_match_markup_parsed_as(re, "#My Headline#\n\nthat SmartEngineGUI") str1 = %{
\n
Definition
\n\n

Let H} + %{ be a subgroup of a group G. A left coset of H in G} + %{ is a subset of G that is of the form xH, where x} + %{\342\210\210G and xH=\\\{<} + %{/mo>xh:h\342\210\210H\\\}.

\n\n

Similarly a right coset of H in G is a subset of G that is of the form Hx, where Hx=\\\{hx:h\342\210\210} + %{H\\\}.

\n
\n\n} + %{
\n
Lemma
\n\n

} + %{Let } + %{H be a subgroup of a group G, and let x and y be elements of G. Suppose that xH\342\210\251y} + %{H is non-empty. Then xH=yH.

\n\n\n
\n
Proof
\n\n

Let z be some e} + %{lement of xH\342\210\251yH.

\n
\n\n} + %{
\n
Lemma
\n\n

} + %{Let } + %{H be a finite subgroup of a group G.

\n
\n\n} + %{
\n
Theorem
\n\n

\\(Lagrange\342\200\231s Theorem\\). Let G be a finite group, and let H be a subgroup of G.

\n
} str2 = <SVG } + %{Math ML?

}, "SVG MathML") assert_markup_parsed_as( %{
sin} + %{(x)} + %{} + %{\\sin(x) \\begin{svg}<svg/>\\end{svg}
}, "$$\\sin(x) \\begin{svg}\\end{svg}$$") code_block = [ 'This is a code block:', '', ' def a_method(arg)', ' return ThatWay', '', 'Nice!' ].join("\n") assert_markup_parsed_as( %{

This is a code block:

\n\n
def a_method(arg)\n} +
        %{return ThatWay
\n\n

Nice!

}, code_block) assert_markup_parsed_as(%{

You then needed to edit (or create) a user.js file in} + %{ your Mozilla profile, which read either (Mac OSX?)

\n\n
  user_pref("font.mat} +
          %{hfont-family", "Math1,Math2,Math4,Symbol");
}, %{You then needed to edit (or create) a user.js file in your Mozilla profile, whic} + %{h read either (MacOSX)\n\n user_pref("font.mathfont-family", "Math1,Math2,Math4,Symbol");}) assert_markup_parsed_as( %{

} + %{sin(x) ecuasi\303\263n

}, "$\\sin(x)$ ecuasi\303\263n") assert_markup_parsed_as( %{

ecuasi\303\263n

\n
sin} + %{(x)} + %{\\sin(x)
}, "ecuasi\303\263n\n$$\\sin(x)$$") assert_markup_parsed_as( %{

ecuasi\303\263n

\n\n

} + %{sin(x)

}, "ecuasi\303\263n \n\n$\\sin(x)$") assert_markup_parsed_as( %{

ecuasi\303\263n } + %{sin(x)

}, "ecuasi\303\263n $\\sin(x)$") end def test_have_latest_itex2mml assert_markup_parsed_as( %{

equation AB

}, "equation $A \\mathllap{B}$") assert_markup_parsed_as( %{

equation A\342\211\224B

}, "equation $A \\coloneqq B$") assert_markup_parsed_as( %{

equation A\342\206\255B

}, "equation $A \\leftrightsquigarrow B$") assert_markup_parsed_as( %{

equation A\314\262

}, "equation $\\underline{A}$") assert_markup_parsed_as( %{

equation } + %{A\342\205\213B

}, "equation $A \\invamp B$") assert_markup_parsed_as( %{

blackboard digits: math} + %{bb123

}, "blackboard digits: $\mathbb{123}$") assert_markup_parsed_as( %{

\\rlap: } + %{123

}, '\rlap: $\rlap{123}$') end def test_blahtex if Kernel::system('blahtex --help > /dev/null 2>&1') set_web_property :markup, :markdownPNG re = Regexp.new( %{

equation \\$a\\\\sin\\(\\\\theta\\)\\$

}) assert_match_markup_parsed_as(re, 'equation $a\sin(\theta)$') re = Regexp.new( %{
\\$a\\\\sin\\(\\\\theta\\)\\$a\\\\sin} + %{\\(\\\\theta\\)<\/code><\/span><\/div>}) assert_match_markup_parsed_as(re, '$$a\sin(\theta)$$') else print "\nBlahTeX not found ... skipping.\n" end end def test_markdown_hyperlink_with_slash # in response to a bug, see http://dev.instiki.org/attachment/ticket/177 set_web_property :markup, :markdown assert_markup_parsed_as( "

text

", '[text](http://example/with/slash)') end def test_mixed_formatting textile_and_markdown = [ 'Markdown heading', '================', '', 'h2. Textile heading', '', '*some* **text** _with_ -styles-', '', '* list 1', '* list 2' ].join("\n") set_web_property :markup, :markdownMML re = Regexp.new( '

Markdown heading

\n\n' + "

h2. Textile heading

\n\n" + "

some text with -styles-

\n\n" + "
    \n
  • list 1
  • \n\n
  • list 2
  • \n
") assert_match_markup_parsed_as(re, textile_and_markdown) set_web_property :markup, :textile assert_markup_parsed_as( "

Markdown heading
================

\n\n\n\t

Textile heading

" + "\n\n\n\t

some text with styles

" + "\n\n\n\t
    \n\t
  • list 1
  • \n\t\t
  • list 2
  • \n\t
", textile_and_markdown) set_web_property :markup, :mixed assert_markup_parsed_as( "

Markdown heading

\n\n\n\t

Textile heading

\n\n\n\t" + "

some text with styles

\n\n\n\t" + "
    \n\t
  • list 1
  • \n\t\t
  • list 2
  • \n\t
", textile_and_markdown) end def test_rdoc set_web_property :markup, :rdoc @revision = Revision.new(:page => @page, :content => '+hello+ that SmartEngineGUI', :author => Author.new('DavidHeinemeierHansson')) assert_equal "hello that Smart Engine GUI" + "?\n\n", test_renderer(@revision).display_content end # def test_content_with_auto_links # assert_markup_parsed_as( # '

http://www.loudthinking.com/ ' + # 'points to That Way from ' + # 'david@loudthinking.com

', # 'http://www.loudthinking.com/ points to ThatWay from david@loudthinking.com') # # end def test_content_with_aliased_links assert_markup_parsed_as( "

Would a clever motor" + ' go by any other name?

', 'Would a [[SmartEngine|clever motor]] go by any other name?') end def test_content_with_wikiword_in_em assert_markup_parsed_as( "

should we go " + "That Way or This Way?" + '

', '_should we go ThatWay or ThisWay _') end def test_content_with_redirected_link assert_markup_parsed_as( "

This is a redirected link: " + "booze. This is not: hooch?" + '

', 'This is a redirected link: [[booze]]. This is not: [[hooch]]') end def test_content_with_wikiword_in_equations assert_markup_parsed_as( "

should we go " + "That Way or

\n
" + "ThisWay" + "ThisWay" + "(1)
", "should we go ThatWay or \n\\[ThisWay\\]\n") assert_markup_parsed_as( "

should we go " + "That Way or

\n
" + "ThisWay" + "ThisWay" + "
", "should we go ThatWay or \n$$ThisWay$$\n") assert_markup_parsed_as( "

should we go " + "That Way or

\n
" + "ThisWay$" + "100 ThatWay" + "ThisWay \\$100 " + "ThatWay
", "should we go ThatWay or \n$$ThisWay \\$100 ThatWay $$\n") assert_markup_parsed_as( "

should we go " + "That Way or ThisWay today.

", "should we go ThatWay or ThisWay today.") assert_markup_parsed_as( "

should we go " + "That Way or ThisWay.

", "should we go ThatWay or $ThisWay$.") end def test_content_with_wikiword_in_equations_textile set_web_property :markup, :textile assert_markup_parsed_as( "

$$foo?" + "$$
$foo?$

", "$$[[foo]]$$\n$[[foo]]$") end # wikiwords are invalid as styles, must be in "name: value" form def test_content_with_wikiword_in_style_tag assert_markup_parsed_as( "

That is some Stylish Emphasis

", "That is some Stylish Emphasis") end # validates format of style.. def test_content_with_valid_style_in_style_tag assert_markup_parsed_as( "

That is some Stylish Emphasis

", "That is some Stylish Emphasis") end def test_content_with_escaped_wikiword # there should be no wiki link assert_markup_parsed_as('

WikiWord

', '\WikiWord') end def test_content_with_pre_blocks set_web_property :markup, :markdownMML assert_markup_parsed_as( "

A class SmartEngine would not mark up

\n\n
CodeBlocks
\n\n

would it?

", "A `class SmartEngine` would not mark up\n\n CodeBlocks\n\nwould it?") assert_markup_parsed_as( "

A class SmartEngine would not mark up

\n
CodeBlocks
\n

would it?

", "A class SmartEngine would not mark up\n\n
CodeBlocks
\n\nwould it?") end # def test_content_with_autolink_in_parentheses # assert_markup_parsed_as( # '

The W3C body (' + # 'http://www.w3c.org) sets web standards

', # 'The W3C body (http://www.w3c.org) sets web standards') # end def test_content_with_link_in_parentheses assert_markup_parsed_as( "

(What is a wiki?)

", '([What is a wiki?](http://wiki.org/wiki.cgi?WhatIsWiki))') end def test_content_with_image_link assert_markup_parsed_as( "

This is a Markdown image link.

", 'This ![](http://hobix.com/sample.jpg) is a Markdown image link.') end def test_content_with_inlined_img_tag assert_markup_parsed_as( "

This is an inline image link.

", 'This is an inline image link.') # currently, upper case HTML elements are not allowed assert_markup_parsed_as( "

This <IMG SRC='http://hobix.com/sample.jpg' alt=''/> is an inline image link.

", 'This is an inline image link.') end def test_nowiki_tag assert_markup_parsed_as( '

Do not mark up [[this text]] or http://www.thislink.com.

', 'Do not mark up [[this text]] ' + 'or http://www.thislink.com.') end def test_malformed_nowiki assert_markup_parsed_as( '

', ' ') end def test_multiline_nowiki_tag assert_markup_parsed_as( "

Do not mark \n up [[this text]] \nand http://this.url.com but markup " + "this?

", "Do not mark \n up [[this text]] \n" + "and http://this.url.com but markup [[this]]") end def test_markdown_nowiki_tag assert_markup_parsed_as( '

Do not mark up *this text* or http://www.thislink.com.

', 'Do not mark up *this text* ' + 'or http://www.thislink.com.') end def test_sanitize_nowiki_tag assert_markup_parsed_as( '

[[test]]&shebang <script>alert("xss!");</script> *foo*

', '[[test]]&shebang *foo*') end def test_entities assert_markup_parsed_as( "

Known: \342\210\256. Pass-through: &. Unknown: &foo;.

", "Known: ∮. Pass-through: &. Unknown: &foo;.") end def test_content_with_bracketted_wiki_word set_web_property :brackets_only, true assert_markup_parsed_as( "

This is a WikiWord and a tricky name " + "Sperberg-McQueen?.

", 'This is a WikiWord and a tricky name [[Sperberg-McQueen]].') end def test_content_for_export assert_equal "

His Way would be " + "My Way " + "" + "sin(x)" + " in kinda " + "That Way in " + "His Way though " + %{My Way OverThere \342\200\223 see } + "Smart Engine in that " + "Smart Engine GUI

", test_renderer(@revision).display_content_for_export end def test_double_replacing @revision.content = "VersionHistory\r\n\r\ncry VersionHistory" assert_equal "

Version History" + "?

\n\n

cry " + "Version History?" + '

', test_renderer(@revision).display_content @revision.content = "f\r\nVersionHistory\r\n\r\ncry VersionHistory" assert_equal "

f Version History" + "?

\n\n

cry " + "Version History?" + "

", test_renderer(@revision).display_content end def test_difficult_wiki_words @revision.content = "[[It's just awesome GUI!]]" assert_equal "

It's just awesome GUI!" + "?

", test_renderer(@revision).display_content end def test_revisions_diff Revision.create(:page => @page, :content => 'What a blue and lovely morning', :author => Author.new('DavidHeinemeierHansson'), :revised_at => Time.now) Revision.create(:page => @page, :content => 'What a red and lovely morning today', :author => Author.new('DavidHeinemeierHansson'), :revised_at => Time.now) @page.reload assert_equal "

What a blue red" + " and lovely morning today

", test_renderer(@page.revisions.last).display_diff end def test_nowiki_sanitization assert_markup_parsed_as('

This sentence contains a & b ' + '<script>alert("XSS!");</script>. Do not touch!

', 'This sentence contains a & b . Do not touch!') end def test_link_to_file assert_markup_parsed_as( "

doc.pdf?

", '[[doc.pdf:file]]') end def test_link_to_pic_and_file WikiFile.delete_all require 'fileutils' FileUtils.rm_rf("#{RAILS_ROOT}/webs/wiki1/files/*") @web.wiki_files.create(:file_name => 'square.jpg', :description => 'Square', :content => 'never mind') assert_markup_parsed_as( "

Blue Square

", '[[square.jpg|Blue Square:pic]]') assert_markup_parsed_as( "

Square

", '[[square.jpg:pic]]') assert_markup_parsed_as( "

Blue Square

", '[[square.jpg|Blue Square:file]]') assert_markup_parsed_as( "

", '[[square.jpg|Blue Square:video]]') assert_markup_parsed_as( "

", '[[square.jpg|Blue Square:audio]]') assert_markup_parsed_as( "

Square

", '[[square.jpg:file]]') end def test_link_to_pic_and_file_null_desc WikiFile.delete_all require 'fileutils' FileUtils.rm_rf("#{RAILS_ROOT}/webs/wiki1/files/*") @web.wiki_files.create(:file_name => 'square.jpg', :description => '', :content => 'never mind') assert_markup_parsed_as( "

Blue Square

", '[[square.jpg|Blue Square:pic]]') assert_markup_parsed_as( "

", '[[square.jpg:pic]]') assert_markup_parsed_as( "

Blue Square

", '[[square.jpg|Blue Square:file]]') assert_markup_parsed_as( "

", '[[square.jpg:file]]') end def test_link_to_non_existant_pic assert_markup_parsed_as( "

NonExistant?" + '

', '[[NonExistant.jpg|NonExistant:pic]]') assert_markup_parsed_as( "

NonExistant.jpg?" + '

', '[[NonExistant.jpg:pic]]') end def test_wiki_link_with_colon assert_markup_parsed_as( "

Instiki:Colon?

", '[[Instiki:Colon]]') end def test_list_with_tildas list_with_tildas = <<-EOL * [a](~b) * c~ d EOL assert_markup_parsed_as( "
    \n
  • a
  • \n\n
  • c~ d
  • \n
", list_with_tildas) end def test_textile_image_in_mixed_wiki set_web_property :markup, :mixed assert_markup_parsed_as( "

\nss

", "!http://google.com!\r\nss") end def test_references_creation_links new_page = @web.add_page('NewPage', 'HomePage NewPage', Time.local(2004, 4, 4, 16, 50), 'AlexeyVerkhovsky', test_renderer) references = new_page.wiki_references(true) assert_equal 2, references.size assert_equal 'HomePage', references[0].referenced_name assert_equal WikiReference::LINKED_PAGE, references[0].link_type assert_equal 'NewPage', references[1].referenced_name assert_equal WikiReference::LINKED_PAGE, references[1].link_type end def test_references_creation_includes new_page = @web.add_page('NewPage', '[[!include IncludedPage]]', Time.local(2004, 4, 4, 16, 50), 'AlexeyVerkhovsky', test_renderer) references = new_page.wiki_references(true) assert_equal 1, references.size assert_equal 'IncludedPage', references[0].referenced_name assert_equal WikiReference::INCLUDED_PAGE, references[0].link_type end def test_references_creation_categories new_page = @web.add_page('NewPage', "Foo\ncategory: NewPageCategory", Time.local(2004, 4, 4, 16, 50), 'AlexeyVerkhovsky', test_renderer) references = new_page.wiki_references(true) assert_equal 1, references.size assert_equal 'NewPageCategory', references[0].referenced_name assert_equal WikiReference::CATEGORY, references[0].link_type end def test_references_creation_sanitized_categories new_page = @web.add_page('NewPage', "Foo\ncategory: ", Time.local(2004, 4, 4, 16, 50), 'AlexeyVerkhovsky', test_renderer) references = new_page.wiki_references(true) assert_equal 1, references.size assert_equal "<script>alert('XSS');</script>", references[0].referenced_name assert_equal WikiReference::CATEGORY, references[0].link_type end def test_rendering_included_page_under_different_modes included = @web.add_page('Included', 'link to HomePage', Time.now, 'AnAuthor', test_renderer) main = @web.add_page('Main', '[[!include Included]]', Time.now, 'AnAuthor', test_renderer) assert_equal "

link to Home Page

", test_renderer(main).display_content assert_equal "

link to Home Page

", test_renderer(main).display_published assert_equal "

link to Home Page

", test_renderer(main).display_content_for_export end def test_rendering_included_page_backslashes_in_equations included = @web.add_page('Included', '\\\\ $\begin{matrix} a \\\\ b\end{matrix}$', Time.now, 'AnAuthor', test_renderer) main = @web.add_page('Main', '[[!include Included]]', Time.now, 'AnAuthor', test_renderer) assert_equal "

\\ " + "a b" + "

", test_renderer(main).display_content end private def add_sample_pages @in_love = @web.add_page('EverBeenInLove', 'Who am I me', Time.local(2004, 4, 4, 16, 50), 'DavidHeinemeierHansson', test_renderer) @hated = @web.add_page('EverBeenHated', 'I am me EverBeenHated', Time.local(2004, 4, 4, 16, 51), 'DavidHeinemeierHansson', test_renderer) end def assert_markup_parsed_as(expected_output, input) revision = Revision.new(:page => @page, :content => input, :author => Author.new('AnAuthor')) assert_equal expected_output, test_renderer(revision).display_content(true), 'Rendering output not as expected' end def assert_match_markup_parsed_as(expected_output, input) revision = Revision.new(:page => @page, :content => input, :author => Author.new('AnAuthor')) assert_match expected_output, test_renderer(revision).display_content, 'Rendering output not as expected' end def rendered_content(page) test_renderer(page.revisions.last).display_content end end