Fix autolinks in rinku and add specs

This commit is contained in:
Oliver Günther
2018-02-22 09:23:01 +01:00
parent dafb2e875f
commit da009d8d7e
8 changed files with 1098 additions and 16 deletions
+3 -2
View File
@@ -47,8 +47,6 @@ gem 'request_store', '~> 1.3.1'
gem 'warden', '~> 1.2'
gem 'warden-basic_auth', '~> 0.2.1'
# TODO: adds #auto_link which was deprecated in rails 3.1
gem 'rails_autolink', '~> 1.1.6'
gem 'will_paginate', '~> 3.1.0'
gem 'friendly_id', '~> 5.2.1'
@@ -78,6 +76,9 @@ gem 'commonmarker', '~> 0.17.5'
gem 'html-pipeline', '~> 2.7.1'
# HTML sanitization used for html-pipeline
gem 'sanitize', '~> 4.5.0'
# HTML autolinking for mails and urls (replaces autolink)
gem 'rinku', '~> 2.0.4'
# generates SVG Graphs
# used for statistics on svn repositories
+2 -3
View File
@@ -454,8 +454,6 @@ GEM
rails_12factor (0.0.3)
rails_serve_static_assets
rails_stdout_logging
rails_autolink (1.1.6)
rails (> 3.1)
rails_serve_static_assets (0.0.5)
rails_stdout_logging (0.0.5)
railties (5.0.6)
@@ -492,6 +490,7 @@ GEM
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
retriable (3.0.2)
rinku (2.0.4)
roar (1.1.0)
representable (~> 3.0.0)
rspec (3.5.0)
@@ -705,13 +704,13 @@ DEPENDENCIES
rails-angular-xss!
rails-controller-testing (~> 1.0.2)
rails_12factor
rails_autolink (~> 1.1.6)
rdoc (>= 2.4.2)
reform (~> 2.2.0)
reform-rails (~> 0.1.7)
request_store (~> 1.3.1)
responders (~> 2.4)
retriable (~> 3.0)
rinku (~> 2.0.4)
roar (~> 1.1.0)
rspec (~> 3.5.0)
rspec-activemodel-mocks (~> 1.0.3)!
@@ -0,0 +1,59 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'rinku'
module OpenProject::TextFormatting
module Filters
# HTML Filter for auto_linking urls in HTML.
#
# Context options:
#
# autolink:
# classes: (string) Classes to add to auto linked urls and mails
# enabled: (boolean)
#
# This filter does not write additional information to the context.
class AutolinkFilter < HTML::Pipeline::Filter
def call
autolink_context = default_autolink_options.merge context.fetch(:autolink, {})
return doc if autolink_context[:enabled] == false
::Rinku.auto_link(html, :all, "class=\"#{autolink_context[:classes]}\"")
end
def default_autolink_options
{
enabled: true,
classes: 'rinku-autolink',
}
end
end
end
end
@@ -37,7 +37,7 @@ module OpenProject::TextFormatting
options = [:GITHUB_PRE_LANG]
options << :HARDBREAKS if context[:gfm] != false
extensions = context.fetch :commonmarker_extensions,
%i[table strikethrough tagfilter autolink]
%i[table strikethrough tagfilter]
html = CommonMarker.render_html(text, options, extensions)
html.rstrip!
@@ -35,11 +35,14 @@ module OpenProject::TextFormatting::Formatters
def initialize(context)
@context = context
@pipeline = HTML::Pipeline.new(located_filters, context)
@pipeline = ::HTML::Pipeline.new(located_filters, context)
end
def to_html(text)
pipeline.to_html(text, context).html_safe
result = pipeline.call(text, context)
output = result[:output].to_s
output.html_safe
end
def to_document(text)
@@ -50,7 +53,8 @@ module OpenProject::TextFormatting::Formatters
[
:markdown,
:sanitization,
:pattern_matcher
:pattern_matcher,
:autolink
]
end
@@ -70,7 +70,7 @@ module OpenProject::TextFormatting::Matchers
end
def render_version
if project && version = project.versions.visible.find_by(name: name)
if project && version = project.versions.visible.find_by(name: oid)
link_to h(version.name),
{ only_path: context[:only_path], controller: '/versions', action: 'show', id: version },
class: 'version'
@@ -78,7 +78,7 @@ module OpenProject::TextFormatting::Matchers
end
def render_commit
if project && project.repository && (changeset = Changeset.visible.where(['repository_id = ? AND scmid LIKE ?', project.repository.id, "#{name}%"]).first)
if project && project.repository && (changeset = Changeset.visible.where(['repository_id = ? AND scmid LIKE ?', project.repository.id, "#{oid}%"]).first)
link_to h("#{project_prefix}#{name}"),
{ only_path: context[:only_path], controller: '/repositories', action: 'revision', project_id: project, rev: changeset.identifier },
class: 'changeset',
@@ -88,11 +88,11 @@ module OpenProject::TextFormatting::Matchers
def render_source
if project && project.repository && User.current.allowed_to?(:browse_repository, project)
name =~ %r{\A[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?\z}
oid =~ %r{\A[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?\z}
path = $1
rev = $3
anchor = $5
link_to h("#{project_prefix}#{prefix}:#{name}"),
link_to h("#{project_prefix}#{prefix}:#{oid}"),
{ controller: '/repositories',
action: 'entry',
project_id: project,
@@ -108,7 +108,7 @@ module OpenProject::TextFormatting::Matchers
def render_attachment
attachments = context[:attachments] || context[:object].try(:attachments)
if attachments && attachment = attachments.detect { |a| a.filename == name }
if attachments && attachment = attachments.detect { |a| a.filename == oid }
link_to h(attachment.filename),
{ only_path: context[:only_path], controller: '/attachments', action: 'download', id: attachment },
class: 'attachment'
@@ -118,7 +118,7 @@ module OpenProject::TextFormatting::Matchers
def render_project
p = Project
.visible
.where(['projects.identifier = :s OR LOWER(projects.name) = :s', { s: name.downcase }])
.where(['projects.identifier = :s OR LOWER(projects.name) = :s', { s: oid.downcase }])
.first
if p
link_to_project(p, { only_path: context[:only_path] }, class: 'project')
@@ -126,7 +126,7 @@ module OpenProject::TextFormatting::Matchers
end
def render_user
if user = User.in_visible_project.find_by(login: name)
if user = User.in_visible_project.find_by(login: oid)
link_to_user(user, class: 'user-mention')
end
end
@@ -0,0 +1,264 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See docs/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
describe OpenProject::TextFormatting::Formatters::Markdown::Formatter do
MODIFIERS = {
'**' => 'strong', # bold
'_' => 'em', # italic
'~' => 'del' # deleted
}
it 'should modifiers' do
assert_html_output(
'**bold**' => '<strong>bold</strong>',
'before **bold**' => 'before <strong>bold</strong>',
'**bold** after' => '<strong>bold</strong> after',
'**two words**' => '<strong>two words</strong>',
'**two*words**' => '<strong>two*words</strong>',
'**two * words**' => '<strong>two * words</strong>',
'**two** **words**' => '<strong>two</strong> <strong>words</strong>',
'**(two)** **(words)**' => '<strong>(two)</strong> <strong>(words)</strong>'
)
end
it 'should modifiers combination' do
MODIFIERS.each do |m1, tag1|
MODIFIERS.each do |m2, tag2|
next if m1 == m2
text = "#{m2}#{m1}Phrase modifiers#{m1}#{m2}"
html = "<#{tag2}><#{tag1}>Phrase modifiers</#{tag1}></#{tag2}>"
assert_html_output text => html
end
end
end
it 'should inline code' do
assert_html_output(
'this is `some code`' => 'this is <code>some code</code>',
'`<Location /redmine>`' => '<code>&lt;Location /redmine&gt;</code>'
)
end
it 'should escaping' do
assert_html_output(
'this is a <script>' => 'this is a &lt;script&gt;'
)
end
it 'should use of backslashes followed by numbers in headers' do
assert_html_output({
'# 2009\02\09' => '<h1>2009\02\09</h1>'
}, false)
end
it 'should double dashes should not strikethrough' do
assert_html_output(
'double -- dashes -- test' => 'double -- dashes -- test',
'double -- **dashes** -- test' => 'double -- <strong>dashes</strong> -- test'
)
end
it 'should inline auto link' do
assert_html_output(
'Autolink to http://www.google.com' => 'Autolink to <a class="rinku-autolink" href="http://www.google.com">http://www.google.com</a>'
)
end
it 'should inline auto link email addresses' do
assert_html_output(
'Mailto link to foo@bar.com' => 'Mailto link to <a class="rinku-autolink" href="mailto:foo@bar.com">foo@bar.com</a>'
)
end
describe 'mail address autolink' do
it 'prints autolinks for user references not existing' do
assert_html_output(
'Link to user:"foo@bar.com"' => 'Link to user:"<a href="mailto:foo@bar.com" class="rinku-autolink">foo@bar.com</a>"'
)
end
context 'when visible user exists' do
let(:project) { FactoryGirl.create :project }
let(:role) { FactoryGirl.create(:role, permissions: %i(view_work_packages)) }
let(:current_user) do
FactoryGirl.create(:user,
member_in_project: project,
member_through_role: role)
end
let(:user) do
FactoryGirl.create(:user,
login: 'foo@bar.com',
firstname: 'Foo',
lastname: 'Barrit',
member_in_project: project,
member_through_role: role)
end
before do
user
login_as current_user
end
it 'outputs the reference' do
assert_html_output(
'Link to user:"foo@bar.com"' => %(Link to <a class="user-mention" href="/users/#{user.id}">Foo Barrit</a>)
)
end
end
end
it 'should blockquote' do
# orig raw text
raw = <<-RAW
John said:
> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.
> Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
> * Donec odio lorem,
> * sagittis ac,
> * malesuada in,
> * adipiscing eu, dolor.
>
> >Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.
> Proin a tellus. Nam vel neque.
He's right.
RAW
# expected html
expected = <<-EXPECTED
<p>John said:</p>
<blockquote class="icon icon-quote2">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed libero.<br />
Nullam commodo metus accumsan nulla. Curabitur lobortis dui id dolor.
<ul>
<li>Donec odio lorem,</li>
<li>sagittis ac,</li>
<li>malesuada in,</li>
<li>adipiscing eu, dolor.</li>
</ul>
<blockquote class="icon icon-quote2">
<p>Nulla varius pulvinar diam. Proin id arcu id lorem scelerisque condimentum. Proin vehicula turpis vitae lacus.</p>
</blockquote>
<p>Proin a tellus. Nam vel neque.</p>
</blockquote>
<p>He's right.</p>
EXPECTED
expect(expected.gsub(%r{\s+}, '')).to eq(to_html(raw).gsub(%r{\s+}, ''))
end
it 'should table' do
raw = <<-RAW
This is a table with empty cells:
|cell11|cell12||
|cell21||cell23|
|cell31|cell32|cell33|
RAW
expected = <<-EXPECTED
<p>This is a table with empty cells:</p>
<table>
<tr><td>cell11</td><td>cell12</td><td></td></tr>
<tr><td>cell21</td><td></td><td>cell23</td></tr>
<tr><td>cell31</td><td>cell32</td><td>cell33</td></tr>
</table>
EXPECTED
expect(expected.gsub(%r{\s+}, '')).to eq(to_html(raw).gsub(%r{\s+}, ''))
end
it 'should table with line breaks' do
raw = <<-RAW
This is a table with line breaks:
|cell11
continued|cell12||
|~cell21~||cell23
cell23 line2
cell23 **line3**|
|cell31|cell32
cell32 line2|cell33|
RAW
expected = <<-EXPECTED
<p>This is a table with line breaks:</p>
<table>
<tr>
<td>cell11<br />continued</td>
<td>cell12</td>
<td></td>
</tr>
<tr>
<td><del>cell21</del></td>
<td></td>
<td>cell23<br/>cell23 line2<br/>cell23 <strong>line3</strong></td>
</tr>
<tr>
<td>cell31</td>
<td>cell32<br/>cell32 line2</td>
<td>cell33</td>
</tr>
</table>
EXPECTED
expect(expected.gsub(%r{\s+}, '')).to eq(to_html(raw).gsub(%r{\s+}, ''))
end
it 'should not mangle brackets' do
expect(to_html('[msg1][msg2]')).to eq '<p>[msg1][msg2]</p>'
end
it 'should textile should escape image urls' do
# this is onclick="alert('XSS');" in encoded form
raw = '![](/images/comment.png"onclick=&#x61;&#x6c;&#x65;&#x72;&#x74;&#x28;&#x27;&#x58;&#x53;&#x53;&#x27;&#x29;;&#x22;)'
expected = %[<p><imgsrc="/images/comment.png%22onclick=alert('XSS');%22" alt=""></p>]
expect(expected.gsub(%r{\s+}, '')).to eq(to_html(raw).gsub(%r{\s+}, ''))
end
private
def assert_html_output(to_test, expect_paragraph = true)
to_test.each do |text, expected|
expected = expect_paragraph ? "<p>#{expected}</p>" : expected
expect(to_html(text)).to be_html_eql expected
end
end
def to_html(text)
described_class.new({}).to_html(text)
end
end
@@ -0,0 +1,755 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See docs/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
describe OpenProject::TextFormatting do
include OpenProject::TextFormatting
include ERB::Util
include WorkPackagesHelper # soft-dependency
include ActionView::Helpers::UrlHelper # soft-dependency
include ActionView::Context
include OpenProject::StaticRouting::UrlHelpers
def controller
# no-op
end
describe '.format_text' do
let(:project) { FactoryGirl.create :valid_project }
let(:identifier) { project.identifier }
let(:role) do
FactoryGirl.create :role,
permissions: %i(view_work_packages edit_work_packages
browse_repository view_changesets view_wiki_pages)
end
let(:project_member) do
FactoryGirl.create :user,
member_in_project: project,
member_through_role: role
end
let(:issue) do
FactoryGirl.create :work_package,
project: project,
author: project_member,
type: project.types.first
end
let!(:non_member) do
FactoryGirl.create(:non_member)
end
before do
@project = project
allow(User).to receive(:current).and_return(project_member)
allow(Setting).to receive(:text_formatting).and_return('textile')
end
context 'Changeset links' do
let(:repository) do
FactoryGirl.build_stubbed :repository_subversion,
project: project
end
let(:changeset1) do
FactoryGirl.build_stubbed :changeset,
repository: repository,
comments: 'My very first commit'
end
let(:changeset2) do
FactoryGirl.build_stubbed :changeset,
repository: repository,
comments: 'This commit fixes #1, #2 and references #1 & #3'
end
let(:changeset_link) do
link_to("r#{changeset1.revision}",
{ controller: 'repositories', action: 'revision', project_id: identifier, rev: changeset1.revision },
class: 'changeset', title: 'My very first commit')
end
let(:changeset_link2) do
link_to("r#{changeset2.revision}",
{ controller: 'repositories', action: 'revision', project_id: identifier, rev: changeset2.revision },
class: 'changeset', title: 'This commit fixes #1, #2 and references #1 & #3')
end
before do
allow(project).to receive(:repository).and_return(repository)
changesets = [changeset1, changeset2]
allow(Changeset).to receive(:visible).and_return(changesets)
changesets.each do |changeset|
allow(changesets)
.to receive(:find_by)
.with(repository_id: project.repository.id, revision: changeset.revision)
.and_return(changeset)
end
end
context 'Single link' do
subject { format_text("r#{changeset1.revision}") }
it { is_expected.to be_html_eql("<p>#{changeset_link}</p>") }
end
context 'Single link with dot' do
subject { format_text("r#{changeset1.revision}.") }
it { is_expected.to be_html_eql("<p>#{changeset_link}.</p>") }
end
context 'Two links comma separated' do
subject { format_text("r#{changeset1.revision}, r#{changeset2.revision}") }
it { is_expected.to be_html_eql("<p>#{changeset_link}, #{changeset_link2}</p>") }
end
context 'Single link comma separated without a space' do
subject { format_text("r#{changeset1.revision},r#{changeset2.revision}") }
it { is_expected.to be_html_eql("<p>#{changeset_link},#{changeset_link2}</p>") }
end
context 'Escaping' do
subject { format_text("!r#{changeset1.id}") }
it { is_expected.to be_html_eql("<p>r#{changeset1.id}</p>") }
end
end
context 'Version link' do
let!(:version) {
FactoryGirl.create :version,
name: '1.0',
project: project
}
let(:version_link) {
link_to('1.0',
{ controller: 'versions', action: 'show', id: version.id },
class: 'version')
}
context 'Link with version id' do
subject { format_text("version##{version.id}") }
it { is_expected.to be_html_eql("<p>#{version_link}</p>") }
end
context 'Link with version' do
subject { format_text('version:1.0') }
it { is_expected.to be_html_eql("<p>#{version_link}</p>") }
end
context 'Link with quoted version' do
subject { format_text('version:"1.0"') }
it { is_expected.to be_html_eql("<p>#{version_link}</p>") }
end
context 'Escaping link with version id' do
subject { format_text("!version##{version.id}") }
it { is_expected.to be_html_eql("<p>version##{version.id}</p>") }
end
context 'Escaping link with version' do
subject { format_text('!version:1.0') }
it { is_expected.to be_html_eql('<p>version:1.0</p>') }
end
context 'Escaping link with quoted version' do
subject { format_text('!version:"1.0"') }
it { is_expected.to be_html_eql('<p>version:"1.0"</p>') }
end
end
context 'Message links' do
let(:board) { FactoryGirl.create :board, project: project }
let(:message1) { FactoryGirl.create :message, board: board }
let(:message2) {
FactoryGirl.create :message,
board: board,
parent: message1
}
before do
message1.reload
end
context 'Plain message' do
subject { format_text("message##{message1.id}") }
it { is_expected.to be_html_eql("<p>#{link_to(message1.subject, topic_path(message1), class: 'message')}</p>") }
end
context 'Message with parent' do
subject { format_text("message##{message2.id}") }
it { is_expected.to be_html_eql("<p>#{link_to(message2.subject, topic_path(message1, anchor: "message-#{message2.id}", r: message2.id), class: 'message')}</p>") }
end
end
context 'Issue links' do
let(:issue_link) {
link_to("##{issue.id}",
work_package_path(issue),
class: 'issue work_package status-3 priority-1 created-by-me', title: "#{issue.subject} (#{issue.status})")
}
context 'Plain issue link' do
subject { format_text("##{issue.id}, [##{issue.id}], (##{issue.id}) and ##{issue.id}.") }
it { is_expected.to be_html_eql("<p>#{issue_link}, [#{issue_link}], (#{issue_link}) and #{issue_link}.</p>") }
end
context 'Plain issue link to non-existing element' do
subject { format_text('#0123456789') }
it { is_expected.to be_html_eql('<p>#0123456789</p>') }
end
context 'Escaping issue link' do
subject { format_text("!##{issue.id}.") }
it { is_expected.to be_html_eql("<p>##{issue.id}.</p>") }
end
context 'Cyclic Description Links' do
let(:issue2) {
FactoryGirl.create :work_package,
project: project,
author: project_member,
type: project.types.first
}
before do
issue2.description = "####{issue.id}"
issue2.save!
issue.description = "####{issue2.id}"
issue.save!
end
subject { format_text issue, :description }
it "doesn't replace description links with a cycle" do
expect(subject).to match("###{issue.id}")
end
end
context 'Description links' do
subject { format_text issue, :description }
it 'replaces the macro with the issue description' do
expect(subject).to be_html_eql("<p>#{issue.description}</p>")
end
end
end
context 'Project links' do
let(:subproject) { FactoryGirl.create :valid_project, parent: project, is_public: true }
let(:project_url) { { controller: 'projects', action: 'show', id: subproject.identifier } }
context 'Plain project link' do
subject { format_text("project##{subproject.id}") }
it { is_expected.to be_html_eql("<p>#{link_to(subproject.name, project_url, class: 'project')}</p>") }
end
context 'Plain project link via identifier' do
subject { format_text("project:#{subproject.identifier}") }
it { is_expected.to be_html_eql("<p>#{link_to(subproject.name, project_url, class: 'project')}</p>") }
end
context 'Plain project link via name' do
subject { format_text("project:\"#{subproject.name}\"") }
it { is_expected.to be_html_eql("<p>#{link_to(subproject.name, project_url, class: 'project')}</p>") }
end
end
context 'User links' do
let(:role) do
FactoryGirl.create :role,
permissions: %i[view_work_packages edit_work_packages
browse_repository view_changesets view_wiki_pages]
end
let(:linked_project_member) do
FactoryGirl.create :user,
member_in_project: project,
member_through_role: role
end
context 'User link via ID' do
context 'when linked user visible for reader' do
subject { format_text("user##{linked_project_member.id}") }
it {
is_expected.to be_html_eql("<p>#{link_to(linked_project_member.name, { controller: :users, action: :show, id: linked_project_member.id }, class: 'user-mention')}</p>")
}
end
context 'when linked user not visible for reader' do
let(:role) { FactoryGirl.create(:non_member) }
subject { format_text("user##{linked_project_member.id}") }
it {
is_expected.to be_html_eql("<p>user##{linked_project_member.id}</p>")
}
end
end
context 'User link via login name' do
context 'when linked user visible for reader' do
context 'with a common login name' do
subject { format_text("user:\"#{linked_project_member.login}\"") }
it { is_expected.to be_html_eql("<p>#{link_to(linked_project_member.name, { controller: :users, action: :show, id: linked_project_member.id }, class: 'user-mention')}</p>") }
end
context "with an email address as login name" do
let(:linked_project_member) do
FactoryGirl.create :user,
member_in_project: project,
member_through_role: role,
login: "foo@bar.com"
end
subject { format_text("user:\"#{linked_project_member.login}\"") }
it { is_expected.to be_html_eql("<p>#{link_to(linked_project_member.name, { controller: :users, action: :show, id: linked_project_member.id }, class: 'user-mention')}</p>") }
end
end
context 'when linked user not visible for reader' do
let(:role) { FactoryGirl.create(:non_member) }
subject { format_text("user:\"#{linked_project_member.login}\"") }
it {
is_expected.to be_html_eql("<p>user:\"#{linked_project_member.login}\"</p>")
}
end
end
end
context 'Group reference' do
let(:role) do
FactoryGirl.create :role,
permissions: []
end
let(:linked_project_member_group) do
FactoryGirl.create(:group).tap do |group|
FactoryGirl.create(:member,
principal: group,
project: project,
roles: [role])
end
end
context 'group exists' do
subject { format_text("group##{linked_project_member_group.id}") }
it 'produces the expected html' do
is_expected.to be_html_eql("<p><span class='user-mention'>#{linked_project_member_group.name}</span></p>")
end
end
context 'group does not exist' do
subject { format_text("group#000000") }
it 'leaves the text unchangd' do
is_expected.to be_html_eql("<p>group#000000</p>")
end
end
end
context 'Url links' do
subject { format_text('http://foo.bar/FAQ#3') }
it { is_expected.to be_html_eql('<p><a class="external icon-context icon-copy" href="http://foo.bar/FAQ#3">http://foo.bar/FAQ#3</a></p>') }
end
context 'Wiki links' do
let(:project_2) {
FactoryGirl.create :valid_project,
identifier: 'onlinestore'
}
let(:wiki_1) {
FactoryGirl.create :wiki,
start_page: 'CookBook documentation',
project: project
}
let(:wiki_page_1_1) {
FactoryGirl.create :wiki_page_with_content,
wiki: wiki_1,
title: 'CookBook documentation'
}
let(:wiki_page_1_2) {
FactoryGirl.create :wiki_page_with_content,
wiki: wiki_1,
title: 'Another page'
}
let(:wiki_page_1_3) {
FactoryGirl.create :wiki_page_with_content,
wiki: wiki_1,
title: '<script>alert("FOO")</script>'
}
before do
project_2.reload
wiki_page_2_1 = FactoryGirl.create :wiki_page_with_content,
wiki: project_2.wiki,
title: 'Start Page'
project_2.wiki.pages << wiki_page_2_1
project_2.wiki.start_page = 'Start Page'
project_2.wiki.save!
project.wiki = wiki_1
wiki_1.pages << wiki_page_1_1
wiki_1.pages << wiki_page_1_2
wiki_1.pages << wiki_page_1_3
end
context 'Plain wiki link' do
subject { format_text('[[CookBook documentation]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/#{project.identifier}/wiki/cookbook-documentation\">CookBook documentation</a></p>") }
end
context 'Arbitrary wiki link' do
title = '<script>alert("FOO")</script>'
subject { format_text("[[#{title}]]") }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/#{project.identifier}/wiki/alert-foo\">#{h(title)}</a></p>") }
end
context 'Plain wiki page link' do
subject { format_text('[[Another page|Page]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/#{project.identifier}/wiki/another-page\">Page</a></p>") }
end
context 'Wiki link with anchor' do
subject { format_text('[[CookBook documentation#One-section]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/#{project.identifier}/wiki/cookbook-documentation#One-section\">CookBook documentation</a></p>") }
end
context 'Wiki page link with anchor' do
subject { format_text('[[Another page#anchor|Page]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/#{project.identifier}/wiki/another-page#anchor\">Page</a></p>") }
end
context 'Wiki link to an unknown page' do
subject { format_text('[[Unknown page]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page new\" href=\"/projects/#{project.identifier}/wiki/unknown-page\">Unknown page</a></p>") }
end
context 'Wiki page link to an unknown page' do
subject { format_text('[[Unknown page|404]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page new\" href=\"/projects/#{project.identifier}/wiki/unknown-page\">404</a></p>") }
end
context "Link to another project's wiki" do
subject { format_text('[[onlinestore:]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/onlinestore/wiki/start-page\">onlinestore</a></p>") }
end
context "Link to another project's wiki with label" do
subject { format_text('[[onlinestore:|Wiki]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/onlinestore/wiki/start-page\">Wiki</a></p>") }
end
context "Link to another project's wiki page" do
subject { format_text('[[onlinestore:Start page]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/onlinestore/wiki/start-page\">Start Page</a></p>") }
end
context "Link to another project's wiki page with label" do
subject { format_text('[[onlinestore:Start page|Text]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page\" href=\"/projects/onlinestore/wiki/start-page\">Text</a></p>") }
end
context 'Link to an unknown wiki page in another project' do
subject { format_text('[[onlinestore:Unknown page]]') }
it { is_expected.to be_html_eql("<p><a class=\"wiki-page new\" href=\"/projects/onlinestore/wiki/unknown-page\">Unknown page</a></p>") }
end
context 'Struck through link to wiki page' do
subject { format_text('-[[Another page|Page]]-') }
it { is_expected.to be_html_eql("<p><del><a class=\"wiki-page\" href=\"/projects/#{project.identifier}/wiki/another-page\">Page</a></del></p>") }
end
context 'Named struck through link to wiki page' do
subject { format_text('-[[Another page|Page]] link-') }
it { is_expected.to be_html_eql("<p><del><a class=\"wiki-page\" href=\"/projects/#{project.identifier}/wiki/another-page\">Page</a> link</del></p>") }
end
context 'Escaped link to wiki page' do
subject { format_text('![[Another page|Page]]') }
it { is_expected.to be_html_eql('<p>[[Another page|Page]]</p>') }
end
context 'Link to wiki of non-existing project' do
subject { format_text('[[unknowproject:Start]]') }
it { is_expected.to be_html_eql('<p>[[unknowproject:Start]]</p>') }
end
context 'Link to wiki page of non-existing project' do
subject { format_text('[[unknowproject:Start|Page title]]') }
it { is_expected.to be_html_eql('<p>[[unknowproject:Start|Page title]]</p>') }
end
end
context 'Redmine links' do
let(:repository) do
FactoryGirl.build_stubbed :repository_subversion, project: project
end
let(:source_url) do
{ controller: 'repositories',
action: 'entry',
project_id: identifier,
path: 'some/file' }
end
let(:source_url_with_ext) do
{ controller: 'repositories',
action: 'entry',
project_id: identifier,
path: 'some/file.ext' }
end
before do
allow(project).to receive(:repository).and_return(repository)
allow(User).to receive(:current).and_return(project_member)
allow(project_member)
.to receive(:allowed_to?)
.with(:browse_repository, project)
.and_return(true)
@to_test = {
# source
'source:/some/file' => link_to('source:/some/file', source_url, class: 'source'),
'source:/some/file.' => link_to('source:/some/file', source_url, class: 'source') + '.',
'source:/some/file.ext.' => link_to('source:/some/file.ext', source_url_with_ext, class: 'source') + '.',
'source:/some/file. ' => link_to('source:/some/file', source_url, class: 'source') + '.',
'source:/some/file.ext. ' => link_to('source:/some/file.ext', source_url_with_ext, class: 'source') + '.',
'source:/some/file, ' => link_to('source:/some/file', source_url, class: 'source') + ',',
'source:/some/file@52' => link_to('source:/some/file@52', source_url.merge(rev: 52), class: 'source'),
'source:/some/file.ext@52' => link_to('source:/some/file.ext@52', source_url_with_ext.merge(rev: 52), class: 'source'),
'source:/some/file#L110' => link_to('source:/some/file#L110', source_url.merge(anchor: 'L110'), class: 'source'),
'source:/some/file.ext#L110' => link_to('source:/some/file.ext#L110', source_url_with_ext.merge(anchor: 'L110'), class: 'source'),
'source:/some/file@52#L110' => link_to('source:/some/file@52#L110', source_url.merge(rev: 52, anchor: 'L110'), class: 'source'),
'export:/some/file' => link_to('export:/some/file', source_url.merge(format: 'raw'), class: 'source download'),
# escaping
'!source:/some/file' => 'source:/some/file',
# invalid expressions
'source:' => 'source:'
}
end
it '' do
@to_test.each do |text, result|
expect(format_text(text)).to be_html_eql("<p>#{result}</p>")
end
end
end
context 'Pre content should not parse wiki and redmine links' do
let(:wiki) {
FactoryGirl.create :wiki,
start_page: 'CookBook documentation',
project: project
}
let(:wiki_page) {
FactoryGirl.create :wiki_page_with_content,
wiki: wiki,
title: 'CookBook documentation'
}
let(:raw) {
<<-RAW
[[CookBook documentation]]
##{issue.id}
<pre>
[[CookBook documentation]]
##{issue.id}
</pre>
RAW
}
let(:expected) {
<<-EXPECTED
<p><a class="wiki-page" href="/projects/#{project.identifier}/wiki/cookbook-documentation">CookBook documentation</a></p>
<p><a class="issue work_package status-3 priority-1 created-by-me" href="/work_packages/#{issue.id}" title="#{issue.subject} (#{issue.status})">##{issue.id}</a></p>
<pre>
[[CookBook documentation]]
##{issue.id}
</pre>
EXPECTED
}
before do
project.wiki = wiki
wiki.pages << wiki_page
end
subject { format_text(raw) }
it { is_expected.to be_html_eql(expected) }
end
describe 'options' do
describe '#format' do
it 'uses format of Settings, if nothing is specified' do
expect(format_text('*Stars!*')).to be_html_eql('<p><strong>Stars!</strong></p>')
end
it 'uses format of options, if specified' do
expect(format_text('*Stars!*', format: 'plain')).to be_html_eql('<p>*Stars!*</p>')
end
end
end
end
describe 'macros within pre block' do
let(:wiki_text) {
<<-WIKI_TEXT
<pre>{{include(wiki)}}</pre>
{{include(wiki)}}
WIKI_TEXT
}
subject(:html) { format_text(wiki_text) }
it 'does not expand the macro within <pre>' do
expect(html).to be_html_eql(%[
<pre>{{ $root.DOUBLE_LEFT_CURLY_BRACE }}include(wiki)}}</pre>
<p>
<span class=\"flash error macro-unavailable permanent\"> Error executing the macro include (Page not found) </span>
</p>
])
end
end
describe '{{toc}}', 'table of contents macro' do
# Source: http://en.wikipedia.org/wiki/Orange_(fruit)
let(:wiki_text) {
<<-WIKI_TEXT
{{toc}}
h1. Orange
h2. Varietes
h3. Common Oranges
h4. Valencia
h5. Naranjito
h4. Hart's Tardiff Valencia
h3. Navel Oranges
h3. Blood Oranges
h3. Acidless Oranges
h2. Attributes
WIKI_TEXT
}
subject(:html) { format_text(wiki_text) }
context 'w/o request present' do
let(:request) { nil }
it 'emits a table of contents for headings h1-h4 with anchors' do
expect(html).to be_html_eql(%{
<fieldset class='form--fieldset -collapsible'>
<legend class='form--fieldset-legend' title='Show/Hide table of contents' onclick='toggleFieldset(this);'>
<a href='javascript:'>Table of Contents</a>
</legend>
<div>
<ul class="toc">
<li>
<a href="#Orange">Orange</a>
<ul>
<li>
<a href="#Varietes">Varietes</a>
<ul>
<li>
<a href="#Common-Oranges">Common Oranges</a>
<ul>
<li><a href="#Valencia">Valencia</a></li>
<li><a href="#Harts-Tardiff-Valencia">Hart's Tardiff Valencia</a></li>
</ul>
</li>
<li><a href="#Navel-Oranges">Navel Oranges</a></li>
<li><a href="#Blood-Oranges">Blood Oranges</a></li>
<li><a href="#Acidless-Oranges">Acidless Oranges</a></li>
</ul>
</li>
<li><a href="#Attributes">Attributes</a></li>
</ul>
</li>
</ul>
</div>
</fieldset>
}).at_path('fieldset')
end
end
end
end