2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-19 16:12:03 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Banzai::Filter::GollumTagsFilter do
|
2016-01-19 16:12:03 +05:30
|
|
|
include FilterSpecHelper
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project) }
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:wiki) { ProjectWiki.new(project, nil) }
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
describe 'validation' do
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'ensure that a :wiki key exists in context' do
|
|
|
|
expect { filter("See [[images/image.jpg]]", {}) }.to raise_error ArgumentError, "Missing context keys for Banzai::Filter::GollumTagsFilter: :wiki"
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'linking internal images' do
|
|
|
|
it 'creates img tag if image exists' do
|
2018-03-17 18:26:18 +05:30
|
|
|
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)
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(wiki).to receive(:find_file).with('images/image.jpg').and_return(wiki_file)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
tag = '[[images/image.jpg]]'
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter("See #{tag}", wiki: wiki)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(doc.at_css('img')['data-src']).to eq "#{wiki.wiki_base_path}/images/image.jpg"
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not creates img tag if image does not exist' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(wiki).to receive(:find_file).with('images/image.jpg').and_return(nil)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
tag = '[[images/image.jpg]]'
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter("See #{tag}", wiki: wiki)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
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]]'
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter("See #{tag}", wiki: wiki)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
expect(doc.at_css('img')['data-src']).to eq "http://example.com/image.jpg"
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not creates img tag for invalid URL' do
|
|
|
|
tag = '[[http://example.com/image.pdf]]'
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter("See #{tag}", wiki: wiki)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
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]]'
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter("See #{tag}", wiki: wiki)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
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]]'
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter("See #{tag}", wiki: wiki)
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
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-06-02 11:05:42 +05:30
|
|
|
it "the created link's text includes the resource's text and wiki base path" do
|
2016-01-19 16:12:03 +05:30
|
|
|
tag = '[[wiki-slug]]'
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter("See #{tag}", wiki: wiki)
|
|
|
|
expected_path = ::File.join(wiki.wiki_base_path, 'wiki-slug')
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
expect(doc.at_css('a').text).to eq 'wiki-slug'
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(doc.at_css('a')['href']).to eq expected_path
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "the created link's text will be link-text" do
|
|
|
|
tag = '[[link-text|wiki-slug]]'
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter("See #{tag}", wiki: wiki)
|
|
|
|
expected_path = ::File.join(wiki.wiki_base_path, 'wiki-slug')
|
2016-01-19 16:12:03 +05:30
|
|
|
|
|
|
|
expect(doc.at_css('a').text).to eq 'link-text'
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(doc.at_css('a')['href']).to eq expected_path
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
it "inside back ticks will be exempt from linkification" do
|
2020-06-23 00:09:42 +05:30
|
|
|
doc = filter('<code>[[link-in-backticks]]</code>', wiki: wiki)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
expect(doc.at_css('code').text).to eq '[[link-in-backticks]]'
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2016-01-19 16:12:03 +05:30
|
|
|
end
|