annotate test/unit/helpers/application_helper_test.rb @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents fb9a13467253
children
rev   line source
Chris@1115 1 # encoding: utf-8
Chris@1115 2 #
Chris@0 3 # Redmine - project management software
Chris@1494 4 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@441 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@441 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@119 20 require File.expand_path('../../../test_helper', __FILE__)
Chris@0 21
chris@22 22 class ApplicationHelperTest < ActionView::TestCase
Chris@1464 23 include Redmine::I18n
Chris@1115 24 include ERB::Util
Chris@1464 25 include Rails.application.routes.url_helpers
Chris@1115 26
Chris@0 27 fixtures :projects, :roles, :enabled_modules, :users,
Chris@909 28 :repositories, :changesets,
Chris@909 29 :trackers, :issue_statuses, :issues, :versions, :documents,
Chris@909 30 :wikis, :wiki_pages, :wiki_contents,
Chris@909 31 :boards, :messages, :news,
Chris@909 32 :attachments, :enumerations
Chris@0 33
Chris@0 34 def setup
Chris@0 35 super
Chris@909 36 set_tmp_attachments_directory
Chris@1464 37 @russian_test = "\xd1\x82\xd0\xb5\xd1\x81\xd1\x82"
Chris@1464 38 if @russian_test.respond_to?(:force_encoding)
Chris@1464 39 @russian_test.force_encoding('UTF-8')
Chris@1464 40 end
Chris@0 41 end
chris@22 42
Chris@1464 43 test "#link_to_if_authorized for authorized user should allow using the :controller and :action for the target link" do
Chris@1464 44 User.current = User.find_by_login('admin')
Chris@441 45
Chris@1464 46 @project = Issue.first.project # Used by helper
Chris@1464 47 response = link_to_if_authorized('By controller/actionr',
Chris@1464 48 {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
Chris@1464 49 assert_match /href/, response
Chris@1464 50 end
Chris@441 51
Chris@1464 52 test "#link_to_if_authorized for unauthorized user should display nothing if user isn't authorized" do
Chris@1464 53 User.current = User.find_by_login('dlopper')
Chris@1464 54 @project = Project.find('private-child')
Chris@1464 55 issue = @project.issues.first
Chris@1464 56 assert !issue.visible?
chris@22 57
Chris@1464 58 response = link_to_if_authorized('Never displayed',
Chris@1464 59 {:controller => 'issues', :action => 'show', :id => issue})
Chris@1464 60 assert_nil response
chris@22 61 end
Chris@441 62
Chris@0 63 def test_auto_links
Chris@0 64 to_test = {
Chris@0 65 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
Chris@0 66 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
Chris@0 67 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
Chris@0 68 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
Chris@0 69 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
Chris@0 70 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
Chris@0 71 'http://foo.bar/foo.bar#foo.bar.' => '<a class="external" href="http://foo.bar/foo.bar#foo.bar">http://foo.bar/foo.bar#foo.bar</a>.',
Chris@0 72 'http://www.foo.bar/Test_(foobar)' => '<a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>',
Chris@0 73 '(see inline link : http://www.foo.bar/Test_(foobar))' => '(see inline link : <a class="external" href="http://www.foo.bar/Test_(foobar)">http://www.foo.bar/Test_(foobar)</a>)',
Chris@0 74 '(see inline link : http://www.foo.bar/Test)' => '(see inline link : <a class="external" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>)',
Chris@0 75 '(see inline link : http://www.foo.bar/Test).' => '(see inline link : <a class="external" href="http://www.foo.bar/Test">http://www.foo.bar/Test</a>).',
Chris@0 76 '(see "inline link":http://www.foo.bar/Test_(foobar))' => '(see <a href="http://www.foo.bar/Test_(foobar)" class="external">inline link</a>)',
Chris@0 77 '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
Chris@0 78 '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
Chris@0 79 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
Chris@0 80 'http://foo.bar/page?p=1&t=z&s=' => '<a class="external" href="http://foo.bar/page?p=1&#38;t=z&#38;s=">http://foo.bar/page?p=1&#38;t=z&#38;s=</a>',
Chris@0 81 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
Chris@0 82 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
Chris@0 83 'http://foo:bar@www.bar.com' => '<a class="external" href="http://foo:bar@www.bar.com">http://foo:bar@www.bar.com</a>',
Chris@0 84 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
Chris@0 85 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
Chris@0 86 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
Chris@0 87 # two exclamation marks
Chris@0 88 'http://example.net/path!602815048C7B5C20!302.html' => '<a class="external" href="http://example.net/path!602815048C7B5C20!302.html">http://example.net/path!602815048C7B5C20!302.html</a>',
Chris@0 89 # escaping
Chris@1115 90 'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo&quot;bar</a>',
chris@37 91 # wrap in angle brackets
Chris@1464 92 '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;',
Chris@1464 93 # invalid urls
Chris@1464 94 'http://' => 'http://',
Chris@1464 95 'www.' => 'www.',
Chris@1464 96 'test-www.bar.com' => 'test-www.bar.com',
Chris@0 97 }
Chris@0 98 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 99 end
Chris@441 100
Chris@1115 101 if 'ruby'.respond_to?(:encoding)
Chris@1115 102 def test_auto_links_with_non_ascii_characters
Chris@1115 103 to_test = {
Chris@1464 104 "http://foo.bar/#{@russian_test}" =>
Chris@1464 105 %|<a class="external" href="http://foo.bar/#{@russian_test}">http://foo.bar/#{@russian_test}</a>|
Chris@1115 106 }
Chris@1115 107 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@1115 108 end
Chris@1115 109 else
Chris@1115 110 puts 'Skipping test_auto_links_with_non_ascii_characters, unsupported ruby version'
Chris@1115 111 end
Chris@1115 112
Chris@0 113 def test_auto_mailto
Chris@1464 114 to_test = {
Chris@1464 115 'test@foo.bar' => '<a class="email" href="mailto:test@foo.bar">test@foo.bar</a>',
Chris@1464 116 'test@www.foo.bar' => '<a class="email" href="mailto:test@www.foo.bar">test@www.foo.bar</a>',
Chris@1464 117 }
Chris@1464 118 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 119 end
Chris@441 120
Chris@0 121 def test_inline_images
Chris@0 122 to_test = {
Chris@0 123 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
Chris@0 124 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
Chris@0 125 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
Chris@1115 126 'with style !{width:100px;height:100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" style="width:100px;height:100px;" alt="" />',
Chris@0 127 'with title !http://foo.bar/image.jpg(This is a title)!' => 'with title <img src="http://foo.bar/image.jpg" title="This is a title" alt="This is a title" />',
Chris@0 128 'with title !http://foo.bar/image.jpg(This is a double-quoted "title")!' => 'with title <img src="http://foo.bar/image.jpg" title="This is a double-quoted &quot;title&quot;" alt="This is a double-quoted &quot;title&quot;" />',
Chris@0 129 }
Chris@0 130 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 131 end
Chris@441 132
Chris@0 133 def test_inline_images_inside_tags
Chris@0 134 raw = <<-RAW
Chris@0 135 h1. !foo.png! Heading
Chris@0 136
Chris@0 137 Centered image:
Chris@0 138
Chris@0 139 p=. !bar.gif!
Chris@0 140 RAW
Chris@0 141
Chris@0 142 assert textilizable(raw).include?('<img src="foo.png" alt="" />')
Chris@0 143 assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
Chris@0 144 end
Chris@441 145
Chris@0 146 def test_attached_images
Chris@0 147 to_test = {
Chris@1464 148 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
Chris@1464 149 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" />',
Chris@0 150 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
Chris@0 151 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
Chris@0 152 # link image
Chris@1464 153 '!logo.gif!:http://foo.bar/' => '<a href="http://foo.bar/"><img src="/attachments/download/3/logo.gif" title="This is a logo" alt="This is a logo" /></a>',
Chris@0 154 }
Chris@1464 155 attachments = Attachment.all
Chris@0 156 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
Chris@0 157 end
Chris@441 158
Chris@909 159 def test_attached_images_filename_extension
Chris@909 160 set_tmp_attachments_directory
Chris@909 161 a1 = Attachment.new(
Chris@909 162 :container => Issue.find(1),
Chris@909 163 :file => mock_file_with_options({:original_filename => "testtest.JPG"}),
Chris@909 164 :author => User.find(1))
Chris@909 165 assert a1.save
Chris@909 166 assert_equal "testtest.JPG", a1.filename
Chris@909 167 assert_equal "image/jpeg", a1.content_type
Chris@909 168 assert a1.image?
Chris@909 169
Chris@909 170 a2 = Attachment.new(
Chris@909 171 :container => Issue.find(1),
Chris@909 172 :file => mock_file_with_options({:original_filename => "testtest.jpeg"}),
Chris@909 173 :author => User.find(1))
Chris@909 174 assert a2.save
Chris@909 175 assert_equal "testtest.jpeg", a2.filename
Chris@909 176 assert_equal "image/jpeg", a2.content_type
Chris@909 177 assert a2.image?
Chris@909 178
Chris@909 179 a3 = Attachment.new(
Chris@909 180 :container => Issue.find(1),
Chris@909 181 :file => mock_file_with_options({:original_filename => "testtest.JPE"}),
Chris@909 182 :author => User.find(1))
Chris@909 183 assert a3.save
Chris@909 184 assert_equal "testtest.JPE", a3.filename
Chris@909 185 assert_equal "image/jpeg", a3.content_type
Chris@909 186 assert a3.image?
Chris@909 187
Chris@909 188 a4 = Attachment.new(
Chris@909 189 :container => Issue.find(1),
Chris@909 190 :file => mock_file_with_options({:original_filename => "Testtest.BMP"}),
Chris@909 191 :author => User.find(1))
Chris@909 192 assert a4.save
Chris@909 193 assert_equal "Testtest.BMP", a4.filename
Chris@909 194 assert_equal "image/x-ms-bmp", a4.content_type
Chris@909 195 assert a4.image?
Chris@909 196
Chris@909 197 to_test = {
Chris@909 198 'Inline image: !testtest.jpg!' =>
Chris@1464 199 'Inline image: <img src="/attachments/download/' + a1.id.to_s + '/testtest.JPG" alt="" />',
Chris@909 200 'Inline image: !testtest.jpeg!' =>
Chris@1464 201 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testtest.jpeg" alt="" />',
Chris@909 202 'Inline image: !testtest.jpe!' =>
Chris@1464 203 'Inline image: <img src="/attachments/download/' + a3.id.to_s + '/testtest.JPE" alt="" />',
Chris@909 204 'Inline image: !testtest.bmp!' =>
Chris@1464 205 'Inline image: <img src="/attachments/download/' + a4.id.to_s + '/Testtest.BMP" alt="" />',
Chris@909 206 }
Chris@909 207
Chris@909 208 attachments = [a1, a2, a3, a4]
Chris@909 209 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
Chris@909 210 end
Chris@909 211
Chris@909 212 def test_attached_images_should_read_later
Chris@1115 213 set_fixtures_attachments_directory
Chris@909 214 a1 = Attachment.find(16)
Chris@909 215 assert_equal "testfile.png", a1.filename
Chris@909 216 assert a1.readable?
Chris@909 217 assert (! a1.visible?(User.anonymous))
Chris@909 218 assert a1.visible?(User.find(2))
Chris@909 219 a2 = Attachment.find(17)
Chris@909 220 assert_equal "testfile.PNG", a2.filename
Chris@909 221 assert a2.readable?
Chris@909 222 assert (! a2.visible?(User.anonymous))
Chris@909 223 assert a2.visible?(User.find(2))
Chris@909 224 assert a1.created_on < a2.created_on
Chris@909 225
Chris@909 226 to_test = {
Chris@909 227 'Inline image: !testfile.png!' =>
Chris@1464 228 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
Chris@909 229 'Inline image: !Testfile.PNG!' =>
Chris@1464 230 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '/testfile.PNG" alt="" />',
Chris@909 231 }
Chris@909 232 attachments = [a1, a2]
Chris@909 233 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
Chris@909 234 set_tmp_attachments_directory
Chris@909 235 end
Chris@909 236
Chris@0 237 def test_textile_external_links
Chris@0 238 to_test = {
Chris@0 239 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
Chris@0 240 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
Chris@0 241 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
Chris@0 242 '"link (Link title with "double-quotes")":http://foo.bar' => '<a href="http://foo.bar" title="Link title with &quot;double-quotes&quot;" class="external">link</a>',
Chris@0 243 "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
Chris@0 244 # no multiline link text
Chris@0 245 "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<br />and another on a second line\":test",
Chris@0 246 # mailto link
Chris@0 247 "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
Chris@0 248 # two exclamation marks
Chris@0 249 '"a link":http://example.net/path!602815048C7B5C20!302.html' => '<a href="http://example.net/path!602815048C7B5C20!302.html" class="external">a link</a>',
Chris@0 250 # escaping
Chris@0 251 '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
Chris@0 252 }
Chris@0 253 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 254 end
Chris@119 255
Chris@1115 256 if 'ruby'.respond_to?(:encoding)
Chris@1115 257 def test_textile_external_links_with_non_ascii_characters
Chris@1115 258 to_test = {
Chris@1464 259 %|This is a "link":http://foo.bar/#{@russian_test}| =>
Chris@1464 260 %|This is a <a href="http://foo.bar/#{@russian_test}" class="external">link</a>|
Chris@1115 261 }
Chris@1115 262 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@1115 263 end
Chris@1115 264 else
Chris@1115 265 puts 'Skipping test_textile_external_links_with_non_ascii_characters, unsupported ruby version'
Chris@1115 266 end
Chris@1115 267
Chris@0 268 def test_redmine_links
Chris@441 269 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
Chris@1464 270 :class => Issue.find(3).css_classes, :title => 'Error 281 when updating a recipe (New)')
Chris@1464 271 note_link = link_to('#3-14', {:controller => 'issues', :action => 'show', :id => 3, :anchor => 'note-14'},
Chris@1464 272 :class => Issue.find(3).css_classes, :title => 'Error 281 when updating a recipe (New)')
Chris@1464 273 note_link2 = link_to('#3#note-14', {:controller => 'issues', :action => 'show', :id => 3, :anchor => 'note-14'},
Chris@1464 274 :class => Issue.find(3).css_classes, :title => 'Error 281 when updating a recipe (New)')
Chris@441 275
Chris@1464 276 revision_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
Chris@1464 277 :class => 'changeset', :title => 'My very first commit do not escaping #<>&')
Chris@1464 278 revision_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
Chris@0 279 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
Chris@441 280
Chris@1464 281 changeset_link2 = link_to('691322a8eb01e11fd7',
Chris@1464 282 {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
Chris@1464 283 :class => 'changeset', :title => 'My very first commit do not escaping #<>&')
Chris@1464 284
Chris@0 285 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
Chris@0 286 :class => 'document')
Chris@441 287
Chris@0 288 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
Chris@0 289 :class => 'version')
Chris@0 290
Chris@909 291 board_url = {:controller => 'boards', :action => 'show', :id => 2, :project_id => 'ecookbook'}
Chris@909 292
Chris@0 293 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
Chris@909 294
Chris@909 295 news_url = {:controller => 'news', :action => 'show', :id => 1}
Chris@441 296
Chris@0 297 project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
Chris@441 298
Chris@1115 299 source_url = '/projects/ecookbook/repository/entry/some/file'
Chris@1115 300 source_url_with_rev = '/projects/ecookbook/repository/revisions/52/entry/some/file'
Chris@1115 301 source_url_with_ext = '/projects/ecookbook/repository/entry/some/file.ext'
Chris@1115 302 source_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/entry/some/file.ext'
Chris@1464 303 source_url_with_branch = '/projects/ecookbook/repository/revisions/branch/entry/some/file'
Chris@1115 304
Chris@1115 305 export_url = '/projects/ecookbook/repository/raw/some/file'
Chris@1115 306 export_url_with_rev = '/projects/ecookbook/repository/revisions/52/raw/some/file'
Chris@1115 307 export_url_with_ext = '/projects/ecookbook/repository/raw/some/file.ext'
Chris@1115 308 export_url_with_rev_and_ext = '/projects/ecookbook/repository/revisions/52/raw/some/file.ext'
Chris@1464 309 export_url_with_branch = '/projects/ecookbook/repository/revisions/branch/raw/some/file'
Chris@441 310
Chris@0 311 to_test = {
Chris@0 312 # tickets
Chris@0 313 '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
Chris@1115 314 # ticket notes
Chris@1115 315 '#3-14' => note_link,
Chris@1464 316 '#3#note-14' => note_link2,
Chris@1115 317 # should not ignore leading zero
Chris@1115 318 '#03' => '#03',
Chris@0 319 # changesets
Chris@1464 320 'r1' => revision_link,
Chris@1464 321 'r1.' => "#{revision_link}.",
Chris@1464 322 'r1, r2' => "#{revision_link}, #{revision_link2}",
Chris@1464 323 'r1,r2' => "#{revision_link},#{revision_link2}",
Chris@1464 324 'commit:691322a8eb01e11fd7' => changeset_link2,
Chris@0 325 # documents
Chris@0 326 'document#1' => document_link,
Chris@0 327 'document:"Test document"' => document_link,
Chris@0 328 # versions
Chris@0 329 'version#2' => version_link,
Chris@0 330 'version:1.0' => version_link,
Chris@0 331 'version:"1.0"' => version_link,
Chris@0 332 # source
Chris@1115 333 'source:some/file' => link_to('source:some/file', source_url, :class => 'source'),
Chris@0 334 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
Chris@0 335 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
Chris@0 336 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
Chris@0 337 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
Chris@0 338 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
Chris@0 339 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
Chris@1115 340 'source:/some/file@52' => link_to('source:/some/file@52', source_url_with_rev, :class => 'source'),
Chris@1464 341 'source:/some/file@branch' => link_to('source:/some/file@branch', source_url_with_branch, :class => 'source'),
Chris@1115 342 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_rev_and_ext, :class => 'source'),
Chris@1115 343 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url + "#L110", :class => 'source'),
Chris@1115 344 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext + "#L110", :class => 'source'),
Chris@1115 345 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url_with_rev + "#L110", :class => 'source'),
Chris@1115 346 # export
Chris@1115 347 'export:/some/file' => link_to('export:/some/file', export_url, :class => 'source download'),
Chris@1115 348 'export:/some/file.ext' => link_to('export:/some/file.ext', export_url_with_ext, :class => 'source download'),
Chris@1115 349 'export:/some/file@52' => link_to('export:/some/file@52', export_url_with_rev, :class => 'source download'),
Chris@1115 350 'export:/some/file.ext@52' => link_to('export:/some/file.ext@52', export_url_with_rev_and_ext, :class => 'source download'),
Chris@1464 351 'export:/some/file@branch' => link_to('export:/some/file@branch', export_url_with_branch, :class => 'source download'),
Chris@909 352 # forum
Chris@909 353 'forum#2' => link_to('Discussion', board_url, :class => 'board'),
Chris@909 354 'forum:Discussion' => link_to('Discussion', board_url, :class => 'board'),
Chris@0 355 # message
Chris@0 356 'message#4' => link_to('Post 2', message_url, :class => 'message'),
Chris@210 357 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5', :r => 5), :class => 'message'),
Chris@909 358 # news
Chris@909 359 'news#1' => link_to('eCookbook first release !', news_url, :class => 'news'),
Chris@909 360 'news:"eCookbook first release !"' => link_to('eCookbook first release !', news_url, :class => 'news'),
Chris@0 361 # project
Chris@0 362 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
Chris@0 363 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
Chris@0 364 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
Chris@0 365 # not found
Chris@0 366 '#0123456789' => '#0123456789',
Chris@0 367 # invalid expressions
Chris@0 368 'source:' => 'source:',
Chris@0 369 # url hash
Chris@0 370 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
Chris@0 371 }
Chris@0 372 @project = Project.find(1)
Chris@0 373 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
Chris@0 374 end
Chris@441 375
Chris@1294 376 def test_redmine_links_with_a_different_project_before_current_project
Chris@1294 377 vp1 = Version.generate!(:project_id => 1, :name => '1.4.4')
Chris@1294 378 vp3 = Version.generate!(:project_id => 3, :name => '1.4.4')
Chris@1294 379 @project = Project.find(3)
Chris@1517 380 result1 = link_to("1.4.4", "/versions/#{vp1.id}", :class => "version")
Chris@1517 381 result2 = link_to("1.4.4", "/versions/#{vp3.id}", :class => "version")
Chris@1517 382 assert_equal "<p>#{result1} #{result2}</p>",
Chris@1517 383 textilizable("ecookbook:version:1.4.4 version:1.4.4")
Chris@1294 384 end
Chris@1294 385
Chris@1115 386 def test_escaped_redmine_links_should_not_be_parsed
Chris@1115 387 to_test = [
Chris@1115 388 '#3.',
Chris@1115 389 '#3-14.',
Chris@1115 390 '#3#-note14.',
Chris@1115 391 'r1',
Chris@1115 392 'document#1',
Chris@1115 393 'document:"Test document"',
Chris@1115 394 'version#2',
Chris@1115 395 'version:1.0',
Chris@1115 396 'version:"1.0"',
Chris@1115 397 'source:/some/file'
Chris@1115 398 ]
Chris@1115 399 @project = Project.find(1)
Chris@1115 400 to_test.each { |text| assert_equal "<p>#{text}</p>", textilizable("!" + text), "#{text} failed" }
Chris@1115 401 end
Chris@1115 402
Chris@210 403 def test_cross_project_redmine_links
Chris@1517 404 source_link = link_to('ecookbook:source:/some/file',
Chris@1517 405 {:controller => 'repositories', :action => 'entry',
Chris@1517 406 :id => 'ecookbook', :path => ['some', 'file']},
Chris@1517 407 :class => 'source')
Chris@1517 408 changeset_link = link_to('ecookbook:r2',
Chris@1517 409 {:controller => 'repositories', :action => 'revision',
Chris@1517 410 :id => 'ecookbook', :rev => 2},
Chris@1517 411 :class => 'changeset',
Chris@1517 412 :title => 'This commit fixes #1, #2 and references #1 & #3')
Chris@210 413 to_test = {
Chris@210 414 # documents
Chris@210 415 'document:"Test document"' => 'document:"Test document"',
Chris@1517 416 'ecookbook:document:"Test document"' =>
Chris@1517 417 link_to("Test document", "/documents/1", :class => "document"),
Chris@210 418 'invalid:document:"Test document"' => 'invalid:document:"Test document"',
Chris@210 419 # versions
Chris@210 420 'version:"1.0"' => 'version:"1.0"',
Chris@1517 421 'ecookbook:version:"1.0"' =>
Chris@1517 422 link_to("1.0", "/versions/2", :class => "version"),
Chris@210 423 'invalid:version:"1.0"' => 'invalid:version:"1.0"',
Chris@210 424 # changeset
Chris@210 425 'r2' => 'r2',
Chris@210 426 'ecookbook:r2' => changeset_link,
Chris@210 427 'invalid:r2' => 'invalid:r2',
Chris@210 428 # source
Chris@210 429 'source:/some/file' => 'source:/some/file',
Chris@210 430 'ecookbook:source:/some/file' => source_link,
Chris@210 431 'invalid:source:/some/file' => 'invalid:source:/some/file',
Chris@210 432 }
Chris@210 433 @project = Project.find(3)
Chris@1517 434 to_test.each do |text, result|
Chris@1517 435 assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed"
Chris@1517 436 end
Chris@1517 437 end
Chris@1517 438
Chris@1517 439 def test_redmine_links_by_name_should_work_with_html_escaped_characters
Chris@1517 440 v = Version.generate!(:name => "Test & Show.txt", :project_id => 1)
Chris@1517 441 link = link_to("Test & Show.txt", "/versions/#{v.id}", :class => "version")
Chris@1517 442
Chris@1517 443 @project = v.project
Chris@1517 444 assert_equal "<p>#{link}</p>", textilizable('version:"Test & Show.txt"')
Chris@1517 445 end
Chris@1517 446
Chris@1517 447 def test_link_to_issue_subject
Chris@1517 448 issue = Issue.generate!(:subject => "01234567890123456789")
Chris@1517 449 str = link_to_issue(issue, :truncate => 10)
Chris@1517 450 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
Chris@1517 451 assert_equal "#{result}: 0123456...", str
Chris@1517 452
Chris@1517 453 issue = Issue.generate!(:subject => "<&>")
Chris@1517 454 str = link_to_issue(issue)
Chris@1517 455 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
Chris@1517 456 assert_equal "#{result}: &lt;&amp;&gt;", str
Chris@1517 457
Chris@1517 458 issue = Issue.generate!(:subject => "<&>0123456789012345")
Chris@1517 459 str = link_to_issue(issue, :truncate => 10)
Chris@1517 460 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}", :class => issue.css_classes)
Chris@1517 461 assert_equal "#{result}: &lt;&amp;&gt;0123...", str
Chris@1517 462 end
Chris@1517 463
Chris@1517 464 def test_link_to_issue_title
Chris@1517 465 long_str = "0123456789" * 5
Chris@1517 466
Chris@1517 467 issue = Issue.generate!(:subject => "#{long_str}01234567890123456789")
Chris@1517 468 str = link_to_issue(issue, :subject => false)
Chris@1517 469 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}",
Chris@1517 470 :class => issue.css_classes,
Chris@1517 471 :title => "#{long_str}0123456...")
Chris@1517 472 assert_equal result, str
Chris@1517 473
Chris@1517 474 issue = Issue.generate!(:subject => "<&>#{long_str}01234567890123456789")
Chris@1517 475 str = link_to_issue(issue, :subject => false)
Chris@1517 476 result = link_to("Bug ##{issue.id}", "/issues/#{issue.id}",
Chris@1517 477 :class => issue.css_classes,
Chris@1517 478 :title => "<&>#{long_str}0123...")
Chris@1517 479 assert_equal result, str
Chris@210 480 end
Chris@119 481
Chris@1115 482 def test_multiple_repositories_redmine_links
Chris@1294 483 svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn_repo-1', :url => 'file:///foo/hg')
Chris@1115 484 Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
Chris@1115 485 hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
Chris@1115 486 Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
Chris@1115 487
Chris@1115 488 changeset_link = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
Chris@1115 489 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
Chris@1294 490 svn_changeset_link = link_to('svn_repo-1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn_repo-1', :rev => 123},
Chris@1115 491 :class => 'changeset', :title => '')
Chris@1115 492 hg_changeset_link = link_to('hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
Chris@1115 493 :class => 'changeset', :title => '')
Chris@1115 494
Chris@1115 495 source_link = link_to('source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
Chris@1115 496 hg_source_link = link_to('source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
Chris@1115 497
Chris@1115 498 to_test = {
Chris@1115 499 'r2' => changeset_link,
Chris@1294 500 'svn_repo-1|r123' => svn_changeset_link,
Chris@1115 501 'invalid|r123' => 'invalid|r123',
Chris@1115 502 'commit:hg1|abcd' => hg_changeset_link,
Chris@1115 503 'commit:invalid|abcd' => 'commit:invalid|abcd',
Chris@1115 504 # source
Chris@1115 505 'source:some/file' => source_link,
Chris@1115 506 'source:hg1|some/file' => hg_source_link,
Chris@1115 507 'source:invalid|some/file' => 'source:invalid|some/file',
Chris@1115 508 }
Chris@1115 509
Chris@1115 510 @project = Project.find(1)
Chris@1115 511 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
Chris@1115 512 end
Chris@1115 513
Chris@1115 514 def test_cross_project_multiple_repositories_redmine_links
Chris@1115 515 svn = Repository::Subversion.create!(:project_id => 1, :identifier => 'svn1', :url => 'file:///foo/hg')
Chris@1115 516 Changeset.create!(:repository => svn, :committed_on => Time.now, :revision => '123')
Chris@1115 517 hg = Repository::Mercurial.create!(:project_id => 1, :identifier => 'hg1', :url => '/foo/hg')
Chris@1115 518 Changeset.create!(:repository => hg, :committed_on => Time.now, :revision => '123', :scmid => 'abcd')
Chris@1115 519
Chris@1115 520 changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
Chris@1115 521 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
Chris@1115 522 svn_changeset_link = link_to('ecookbook:svn1|r123', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'svn1', :rev => 123},
Chris@1115 523 :class => 'changeset', :title => '')
Chris@1115 524 hg_changeset_link = link_to('ecookbook:hg1|abcd', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :repository_id => 'hg1', :rev => 'abcd'},
Chris@1115 525 :class => 'changeset', :title => '')
Chris@1115 526
Chris@1115 527 source_link = link_to('ecookbook:source:some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}, :class => 'source')
Chris@1115 528 hg_source_link = link_to('ecookbook:source:hg1|some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :repository_id => 'hg1', :path => ['some', 'file']}, :class => 'source')
Chris@1115 529
Chris@1115 530 to_test = {
Chris@1115 531 'ecookbook:r2' => changeset_link,
Chris@1115 532 'ecookbook:svn1|r123' => svn_changeset_link,
Chris@1115 533 'ecookbook:invalid|r123' => 'ecookbook:invalid|r123',
Chris@1115 534 'ecookbook:commit:hg1|abcd' => hg_changeset_link,
Chris@1115 535 'ecookbook:commit:invalid|abcd' => 'ecookbook:commit:invalid|abcd',
Chris@1115 536 'invalid:commit:invalid|abcd' => 'invalid:commit:invalid|abcd',
Chris@1115 537 # source
Chris@1115 538 'ecookbook:source:some/file' => source_link,
Chris@1115 539 'ecookbook:source:hg1|some/file' => hg_source_link,
Chris@1115 540 'ecookbook:source:invalid|some/file' => 'ecookbook:source:invalid|some/file',
Chris@1115 541 'invalid:source:invalid|some/file' => 'invalid:source:invalid|some/file',
Chris@1115 542 }
Chris@1115 543
Chris@1115 544 @project = Project.find(3)
Chris@1115 545 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
Chris@1115 546 end
Chris@1115 547
Chris@119 548 def test_redmine_links_git_commit
Chris@119 549 changeset_link = link_to('abcd',
Chris@119 550 {
Chris@119 551 :controller => 'repositories',
Chris@119 552 :action => 'revision',
Chris@119 553 :id => 'subproject1',
Chris@119 554 :rev => 'abcd',
Chris@119 555 },
Chris@119 556 :class => 'changeset', :title => 'test commit')
Chris@119 557 to_test = {
Chris@119 558 'commit:abcd' => changeset_link,
Chris@119 559 }
Chris@119 560 @project = Project.find(3)
Chris@119 561 r = Repository::Git.create!(:project => @project, :url => '/tmp/test/git')
Chris@119 562 assert r
Chris@119 563 c = Changeset.new(:repository => r,
Chris@119 564 :committed_on => Time.now,
Chris@119 565 :revision => 'abcd',
Chris@119 566 :scmid => 'abcd',
Chris@119 567 :comments => 'test commit')
Chris@119 568 assert( c.save )
Chris@119 569 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@119 570 end
Chris@119 571
Chris@119 572 # TODO: Bazaar commit id contains mail address, so it contains '@' and '_'.
Chris@119 573 def test_redmine_links_darcs_commit
Chris@119 574 changeset_link = link_to('20080308225258-98289-abcd456efg.gz',
Chris@119 575 {
Chris@119 576 :controller => 'repositories',
Chris@119 577 :action => 'revision',
Chris@119 578 :id => 'subproject1',
Chris@119 579 :rev => '123',
Chris@119 580 },
Chris@119 581 :class => 'changeset', :title => 'test commit')
Chris@119 582 to_test = {
Chris@119 583 'commit:20080308225258-98289-abcd456efg.gz' => changeset_link,
Chris@119 584 }
Chris@119 585 @project = Project.find(3)
Chris@245 586 r = Repository::Darcs.create!(
Chris@245 587 :project => @project, :url => '/tmp/test/darcs',
Chris@245 588 :log_encoding => 'UTF-8')
Chris@119 589 assert r
Chris@119 590 c = Changeset.new(:repository => r,
Chris@119 591 :committed_on => Time.now,
Chris@119 592 :revision => '123',
Chris@119 593 :scmid => '20080308225258-98289-abcd456efg.gz',
Chris@119 594 :comments => 'test commit')
Chris@119 595 assert( c.save )
Chris@119 596 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@119 597 end
Chris@119 598
Chris@119 599 def test_redmine_links_mercurial_commit
Chris@119 600 changeset_link_rev = link_to('r123',
Chris@119 601 {
Chris@119 602 :controller => 'repositories',
Chris@119 603 :action => 'revision',
Chris@119 604 :id => 'subproject1',
Chris@119 605 :rev => '123' ,
Chris@119 606 },
Chris@119 607 :class => 'changeset', :title => 'test commit')
Chris@119 608 changeset_link_commit = link_to('abcd',
Chris@119 609 {
Chris@119 610 :controller => 'repositories',
Chris@119 611 :action => 'revision',
Chris@119 612 :id => 'subproject1',
Chris@119 613 :rev => 'abcd' ,
Chris@119 614 },
Chris@119 615 :class => 'changeset', :title => 'test commit')
Chris@119 616 to_test = {
Chris@119 617 'r123' => changeset_link_rev,
Chris@119 618 'commit:abcd' => changeset_link_commit,
Chris@119 619 }
Chris@119 620 @project = Project.find(3)
Chris@119 621 r = Repository::Mercurial.create!(:project => @project, :url => '/tmp/test')
Chris@119 622 assert r
Chris@119 623 c = Changeset.new(:repository => r,
Chris@119 624 :committed_on => Time.now,
Chris@119 625 :revision => '123',
Chris@119 626 :scmid => 'abcd',
Chris@119 627 :comments => 'test commit')
Chris@119 628 assert( c.save )
Chris@119 629 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@119 630 end
Chris@119 631
Chris@0 632 def test_attachment_links
Chris@1517 633 text = 'attachment:error281.txt'
Chris@1517 634 result = link_to("error281.txt", "/attachments/download/1/error281.txt",
Chris@1517 635 :class => "attachment")
Chris@1517 636 assert_equal "<p>#{result}</p>",
Chris@1517 637 textilizable(text,
Chris@1517 638 :attachments => Issue.find(3).attachments),
Chris@1517 639 "#{text} failed"
Chris@0 640 end
Chris@441 641
Chris@1294 642 def test_attachment_link_should_link_to_latest_attachment
Chris@1294 643 set_tmp_attachments_directory
Chris@1294 644 a1 = Attachment.generate!(:filename => "test.txt", :created_on => 1.hour.ago)
Chris@1294 645 a2 = Attachment.generate!(:filename => "test.txt")
Chris@1517 646 result = link_to("test.txt", "/attachments/download/#{a2.id}/test.txt",
Chris@1517 647 :class => "attachment")
Chris@1517 648 assert_equal "<p>#{result}</p>",
Chris@1517 649 textilizable('attachment:test.txt', :attachments => [a1, a2])
Chris@1294 650 end
Chris@1294 651
Chris@0 652 def test_wiki_links
Chris@1464 653 russian_eacape = CGI.escape(@russian_test)
Chris@0 654 to_test = {
Chris@1517 655 '[[CookBook documentation]]' =>
Chris@1517 656 link_to("CookBook documentation",
Chris@1517 657 "/projects/ecookbook/wiki/CookBook_documentation",
Chris@1517 658 :class => "wiki-page"),
Chris@1517 659 '[[Another page|Page]]' =>
Chris@1517 660 link_to("Page",
Chris@1517 661 "/projects/ecookbook/wiki/Another_page",
Chris@1517 662 :class => "wiki-page"),
Chris@909 663 # title content should be formatted
Chris@1517 664 '[[Another page|With _styled_ *title*]]' =>
Chris@1517 665 link_to("With <em>styled</em> <strong>title</strong>".html_safe,
Chris@1517 666 "/projects/ecookbook/wiki/Another_page",
Chris@1517 667 :class => "wiki-page"),
Chris@1517 668 '[[Another page|With title containing <strong>HTML entities &amp; markups</strong>]]' =>
Chris@1517 669 link_to("With title containing &lt;strong&gt;HTML entities &amp; markups&lt;/strong&gt;".html_safe,
Chris@1517 670 "/projects/ecookbook/wiki/Another_page",
Chris@1517 671 :class => "wiki-page"),
Chris@0 672 # link with anchor
Chris@1517 673 '[[CookBook documentation#One-section]]' =>
Chris@1517 674 link_to("CookBook documentation",
Chris@1517 675 "/projects/ecookbook/wiki/CookBook_documentation#One-section",
Chris@1517 676 :class => "wiki-page"),
Chris@1517 677 '[[Another page#anchor|Page]]' =>
Chris@1517 678 link_to("Page",
Chris@1517 679 "/projects/ecookbook/wiki/Another_page#anchor",
Chris@1517 680 :class => "wiki-page"),
Chris@1115 681 # UTF8 anchor
Chris@1464 682 "[[Another_page##{@russian_test}|#{@russian_test}]]" =>
Chris@1517 683 link_to(@russian_test,
Chris@1517 684 "/projects/ecookbook/wiki/Another_page##{russian_eacape}",
Chris@1517 685 :class => "wiki-page"),
Chris@0 686 # page that doesn't exist
Chris@1517 687 '[[Unknown page]]' =>
Chris@1517 688 link_to("Unknown page",
Chris@1517 689 "/projects/ecookbook/wiki/Unknown_page",
Chris@1517 690 :class => "wiki-page new"),
Chris@1517 691 '[[Unknown page|404]]' =>
Chris@1517 692 link_to("404",
Chris@1517 693 "/projects/ecookbook/wiki/Unknown_page",
Chris@1517 694 :class => "wiki-page new"),
Chris@0 695 # link to another project wiki
Chris@1517 696 '[[onlinestore:]]' =>
Chris@1517 697 link_to("onlinestore",
Chris@1517 698 "/projects/onlinestore/wiki",
Chris@1517 699 :class => "wiki-page"),
Chris@1517 700 '[[onlinestore:|Wiki]]' =>
Chris@1517 701 link_to("Wiki",
Chris@1517 702 "/projects/onlinestore/wiki",
Chris@1517 703 :class => "wiki-page"),
Chris@1517 704 '[[onlinestore:Start page]]' =>
Chris@1517 705 link_to("Start page",
Chris@1517 706 "/projects/onlinestore/wiki/Start_page",
Chris@1517 707 :class => "wiki-page"),
Chris@1517 708 '[[onlinestore:Start page|Text]]' =>
Chris@1517 709 link_to("Text",
Chris@1517 710 "/projects/onlinestore/wiki/Start_page",
Chris@1517 711 :class => "wiki-page"),
Chris@1517 712 '[[onlinestore:Unknown page]]' =>
Chris@1517 713 link_to("Unknown page",
Chris@1517 714 "/projects/onlinestore/wiki/Unknown_page",
Chris@1517 715 :class => "wiki-page new"),
Chris@0 716 # striked through link
Chris@1517 717 '-[[Another page|Page]]-' =>
Chris@1517 718 "<del>".html_safe +
Chris@1517 719 link_to("Page",
Chris@1517 720 "/projects/ecookbook/wiki/Another_page",
Chris@1517 721 :class => "wiki-page").html_safe +
Chris@1517 722 "</del>".html_safe,
Chris@1517 723 '-[[Another page|Page]] link-' =>
Chris@1517 724 "<del>".html_safe +
Chris@1517 725 link_to("Page",
Chris@1517 726 "/projects/ecookbook/wiki/Another_page",
Chris@1517 727 :class => "wiki-page").html_safe +
Chris@1517 728 " link</del>".html_safe,
Chris@0 729 # escaping
Chris@0 730 '![[Another page|Page]]' => '[[Another page|Page]]',
Chris@0 731 # project does not exist
Chris@0 732 '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
Chris@0 733 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
Chris@0 734 }
Chris@0 735 @project = Project.find(1)
Chris@0 736 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 737 end
Chris@441 738
Chris@909 739 def test_wiki_links_within_local_file_generation_context
Chris@909 740 to_test = {
Chris@909 741 # link to a page
Chris@1517 742 '[[CookBook documentation]]' =>
Chris@1517 743 link_to("CookBook documentation", "CookBook_documentation.html",
Chris@1517 744 :class => "wiki-page"),
Chris@1517 745 '[[CookBook documentation|documentation]]' =>
Chris@1517 746 link_to("documentation", "CookBook_documentation.html",
Chris@1517 747 :class => "wiki-page"),
Chris@1517 748 '[[CookBook documentation#One-section]]' =>
Chris@1517 749 link_to("CookBook documentation", "CookBook_documentation.html#One-section",
Chris@1517 750 :class => "wiki-page"),
Chris@1517 751 '[[CookBook documentation#One-section|documentation]]' =>
Chris@1517 752 link_to("documentation", "CookBook_documentation.html#One-section",
Chris@1517 753 :class => "wiki-page"),
Chris@909 754 # page that doesn't exist
Chris@1517 755 '[[Unknown page]]' =>
Chris@1517 756 link_to("Unknown page", "Unknown_page.html",
Chris@1517 757 :class => "wiki-page new"),
Chris@1517 758 '[[Unknown page|404]]' =>
Chris@1517 759 link_to("404", "Unknown_page.html",
Chris@1517 760 :class => "wiki-page new"),
Chris@1517 761 '[[Unknown page#anchor]]' =>
Chris@1517 762 link_to("Unknown page", "Unknown_page.html#anchor",
Chris@1517 763 :class => "wiki-page new"),
Chris@1517 764 '[[Unknown page#anchor|404]]' =>
Chris@1517 765 link_to("404", "Unknown_page.html#anchor",
Chris@1517 766 :class => "wiki-page new"),
Chris@909 767 }
Chris@909 768 @project = Project.find(1)
Chris@1517 769 to_test.each do |text, result|
Chris@1517 770 assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :local)
Chris@1517 771 end
Chris@909 772 end
Chris@909 773
Chris@1115 774 def test_wiki_links_within_wiki_page_context
Chris@1115 775 page = WikiPage.find_by_title('Another_page' )
Chris@1115 776 to_test = {
Chris@1517 777 '[[CookBook documentation]]' =>
Chris@1517 778 link_to("CookBook documentation",
Chris@1517 779 "/projects/ecookbook/wiki/CookBook_documentation",
Chris@1517 780 :class => "wiki-page"),
Chris@1517 781 '[[CookBook documentation|documentation]]' =>
Chris@1517 782 link_to("documentation",
Chris@1517 783 "/projects/ecookbook/wiki/CookBook_documentation",
Chris@1517 784 :class => "wiki-page"),
Chris@1517 785 '[[CookBook documentation#One-section]]' =>
Chris@1517 786 link_to("CookBook documentation",
Chris@1517 787 "/projects/ecookbook/wiki/CookBook_documentation#One-section",
Chris@1517 788 :class => "wiki-page"),
Chris@1517 789 '[[CookBook documentation#One-section|documentation]]' =>
Chris@1517 790 link_to("documentation",
Chris@1517 791 "/projects/ecookbook/wiki/CookBook_documentation#One-section",
Chris@1517 792 :class => "wiki-page"),
Chris@1115 793 # link to the current page
Chris@1517 794 '[[Another page]]' =>
Chris@1517 795 link_to("Another page",
Chris@1517 796 "/projects/ecookbook/wiki/Another_page",
Chris@1517 797 :class => "wiki-page"),
Chris@1517 798 '[[Another page|Page]]' =>
Chris@1517 799 link_to("Page",
Chris@1517 800 "/projects/ecookbook/wiki/Another_page",
Chris@1517 801 :class => "wiki-page"),
Chris@1517 802 '[[Another page#anchor]]' =>
Chris@1517 803 link_to("Another page",
Chris@1517 804 "#anchor",
Chris@1517 805 :class => "wiki-page"),
Chris@1517 806 '[[Another page#anchor|Page]]' =>
Chris@1517 807 link_to("Page",
Chris@1517 808 "#anchor",
Chris@1517 809 :class => "wiki-page"),
Chris@1115 810 # page that doesn't exist
Chris@1517 811 '[[Unknown page]]' =>
Chris@1517 812 link_to("Unknown page",
Chris@1517 813 "/projects/ecookbook/wiki/Unknown_page?parent=Another_page",
Chris@1517 814 :class => "wiki-page new"),
Chris@1517 815 '[[Unknown page|404]]' =>
Chris@1517 816 link_to("404",
Chris@1517 817 "/projects/ecookbook/wiki/Unknown_page?parent=Another_page",
Chris@1517 818 :class => "wiki-page new"),
Chris@1517 819 '[[Unknown page#anchor]]' =>
Chris@1517 820 link_to("Unknown page",
Chris@1517 821 "/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor",
Chris@1517 822 :class => "wiki-page new"),
Chris@1517 823 '[[Unknown page#anchor|404]]' =>
Chris@1517 824 link_to("404",
Chris@1517 825 "/projects/ecookbook/wiki/Unknown_page?parent=Another_page#anchor",
Chris@1517 826 :class => "wiki-page new"),
Chris@1115 827 }
Chris@1115 828 @project = Project.find(1)
Chris@1517 829 to_test.each do |text, result|
Chris@1517 830 assert_equal "<p>#{result}</p>",
Chris@1517 831 textilizable(WikiContent.new( :text => text, :page => page ), :text)
Chris@1517 832 end
Chris@1115 833 end
Chris@1115 834
Chris@1115 835 def test_wiki_links_anchor_option_should_prepend_page_title_to_href
Chris@1115 836 to_test = {
Chris@1115 837 # link to a page
Chris@1517 838 '[[CookBook documentation]]' =>
Chris@1517 839 link_to("CookBook documentation",
Chris@1517 840 "#CookBook_documentation",
Chris@1517 841 :class => "wiki-page"),
Chris@1517 842 '[[CookBook documentation|documentation]]' =>
Chris@1517 843 link_to("documentation",
Chris@1517 844 "#CookBook_documentation",
Chris@1517 845 :class => "wiki-page"),
Chris@1517 846 '[[CookBook documentation#One-section]]' =>
Chris@1517 847 link_to("CookBook documentation",
Chris@1517 848 "#CookBook_documentation_One-section",
Chris@1517 849 :class => "wiki-page"),
Chris@1517 850 '[[CookBook documentation#One-section|documentation]]' =>
Chris@1517 851 link_to("documentation",
Chris@1517 852 "#CookBook_documentation_One-section",
Chris@1517 853 :class => "wiki-page"),
Chris@1115 854 # page that doesn't exist
Chris@1517 855 '[[Unknown page]]' =>
Chris@1517 856 link_to("Unknown page",
Chris@1517 857 "#Unknown_page",
Chris@1517 858 :class => "wiki-page new"),
Chris@1517 859 '[[Unknown page|404]]' =>
Chris@1517 860 link_to("404",
Chris@1517 861 "#Unknown_page",
Chris@1517 862 :class => "wiki-page new"),
Chris@1517 863 '[[Unknown page#anchor]]' =>
Chris@1517 864 link_to("Unknown page",
Chris@1517 865 "#Unknown_page_anchor",
Chris@1517 866 :class => "wiki-page new"),
Chris@1517 867 '[[Unknown page#anchor|404]]' =>
Chris@1517 868 link_to("404",
Chris@1517 869 "#Unknown_page_anchor",
Chris@1517 870 :class => "wiki-page new"),
Chris@1115 871 }
Chris@1115 872 @project = Project.find(1)
Chris@1517 873 to_test.each do |text, result|
Chris@1517 874 assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :anchor)
Chris@1517 875 end
Chris@1115 876 end
Chris@1115 877
Chris@0 878 def test_html_tags
Chris@0 879 to_test = {
Chris@0 880 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
Chris@0 881 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
Chris@0 882 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
Chris@0 883 # do not escape pre/code tags
Chris@0 884 "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
Chris@0 885 "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
Chris@0 886 "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
Chris@0 887 "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
Chris@0 888 "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
Chris@0 889 # remove attributes except class
Chris@0 890 "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
Chris@119 891 '<pre class="foo">some text</pre>' => '<pre class="foo">some text</pre>',
Chris@119 892 "<pre class='foo bar'>some text</pre>" => "<pre class='foo bar'>some text</pre>",
Chris@119 893 '<pre class="foo bar">some text</pre>' => '<pre class="foo bar">some text</pre>',
Chris@0 894 "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
Chris@119 895 # xss
Chris@119 896 '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
Chris@119 897 '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
Chris@0 898 }
Chris@0 899 to_test.each { |text, result| assert_equal result, textilizable(text) }
Chris@0 900 end
Chris@441 901
Chris@0 902 def test_allowed_html_tags
Chris@0 903 to_test = {
Chris@0 904 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
Chris@0 905 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
Chris@0 906 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
Chris@0 907 }
Chris@0 908 to_test.each { |text, result| assert_equal result, textilizable(text) }
Chris@0 909 end
Chris@441 910
Chris@0 911 def test_pre_tags
Chris@0 912 raw = <<-RAW
Chris@0 913 Before
Chris@0 914
Chris@0 915 <pre>
Chris@0 916 <prepared-statement-cache-size>32</prepared-statement-cache-size>
Chris@0 917 </pre>
Chris@0 918
Chris@0 919 After
Chris@0 920 RAW
Chris@0 921
Chris@0 922 expected = <<-EXPECTED
Chris@0 923 <p>Before</p>
Chris@0 924 <pre>
Chris@0 925 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
Chris@0 926 </pre>
Chris@0 927 <p>After</p>
Chris@0 928 EXPECTED
Chris@441 929
Chris@0 930 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 931 end
Chris@441 932
Chris@0 933 def test_pre_content_should_not_parse_wiki_and_redmine_links
Chris@0 934 raw = <<-RAW
Chris@0 935 [[CookBook documentation]]
Chris@0 936
Chris@0 937 #1
Chris@0 938
Chris@0 939 <pre>
Chris@0 940 [[CookBook documentation]]
Chris@0 941
Chris@0 942 #1
Chris@0 943 </pre>
Chris@0 944 RAW
Chris@0 945
Chris@1517 946 result1 = link_to("CookBook documentation",
Chris@1517 947 "/projects/ecookbook/wiki/CookBook_documentation",
Chris@1517 948 :class => "wiki-page")
Chris@1517 949 result2 = link_to('#1',
Chris@1517 950 "/issues/1",
Chris@1517 951 :class => Issue.find(1).css_classes,
Chris@1517 952 :title => "Can't print recipes (New)")
Chris@1517 953
Chris@0 954 expected = <<-EXPECTED
Chris@1517 955 <p>#{result1}</p>
Chris@1517 956 <p>#{result2}</p>
Chris@0 957 <pre>
Chris@0 958 [[CookBook documentation]]
Chris@0 959
Chris@0 960 #1
Chris@0 961 </pre>
Chris@0 962 EXPECTED
Chris@441 963
Chris@0 964 @project = Project.find(1)
Chris@0 965 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 966 end
Chris@441 967
Chris@0 968 def test_non_closing_pre_blocks_should_be_closed
Chris@0 969 raw = <<-RAW
Chris@0 970 <pre><code>
Chris@0 971 RAW
Chris@0 972
Chris@0 973 expected = <<-EXPECTED
Chris@0 974 <pre><code>
Chris@0 975 </code></pre>
Chris@0 976 EXPECTED
Chris@441 977
Chris@0 978 @project = Project.find(1)
Chris@0 979 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 980 end
Chris@441 981
Chris@0 982 def test_syntax_highlight
Chris@0 983 raw = <<-RAW
Chris@0 984 <pre><code class="ruby">
Chris@0 985 # Some ruby code here
Chris@0 986 </code></pre>
Chris@0 987 RAW
Chris@0 988
Chris@0 989 expected = <<-EXPECTED
Chris@1115 990 <pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="comment"># Some ruby code here</span></span>
Chris@0 991 </code></pre>
Chris@0 992 EXPECTED
Chris@0 993
Chris@0 994 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 995 end
Chris@441 996
Chris@1115 997 def test_to_path_param
Chris@1115 998 assert_equal 'test1/test2', to_path_param('test1/test2')
Chris@1115 999 assert_equal 'test1/test2', to_path_param('/test1/test2/')
Chris@1115 1000 assert_equal 'test1/test2', to_path_param('//test1/test2/')
Chris@1115 1001 assert_equal nil, to_path_param('/')
Chris@1115 1002 end
Chris@1115 1003
Chris@0 1004 def test_wiki_links_in_tables
Chris@1517 1005 text = "|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|"
Chris@1517 1006 link1 = link_to("Link title", "/projects/ecookbook/wiki/Page", :class => "wiki-page new")
Chris@1517 1007 link2 = link_to("Other title", "/projects/ecookbook/wiki/Other_Page", :class => "wiki-page new")
Chris@1517 1008 link3 = link_to("Last page", "/projects/ecookbook/wiki/Last_page", :class => "wiki-page new")
Chris@1517 1009 result = "<tr><td>#{link1}</td>" +
Chris@1517 1010 "<td>#{link2}</td>" +
Chris@1517 1011 "</tr><tr><td>Cell 21</td><td>#{link3}</td></tr>"
Chris@0 1012 @project = Project.find(1)
Chris@1517 1013 assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '')
Chris@0 1014 end
Chris@441 1015
Chris@0 1016 def test_text_formatting
Chris@0 1017 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
Chris@0 1018 '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
Chris@0 1019 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
Chris@0 1020 'a H *umane* W *eb* T *ext* G *enerator*' => 'a H <strong>umane</strong> W <strong>eb</strong> T <strong>ext</strong> G <strong>enerator</strong>',
Chris@0 1021 'a *H* umane *W* eb *T* ext *G* enerator' => 'a <strong>H</strong> umane <strong>W</strong> eb <strong>T</strong> ext <strong>G</strong> enerator',
Chris@0 1022 }
Chris@0 1023 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 1024 end
Chris@441 1025
Chris@0 1026 def test_wiki_horizontal_rule
Chris@0 1027 assert_equal '<hr />', textilizable('---')
Chris@0 1028 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
Chris@0 1029 end
Chris@441 1030
Chris@0 1031 def test_footnotes
Chris@0 1032 raw = <<-RAW
Chris@0 1033 This is some text[1].
Chris@0 1034
Chris@0 1035 fn1. This is the foot note
Chris@0 1036 RAW
Chris@0 1037
Chris@0 1038 expected = <<-EXPECTED
Chris@0 1039 <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
Chris@0 1040 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
Chris@0 1041 EXPECTED
Chris@0 1042
Chris@0 1043 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 1044 end
Chris@441 1045
Chris@441 1046 def test_headings
Chris@441 1047 raw = 'h1. Some heading'
Chris@441 1048 expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
Chris@441 1049
Chris@441 1050 assert_equal expected, textilizable(raw)
Chris@441 1051 end
Chris@441 1052
Chris@909 1053 def test_headings_with_special_chars
Chris@909 1054 # This test makes sure that the generated anchor names match the expected
Chris@909 1055 # ones even if the heading text contains unconventional characters
Chris@909 1056 raw = 'h1. Some heading related to version 0.5'
Chris@909 1057 anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5")
Chris@909 1058 expected = %|<a name="#{anchor}"></a>\n<h1 >Some heading related to version 0.5<a href="##{anchor}" class="wiki-anchor">&para;</a></h1>|
Chris@909 1059
Chris@909 1060 assert_equal expected, textilizable(raw)
Chris@909 1061 end
Chris@909 1062
Chris@909 1063 def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title
Chris@1115 1064 page = WikiPage.new( :title => 'Page Title', :wiki_id => 1 )
Chris@1115 1065 content = WikiContent.new( :text => 'h1. Some heading', :page => page )
Chris@909 1066
Chris@909 1067 expected = %|<a name="Page_Title_Some-heading"></a>\n<h1 >Some heading<a href="#Page_Title_Some-heading" class="wiki-anchor">&para;</a></h1>|
Chris@909 1068
Chris@909 1069 assert_equal expected, textilizable(content, :text, :wiki_links => :anchor )
Chris@909 1070 end
Chris@909 1071
Chris@0 1072 def test_table_of_content
Chris@0 1073 raw = <<-RAW
Chris@0 1074 {{toc}}
Chris@0 1075
Chris@0 1076 h1. Title
Chris@0 1077
Chris@0 1078 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
Chris@0 1079
Chris@0 1080 h2. Subtitle with a [[Wiki]] link
Chris@0 1081
Chris@0 1082 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
Chris@0 1083
Chris@0 1084 h2. Subtitle with [[Wiki|another Wiki]] link
Chris@0 1085
Chris@0 1086 h2. Subtitle with %{color:red}red text%
Chris@119 1087
Chris@119 1088 <pre>
Chris@119 1089 some code
Chris@119 1090 </pre>
Chris@119 1091
chris@37 1092 h3. Subtitle with *some* _modifiers_
Chris@0 1093
Chris@929 1094 h3. Subtitle with @inline code@
Chris@929 1095
Chris@0 1096 h1. Another title
Chris@0 1097
chris@37 1098 h3. An "Internet link":http://www.redmine.org/ inside subtitle
Chris@0 1099
Chris@0 1100 h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
Chris@0 1101
Chris@0 1102 RAW
Chris@0 1103
chris@37 1104 expected = '<ul class="toc">' +
chris@37 1105 '<li><a href="#Title">Title</a>' +
chris@37 1106 '<ul>' +
Chris@441 1107 '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
Chris@441 1108 '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
chris@37 1109 '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
chris@37 1110 '<ul>' +
chris@37 1111 '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
Chris@929 1112 '<li><a href="#Subtitle-with-inline-code">Subtitle with inline code</a></li>' +
chris@37 1113 '</ul>' +
chris@37 1114 '</li>' +
chris@37 1115 '</ul>' +
chris@37 1116 '</li>' +
chris@37 1117 '<li><a href="#Another-title">Another title</a>' +
chris@37 1118 '<ul>' +
chris@37 1119 '<li>' +
chris@37 1120 '<ul>' +
chris@37 1121 '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
chris@37 1122 '</ul>' +
chris@37 1123 '</li>' +
chris@37 1124 '<li><a href="#Project-Name">Project Name</a></li>' +
chris@37 1125 '</ul>' +
chris@37 1126 '</li>' +
Chris@0 1127 '</ul>'
Chris@0 1128
chris@37 1129 @project = Project.find(1)
Chris@909 1130 assert textilizable(raw).gsub("\n", "").include?(expected)
chris@37 1131 end
Chris@441 1132
Chris@929 1133 def test_table_of_content_should_generate_unique_anchors
Chris@929 1134 raw = <<-RAW
Chris@929 1135 {{toc}}
Chris@929 1136
Chris@929 1137 h1. Title
Chris@929 1138
Chris@929 1139 h2. Subtitle
Chris@929 1140
Chris@929 1141 h2. Subtitle
Chris@929 1142 RAW
Chris@929 1143
Chris@929 1144 expected = '<ul class="toc">' +
Chris@929 1145 '<li><a href="#Title">Title</a>' +
Chris@929 1146 '<ul>' +
Chris@929 1147 '<li><a href="#Subtitle">Subtitle</a></li>' +
Chris@929 1148 '<li><a href="#Subtitle-2">Subtitle</a></li>'
Chris@929 1149 '</ul>'
Chris@929 1150 '</li>' +
Chris@929 1151 '</ul>'
Chris@929 1152
Chris@929 1153 @project = Project.find(1)
Chris@929 1154 result = textilizable(raw).gsub("\n", "")
Chris@929 1155 assert_include expected, result
Chris@929 1156 assert_include '<a name="Subtitle">', result
Chris@929 1157 assert_include '<a name="Subtitle-2">', result
Chris@929 1158 end
Chris@929 1159
chris@37 1160 def test_table_of_content_should_contain_included_page_headings
chris@37 1161 raw = <<-RAW
chris@37 1162 {{toc}}
chris@37 1163
chris@37 1164 h1. Included
chris@37 1165
chris@37 1166 {{include(Child_1)}}
chris@37 1167 RAW
chris@37 1168
chris@37 1169 expected = '<ul class="toc">' +
chris@37 1170 '<li><a href="#Included">Included</a></li>' +
Chris@441 1171 '<li><a href="#Child-page-1">Child page 1</a></li>' +
chris@37 1172 '</ul>'
chris@37 1173
chris@37 1174 @project = Project.find(1)
Chris@0 1175 assert textilizable(raw).gsub("\n", "").include?(expected)
Chris@0 1176 end
Chris@0 1177
Chris@1517 1178 def test_toc_with_textile_formatting_should_be_parsed
Chris@1517 1179 with_settings :text_formatting => 'textile' do
Chris@1517 1180 assert_select_in textilizable("{{toc}}\n\nh1. Heading"), 'ul.toc li', :text => 'Heading'
Chris@1517 1181 assert_select_in textilizable("{{<toc}}\n\nh1. Heading"), 'ul.toc.left li', :text => 'Heading'
Chris@1517 1182 assert_select_in textilizable("{{>toc}}\n\nh1. Heading"), 'ul.toc.right li', :text => 'Heading'
Chris@1517 1183 end
Chris@1517 1184 end
Chris@1517 1185
Chris@1517 1186 if Object.const_defined?(:Redcarpet)
Chris@1517 1187 def test_toc_with_markdown_formatting_should_be_parsed
Chris@1517 1188 with_settings :text_formatting => 'markdown' do
Chris@1517 1189 assert_select_in textilizable("{{toc}}\n\n# Heading"), 'ul.toc li', :text => 'Heading'
Chris@1517 1190 assert_select_in textilizable("{{<toc}}\n\n# Heading"), 'ul.toc.left li', :text => 'Heading'
Chris@1517 1191 assert_select_in textilizable("{{>toc}}\n\n# Heading"), 'ul.toc.right li', :text => 'Heading'
Chris@1517 1192 end
Chris@1517 1193 end
Chris@1517 1194 end
Chris@1517 1195
Chris@929 1196 def test_section_edit_links
Chris@929 1197 raw = <<-RAW
Chris@929 1198 h1. Title
Chris@929 1199
Chris@929 1200 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
Chris@929 1201
Chris@929 1202 h2. Subtitle with a [[Wiki]] link
Chris@929 1203
Chris@929 1204 h2. Subtitle with *some* _modifiers_
Chris@929 1205
Chris@929 1206 h2. Subtitle with @inline code@
Chris@929 1207
Chris@929 1208 <pre>
Chris@929 1209 some code
Chris@929 1210
Chris@929 1211 h2. heading inside pre
Chris@929 1212
Chris@929 1213 <h2>html heading inside pre</h2>
Chris@929 1214 </pre>
Chris@929 1215
Chris@929 1216 h2. Subtitle after pre tag
Chris@929 1217 RAW
Chris@929 1218
Chris@929 1219 @project = Project.find(1)
Chris@929 1220 set_language_if_valid 'en'
Chris@929 1221 result = textilizable(raw, :edit_section_links => {:controller => 'wiki', :action => 'edit', :project_id => '1', :id => 'Test'}).gsub("\n", "")
Chris@929 1222
Chris@929 1223 # heading that contains inline code
Chris@1464 1224 assert_match Regexp.new('<div class="contextual" id="section-4" title="Edit this section">' +
Chris@929 1225 '<a href="/projects/1/wiki/Test/edit\?section=4"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
Chris@929 1226 '<a name="Subtitle-with-inline-code"></a>' +
Chris@929 1227 '<h2 >Subtitle with <code>inline code</code><a href="#Subtitle-with-inline-code" class="wiki-anchor">&para;</a></h2>'),
Chris@929 1228 result
Chris@929 1229
Chris@929 1230 # last heading
Chris@1464 1231 assert_match Regexp.new('<div class="contextual" id="section-5" title="Edit this section">' +
Chris@929 1232 '<a href="/projects/1/wiki/Test/edit\?section=5"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
Chris@929 1233 '<a name="Subtitle-after-pre-tag"></a>' +
Chris@929 1234 '<h2 >Subtitle after pre tag<a href="#Subtitle-after-pre-tag" class="wiki-anchor">&para;</a></h2>'),
Chris@929 1235 result
Chris@929 1236 end
Chris@929 1237
Chris@0 1238 def test_default_formatter
Chris@1115 1239 with_settings :text_formatting => 'unknown' do
Chris@1115 1240 text = 'a *link*: http://www.example.net/'
Chris@1115 1241 assert_equal '<p>a *link*: <a class="external" href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
Chris@1115 1242 end
Chris@0 1243 end
Chris@441 1244
Chris@0 1245 def test_due_date_distance_in_words
Chris@0 1246 to_test = { Date.today => 'Due in 0 days',
Chris@0 1247 Date.today + 1 => 'Due in 1 day',
Chris@0 1248 Date.today + 100 => 'Due in about 3 months',
Chris@0 1249 Date.today + 20000 => 'Due in over 54 years',
Chris@0 1250 Date.today - 1 => '1 day late',
Chris@0 1251 Date.today - 100 => 'about 3 months late',
Chris@0 1252 Date.today - 20000 => 'over 54 years late',
Chris@0 1253 }
Chris@119 1254 ::I18n.locale = :en
Chris@0 1255 to_test.each do |date, expected|
Chris@0 1256 assert_equal expected, due_date_distance_in_words(date)
Chris@0 1257 end
Chris@0 1258 end
Chris@441 1259
Chris@1115 1260 def test_avatar_enabled
Chris@1115 1261 with_settings :gravatar_enabled => '1' do
Chris@1115 1262 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
Chris@1115 1263 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
Chris@1115 1264 # Default size is 50
Chris@1115 1265 assert avatar('jsmith <jsmith@somenet.foo>').include?('size=50')
Chris@1115 1266 assert avatar('jsmith <jsmith@somenet.foo>', :size => 24).include?('size=24')
Chris@1115 1267 # Non-avatar options should be considered html options
Chris@1115 1268 assert avatar('jsmith <jsmith@somenet.foo>', :title => 'John Smith').include?('title="John Smith"')
Chris@1115 1269 # The default class of the img tag should be gravatar
Chris@1115 1270 assert avatar('jsmith <jsmith@somenet.foo>').include?('class="gravatar"')
Chris@1115 1271 assert !avatar('jsmith <jsmith@somenet.foo>', :class => 'picture').include?('class="gravatar"')
Chris@1115 1272 assert_nil avatar('jsmith')
Chris@1115 1273 assert_nil avatar(nil)
Chris@1115 1274 end
Chris@1115 1275 end
Chris@441 1276
Chris@1115 1277 def test_avatar_disabled
Chris@1115 1278 with_settings :gravatar_enabled => '0' do
Chris@1115 1279 assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
Chris@1115 1280 end
Chris@0 1281 end
Chris@441 1282
Chris@0 1283 def test_link_to_user
Chris@0 1284 user = User.find(2)
Chris@1517 1285 result = link_to("John Smith", "/users/2", :class => "user active")
Chris@1517 1286 assert_equal result, link_to_user(user)
Chris@0 1287 end
Chris@441 1288
Chris@0 1289 def test_link_to_user_should_not_link_to_locked_user
Chris@1115 1290 with_current_user nil do
Chris@1115 1291 user = User.find(5)
Chris@1115 1292 assert user.locked?
Chris@1115 1293 assert_equal 'Dave2 Lopper2', link_to_user(user)
Chris@1115 1294 end
Chris@1115 1295 end
Chris@1115 1296
Chris@1115 1297 def test_link_to_user_should_link_to_locked_user_if_current_user_is_admin
Chris@1115 1298 with_current_user User.find(1) do
Chris@1115 1299 user = User.find(5)
Chris@1115 1300 assert user.locked?
Chris@1517 1301 result = link_to("Dave2 Lopper2", "/users/5", :class => "user locked")
Chris@1517 1302 assert_equal result, link_to_user(user)
Chris@1115 1303 end
Chris@0 1304 end
Chris@441 1305
Chris@0 1306 def test_link_to_user_should_not_link_to_anonymous
Chris@0 1307 user = User.anonymous
Chris@0 1308 assert user.anonymous?
Chris@0 1309 t = link_to_user(user)
Chris@0 1310 assert_equal ::I18n.t(:label_user_anonymous), t
Chris@0 1311 end
Chris@14 1312
Chris@1464 1313 def test_link_to_attachment
Chris@1464 1314 a = Attachment.find(3)
Chris@1464 1315 assert_equal '<a href="/attachments/3/logo.gif">logo.gif</a>',
Chris@1464 1316 link_to_attachment(a)
Chris@1464 1317 assert_equal '<a href="/attachments/3/logo.gif">Text</a>',
Chris@1464 1318 link_to_attachment(a, :text => 'Text')
Chris@1517 1319 result = link_to("logo.gif", "/attachments/3/logo.gif", :class => "foo")
Chris@1517 1320 assert_equal result,
Chris@1464 1321 link_to_attachment(a, :class => 'foo')
Chris@1464 1322 assert_equal '<a href="/attachments/download/3/logo.gif">logo.gif</a>',
Chris@1464 1323 link_to_attachment(a, :download => true)
Chris@1464 1324 assert_equal '<a href="http://test.host/attachments/3/logo.gif">logo.gif</a>',
Chris@1464 1325 link_to_attachment(a, :only_path => false)
Chris@1464 1326 end
Chris@1464 1327
Chris@1464 1328 def test_thumbnail_tag
Chris@1464 1329 a = Attachment.find(3)
Chris@1464 1330 assert_equal '<a href="/attachments/3/logo.gif" title="logo.gif"><img alt="3" src="/attachments/thumbnail/3" /></a>',
Chris@1464 1331 thumbnail_tag(a)
Chris@1464 1332 end
Chris@1464 1333
Chris@14 1334 def test_link_to_project
Chris@14 1335 project = Project.find(1)
Chris@14 1336 assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
Chris@14 1337 link_to_project(project)
Chris@14 1338 assert_equal %(<a href="/projects/ecookbook/settings">eCookbook</a>),
Chris@14 1339 link_to_project(project, :action => 'settings')
Chris@14 1340 assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
Chris@14 1341 link_to_project(project, {:only_path => false, :jump => 'blah'})
Chris@1517 1342 result = link_to("eCookbook", "/projects/ecookbook/settings", :class => "project")
Chris@1517 1343 assert_equal result,
Chris@14 1344 link_to_project(project, {:action => 'settings'}, :class => "project")
Chris@14 1345 end
Chris@909 1346
Chris@1464 1347 def test_link_to_project_settings
Chris@1464 1348 project = Project.find(1)
Chris@1464 1349 assert_equal '<a href="/projects/ecookbook/settings">eCookbook</a>', link_to_project_settings(project)
Chris@1464 1350
Chris@1464 1351 project.status = Project::STATUS_CLOSED
Chris@1464 1352 assert_equal '<a href="/projects/ecookbook">eCookbook</a>', link_to_project_settings(project)
Chris@1464 1353
Chris@1464 1354 project.status = Project::STATUS_ARCHIVED
Chris@1464 1355 assert_equal 'eCookbook', link_to_project_settings(project)
Chris@1464 1356 end
Chris@1464 1357
Chris@929 1358 def test_link_to_legacy_project_with_numerical_identifier_should_use_id
Chris@929 1359 # numeric identifier are no longer allowed
Chris@1517 1360 Project.where(:id => 1).update_all(:identifier => 25)
Chris@929 1361 assert_equal '<a href="/projects/1">eCookbook</a>',
Chris@929 1362 link_to_project(Project.find(1))
Chris@929 1363 end
Chris@929 1364
Chris@909 1365 def test_principals_options_for_select_with_users
Chris@1115 1366 User.current = nil
Chris@909 1367 users = [User.find(2), User.find(4)]
Chris@909 1368 assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>),
Chris@909 1369 principals_options_for_select(users)
Chris@909 1370 end
Chris@909 1371
Chris@909 1372 def test_principals_options_for_select_with_selected
Chris@1115 1373 User.current = nil
Chris@909 1374 users = [User.find(2), User.find(4)]
Chris@909 1375 assert_equal %(<option value="2">John Smith</option><option value="4" selected="selected">Robert Hill</option>),
Chris@909 1376 principals_options_for_select(users, User.find(4))
Chris@909 1377 end
Chris@909 1378
Chris@909 1379 def test_principals_options_for_select_with_users_and_groups
Chris@1115 1380 User.current = nil
Chris@909 1381 users = [User.find(2), Group.find(11), User.find(4), Group.find(10)]
Chris@909 1382 assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>) +
Chris@909 1383 %(<optgroup label="Groups"><option value="10">A Team</option><option value="11">B Team</option></optgroup>),
Chris@909 1384 principals_options_for_select(users)
Chris@909 1385 end
Chris@909 1386
Chris@909 1387 def test_principals_options_for_select_with_empty_collection
Chris@909 1388 assert_equal '', principals_options_for_select([])
Chris@909 1389 end
Chris@1115 1390
Chris@1115 1391 def test_principals_options_for_select_should_include_me_option_when_current_user_is_in_collection
Chris@1115 1392 users = [User.find(2), User.find(4)]
Chris@1115 1393 User.current = User.find(4)
Chris@1115 1394 assert_include '<option value="4">&lt;&lt; me &gt;&gt;</option>', principals_options_for_select(users)
Chris@1115 1395 end
Chris@1115 1396
Chris@1115 1397 def test_stylesheet_link_tag_should_pick_the_default_stylesheet
Chris@1115 1398 assert_match 'href="/stylesheets/styles.css"', stylesheet_link_tag("styles")
Chris@1115 1399 end
Chris@1115 1400
Chris@1115 1401 def test_stylesheet_link_tag_for_plugin_should_pick_the_plugin_stylesheet
Chris@1115 1402 assert_match 'href="/plugin_assets/foo/stylesheets/styles.css"', stylesheet_link_tag("styles", :plugin => :foo)
Chris@1115 1403 end
Chris@1115 1404
Chris@1115 1405 def test_image_tag_should_pick_the_default_image
Chris@1115 1406 assert_match 'src="/images/image.png"', image_tag("image.png")
Chris@1115 1407 end
Chris@1115 1408
Chris@1115 1409 def test_image_tag_should_pick_the_theme_image_if_it_exists
Chris@1115 1410 theme = Redmine::Themes.themes.last
Chris@1115 1411 theme.images << 'image.png'
Chris@1115 1412
Chris@1115 1413 with_settings :ui_theme => theme.id do
Chris@1115 1414 assert_match %|src="/themes/#{theme.dir}/images/image.png"|, image_tag("image.png")
Chris@1115 1415 assert_match %|src="/images/other.png"|, image_tag("other.png")
Chris@1115 1416 end
Chris@1115 1417 ensure
Chris@1115 1418 theme.images.delete 'image.png'
Chris@1115 1419 end
Chris@1115 1420
Chris@1115 1421 def test_image_tag_sfor_plugin_should_pick_the_plugin_image
Chris@1115 1422 assert_match 'src="/plugin_assets/foo/images/image.png"', image_tag("image.png", :plugin => :foo)
Chris@1115 1423 end
Chris@1115 1424
Chris@1115 1425 def test_javascript_include_tag_should_pick_the_default_javascript
Chris@1115 1426 assert_match 'src="/javascripts/scripts.js"', javascript_include_tag("scripts")
Chris@1115 1427 end
Chris@1115 1428
Chris@1115 1429 def test_javascript_include_tag_for_plugin_should_pick_the_plugin_javascript
Chris@1115 1430 assert_match 'src="/plugin_assets/foo/javascripts/scripts.js"', javascript_include_tag("scripts", :plugin => :foo)
Chris@1115 1431 end
Chris@1115 1432
Chris@1464 1433 def test_raw_json_should_escape_closing_tags
Chris@1464 1434 s = raw_json(["<foo>bar</foo>"])
Chris@1464 1435 assert_equal '["<foo>bar<\/foo>"]', s
Chris@1464 1436 end
Chris@1115 1437
Chris@1464 1438 def test_raw_json_should_be_html_safe
Chris@1464 1439 s = raw_json(["foo"])
Chris@1464 1440 assert s.html_safe?
Chris@1464 1441 end
Chris@1464 1442
Chris@1464 1443 def test_html_title_should_app_title_if_not_set
Chris@1464 1444 assert_equal 'Redmine', html_title
Chris@1464 1445 end
Chris@1464 1446
Chris@1464 1447 def test_html_title_should_join_items
Chris@1464 1448 html_title 'Foo', 'Bar'
Chris@1464 1449 assert_equal 'Foo - Bar - Redmine', html_title
Chris@1464 1450 end
Chris@1464 1451
Chris@1464 1452 def test_html_title_should_append_current_project_name
Chris@1464 1453 @project = Project.find(1)
Chris@1464 1454 html_title 'Foo', 'Bar'
Chris@1464 1455 assert_equal 'Foo - Bar - eCookbook - Redmine', html_title
Chris@1464 1456 end
Chris@1464 1457
Chris@1464 1458 def test_title_should_return_a_h2_tag
Chris@1464 1459 assert_equal '<h2>Foo</h2>', title('Foo')
Chris@1464 1460 end
Chris@1464 1461
Chris@1464 1462 def test_title_should_set_html_title
Chris@1464 1463 title('Foo')
Chris@1464 1464 assert_equal 'Foo - Redmine', html_title
Chris@1464 1465 end
Chris@1464 1466
Chris@1464 1467 def test_title_should_turn_arrays_into_links
Chris@1464 1468 assert_equal '<h2><a href="/foo">Foo</a></h2>', title(['Foo', '/foo'])
Chris@1464 1469 assert_equal 'Foo - Redmine', html_title
Chris@1464 1470 end
Chris@1464 1471
Chris@1464 1472 def test_title_should_join_items
Chris@1464 1473 assert_equal '<h2>Foo &#187; Bar</h2>', title('Foo', 'Bar')
Chris@1464 1474 assert_equal 'Bar - Foo - Redmine', html_title
Chris@1115 1475 end
Chris@1517 1476
Chris@1517 1477 def test_favicon_path
Chris@1517 1478 assert_match %r{^/favicon\.ico}, favicon_path
Chris@1517 1479 end
Chris@1517 1480
Chris@1517 1481 def test_favicon_path_with_suburi
Chris@1517 1482 Redmine::Utils.relative_url_root = '/foo'
Chris@1517 1483 assert_match %r{^/foo/favicon\.ico}, favicon_path
Chris@1517 1484 ensure
Chris@1517 1485 Redmine::Utils.relative_url_root = ''
Chris@1517 1486 end
Chris@1517 1487
Chris@1517 1488 def test_favicon_url
Chris@1517 1489 assert_match %r{^http://test\.host/favicon\.ico}, favicon_url
Chris@1517 1490 end
Chris@1517 1491
Chris@1517 1492 def test_favicon_url_with_suburi
Chris@1517 1493 Redmine::Utils.relative_url_root = '/foo'
Chris@1517 1494 assert_match %r{^http://test\.host/foo/favicon\.ico}, favicon_url
Chris@1517 1495 ensure
Chris@1517 1496 Redmine::Utils.relative_url_root = ''
Chris@1517 1497 end
Chris@1517 1498
Chris@1517 1499 def test_truncate_single_line
Chris@1517 1500 str = "01234"
Chris@1517 1501 result = truncate_single_line_raw("#{str}\n#{str}", 10)
Chris@1517 1502 assert_equal "01234 0...", result
Chris@1517 1503 assert !result.html_safe?
Chris@1517 1504 result = truncate_single_line_raw("#{str}<&#>\n#{str}#{str}", 16)
Chris@1517 1505 assert_equal "01234<&#> 012...", result
Chris@1517 1506 assert !result.html_safe?
Chris@1517 1507 end
Chris@1517 1508
Chris@1517 1509 def test_truncate_single_line_non_ascii
Chris@1517 1510 ja = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
Chris@1517 1511 ja.force_encoding('UTF-8') if ja.respond_to?(:force_encoding)
Chris@1517 1512 result = truncate_single_line_raw("#{ja}\n#{ja}\n#{ja}", 10)
Chris@1517 1513 assert_equal "#{ja} #{ja}...", result
Chris@1517 1514 assert !result.html_safe?
Chris@1517 1515 end
Chris@0 1516 end