2016-01-12 04:05:18 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe Banzai::Filter::GollumTagsFilter do
|
2016-01-12 04:05:18 +00:00
|
|
|
include FilterSpecHelper
|
|
|
|
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project) }
|
2016-01-12 04:05:18 +00:00
|
|
|
let(:user) { double }
|
|
|
|
let(:project_wiki) { ProjectWiki.new(project, user) }
|
|
|
|
|
|
|
|
describe 'validation' do
|
|
|
|
it 'ensure that a :project_wiki key exists in context' do
|
|
|
|
expect { filter("See [[images/image.jpg]]", {}) }.to raise_error ArgumentError, "Missing context keys for Banzai::Filter::GollumTagsFilter: :project_wiki"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'linking internal images' do
|
|
|
|
it 'creates img tag if image exists' do
|
2017-10-23 21:05:59 +00:00
|
|
|
gollum_file_double = double('Gollum::File',
|
|
|
|
mime_type: 'image/jpeg',
|
|
|
|
name: 'images/image.jpg',
|
|
|
|
path: 'images/image.jpg',
|
|
|
|
raw_data: '')
|
|
|
|
wiki_file = Gitlab::Git::WikiFile.new(gollum_file_double)
|
|
|
|
expect(project_wiki).to receive(:find_file).with('images/image.jpg').and_return(wiki_file)
|
2016-01-12 04:05:18 +00:00
|
|
|
|
|
|
|
tag = '[[images/image.jpg]]'
|
|
|
|
doc = filter("See #{tag}", project_wiki: project_wiki)
|
|
|
|
|
2017-07-24 17:36:52 +00:00
|
|
|
expect(doc.at_css('img')['data-src']).to eq "#{project_wiki.wiki_base_path}/images/image.jpg"
|
2016-01-12 04:05:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not creates img tag if image does not exist' do
|
|
|
|
expect(project_wiki).to receive(:find_file).with('images/image.jpg').and_return(nil)
|
|
|
|
|
|
|
|
tag = '[[images/image.jpg]]'
|
|
|
|
doc = filter("See #{tag}", project_wiki: project_wiki)
|
|
|
|
|
|
|
|
expect(doc.css('img').size).to eq 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'linking external images' do
|
|
|
|
it 'creates img tag for valid URL' do
|
|
|
|
tag = '[[http://example.com/image.jpg]]'
|
|
|
|
doc = filter("See #{tag}", project_wiki: project_wiki)
|
|
|
|
|
2017-07-24 17:36:52 +00:00
|
|
|
expect(doc.at_css('img')['data-src']).to eq "http://example.com/image.jpg"
|
2016-01-12 04:05:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not creates img tag for invalid URL' do
|
|
|
|
tag = '[[http://example.com/image.pdf]]'
|
|
|
|
doc = filter("See #{tag}", project_wiki: project_wiki)
|
|
|
|
|
|
|
|
expect(doc.css('img').size).to eq 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'linking external resources' do
|
|
|
|
it "the created link's text will be equal to the resource's text" do
|
|
|
|
tag = '[[http://example.com]]'
|
|
|
|
doc = filter("See #{tag}", project_wiki: project_wiki)
|
|
|
|
|
|
|
|
expect(doc.at_css('a').text).to eq 'http://example.com'
|
|
|
|
expect(doc.at_css('a')['href']).to eq 'http://example.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "the created link's text will be link-text" do
|
|
|
|
tag = '[[link-text|http://example.com/pdfs/gollum.pdf]]'
|
|
|
|
doc = filter("See #{tag}", project_wiki: project_wiki)
|
|
|
|
|
|
|
|
expect(doc.at_css('a').text).to eq 'link-text'
|
|
|
|
expect(doc.at_css('a')['href']).to eq 'http://example.com/pdfs/gollum.pdf'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'linking internal resources' do
|
2016-04-06 23:09:15 +00:00
|
|
|
it "the created link's text includes the resource's text and wiki base path" do
|
2016-01-12 04:05:18 +00:00
|
|
|
tag = '[[wiki-slug]]'
|
|
|
|
doc = filter("See #{tag}", project_wiki: project_wiki)
|
2016-03-30 18:30:36 +00:00
|
|
|
expected_path = ::File.join(project_wiki.wiki_base_path, 'wiki-slug')
|
2016-01-12 04:05:18 +00:00
|
|
|
|
|
|
|
expect(doc.at_css('a').text).to eq 'wiki-slug'
|
2016-03-30 18:30:36 +00:00
|
|
|
expect(doc.at_css('a')['href']).to eq expected_path
|
2016-01-12 04:05:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "the created link's text will be link-text" do
|
|
|
|
tag = '[[link-text|wiki-slug]]'
|
|
|
|
doc = filter("See #{tag}", project_wiki: project_wiki)
|
2016-03-30 18:30:36 +00:00
|
|
|
expected_path = ::File.join(project_wiki.wiki_base_path, 'wiki-slug')
|
2016-01-12 04:05:18 +00:00
|
|
|
|
|
|
|
expect(doc.at_css('a').text).to eq 'link-text'
|
2016-03-30 18:30:36 +00:00
|
|
|
expect(doc.at_css('a')['href']).to eq expected_path
|
2016-01-12 04:05:18 +00:00
|
|
|
end
|
2018-05-18 20:14:31 +00:00
|
|
|
|
|
|
|
it "inside back ticks will be exempt from linkification" do
|
|
|
|
doc = filter('<code>[[link-in-backticks]]</code>', project_wiki: project_wiki)
|
|
|
|
|
|
|
|
expect(doc.at_css('code').text).to eq '[[link-in-backticks]]'
|
|
|
|
end
|
2016-01-12 04:05:18 +00:00
|
|
|
end
|
2016-02-23 23:05:10 +00:00
|
|
|
|
|
|
|
context 'table of contents' do
|
2016-03-01 21:59:56 +00:00
|
|
|
it 'replaces [[<em>TOC</em>]] with ToC result' do
|
|
|
|
doc = described_class.call("<p>[[<em>TOC</em>]]</p>", { project_wiki: project_wiki }, { toc: "FOO" })
|
2016-02-23 23:05:10 +00:00
|
|
|
|
2016-03-01 21:59:56 +00:00
|
|
|
expect(doc.to_html).to eq("FOO")
|
2016-02-23 23:05:10 +00:00
|
|
|
end
|
|
|
|
|
2016-03-01 21:59:56 +00:00
|
|
|
it 'handles an empty ToC result' do
|
2016-03-03 17:36:36 +00:00
|
|
|
input = "<p>[[<em>TOC</em>]]</p>"
|
2016-03-01 21:59:56 +00:00
|
|
|
doc = described_class.call(input, project_wiki: project_wiki)
|
2016-02-23 23:05:10 +00:00
|
|
|
|
2016-03-03 17:36:36 +00:00
|
|
|
expect(doc.to_html).to eq ''
|
2016-02-23 23:05:10 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-12 04:05:18 +00:00
|
|
|
end
|