2015-02-12 13:53:23 -05:00
|
|
|
require 'spec_helper'
|
2012-08-01 20:26:21 -04:00
|
|
|
|
2012-08-08 04:52:09 -04:00
|
|
|
describe GitlabMarkdownHelper do
|
2013-01-16 16:37:39 -05:00
|
|
|
include ApplicationHelper
|
2015-03-27 07:58:23 -04:00
|
|
|
|
2014-01-22 14:03:52 -05:00
|
|
|
let!(:project) { create(:project) }
|
2012-09-02 02:13:13 -04:00
|
|
|
|
2012-12-06 22:40:42 -05:00
|
|
|
let(:user) { create(:user, username: 'gfm') }
|
2015-04-21 09:13:40 -04:00
|
|
|
let(:commit) { project.commit }
|
2012-09-02 02:13:13 -04:00
|
|
|
let(:issue) { create(:issue, project: project) }
|
2013-04-25 10:15:33 -04:00
|
|
|
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
|
2013-03-25 03:20:14 -04:00
|
|
|
let(:snippet) { create(:project_snippet, project: project) }
|
2012-09-02 02:13:13 -04:00
|
|
|
|
2015-03-27 07:58:23 -04:00
|
|
|
# Helper expects a current_user method.
|
|
|
|
let(:current_user) { user }
|
|
|
|
|
2012-08-01 20:26:21 -04:00
|
|
|
before do
|
2012-09-02 02:13:13 -04:00
|
|
|
# Helper expects a @project instance variable
|
|
|
|
@project = project
|
2012-08-01 20:26:21 -04:00
|
|
|
end
|
|
|
|
|
2015-08-27 16:09:01 -04:00
|
|
|
describe "#markdown" do
|
2012-09-02 02:13:13 -04:00
|
|
|
describe "referencing multiple objects" do
|
2015-05-21 16:35:15 -04:00
|
|
|
let(:actual) { "#{merge_request.to_reference} -> #{commit.to_reference} -> #{issue.to_reference}" }
|
2012-09-02 02:13:13 -04:00
|
|
|
|
|
|
|
it "should link to the merge request" do
|
2015-01-24 13:02:58 -05:00
|
|
|
expected = namespace_project_merge_request_path(project.namespace, project, merge_request)
|
2015-08-27 16:09:01 -04:00
|
|
|
expect(markdown(actual)).to match(expected)
|
2012-08-01 20:26:21 -04:00
|
|
|
end
|
|
|
|
|
2012-09-02 02:13:13 -04:00
|
|
|
it "should link to the commit" do
|
2015-01-24 13:02:58 -05:00
|
|
|
expected = namespace_project_commit_path(project.namespace, project, commit)
|
2015-08-27 16:09:01 -04:00
|
|
|
expect(markdown(actual)).to match(expected)
|
2012-08-01 20:26:21 -04:00
|
|
|
end
|
|
|
|
|
2012-09-02 02:13:13 -04:00
|
|
|
it "should link to the issue" do
|
2015-01-24 13:02:58 -05:00
|
|
|
expected = namespace_project_issue_path(project.namespace, project, issue)
|
2015-08-27 16:09:01 -04:00
|
|
|
expect(markdown(actual)).to match(expected)
|
2012-08-01 20:26:21 -04:00
|
|
|
end
|
|
|
|
end
|
2015-09-18 14:05:27 -04:00
|
|
|
|
|
|
|
describe "override default project" do
|
|
|
|
let(:actual) { issue.to_reference }
|
|
|
|
let(:second_project) { create(:project) }
|
|
|
|
let(:second_issue) { create(:issue, project: second_project) }
|
|
|
|
|
|
|
|
it 'should link to the issue' do
|
|
|
|
expected = namespace_project_issue_path(second_project.namespace, second_project, second_issue)
|
|
|
|
expect(markdown(actual, project: second_project)).to match(expected)
|
|
|
|
end
|
|
|
|
end
|
2012-09-02 02:13:13 -04:00
|
|
|
end
|
2012-08-01 20:26:21 -04:00
|
|
|
|
2015-04-20 15:43:27 -04:00
|
|
|
describe '#link_to_gfm' do
|
2015-01-24 13:02:58 -05:00
|
|
|
let(:commit_path) { namespace_project_commit_path(project.namespace, project, commit) }
|
2012-09-02 02:13:13 -04:00
|
|
|
let(:issues) { create_list(:issue, 2, project: project) }
|
2012-08-01 20:26:21 -04:00
|
|
|
|
2015-04-20 15:43:27 -04:00
|
|
|
it 'should handle references nested in links with all the text' do
|
2015-05-21 16:35:15 -04:00
|
|
|
actual = link_to_gfm("This should finally fix #{issues[0].to_reference} and #{issues[1].to_reference} for real", commit_path)
|
2015-04-20 15:43:27 -04:00
|
|
|
doc = Nokogiri::HTML.parse(actual)
|
2012-08-01 20:26:21 -04:00
|
|
|
|
2015-04-21 13:12:19 -04:00
|
|
|
# Make sure we didn't create invalid markup
|
|
|
|
expect(doc.errors).to be_empty
|
|
|
|
|
2012-09-02 02:13:13 -04:00
|
|
|
# Leading commit link
|
2015-04-20 15:43:27 -04:00
|
|
|
expect(doc.css('a')[0].attr('href')).to eq commit_path
|
|
|
|
expect(doc.css('a')[0].text).to eq 'This should finally fix '
|
2012-08-01 20:26:21 -04:00
|
|
|
|
2012-09-02 02:13:13 -04:00
|
|
|
# First issue link
|
2015-04-20 15:43:27 -04:00
|
|
|
expect(doc.css('a')[1].attr('href')).
|
|
|
|
to eq namespace_project_issue_path(project.namespace, project, issues[0])
|
2015-05-21 16:35:15 -04:00
|
|
|
expect(doc.css('a')[1].text).to eq issues[0].to_reference
|
2012-08-01 20:28:50 -04:00
|
|
|
|
2012-09-02 02:13:13 -04:00
|
|
|
# Internal commit link
|
2015-04-20 15:43:27 -04:00
|
|
|
expect(doc.css('a')[2].attr('href')).to eq commit_path
|
|
|
|
expect(doc.css('a')[2].text).to eq ' and '
|
2012-08-01 20:28:50 -04:00
|
|
|
|
2012-09-02 02:13:13 -04:00
|
|
|
# Second issue link
|
2015-04-20 15:43:27 -04:00
|
|
|
expect(doc.css('a')[3].attr('href')).
|
|
|
|
to eq namespace_project_issue_path(project.namespace, project, issues[1])
|
2015-05-21 16:35:15 -04:00
|
|
|
expect(doc.css('a')[3].text).to eq issues[1].to_reference
|
2012-09-02 02:13:13 -04:00
|
|
|
|
|
|
|
# Trailing commit link
|
2015-04-20 15:43:27 -04:00
|
|
|
expect(doc.css('a')[4].attr('href')).to eq commit_path
|
|
|
|
expect(doc.css('a')[4].text).to eq ' for real'
|
2012-08-01 20:28:50 -04:00
|
|
|
end
|
|
|
|
|
2015-08-27 17:28:45 -04:00
|
|
|
it 'should forward HTML options' do
|
|
|
|
actual = link_to_gfm("Fixed in #{commit.id}", commit_path, class: 'foo')
|
|
|
|
doc = Nokogiri::HTML.parse(actual)
|
|
|
|
|
|
|
|
expect(doc.css('a')).to satisfy do |v|
|
|
|
|
# 'foo' gets added to all links
|
|
|
|
v.all? { |a| a.attr('class').match(/foo$/) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-19 19:42:26 -04:00
|
|
|
it "escapes HTML passed in as the body" do
|
2015-05-21 16:35:15 -04:00
|
|
|
actual = "This is a <h1>test</h1> - see #{issues[0].to_reference}"
|
2015-02-12 13:53:23 -05:00
|
|
|
expect(link_to_gfm(actual, commit_path)).
|
|
|
|
to match('<h1>test</h1>')
|
2012-09-19 19:42:26 -04:00
|
|
|
end
|
2015-05-29 01:56:30 -04:00
|
|
|
|
|
|
|
it 'ignores reference links when they are the entire body' do
|
|
|
|
text = issues[0].to_reference
|
|
|
|
act = link_to_gfm(text, '/foo')
|
|
|
|
expect(act).to eq %Q(<a href="/foo">#{issues[0].to_reference}</a>)
|
|
|
|
end
|
2012-08-01 20:28:50 -04:00
|
|
|
end
|
2012-08-01 20:29:15 -04:00
|
|
|
|
2015-04-13 18:05:42 -04:00
|
|
|
describe '#render_wiki_content' do
|
2013-03-14 02:31:08 -04:00
|
|
|
before do
|
2013-12-14 08:43:48 -05:00
|
|
|
@wiki = double('WikiPage')
|
2015-02-12 13:17:35 -05:00
|
|
|
allow(@wiki).to receive(:content).and_return('wiki content')
|
2013-03-14 02:31:08 -04:00
|
|
|
end
|
|
|
|
|
2013-05-19 05:13:39 -04:00
|
|
|
it "should use GitLab Flavored Markdown for markdown files" do
|
2015-02-12 13:17:35 -05:00
|
|
|
allow(@wiki).to receive(:format).and_return(:markdown)
|
2013-03-14 02:31:08 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(helper).to receive(:markdown).with('wiki content')
|
2013-03-14 02:31:08 -04:00
|
|
|
|
|
|
|
helper.render_wiki_content(@wiki)
|
|
|
|
end
|
|
|
|
|
2015-05-12 19:07:48 -04:00
|
|
|
it "should use Asciidoctor for asciidoc files" do
|
|
|
|
allow(@wiki).to receive(:format).and_return(:asciidoc)
|
|
|
|
|
|
|
|
expect(helper).to receive(:asciidoc).with('wiki content')
|
|
|
|
|
|
|
|
helper.render_wiki_content(@wiki)
|
|
|
|
end
|
|
|
|
|
2013-03-14 02:31:08 -04:00
|
|
|
it "should use the Gollum renderer for all other file types" do
|
2015-02-12 13:17:35 -05:00
|
|
|
allow(@wiki).to receive(:format).and_return(:rdoc)
|
2013-12-14 08:43:48 -05:00
|
|
|
formatted_content_stub = double('formatted_content')
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(formatted_content_stub).to receive(:html_safe)
|
|
|
|
allow(@wiki).to receive(:formatted_content).and_return(formatted_content_stub)
|
2013-03-14 02:31:08 -04:00
|
|
|
|
|
|
|
helper.render_wiki_content(@wiki)
|
|
|
|
end
|
|
|
|
end
|
2015-06-23 21:00:10 -04:00
|
|
|
|
|
|
|
describe 'random_markdown_tip' do
|
|
|
|
it 'returns a random Markdown tip' do
|
|
|
|
stub_const("#{described_class}::MARKDOWN_TIPS", ['Random tip'])
|
2015-07-09 16:38:33 -04:00
|
|
|
expect(random_markdown_tip).to eq 'Random tip'
|
2015-06-23 21:00:10 -04:00
|
|
|
end
|
|
|
|
end
|
2015-09-23 00:56:27 -04:00
|
|
|
|
|
|
|
describe '#first_line_in_markdown' do
|
|
|
|
let(:text) { "@#{user.username}, can you look at this?\nHello world\n"}
|
|
|
|
|
|
|
|
it 'truncates Markdown properly' do
|
|
|
|
actual = first_line_in_markdown(text, 100, project: project)
|
|
|
|
|
|
|
|
doc = Nokogiri::HTML.parse(actual)
|
|
|
|
|
|
|
|
# Make sure we didn't create invalid markup
|
|
|
|
expect(doc.errors).to be_empty
|
|
|
|
|
|
|
|
# Leading user link
|
|
|
|
expect(doc.css('a').length).to eq(1)
|
|
|
|
expect(doc.css('a')[0].attr('href')).to eq user_path(user)
|
|
|
|
expect(doc.css('a')[0].text).to eq "@#{user.username}"
|
|
|
|
|
|
|
|
expect(doc.content).to eq "@#{user.username}, can you look at this?..."
|
|
|
|
end
|
|
|
|
end
|
2012-08-01 20:26:21 -04:00
|
|
|
end
|