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