Chris@909: # Redmine - project management software
Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909: #
Chris@909: # This program is free software; you can redistribute it and/or
Chris@909: # modify it under the terms of the GNU General Public License
Chris@909: # as published by the Free Software Foundation; either version 2
Chris@909: # of the License, or (at your option) any later version.
Chris@909: #
Chris@909: # This program is distributed in the hope that it will be useful,
Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909: # GNU General Public License for more details.
Chris@909: #
Chris@909: # You should have received a copy of the GNU General Public License
Chris@909: # along with this program; if not, write to the Free Software
Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909:
Chris@909: require File.expand_path('../../../test_helper', __FILE__)
Chris@909:
Chris@909: class ApplicationHelperTest < ActionView::TestCase
Chris@909: fixtures :projects, :roles, :enabled_modules, :users,
Chris@909: :repositories, :changesets,
Chris@909: :trackers, :issue_statuses, :issues, :versions, :documents,
Chris@909: :wikis, :wiki_pages, :wiki_contents,
Chris@909: :boards, :messages, :news,
Chris@909: :attachments, :enumerations
Chris@909:
Chris@909: def setup
Chris@909: super
Chris@909: set_tmp_attachments_directory
Chris@909: end
Chris@909:
Chris@909: context "#link_to_if_authorized" do
Chris@909: context "authorized user" do
Chris@909: should "be tested"
Chris@909: end
Chris@909:
Chris@909: context "unauthorized user" do
Chris@909: should "be tested"
Chris@909: end
Chris@909:
Chris@909: should "allow using the :controller and :action for the target link" do
Chris@909: User.current = User.find_by_login('admin')
Chris@909:
Chris@909: @project = Issue.first.project # Used by helper
Chris@909: response = link_to_if_authorized("By controller/action",
Chris@909: {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
Chris@909: assert_match /href/, response
Chris@909: end
Chris@909:
Chris@909: end
Chris@909:
Chris@909: def test_auto_links
Chris@909: to_test = {
Chris@909: 'http://foo.bar' => 'http://foo.bar',
Chris@909: 'http://foo.bar/~user' => 'http://foo.bar/~user',
Chris@909: 'http://foo.bar.' => 'http://foo.bar.',
Chris@909: 'https://foo.bar.' => 'https://foo.bar.',
Chris@909: 'This is a link: http://foo.bar.' => 'This is a link: http://foo.bar.',
Chris@909: 'A link (eg. http://foo.bar).' => 'A link (eg. http://foo.bar).',
Chris@909: 'http://foo.bar/foo.bar#foo.bar.' => 'http://foo.bar/foo.bar#foo.bar.',
Chris@909: 'http://www.foo.bar/Test_(foobar)' => 'http://www.foo.bar/Test_(foobar)',
Chris@909: '(see inline link : http://www.foo.bar/Test_(foobar))' => '(see inline link : http://www.foo.bar/Test_(foobar))',
Chris@909: '(see inline link : http://www.foo.bar/Test)' => '(see inline link : http://www.foo.bar/Test)',
Chris@909: '(see inline link : http://www.foo.bar/Test).' => '(see inline link : http://www.foo.bar/Test).',
Chris@909: '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see inline link)',
Chris@909: '(see "inline link":http://www.foo.bar/Test)' => '(see inline link)',
Chris@909: '(see "inline link":http://www.foo.bar/Test).' => '(see inline link).',
Chris@909: 'www.foo.bar' => 'www.foo.bar',
Chris@909: 'http://foo.bar/page?p=1&t=z&s=' => 'http://foo.bar/page?p=1&t=z&s=',
Chris@909: 'http://foo.bar/page#125' => 'http://foo.bar/page#125',
Chris@909: 'http://foo@www.bar.com' => 'http://foo@www.bar.com',
Chris@909: 'http://foo:bar@www.bar.com' => 'http://foo:bar@www.bar.com',
Chris@909: 'ftp://foo.bar' => 'ftp://foo.bar',
Chris@909: 'ftps://foo.bar' => 'ftps://foo.bar',
Chris@909: 'sftp://foo.bar' => 'sftp://foo.bar',
Chris@909: # two exclamation marks
Chris@909: 'http://example.net/path!602815048C7B5C20!302.html' => 'http://example.net/path!602815048C7B5C20!302.html',
Chris@909: # escaping
Chris@909: 'http://foo"bar' => 'http://foo"bar',
Chris@909: # wrap in angle brackets
Chris@909: ' #{result} #{result} #{result} #{result} #{result}',
Chris@909: 'floating !>http://foo.bar/image.jpg!' => 'floating
',
Chris@909: # inline styles should be stripped
Chris@909: 'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style
',
Chris@909: 'with title !http://foo.bar/image.jpg(This is a title)!' => 'with title
',
Chris@909: 'with title !http://foo.bar/image.jpg(This is a double-quoted "title")!' => 'with title
',
Chris@909: }
Chris@909: to_test.each { |text, result| assert_equal "
')
Chris@909: assert textilizable(raw).include?('
')
Chris@909: end
Chris@909:
Chris@909: def test_attached_images
Chris@909: to_test = {
Chris@909: 'Inline image: !logo.gif!' => 'Inline image:
',
Chris@909: 'Inline image: !logo.GIF!' => 'Inline image:
',
Chris@909: 'No match: !ogo.gif!' => 'No match:
',
Chris@909: 'No match: !ogo.GIF!' => 'No match:
',
Chris@909: # link image
Chris@909: '!logo.gif!:http://foo.bar/' => '
',
Chris@909: }
Chris@909: attachments = Attachment.find(:all)
Chris@909: to_test.each { |text, result| assert_equal "
',
Chris@909: 'Inline image: !testtest.jpeg!' =>
Chris@909: 'Inline image:
',
Chris@909: 'Inline image: !testtest.jpe!' =>
Chris@909: 'Inline image:
',
Chris@909: 'Inline image: !testtest.bmp!' =>
Chris@909: 'Inline image:
',
Chris@909: }
Chris@909:
Chris@909: attachments = [a1, a2, a3, a4]
Chris@909: to_test.each { |text, result| assert_equal "
',
Chris@909: 'Inline image: !Testfile.PNG!' =>
Chris@909: 'Inline image:
',
Chris@909: }
Chris@909: attachments = [a1, a2]
Chris@909: to_test.each { |text, result| assert_equal "
Another paragraph",
Chris@909: # no multiline link text
Chris@909: "This is a double quote \"on the first line\nand another on a second line\":test" => "This is a double quote \"on the first line
and another on a second line\":test",
Chris@909: # mailto link
Chris@909: "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "system administrator",
Chris@909: # two exclamation marks
Chris@909: '"a link":http://example.net/path!602815048C7B5C20!302.html' => 'a link',
Chris@909: # escaping
Chris@909: '"test":http://foo"bar' => 'test',
Chris@909: }
Chris@909: to_test.each { |text, result| assert_equal "
#{result}
", textilizable(text) } Chris@909: end Chris@909: Chris@909: def test_redmine_links Chris@909: issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3}, Chris@909: :class => 'issue status-1 priority-1 overdue', :title => 'Error 281 when updating a recipe (New)') Chris@909: Chris@909: changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1}, Chris@909: :class => 'changeset', :title => 'My very first commit') Chris@909: changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2}, Chris@909: :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3') Chris@909: Chris@909: document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1}, Chris@909: :class => 'document') Chris@909: Chris@909: version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2}, Chris@909: :class => 'version') Chris@909: Chris@909: board_url = {:controller => 'boards', :action => 'show', :id => 2, :project_id => 'ecookbook'} Chris@909: Chris@909: message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4} Chris@909: Chris@909: news_url = {:controller => 'news', :action => 'show', :id => 1} Chris@909: Chris@909: project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'} Chris@909: Chris@909: source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']} Chris@909: source_url_with_ext = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file.ext']} Chris@909: Chris@909: to_test = { Chris@909: # tickets Chris@909: '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.", Chris@909: # changesets Chris@909: 'r1' => changeset_link, Chris@909: 'r1.' => "#{changeset_link}.", Chris@909: 'r1, r2' => "#{changeset_link}, #{changeset_link2}", Chris@909: 'r1,r2' => "#{changeset_link},#{changeset_link2}", Chris@909: # documents Chris@909: 'document#1' => document_link, Chris@909: 'document:"Test document"' => document_link, Chris@909: # versions Chris@909: 'version#2' => version_link, Chris@909: 'version:1.0' => version_link, Chris@909: 'version:"1.0"' => version_link, Chris@909: # source Chris@909: 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'), Chris@909: 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".", Chris@909: 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".", Chris@909: 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".", Chris@909: 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".", Chris@909: 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",", Chris@909: 'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'), Chris@909: 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_ext.merge(:rev => 52), :class => 'source'), Chris@909: 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'), Chris@909: 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext.merge(:anchor => 'L110'), :class => 'source'), Chris@909: 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'), Chris@909: 'export:/some/file' => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'), Chris@909: # forum Chris@909: 'forum#2' => link_to('Discussion', board_url, :class => 'board'), Chris@909: 'forum:Discussion' => link_to('Discussion', board_url, :class => 'board'), Chris@909: # message Chris@909: 'message#4' => link_to('Post 2', message_url, :class => 'message'), Chris@909: 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5', :r => 5), :class => 'message'), Chris@909: # news Chris@909: 'news#1' => link_to('eCookbook first release !', news_url, :class => 'news'), Chris@909: 'news:"eCookbook first release !"' => link_to('eCookbook first release !', news_url, :class => 'news'), Chris@909: # project Chris@909: 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'), Chris@909: 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'), Chris@909: 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'), Chris@909: # escaping Chris@909: '!#3.' => '#3.', Chris@909: '!r1' => 'r1', Chris@909: '!document#1' => 'document#1', Chris@909: '!document:"Test document"' => 'document:"Test document"', Chris@909: '!version#2' => 'version#2', Chris@909: '!version:1.0' => 'version:1.0', Chris@909: '!version:"1.0"' => 'version:"1.0"', Chris@909: '!source:/some/file' => 'source:/some/file', Chris@909: # not found Chris@909: '#0123456789' => '#0123456789', Chris@909: # invalid expressions Chris@909: 'source:' => 'source:', Chris@909: # url hash Chris@909: "http://foo.bar/FAQ#3" => 'http://foo.bar/FAQ#3', Chris@909: } Chris@909: @project = Project.find(1) Chris@909: to_test.each { |text, result| assert_equal "#{result}
", textilizable(text), "#{text} failed" } Chris@909: end Chris@909: Chris@909: def test_cross_project_redmine_links Chris@909: source_link = link_to('ecookbook:source:/some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, Chris@909: :class => 'source') Chris@909: Chris@909: changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2}, Chris@909: :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3') Chris@909: Chris@909: to_test = { Chris@909: # documents Chris@909: 'document:"Test document"' => 'document:"Test document"', Chris@909: 'ecookbook:document:"Test document"' => 'Test document', Chris@909: 'invalid:document:"Test document"' => 'invalid:document:"Test document"', Chris@909: # versions Chris@909: 'version:"1.0"' => 'version:"1.0"', Chris@909: 'ecookbook:version:"1.0"' => '1.0', Chris@909: 'invalid:version:"1.0"' => 'invalid:version:"1.0"', Chris@909: # changeset Chris@909: 'r2' => 'r2', Chris@909: 'ecookbook:r2' => changeset_link, Chris@909: 'invalid:r2' => 'invalid:r2', Chris@909: # source Chris@909: 'source:/some/file' => 'source:/some/file', Chris@909: 'ecookbook:source:/some/file' => source_link, Chris@909: 'invalid:source:/some/file' => 'invalid:source:/some/file', Chris@909: } Chris@909: @project = Project.find(3) Chris@909: to_test.each { |text, result| assert_equal "#{result}
", textilizable(text), "#{text} failed" } Chris@909: end Chris@909: Chris@909: def test_redmine_links_git_commit Chris@909: changeset_link = link_to('abcd', Chris@909: { Chris@909: :controller => 'repositories', Chris@909: :action => 'revision', Chris@909: :id => 'subproject1', Chris@909: :rev => 'abcd', Chris@909: }, Chris@909: :class => 'changeset', :title => 'test commit') Chris@909: to_test = { Chris@909: 'commit:abcd' => changeset_link, Chris@909: } Chris@909: @project = Project.find(3) Chris@909: r = Repository::Git.create!(:project => @project, :url => '/tmp/test/git') Chris@909: assert r Chris@909: c = Changeset.new(:repository => r, Chris@909: :committed_on => Time.now, Chris@909: :revision => 'abcd', Chris@909: :scmid => 'abcd', Chris@909: :comments => 'test commit') Chris@909: assert( c.save ) Chris@909: to_test.each { |text, result| assert_equal "#{result}
", textilizable(text) } Chris@909: end Chris@909: Chris@909: # TODO: Bazaar commit id contains mail address, so it contains '@' and '_'. Chris@909: def test_redmine_links_darcs_commit Chris@909: changeset_link = link_to('20080308225258-98289-abcd456efg.gz', Chris@909: { Chris@909: :controller => 'repositories', Chris@909: :action => 'revision', Chris@909: :id => 'subproject1', Chris@909: :rev => '123', Chris@909: }, Chris@909: :class => 'changeset', :title => 'test commit') Chris@909: to_test = { Chris@909: 'commit:20080308225258-98289-abcd456efg.gz' => changeset_link, Chris@909: } Chris@909: @project = Project.find(3) Chris@909: r = Repository::Darcs.create!( Chris@909: :project => @project, :url => '/tmp/test/darcs', Chris@909: :log_encoding => 'UTF-8') Chris@909: assert r Chris@909: c = Changeset.new(:repository => r, Chris@909: :committed_on => Time.now, Chris@909: :revision => '123', Chris@909: :scmid => '20080308225258-98289-abcd456efg.gz', Chris@909: :comments => 'test commit') Chris@909: assert( c.save ) Chris@909: to_test.each { |text, result| assert_equal "#{result}
", textilizable(text) } Chris@909: end Chris@909: Chris@909: def test_redmine_links_mercurial_commit Chris@909: changeset_link_rev = link_to('r123', Chris@909: { Chris@909: :controller => 'repositories', Chris@909: :action => 'revision', Chris@909: :id => 'subproject1', Chris@909: :rev => '123' , Chris@909: }, Chris@909: :class => 'changeset', :title => 'test commit') Chris@909: changeset_link_commit = link_to('abcd', Chris@909: { Chris@909: :controller => 'repositories', Chris@909: :action => 'revision', Chris@909: :id => 'subproject1', Chris@909: :rev => 'abcd' , Chris@909: }, Chris@909: :class => 'changeset', :title => 'test commit') Chris@909: to_test = { Chris@909: 'r123' => changeset_link_rev, Chris@909: 'commit:abcd' => changeset_link_commit, Chris@909: } Chris@909: @project = Project.find(3) Chris@909: r = Repository::Mercurial.create!(:project => @project, :url => '/tmp/test') Chris@909: assert r Chris@909: c = Changeset.new(:repository => r, Chris@909: :committed_on => Time.now, Chris@909: :revision => '123', Chris@909: :scmid => 'abcd', Chris@909: :comments => 'test commit') Chris@909: assert( c.save ) Chris@909: to_test.each { |text, result| assert_equal "#{result}
", textilizable(text) } Chris@909: end Chris@909: Chris@909: def test_attachment_links Chris@909: attachment_link = link_to('error281.txt', {:controller => 'attachments', :action => 'download', :id => '1'}, :class => 'attachment') Chris@909: to_test = { Chris@909: 'attachment:error281.txt' => attachment_link Chris@909: } Chris@909: to_test.each { |text, result| assert_equal "#{result}
", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" } Chris@909: end Chris@909: Chris@909: def test_wiki_links Chris@909: to_test = { Chris@909: '[[CookBook documentation]]' => 'CookBook documentation', Chris@909: '[[Another page|Page]]' => 'Page', Chris@909: # title content should be formatted Chris@909: '[[Another page|With _styled_ *title*]]' => 'With styled title', Chris@909: '[[Another page|With title containing HTML entities & markups]]' => 'With title containing <strong>HTML entities & markups</strong>', Chris@909: # link with anchor Chris@909: '[[CookBook documentation#One-section]]' => 'CookBook documentation', Chris@909: '[[Another page#anchor|Page]]' => 'Page', Chris@909: # page that doesn't exist Chris@909: '[[Unknown page]]' => 'Unknown page', Chris@909: '[[Unknown page|404]]' => '404', Chris@909: # link to another project wiki Chris@909: '[[onlinestore:]]' => 'onlinestore', Chris@909: '[[onlinestore:|Wiki]]' => 'Wiki', Chris@909: '[[onlinestore:Start page]]' => 'Start page', Chris@909: '[[onlinestore:Start page|Text]]' => 'Text', Chris@909: '[[onlinestore:Unknown page]]' => 'Unknown page', Chris@909: # striked through link Chris@909: '-[[Another page|Page]]-' => '#{result}
", textilizable(text) } Chris@909: end Chris@909: Chris@909: def test_wiki_links_within_local_file_generation_context Chris@909: Chris@909: to_test = { Chris@909: # link to a page Chris@909: '[[CookBook documentation]]' => 'CookBook documentation', Chris@909: '[[CookBook documentation|documentation]]' => 'documentation', Chris@909: '[[CookBook documentation#One-section]]' => 'CookBook documentation', Chris@909: '[[CookBook documentation#One-section|documentation]]' => 'documentation', Chris@909: # page that doesn't exist Chris@909: '[[Unknown page]]' => 'Unknown page', Chris@909: '[[Unknown page|404]]' => '404', Chris@909: '[[Unknown page#anchor]]' => 'Unknown page', Chris@909: '[[Unknown page#anchor|404]]' => '404', Chris@909: } Chris@909: Chris@909: @project = Project.find(1) Chris@909: Chris@909: to_test.each { |text, result| assert_equal "#{result}
", textilizable(text, :wiki_links => :local) } Chris@909: end Chris@909: Chris@909: def test_html_tags Chris@909: to_test = { Chris@909: "<div>content</div>
", Chris@909: "<div class=\"bold\">content</div>
", Chris@909: "" => "<script>some script;</script>
", Chris@909: # do not escape pre/code tags Chris@909: "\nline 1\nline2" => "
\nline 1\nline2", Chris@909: "
\nline 1\nline2
" => "\nline 1\nline2
",
Chris@909: "" => "content
<div>content</div>", Chris@909: "HTML comment: " => "
HTML comment: <!-- no comments -->
", Chris@909: "