annotate test/unit/helpers/application_helper_test.rb @ 1474:c4436fec34bf bug_494

Close obsolete branch bug_494
author Chris Cannam
date Sat, 10 Nov 2012 13:57:53 +0000
parents ec1c49528f36
children bb32da3bea34
rev   line source
Chris@0 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@441 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@441 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@119 18 require File.expand_path('../../../test_helper', __FILE__)
Chris@0 19
chris@22 20 class ApplicationHelperTest < ActionView::TestCase
Chris@0 21 fixtures :projects, :roles, :enabled_modules, :users,
Chris@909 22 :repositories, :changesets,
Chris@909 23 :trackers, :issue_statuses, :issues, :versions, :documents,
Chris@909 24 :wikis, :wiki_pages, :wiki_contents,
Chris@909 25 :boards, :messages, :news,
Chris@909 26 :attachments, :enumerations
Chris@0 27
Chris@0 28 def setup
Chris@0 29 super
Chris@909 30 set_tmp_attachments_directory
Chris@0 31 end
chris@22 32
chris@22 33 context "#link_to_if_authorized" do
chris@22 34 context "authorized user" do
chris@22 35 should "be tested"
chris@22 36 end
Chris@441 37
chris@22 38 context "unauthorized user" do
chris@22 39 should "be tested"
chris@22 40 end
Chris@441 41
chris@22 42 should "allow using the :controller and :action for the target link" do
chris@22 43 User.current = User.find_by_login('admin')
chris@22 44
chris@22 45 @project = Issue.first.project # Used by helper
chris@22 46 response = link_to_if_authorized("By controller/action",
chris@22 47 {:controller => 'issues', :action => 'edit', :id => Issue.first.id})
chris@22 48 assert_match /href/, response
chris@22 49 end
Chris@441 50
chris@22 51 end
Chris@441 52
Chris@0 53 def test_auto_links
Chris@0 54 to_test = {
Chris@0 55 'http://foo.bar' => '<a class="external" href="http://foo.bar">http://foo.bar</a>',
Chris@0 56 'http://foo.bar/~user' => '<a class="external" href="http://foo.bar/~user">http://foo.bar/~user</a>',
Chris@0 57 'http://foo.bar.' => '<a class="external" href="http://foo.bar">http://foo.bar</a>.',
Chris@0 58 'https://foo.bar.' => '<a class="external" href="https://foo.bar">https://foo.bar</a>.',
Chris@0 59 'This is a link: http://foo.bar.' => 'This is a link: <a class="external" href="http://foo.bar">http://foo.bar</a>.',
Chris@0 60 'A link (eg. http://foo.bar).' => 'A link (eg. <a class="external" href="http://foo.bar">http://foo.bar</a>).',
Chris@0 61 '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 62 '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 63 '(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 64 '(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 65 '(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 66 '(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 67 '(see "inline link":http://www.foo.bar/Test)' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>)',
Chris@0 68 '(see "inline link":http://www.foo.bar/Test).' => '(see <a href="http://www.foo.bar/Test" class="external">inline link</a>).',
Chris@0 69 'www.foo.bar' => '<a class="external" href="http://www.foo.bar">www.foo.bar</a>',
Chris@0 70 '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 71 'http://foo.bar/page#125' => '<a class="external" href="http://foo.bar/page#125">http://foo.bar/page#125</a>',
Chris@0 72 'http://foo@www.bar.com' => '<a class="external" href="http://foo@www.bar.com">http://foo@www.bar.com</a>',
Chris@0 73 '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 74 'ftp://foo.bar' => '<a class="external" href="ftp://foo.bar">ftp://foo.bar</a>',
Chris@0 75 'ftps://foo.bar' => '<a class="external" href="ftps://foo.bar">ftps://foo.bar</a>',
Chris@0 76 'sftp://foo.bar' => '<a class="external" href="sftp://foo.bar">sftp://foo.bar</a>',
Chris@0 77 # two exclamation marks
Chris@0 78 '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 79 # escaping
Chris@0 80 'http://foo"bar' => '<a class="external" href="http://foo&quot;bar">http://foo"bar</a>',
chris@37 81 # wrap in angle brackets
chris@37 82 '<http://foo.bar>' => '&lt;<a class="external" href="http://foo.bar">http://foo.bar</a>&gt;'
Chris@0 83 }
Chris@0 84 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 85 end
Chris@441 86
Chris@0 87 def test_auto_mailto
Chris@441 88 assert_equal '<p><a class="email" href="mailto:test@foo.bar">test@foo.bar</a></p>',
Chris@0 89 textilizable('test@foo.bar')
Chris@0 90 end
Chris@441 91
Chris@0 92 def test_inline_images
Chris@0 93 to_test = {
Chris@0 94 '!http://foo.bar/image.jpg!' => '<img src="http://foo.bar/image.jpg" alt="" />',
Chris@0 95 'floating !>http://foo.bar/image.jpg!' => 'floating <div style="float:right"><img src="http://foo.bar/image.jpg" alt="" /></div>',
Chris@0 96 'with class !(some-class)http://foo.bar/image.jpg!' => 'with class <img src="http://foo.bar/image.jpg" class="some-class" alt="" />',
Chris@0 97 # inline styles should be stripped
Chris@0 98 'with style !{width:100px;height100px}http://foo.bar/image.jpg!' => 'with style <img src="http://foo.bar/image.jpg" alt="" />',
Chris@0 99 '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 100 '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 101 }
Chris@0 102 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 103 end
Chris@441 104
Chris@0 105 def test_inline_images_inside_tags
Chris@0 106 raw = <<-RAW
Chris@0 107 h1. !foo.png! Heading
Chris@0 108
Chris@0 109 Centered image:
Chris@0 110
Chris@0 111 p=. !bar.gif!
Chris@0 112 RAW
Chris@0 113
Chris@0 114 assert textilizable(raw).include?('<img src="foo.png" alt="" />')
Chris@0 115 assert textilizable(raw).include?('<img src="bar.gif" alt="" />')
Chris@0 116 end
Chris@441 117
Chris@0 118 def test_attached_images
Chris@0 119 to_test = {
Chris@0 120 'Inline image: !logo.gif!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
Chris@0 121 'Inline image: !logo.GIF!' => 'Inline image: <img src="/attachments/download/3" title="This is a logo" alt="This is a logo" />',
Chris@0 122 'No match: !ogo.gif!' => 'No match: <img src="ogo.gif" alt="" />',
Chris@0 123 'No match: !ogo.GIF!' => 'No match: <img src="ogo.GIF" alt="" />',
Chris@0 124 # link image
Chris@0 125 '!logo.gif!:http://foo.bar/' => '<a href="http://foo.bar/"><img src="/attachments/download/3" title="This is a logo" alt="This is a logo" /></a>',
Chris@0 126 }
Chris@0 127 attachments = Attachment.find(:all)
Chris@0 128 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
Chris@0 129 end
Chris@441 130
Chris@909 131 def test_attached_images_filename_extension
Chris@909 132 set_tmp_attachments_directory
Chris@909 133 a1 = Attachment.new(
Chris@909 134 :container => Issue.find(1),
Chris@909 135 :file => mock_file_with_options({:original_filename => "testtest.JPG"}),
Chris@909 136 :author => User.find(1))
Chris@909 137 assert a1.save
Chris@909 138 assert_equal "testtest.JPG", a1.filename
Chris@909 139 assert_equal "image/jpeg", a1.content_type
Chris@909 140 assert a1.image?
Chris@909 141
Chris@909 142 a2 = Attachment.new(
Chris@909 143 :container => Issue.find(1),
Chris@909 144 :file => mock_file_with_options({:original_filename => "testtest.jpeg"}),
Chris@909 145 :author => User.find(1))
Chris@909 146 assert a2.save
Chris@909 147 assert_equal "testtest.jpeg", a2.filename
Chris@909 148 assert_equal "image/jpeg", a2.content_type
Chris@909 149 assert a2.image?
Chris@909 150
Chris@909 151 a3 = Attachment.new(
Chris@909 152 :container => Issue.find(1),
Chris@909 153 :file => mock_file_with_options({:original_filename => "testtest.JPE"}),
Chris@909 154 :author => User.find(1))
Chris@909 155 assert a3.save
Chris@909 156 assert_equal "testtest.JPE", a3.filename
Chris@909 157 assert_equal "image/jpeg", a3.content_type
Chris@909 158 assert a3.image?
Chris@909 159
Chris@909 160 a4 = Attachment.new(
Chris@909 161 :container => Issue.find(1),
Chris@909 162 :file => mock_file_with_options({:original_filename => "Testtest.BMP"}),
Chris@909 163 :author => User.find(1))
Chris@909 164 assert a4.save
Chris@909 165 assert_equal "Testtest.BMP", a4.filename
Chris@909 166 assert_equal "image/x-ms-bmp", a4.content_type
Chris@909 167 assert a4.image?
Chris@909 168
Chris@909 169 to_test = {
Chris@909 170 'Inline image: !testtest.jpg!' =>
Chris@909 171 'Inline image: <img src="/attachments/download/' + a1.id.to_s + '" alt="" />',
Chris@909 172 'Inline image: !testtest.jpeg!' =>
Chris@909 173 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '" alt="" />',
Chris@909 174 'Inline image: !testtest.jpe!' =>
Chris@909 175 'Inline image: <img src="/attachments/download/' + a3.id.to_s + '" alt="" />',
Chris@909 176 'Inline image: !testtest.bmp!' =>
Chris@909 177 'Inline image: <img src="/attachments/download/' + a4.id.to_s + '" alt="" />',
Chris@909 178 }
Chris@909 179
Chris@909 180 attachments = [a1, a2, a3, a4]
Chris@909 181 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
Chris@909 182 end
Chris@909 183
Chris@909 184 def test_attached_images_should_read_later
Chris@909 185 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
Chris@909 186 a1 = Attachment.find(16)
Chris@909 187 assert_equal "testfile.png", a1.filename
Chris@909 188 assert a1.readable?
Chris@909 189 assert (! a1.visible?(User.anonymous))
Chris@909 190 assert a1.visible?(User.find(2))
Chris@909 191 a2 = Attachment.find(17)
Chris@909 192 assert_equal "testfile.PNG", a2.filename
Chris@909 193 assert a2.readable?
Chris@909 194 assert (! a2.visible?(User.anonymous))
Chris@909 195 assert a2.visible?(User.find(2))
Chris@909 196 assert a1.created_on < a2.created_on
Chris@909 197
Chris@909 198 to_test = {
Chris@909 199 'Inline image: !testfile.png!' =>
Chris@909 200 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '" alt="" />',
Chris@909 201 'Inline image: !Testfile.PNG!' =>
Chris@909 202 'Inline image: <img src="/attachments/download/' + a2.id.to_s + '" alt="" />',
Chris@909 203 }
Chris@909 204 attachments = [a1, a2]
Chris@909 205 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => attachments) }
Chris@909 206 set_tmp_attachments_directory
Chris@909 207 end
Chris@909 208
Chris@0 209 def test_textile_external_links
Chris@0 210 to_test = {
Chris@0 211 'This is a "link":http://foo.bar' => 'This is a <a href="http://foo.bar" class="external">link</a>',
Chris@0 212 'This is an intern "link":/foo/bar' => 'This is an intern <a href="/foo/bar">link</a>',
Chris@0 213 '"link (Link title)":http://foo.bar' => '<a href="http://foo.bar" title="Link title" class="external">link</a>',
Chris@0 214 '"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 215 "This is not a \"Link\":\n\nAnother paragraph" => "This is not a \"Link\":</p>\n\n\n\t<p>Another paragraph",
Chris@0 216 # no multiline link text
Chris@0 217 "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 218 # mailto link
Chris@0 219 "\"system administrator\":mailto:sysadmin@example.com?subject=redmine%20permissions" => "<a href=\"mailto:sysadmin@example.com?subject=redmine%20permissions\">system administrator</a>",
Chris@0 220 # two exclamation marks
Chris@0 221 '"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 222 # escaping
Chris@0 223 '"test":http://foo"bar' => '<a href="http://foo&quot;bar" class="external">test</a>',
Chris@0 224 }
Chris@0 225 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 226 end
Chris@119 227
Chris@0 228 def test_redmine_links
Chris@441 229 issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3},
Chris@0 230 :class => 'issue status-1 priority-1 overdue', :title => 'Error 281 when updating a recipe (New)')
Chris@441 231
Chris@0 232 changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
Chris@0 233 :class => 'changeset', :title => 'My very first commit')
Chris@0 234 changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
Chris@0 235 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
Chris@441 236
Chris@0 237 document_link = link_to('Test document', {:controller => 'documents', :action => 'show', :id => 1},
Chris@0 238 :class => 'document')
Chris@441 239
Chris@0 240 version_link = link_to('1.0', {:controller => 'versions', :action => 'show', :id => 2},
Chris@0 241 :class => 'version')
Chris@0 242
Chris@909 243 board_url = {:controller => 'boards', :action => 'show', :id => 2, :project_id => 'ecookbook'}
Chris@909 244
Chris@0 245 message_url = {:controller => 'messages', :action => 'show', :board_id => 1, :id => 4}
Chris@909 246
Chris@909 247 news_url = {:controller => 'news', :action => 'show', :id => 1}
Chris@441 248
Chris@0 249 project_url = {:controller => 'projects', :action => 'show', :id => 'subproject1'}
Chris@441 250
Chris@0 251 source_url = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']}
Chris@0 252 source_url_with_ext = {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file.ext']}
Chris@441 253
Chris@0 254 to_test = {
Chris@0 255 # tickets
Chris@0 256 '#3, [#3], (#3) and #3.' => "#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.",
Chris@0 257 # changesets
Chris@0 258 'r1' => changeset_link,
Chris@0 259 'r1.' => "#{changeset_link}.",
Chris@0 260 'r1, r2' => "#{changeset_link}, #{changeset_link2}",
Chris@0 261 'r1,r2' => "#{changeset_link},#{changeset_link2}",
Chris@0 262 # documents
Chris@0 263 'document#1' => document_link,
Chris@0 264 'document:"Test document"' => document_link,
Chris@0 265 # versions
Chris@0 266 'version#2' => version_link,
Chris@0 267 'version:1.0' => version_link,
Chris@0 268 'version:"1.0"' => version_link,
Chris@0 269 # source
Chris@0 270 'source:/some/file' => link_to('source:/some/file', source_url, :class => 'source'),
Chris@0 271 'source:/some/file.' => link_to('source:/some/file', source_url, :class => 'source') + ".",
Chris@0 272 'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
Chris@0 273 'source:/some/file. ' => link_to('source:/some/file', source_url, :class => 'source') + ".",
Chris@0 274 'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, :class => 'source') + ".",
Chris@0 275 'source:/some/file, ' => link_to('source:/some/file', source_url, :class => 'source') + ",",
Chris@0 276 'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(:rev => 52), :class => 'source'),
Chris@0 277 'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_ext.merge(:rev => 52), :class => 'source'),
Chris@0 278 'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(:anchor => 'L110'), :class => 'source'),
Chris@0 279 'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext.merge(:anchor => 'L110'), :class => 'source'),
Chris@0 280 'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(:rev => 52, :anchor => 'L110'), :class => 'source'),
Chris@0 281 'export:/some/file' => link_to('export:/some/file', source_url.merge(:format => 'raw'), :class => 'source download'),
Chris@909 282 # forum
Chris@909 283 'forum#2' => link_to('Discussion', board_url, :class => 'board'),
Chris@909 284 'forum:Discussion' => link_to('Discussion', board_url, :class => 'board'),
Chris@0 285 # message
Chris@0 286 'message#4' => link_to('Post 2', message_url, :class => 'message'),
Chris@210 287 'message#5' => link_to('RE: post 2', message_url.merge(:anchor => 'message-5', :r => 5), :class => 'message'),
Chris@909 288 # news
Chris@909 289 'news#1' => link_to('eCookbook first release !', news_url, :class => 'news'),
Chris@909 290 'news:"eCookbook first release !"' => link_to('eCookbook first release !', news_url, :class => 'news'),
Chris@0 291 # project
Chris@0 292 'project#3' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
Chris@0 293 'project:subproject1' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
Chris@0 294 'project:"eCookbook subProject 1"' => link_to('eCookbook Subproject 1', project_url, :class => 'project'),
Chris@0 295 # escaping
Chris@0 296 '!#3.' => '#3.',
Chris@0 297 '!r1' => 'r1',
Chris@0 298 '!document#1' => 'document#1',
Chris@0 299 '!document:"Test document"' => 'document:"Test document"',
Chris@0 300 '!version#2' => 'version#2',
Chris@0 301 '!version:1.0' => 'version:1.0',
Chris@0 302 '!version:"1.0"' => 'version:"1.0"',
Chris@0 303 '!source:/some/file' => 'source:/some/file',
Chris@0 304 # not found
Chris@0 305 '#0123456789' => '#0123456789',
Chris@0 306 # invalid expressions
Chris@0 307 'source:' => 'source:',
Chris@0 308 # url hash
Chris@0 309 "http://foo.bar/FAQ#3" => '<a class="external" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a>',
Chris@0 310 }
Chris@0 311 @project = Project.find(1)
Chris@0 312 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
Chris@0 313 end
Chris@441 314
Chris@210 315 def test_cross_project_redmine_links
Chris@210 316 source_link = link_to('ecookbook:source:/some/file', {:controller => 'repositories', :action => 'entry', :id => 'ecookbook', :path => ['some', 'file']},
Chris@210 317 :class => 'source')
Chris@441 318
Chris@210 319 changeset_link = link_to('ecookbook:r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
Chris@210 320 :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
Chris@441 321
Chris@210 322 to_test = {
Chris@210 323 # documents
Chris@210 324 'document:"Test document"' => 'document:"Test document"',
Chris@210 325 'ecookbook:document:"Test document"' => '<a href="/documents/1" class="document">Test document</a>',
Chris@210 326 'invalid:document:"Test document"' => 'invalid:document:"Test document"',
Chris@210 327 # versions
Chris@210 328 'version:"1.0"' => 'version:"1.0"',
Chris@909 329 'ecookbook:version:"1.0"' => '<a href="/versions/2" class="version">1.0</a>',
Chris@210 330 'invalid:version:"1.0"' => 'invalid:version:"1.0"',
Chris@210 331 # changeset
Chris@210 332 'r2' => 'r2',
Chris@210 333 'ecookbook:r2' => changeset_link,
Chris@210 334 'invalid:r2' => 'invalid:r2',
Chris@210 335 # source
Chris@210 336 'source:/some/file' => 'source:/some/file',
Chris@210 337 'ecookbook:source:/some/file' => source_link,
Chris@210 338 'invalid:source:/some/file' => 'invalid:source:/some/file',
Chris@210 339 }
Chris@210 340 @project = Project.find(3)
Chris@210 341 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text), "#{text} failed" }
Chris@210 342 end
Chris@119 343
Chris@119 344 def test_redmine_links_git_commit
Chris@119 345 changeset_link = link_to('abcd',
Chris@119 346 {
Chris@119 347 :controller => 'repositories',
Chris@119 348 :action => 'revision',
Chris@119 349 :id => 'subproject1',
Chris@119 350 :rev => 'abcd',
Chris@119 351 },
Chris@119 352 :class => 'changeset', :title => 'test commit')
Chris@119 353 to_test = {
Chris@119 354 'commit:abcd' => changeset_link,
Chris@119 355 }
Chris@119 356 @project = Project.find(3)
Chris@119 357 r = Repository::Git.create!(:project => @project, :url => '/tmp/test/git')
Chris@119 358 assert r
Chris@119 359 c = Changeset.new(:repository => r,
Chris@119 360 :committed_on => Time.now,
Chris@119 361 :revision => 'abcd',
Chris@119 362 :scmid => 'abcd',
Chris@119 363 :comments => 'test commit')
Chris@119 364 assert( c.save )
Chris@119 365 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@119 366 end
Chris@119 367
Chris@119 368 # TODO: Bazaar commit id contains mail address, so it contains '@' and '_'.
Chris@119 369 def test_redmine_links_darcs_commit
Chris@119 370 changeset_link = link_to('20080308225258-98289-abcd456efg.gz',
Chris@119 371 {
Chris@119 372 :controller => 'repositories',
Chris@119 373 :action => 'revision',
Chris@119 374 :id => 'subproject1',
Chris@119 375 :rev => '123',
Chris@119 376 },
Chris@119 377 :class => 'changeset', :title => 'test commit')
Chris@119 378 to_test = {
Chris@119 379 'commit:20080308225258-98289-abcd456efg.gz' => changeset_link,
Chris@119 380 }
Chris@119 381 @project = Project.find(3)
Chris@245 382 r = Repository::Darcs.create!(
Chris@245 383 :project => @project, :url => '/tmp/test/darcs',
Chris@245 384 :log_encoding => 'UTF-8')
Chris@119 385 assert r
Chris@119 386 c = Changeset.new(:repository => r,
Chris@119 387 :committed_on => Time.now,
Chris@119 388 :revision => '123',
Chris@119 389 :scmid => '20080308225258-98289-abcd456efg.gz',
Chris@119 390 :comments => 'test commit')
Chris@119 391 assert( c.save )
Chris@119 392 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@119 393 end
Chris@119 394
Chris@119 395 def test_redmine_links_mercurial_commit
Chris@119 396 changeset_link_rev = link_to('r123',
Chris@119 397 {
Chris@119 398 :controller => 'repositories',
Chris@119 399 :action => 'revision',
Chris@119 400 :id => 'subproject1',
Chris@119 401 :rev => '123' ,
Chris@119 402 },
Chris@119 403 :class => 'changeset', :title => 'test commit')
Chris@119 404 changeset_link_commit = link_to('abcd',
Chris@119 405 {
Chris@119 406 :controller => 'repositories',
Chris@119 407 :action => 'revision',
Chris@119 408 :id => 'subproject1',
Chris@119 409 :rev => 'abcd' ,
Chris@119 410 },
Chris@119 411 :class => 'changeset', :title => 'test commit')
Chris@119 412 to_test = {
Chris@119 413 'r123' => changeset_link_rev,
Chris@119 414 'commit:abcd' => changeset_link_commit,
Chris@119 415 }
Chris@119 416 @project = Project.find(3)
Chris@119 417 r = Repository::Mercurial.create!(:project => @project, :url => '/tmp/test')
Chris@119 418 assert r
Chris@119 419 c = Changeset.new(:repository => r,
Chris@119 420 :committed_on => Time.now,
Chris@119 421 :revision => '123',
Chris@119 422 :scmid => 'abcd',
Chris@119 423 :comments => 'test commit')
Chris@119 424 assert( c.save )
Chris@119 425 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@119 426 end
Chris@119 427
Chris@0 428 def test_attachment_links
Chris@0 429 attachment_link = link_to('error281.txt', {:controller => 'attachments', :action => 'download', :id => '1'}, :class => 'attachment')
Chris@0 430 to_test = {
Chris@0 431 'attachment:error281.txt' => attachment_link
Chris@0 432 }
Chris@0 433 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :attachments => Issue.find(3).attachments), "#{text} failed" }
Chris@0 434 end
Chris@441 435
Chris@0 436 def test_wiki_links
Chris@0 437 to_test = {
Chris@0 438 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
Chris@0 439 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
Chris@909 440 # title content should be formatted
Chris@909 441 '[[Another page|With _styled_ *title*]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">With <em>styled</em> <strong>title</strong></a>',
Chris@909 442 '[[Another page|With title containing <strong>HTML entities &amp; markups</strong>]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">With title containing &lt;strong&gt;HTML entities &amp; markups&lt;/strong&gt;</a>',
Chris@0 443 # link with anchor
Chris@0 444 '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
Chris@0 445 '[[Another page#anchor|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page#anchor" class="wiki-page">Page</a>',
Chris@0 446 # page that doesn't exist
Chris@0 447 '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
Chris@0 448 '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
Chris@0 449 # link to another project wiki
chris@37 450 '[[onlinestore:]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">onlinestore</a>',
chris@37 451 '[[onlinestore:|Wiki]]' => '<a href="/projects/onlinestore/wiki" class="wiki-page">Wiki</a>',
Chris@0 452 '[[onlinestore:Start page]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Start page</a>',
Chris@0 453 '[[onlinestore:Start page|Text]]' => '<a href="/projects/onlinestore/wiki/Start_page" class="wiki-page">Text</a>',
Chris@0 454 '[[onlinestore:Unknown page]]' => '<a href="/projects/onlinestore/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
Chris@0 455 # striked through link
Chris@0 456 '-[[Another page|Page]]-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a></del>',
Chris@0 457 '-[[Another page|Page]] link-' => '<del><a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a> link</del>',
Chris@0 458 # escaping
Chris@0 459 '![[Another page|Page]]' => '[[Another page|Page]]',
Chris@0 460 # project does not exist
Chris@0 461 '[[unknowproject:Start]]' => '[[unknowproject:Start]]',
Chris@0 462 '[[unknowproject:Start|Page title]]' => '[[unknowproject:Start|Page title]]',
Chris@0 463 }
Chris@909 464
Chris@0 465 @project = Project.find(1)
Chris@0 466 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 467 end
Chris@441 468
Chris@909 469 def test_wiki_links_within_local_file_generation_context
Chris@909 470
Chris@909 471 to_test = {
Chris@909 472 # link to a page
Chris@909 473 '[[CookBook documentation]]' => '<a href="CookBook_documentation.html" class="wiki-page">CookBook documentation</a>',
Chris@909 474 '[[CookBook documentation|documentation]]' => '<a href="CookBook_documentation.html" class="wiki-page">documentation</a>',
Chris@909 475 '[[CookBook documentation#One-section]]' => '<a href="CookBook_documentation.html#One-section" class="wiki-page">CookBook documentation</a>',
Chris@909 476 '[[CookBook documentation#One-section|documentation]]' => '<a href="CookBook_documentation.html#One-section" class="wiki-page">documentation</a>',
Chris@909 477 # page that doesn't exist
Chris@909 478 '[[Unknown page]]' => '<a href="Unknown_page.html" class="wiki-page new">Unknown page</a>',
Chris@909 479 '[[Unknown page|404]]' => '<a href="Unknown_page.html" class="wiki-page new">404</a>',
Chris@909 480 '[[Unknown page#anchor]]' => '<a href="Unknown_page.html#anchor" class="wiki-page new">Unknown page</a>',
Chris@909 481 '[[Unknown page#anchor|404]]' => '<a href="Unknown_page.html#anchor" class="wiki-page new">404</a>',
Chris@909 482 }
Chris@909 483
Chris@909 484 @project = Project.find(1)
Chris@909 485
Chris@909 486 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :local) }
Chris@909 487 end
Chris@909 488
Chris@0 489 def test_html_tags
Chris@0 490 to_test = {
Chris@0 491 "<div>content</div>" => "<p>&lt;div&gt;content&lt;/div&gt;</p>",
Chris@0 492 "<div class=\"bold\">content</div>" => "<p>&lt;div class=\"bold\"&gt;content&lt;/div&gt;</p>",
Chris@0 493 "<script>some script;</script>" => "<p>&lt;script&gt;some script;&lt;/script&gt;</p>",
Chris@0 494 # do not escape pre/code tags
Chris@0 495 "<pre>\nline 1\nline2</pre>" => "<pre>\nline 1\nline2</pre>",
Chris@0 496 "<pre><code>\nline 1\nline2</code></pre>" => "<pre><code>\nline 1\nline2</code></pre>",
Chris@0 497 "<pre><div>content</div></pre>" => "<pre>&lt;div&gt;content&lt;/div&gt;</pre>",
Chris@0 498 "HTML comment: <!-- no comments -->" => "<p>HTML comment: &lt;!-- no comments --&gt;</p>",
Chris@0 499 "<!-- opening comment" => "<p>&lt;!-- opening comment</p>",
Chris@0 500 # remove attributes except class
Chris@0 501 "<pre class='foo'>some text</pre>" => "<pre class='foo'>some text</pre>",
Chris@119 502 '<pre class="foo">some text</pre>' => '<pre class="foo">some text</pre>',
Chris@119 503 "<pre class='foo bar'>some text</pre>" => "<pre class='foo bar'>some text</pre>",
Chris@119 504 '<pre class="foo bar">some text</pre>' => '<pre class="foo bar">some text</pre>',
Chris@0 505 "<pre onmouseover='alert(1)'>some text</pre>" => "<pre>some text</pre>",
Chris@119 506 # xss
Chris@119 507 '<pre><code class=""onmouseover="alert(1)">text</code></pre>' => '<pre><code>text</code></pre>',
Chris@119 508 '<pre class=""onmouseover="alert(1)">text</pre>' => '<pre>text</pre>',
Chris@0 509 }
Chris@0 510 to_test.each { |text, result| assert_equal result, textilizable(text) }
Chris@0 511 end
Chris@441 512
Chris@0 513 def test_allowed_html_tags
Chris@0 514 to_test = {
Chris@0 515 "<pre>preformatted text</pre>" => "<pre>preformatted text</pre>",
Chris@0 516 "<notextile>no *textile* formatting</notextile>" => "no *textile* formatting",
Chris@0 517 "<notextile>this is <tag>a tag</tag></notextile>" => "this is &lt;tag&gt;a tag&lt;/tag&gt;"
Chris@0 518 }
Chris@0 519 to_test.each { |text, result| assert_equal result, textilizable(text) }
Chris@0 520 end
Chris@441 521
Chris@0 522 def test_pre_tags
Chris@0 523 raw = <<-RAW
Chris@0 524 Before
Chris@0 525
Chris@0 526 <pre>
Chris@0 527 <prepared-statement-cache-size>32</prepared-statement-cache-size>
Chris@0 528 </pre>
Chris@0 529
Chris@0 530 After
Chris@0 531 RAW
Chris@0 532
Chris@0 533 expected = <<-EXPECTED
Chris@0 534 <p>Before</p>
Chris@0 535 <pre>
Chris@0 536 &lt;prepared-statement-cache-size&gt;32&lt;/prepared-statement-cache-size&gt;
Chris@0 537 </pre>
Chris@0 538 <p>After</p>
Chris@0 539 EXPECTED
Chris@441 540
Chris@0 541 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 542 end
Chris@441 543
Chris@0 544 def test_pre_content_should_not_parse_wiki_and_redmine_links
Chris@0 545 raw = <<-RAW
Chris@0 546 [[CookBook documentation]]
Chris@0 547
Chris@0 548 #1
Chris@0 549
Chris@0 550 <pre>
Chris@0 551 [[CookBook documentation]]
Chris@0 552
Chris@0 553 #1
Chris@0 554 </pre>
Chris@0 555 RAW
Chris@0 556
Chris@0 557 expected = <<-EXPECTED
Chris@0 558 <p><a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a></p>
Chris@0 559 <p><a href="/issues/1" class="issue status-1 priority-1" title="Can't print recipes (New)">#1</a></p>
Chris@0 560 <pre>
Chris@0 561 [[CookBook documentation]]
Chris@0 562
Chris@0 563 #1
Chris@0 564 </pre>
Chris@0 565 EXPECTED
Chris@441 566
Chris@0 567 @project = Project.find(1)
Chris@0 568 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 569 end
Chris@441 570
Chris@0 571 def test_non_closing_pre_blocks_should_be_closed
Chris@0 572 raw = <<-RAW
Chris@0 573 <pre><code>
Chris@0 574 RAW
Chris@0 575
Chris@0 576 expected = <<-EXPECTED
Chris@0 577 <pre><code>
Chris@0 578 </code></pre>
Chris@0 579 EXPECTED
Chris@441 580
Chris@0 581 @project = Project.find(1)
Chris@0 582 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 583 end
Chris@441 584
Chris@0 585 def test_syntax_highlight
Chris@0 586 raw = <<-RAW
Chris@0 587 <pre><code class="ruby">
Chris@0 588 # Some ruby code here
Chris@0 589 </code></pre>
Chris@0 590 RAW
Chris@0 591
Chris@0 592 expected = <<-EXPECTED
Chris@909 593 <pre><code class="ruby syntaxhl"><span class=\"CodeRay\"><span class="line-numbers">1</span><span class="comment"># Some ruby code here</span></span>
Chris@0 594 </code></pre>
Chris@0 595 EXPECTED
Chris@0 596
Chris@0 597 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 598 end
Chris@441 599
Chris@0 600 def test_wiki_links_in_tables
Chris@0 601 to_test = {"|[[Page|Link title]]|[[Other Page|Other title]]|\n|Cell 21|[[Last page]]|" =>
Chris@0 602 '<tr><td><a href="/projects/ecookbook/wiki/Page" class="wiki-page new">Link title</a></td>' +
Chris@0 603 '<td><a href="/projects/ecookbook/wiki/Other_Page" class="wiki-page new">Other title</a></td>' +
Chris@0 604 '</tr><tr><td>Cell 21</td><td><a href="/projects/ecookbook/wiki/Last_page" class="wiki-page new">Last page</a></td></tr>'
Chris@0 605 }
Chris@0 606 @project = Project.find(1)
Chris@0 607 to_test.each { |text, result| assert_equal "<table>#{result}</table>", textilizable(text).gsub(/[\t\n]/, '') }
Chris@0 608 end
Chris@441 609
Chris@0 610 def test_text_formatting
Chris@0 611 to_test = {'*_+bold, italic and underline+_*' => '<strong><em><ins>bold, italic and underline</ins></em></strong>',
Chris@0 612 '(_text within parentheses_)' => '(<em>text within parentheses</em>)',
Chris@0 613 'a *Humane Web* Text Generator' => 'a <strong>Humane Web</strong> Text Generator',
Chris@0 614 '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 615 '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 616 }
Chris@0 617 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text) }
Chris@0 618 end
Chris@441 619
Chris@0 620 def test_wiki_horizontal_rule
Chris@0 621 assert_equal '<hr />', textilizable('---')
Chris@0 622 assert_equal '<p>Dashes: ---</p>', textilizable('Dashes: ---')
Chris@0 623 end
Chris@441 624
Chris@0 625 def test_footnotes
Chris@0 626 raw = <<-RAW
Chris@0 627 This is some text[1].
Chris@0 628
Chris@0 629 fn1. This is the foot note
Chris@0 630 RAW
Chris@0 631
Chris@0 632 expected = <<-EXPECTED
Chris@0 633 <p>This is some text<sup><a href=\"#fn1\">1</a></sup>.</p>
Chris@0 634 <p id="fn1" class="footnote"><sup>1</sup> This is the foot note</p>
Chris@0 635 EXPECTED
Chris@0 636
Chris@0 637 assert_equal expected.gsub(%r{[\r\n\t]}, ''), textilizable(raw).gsub(%r{[\r\n\t]}, '')
Chris@0 638 end
Chris@441 639
Chris@441 640 def test_headings
Chris@441 641 raw = 'h1. Some heading'
Chris@441 642 expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
Chris@441 643
Chris@441 644 assert_equal expected, textilizable(raw)
Chris@441 645 end
Chris@441 646
Chris@909 647 def test_headings_with_special_chars
Chris@909 648 # This test makes sure that the generated anchor names match the expected
Chris@909 649 # ones even if the heading text contains unconventional characters
Chris@909 650 raw = 'h1. Some heading related to version 0.5'
Chris@909 651 anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5")
Chris@909 652 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 653
Chris@909 654 assert_equal expected, textilizable(raw)
Chris@909 655 end
Chris@909 656
Chris@909 657 def test_wiki_links_within_wiki_page_context
Chris@909 658
Chris@909 659 page = WikiPage.find_by_title('Another_page' )
Chris@909 660
Chris@909 661 to_test = {
Chris@909 662 # link to another page
Chris@909 663 '[[CookBook documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">CookBook documentation</a>',
Chris@909 664 '[[CookBook documentation|documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation" class="wiki-page">documentation</a>',
Chris@909 665 '[[CookBook documentation#One-section]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">CookBook documentation</a>',
Chris@909 666 '[[CookBook documentation#One-section|documentation]]' => '<a href="/projects/ecookbook/wiki/CookBook_documentation#One-section" class="wiki-page">documentation</a>',
Chris@909 667 # link to the current page
Chris@909 668 '[[Another page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Another page</a>',
Chris@909 669 '[[Another page|Page]]' => '<a href="/projects/ecookbook/wiki/Another_page" class="wiki-page">Page</a>',
Chris@909 670 '[[Another page#anchor]]' => '<a href="#anchor" class="wiki-page">Another page</a>',
Chris@909 671 '[[Another page#anchor|Page]]' => '<a href="#anchor" class="wiki-page">Page</a>',
Chris@909 672 # page that doesn't exist
Chris@909 673 '[[Unknown page]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">Unknown page</a>',
Chris@909 674 '[[Unknown page|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page" class="wiki-page new">404</a>',
Chris@909 675 '[[Unknown page#anchor]]' => '<a href="/projects/ecookbook/wiki/Unknown_page#anchor" class="wiki-page new">Unknown page</a>',
Chris@909 676 '[[Unknown page#anchor|404]]' => '<a href="/projects/ecookbook/wiki/Unknown_page#anchor" class="wiki-page new">404</a>',
Chris@909 677 }
Chris@909 678
Chris@909 679 @project = Project.find(1)
Chris@909 680
Chris@909 681 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(WikiContent.generate!( :text => text, :page => page ), :text) }
Chris@909 682 end
Chris@909 683
Chris@909 684 def test_wiki_links_anchor_option_should_prepend_page_title_to_href
Chris@909 685
Chris@909 686 to_test = {
Chris@909 687 # link to a page
Chris@909 688 '[[CookBook documentation]]' => '<a href="#CookBook_documentation" class="wiki-page">CookBook documentation</a>',
Chris@909 689 '[[CookBook documentation|documentation]]' => '<a href="#CookBook_documentation" class="wiki-page">documentation</a>',
Chris@909 690 '[[CookBook documentation#One-section]]' => '<a href="#CookBook_documentation_One-section" class="wiki-page">CookBook documentation</a>',
Chris@909 691 '[[CookBook documentation#One-section|documentation]]' => '<a href="#CookBook_documentation_One-section" class="wiki-page">documentation</a>',
Chris@909 692 # page that doesn't exist
Chris@909 693 '[[Unknown page]]' => '<a href="#Unknown_page" class="wiki-page new">Unknown page</a>',
Chris@909 694 '[[Unknown page|404]]' => '<a href="#Unknown_page" class="wiki-page new">404</a>',
Chris@909 695 '[[Unknown page#anchor]]' => '<a href="#Unknown_page_anchor" class="wiki-page new">Unknown page</a>',
Chris@909 696 '[[Unknown page#anchor|404]]' => '<a href="#Unknown_page_anchor" class="wiki-page new">404</a>',
Chris@909 697 }
Chris@909 698
Chris@909 699 @project = Project.find(1)
Chris@909 700
Chris@909 701 to_test.each { |text, result| assert_equal "<p>#{result}</p>", textilizable(text, :wiki_links => :anchor) }
Chris@909 702 end
Chris@909 703
Chris@909 704 def test_headings_in_wiki_single_page_export_should_be_prepended_with_page_title
Chris@909 705 page = WikiPage.generate!( :title => 'Page Title' )
Chris@909 706 content = WikiContent.generate!( :text => 'h1. Some heading', :page => page )
Chris@909 707
Chris@909 708 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 709
Chris@909 710 assert_equal expected, textilizable(content, :text, :wiki_links => :anchor )
Chris@909 711 end
Chris@909 712
Chris@0 713 def test_table_of_content
Chris@0 714 raw = <<-RAW
Chris@0 715 {{toc}}
Chris@0 716
Chris@0 717 h1. Title
Chris@0 718
Chris@0 719 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
Chris@0 720
Chris@0 721 h2. Subtitle with a [[Wiki]] link
Chris@0 722
Chris@0 723 Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
Chris@0 724
Chris@0 725 h2. Subtitle with [[Wiki|another Wiki]] link
Chris@0 726
Chris@0 727 h2. Subtitle with %{color:red}red text%
Chris@119 728
Chris@119 729 <pre>
Chris@119 730 some code
Chris@119 731 </pre>
Chris@119 732
chris@37 733 h3. Subtitle with *some* _modifiers_
Chris@0 734
Chris@929 735 h3. Subtitle with @inline code@
Chris@929 736
Chris@0 737 h1. Another title
Chris@0 738
chris@37 739 h3. An "Internet link":http://www.redmine.org/ inside subtitle
Chris@0 740
Chris@0 741 h2. "Project Name !/attachments/1234/logo_small.gif! !/attachments/5678/logo_2.png!":/projects/projectname/issues
Chris@0 742
Chris@0 743 RAW
Chris@0 744
chris@37 745 expected = '<ul class="toc">' +
chris@37 746 '<li><a href="#Title">Title</a>' +
chris@37 747 '<ul>' +
Chris@441 748 '<li><a href="#Subtitle-with-a-Wiki-link">Subtitle with a Wiki link</a></li>' +
Chris@441 749 '<li><a href="#Subtitle-with-another-Wiki-link">Subtitle with another Wiki link</a></li>' +
chris@37 750 '<li><a href="#Subtitle-with-red-text">Subtitle with red text</a>' +
chris@37 751 '<ul>' +
chris@37 752 '<li><a href="#Subtitle-with-some-modifiers">Subtitle with some modifiers</a></li>' +
Chris@929 753 '<li><a href="#Subtitle-with-inline-code">Subtitle with inline code</a></li>' +
chris@37 754 '</ul>' +
chris@37 755 '</li>' +
chris@37 756 '</ul>' +
chris@37 757 '</li>' +
chris@37 758 '<li><a href="#Another-title">Another title</a>' +
chris@37 759 '<ul>' +
chris@37 760 '<li>' +
chris@37 761 '<ul>' +
chris@37 762 '<li><a href="#An-Internet-link-inside-subtitle">An Internet link inside subtitle</a></li>' +
chris@37 763 '</ul>' +
chris@37 764 '</li>' +
chris@37 765 '<li><a href="#Project-Name">Project Name</a></li>' +
chris@37 766 '</ul>' +
chris@37 767 '</li>' +
Chris@0 768 '</ul>'
Chris@0 769
chris@37 770 @project = Project.find(1)
Chris@909 771 assert textilizable(raw).gsub("\n", "").include?(expected)
chris@37 772 end
Chris@441 773
Chris@929 774 def test_table_of_content_should_generate_unique_anchors
Chris@929 775 raw = <<-RAW
Chris@929 776 {{toc}}
Chris@929 777
Chris@929 778 h1. Title
Chris@929 779
Chris@929 780 h2. Subtitle
Chris@929 781
Chris@929 782 h2. Subtitle
Chris@929 783 RAW
Chris@929 784
Chris@929 785 expected = '<ul class="toc">' +
Chris@929 786 '<li><a href="#Title">Title</a>' +
Chris@929 787 '<ul>' +
Chris@929 788 '<li><a href="#Subtitle">Subtitle</a></li>' +
Chris@929 789 '<li><a href="#Subtitle-2">Subtitle</a></li>'
Chris@929 790 '</ul>'
Chris@929 791 '</li>' +
Chris@929 792 '</ul>'
Chris@929 793
Chris@929 794 @project = Project.find(1)
Chris@929 795 result = textilizable(raw).gsub("\n", "")
Chris@929 796 assert_include expected, result
Chris@929 797 assert_include '<a name="Subtitle">', result
Chris@929 798 assert_include '<a name="Subtitle-2">', result
Chris@929 799 end
Chris@929 800
chris@37 801 def test_table_of_content_should_contain_included_page_headings
chris@37 802 raw = <<-RAW
chris@37 803 {{toc}}
chris@37 804
chris@37 805 h1. Included
chris@37 806
chris@37 807 {{include(Child_1)}}
chris@37 808 RAW
chris@37 809
chris@37 810 expected = '<ul class="toc">' +
chris@37 811 '<li><a href="#Included">Included</a></li>' +
Chris@441 812 '<li><a href="#Child-page-1">Child page 1</a></li>' +
chris@37 813 '</ul>'
chris@37 814
chris@37 815 @project = Project.find(1)
Chris@0 816 assert textilizable(raw).gsub("\n", "").include?(expected)
Chris@0 817 end
Chris@0 818
Chris@929 819 def test_section_edit_links
Chris@929 820 raw = <<-RAW
Chris@929 821 h1. Title
Chris@929 822
Chris@929 823 Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
Chris@929 824
Chris@929 825 h2. Subtitle with a [[Wiki]] link
Chris@929 826
Chris@929 827 h2. Subtitle with *some* _modifiers_
Chris@929 828
Chris@929 829 h2. Subtitle with @inline code@
Chris@929 830
Chris@929 831 <pre>
Chris@929 832 some code
Chris@929 833
Chris@929 834 h2. heading inside pre
Chris@929 835
Chris@929 836 <h2>html heading inside pre</h2>
Chris@929 837 </pre>
Chris@929 838
Chris@929 839 h2. Subtitle after pre tag
Chris@929 840 RAW
Chris@929 841
Chris@929 842 @project = Project.find(1)
Chris@929 843 set_language_if_valid 'en'
Chris@929 844 result = textilizable(raw, :edit_section_links => {:controller => 'wiki', :action => 'edit', :project_id => '1', :id => 'Test'}).gsub("\n", "")
Chris@929 845
Chris@929 846 # heading that contains inline code
Chris@929 847 assert_match Regexp.new('<div class="contextual" title="Edit this section">' +
Chris@929 848 '<a href="/projects/1/wiki/Test/edit\?section=4"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
Chris@929 849 '<a name="Subtitle-with-inline-code"></a>' +
Chris@929 850 '<h2 >Subtitle with <code>inline code</code><a href="#Subtitle-with-inline-code" class="wiki-anchor">&para;</a></h2>'),
Chris@929 851 result
Chris@929 852
Chris@929 853 # last heading
Chris@929 854 assert_match Regexp.new('<div class="contextual" title="Edit this section">' +
Chris@929 855 '<a href="/projects/1/wiki/Test/edit\?section=5"><img alt="Edit" src="/images/edit.png(\?\d+)?" /></a></div>' +
Chris@929 856 '<a name="Subtitle-after-pre-tag"></a>' +
Chris@929 857 '<h2 >Subtitle after pre tag<a href="#Subtitle-after-pre-tag" class="wiki-anchor">&para;</a></h2>'),
Chris@929 858 result
Chris@929 859 end
Chris@929 860
Chris@0 861 def test_default_formatter
Chris@0 862 Setting.text_formatting = 'unknown'
Chris@0 863 text = 'a *link*: http://www.example.net/'
Chris@0 864 assert_equal '<p>a *link*: <a href="http://www.example.net/">http://www.example.net/</a></p>', textilizable(text)
Chris@0 865 Setting.text_formatting = 'textile'
Chris@0 866 end
Chris@441 867
Chris@0 868 def test_due_date_distance_in_words
Chris@0 869 to_test = { Date.today => 'Due in 0 days',
Chris@0 870 Date.today + 1 => 'Due in 1 day',
Chris@0 871 Date.today + 100 => 'Due in about 3 months',
Chris@0 872 Date.today + 20000 => 'Due in over 54 years',
Chris@0 873 Date.today - 1 => '1 day late',
Chris@0 874 Date.today - 100 => 'about 3 months late',
Chris@0 875 Date.today - 20000 => 'over 54 years late',
Chris@0 876 }
Chris@119 877 ::I18n.locale = :en
Chris@0 878 to_test.each do |date, expected|
Chris@0 879 assert_equal expected, due_date_distance_in_words(date)
Chris@0 880 end
Chris@0 881 end
Chris@441 882
Chris@0 883 def test_avatar
Chris@0 884 # turn on avatars
Chris@0 885 Setting.gravatar_enabled = '1'
Chris@0 886 assert avatar(User.find_by_mail('jsmith@somenet.foo')).include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
Chris@0 887 assert avatar('jsmith <jsmith@somenet.foo>').include?(Digest::MD5.hexdigest('jsmith@somenet.foo'))
Chris@0 888 assert_nil avatar('jsmith')
Chris@0 889 assert_nil avatar(nil)
Chris@441 890
Chris@0 891 # turn off avatars
Chris@0 892 Setting.gravatar_enabled = '0'
chris@22 893 assert_equal '', avatar(User.find_by_mail('jsmith@somenet.foo'))
Chris@0 894 end
Chris@441 895
Chris@0 896 def test_link_to_user
Chris@0 897 user = User.find(2)
Chris@0 898 t = link_to_user(user)
Chris@0 899 assert_equal "<a href=\"/users/2\">#{ user.name }</a>", t
Chris@0 900 end
Chris@441 901
Chris@0 902 def test_link_to_user_should_not_link_to_locked_user
Chris@0 903 user = User.find(5)
Chris@0 904 assert user.locked?
Chris@0 905 t = link_to_user(user)
Chris@0 906 assert_equal user.name, t
Chris@0 907 end
Chris@441 908
Chris@0 909 def test_link_to_user_should_not_link_to_anonymous
Chris@0 910 user = User.anonymous
Chris@0 911 assert user.anonymous?
Chris@0 912 t = link_to_user(user)
Chris@0 913 assert_equal ::I18n.t(:label_user_anonymous), t
Chris@0 914 end
Chris@14 915
Chris@14 916 def test_link_to_project
Chris@14 917 project = Project.find(1)
Chris@14 918 assert_equal %(<a href="/projects/ecookbook">eCookbook</a>),
Chris@14 919 link_to_project(project)
Chris@14 920 assert_equal %(<a href="/projects/ecookbook/settings">eCookbook</a>),
Chris@14 921 link_to_project(project, :action => 'settings')
Chris@14 922 assert_equal %(<a href="http://test.host/projects/ecookbook?jump=blah">eCookbook</a>),
Chris@14 923 link_to_project(project, {:only_path => false, :jump => 'blah'})
Chris@14 924 assert_equal %(<a href="/projects/ecookbook/settings" class="project">eCookbook</a>),
Chris@14 925 link_to_project(project, {:action => 'settings'}, :class => "project")
Chris@14 926 end
Chris@909 927
Chris@929 928 def test_link_to_legacy_project_with_numerical_identifier_should_use_id
Chris@929 929 # numeric identifier are no longer allowed
Chris@929 930 Project.update_all "identifier=25", "id=1"
Chris@929 931
Chris@929 932 assert_equal '<a href="/projects/1">eCookbook</a>',
Chris@929 933 link_to_project(Project.find(1))
Chris@929 934 end
Chris@929 935
Chris@909 936 def test_principals_options_for_select_with_users
Chris@909 937 users = [User.find(2), User.find(4)]
Chris@909 938 assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>),
Chris@909 939 principals_options_for_select(users)
Chris@909 940 end
Chris@909 941
Chris@909 942 def test_principals_options_for_select_with_selected
Chris@909 943 users = [User.find(2), User.find(4)]
Chris@909 944 assert_equal %(<option value="2">John Smith</option><option value="4" selected="selected">Robert Hill</option>),
Chris@909 945 principals_options_for_select(users, User.find(4))
Chris@909 946 end
Chris@909 947
Chris@909 948 def test_principals_options_for_select_with_users_and_groups
Chris@909 949 users = [User.find(2), Group.find(11), User.find(4), Group.find(10)]
Chris@909 950 assert_equal %(<option value="2">John Smith</option><option value="4">Robert Hill</option>) +
Chris@909 951 %(<optgroup label="Groups"><option value="10">A Team</option><option value="11">B Team</option></optgroup>),
Chris@909 952 principals_options_for_select(users)
Chris@909 953 end
Chris@909 954
Chris@909 955 def test_principals_options_for_select_with_empty_collection
Chris@909 956 assert_equal '', principals_options_for_select([])
Chris@909 957 end
Chris@0 958 end