Fix bug rendering labels in group wikis

This commits fixes a bug when a group wiki have label
references inside.
parent f3bde212
......@@ -126,16 +126,7 @@ module MarkupHelper
text = wiki_page.content
return '' unless text.present?
context.merge!(
pipeline: :wiki,
project: @project,
wiki: @wiki,
repository: @wiki.repository,
page_slug: wiki_page.slug,
issuable_state_filter_enabled: true
)
html = markup_unsafe(wiki_page.path, text, context)
html = markup_unsafe(wiki_page.path, text, render_wiki_content_context(@wiki, wiki_page, context))
prepare_for_rendering(html, context)
end
......@@ -182,6 +173,20 @@ module MarkupHelper
private
def render_wiki_content_context(wiki, wiki_page, context)
context.merge(
pipeline: :wiki,
wiki: wiki,
repository: wiki.repository,
page_slug: wiki_page.slug,
issuable_state_filter_enabled: true
).merge(render_wiki_content_context_container(wiki))
end
def render_wiki_content_context_container(wiki)
{ project: wiki.container }
end
# Return +text+, truncated to +max_chars+ characters, excluding any HTML
# tags.
def truncate_visible(text, max_chars)
......@@ -311,3 +316,5 @@ module MarkupHelper
extend self
end
MarkupHelper.prepend_if_ee('EE::MarkupHelper')
# frozen_string_literal: true
module EE
module MarkupHelper
extend ::Gitlab::Utils::Override
private
override :render_wiki_content_context_container
def render_wiki_content_context_container(wiki)
if wiki.container.is_a?(Project)
super
else
{ project: nil, group: wiki.container }
end
end
end
end
---
title: Fix bug rendering labels in group wikis
merge_request: 48763
author:
type: fixed
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe MarkupHelper do
let(:project) { create(:project, :public) }
let_it_be(:project) { create(:project, :public) }
describe '#first_line_in_markdown' do
context 'with scoped label references' do
......@@ -23,4 +23,47 @@ RSpec.describe MarkupHelper do
end
end
end
describe '#render_wiki_content' do
subject { helper.render_wiki_content(wiki_page) }
context 'when file is Markdown' do
context 'when content has labels' do
let(:wiki_page) { create(:wiki_page, format: :markdown, container: container, title: 'merge', content: '~Bug') }
let(:wiki) { wiki_page.wiki }
before do
allow(helper).to receive(:current_user).and_return(nil)
helper.instance_variable_set(:@wiki, wiki)
end
shared_examples 'renders label' do
specify do
result = subject
doc = Nokogiri::HTML.parse(result)
expect(doc.css('.gl-label-link')).not_to be_empty
end
end
context 'when wiki is a group wiki' do
let_it_be(:group) { create(:group) }
let_it_be(:label) { create(:group_label, group: group, title: 'Bug') }
let(:container) { group }
it_behaves_like 'renders label'
end
context 'when wiki is a project wiki' do
let_it_be(:label) { create(:label, title: 'Bug', project: project) }
let(:container) { project }
it_behaves_like 'renders label'
end
end
end
end
end
......@@ -316,6 +316,7 @@ RSpec.describe MarkupHelper do
describe '#render_wiki_content' do
let(:wiki) { double('WikiPage', path: "file.#{extension}") }
let(:wiki_repository) { double('Repository') }
let(:content) { 'wiki content' }
let(:context) do
{
pipeline: :wiki, project: project, wiki: wiki,
......@@ -325,9 +326,11 @@ RSpec.describe MarkupHelper do
end
before do
expect(wiki).to receive(:content).and_return('wiki content')
expect(wiki).to receive(:content).and_return(content)
expect(wiki).to receive(:slug).and_return('nested/page')
expect(wiki).to receive(:repository).and_return(wiki_repository)
allow(wiki).to receive(:container).and_return(project)
helper.instance_variable_set(:@wiki, wiki)
end
......@@ -339,6 +342,19 @@ RSpec.describe MarkupHelper do
helper.render_wiki_content(wiki)
end
context 'when context has labels' do
let_it_be(:label) { create(:label, title: 'Bug', project: project) }
let(:content) { '~Bug' }
it 'renders label' do
result = helper.render_wiki_content(wiki)
doc = Nokogiri::HTML.parse(result)
expect(doc.css('.gl-label-link')).not_to be_empty
end
end
end
context 'when file is Asciidoc' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment