debian-mirror-gitlab/spec/support/helpers/filter_spec_helper.rb

88 lines
2.4 KiB
Ruby
Raw Normal View History

2015-12-23 02:04:40 +05:30
# Helper methods for Banzai filter specs
2015-09-11 14:41:01 +05:30
#
# Must be included into specs manually
module FilterSpecHelper
extend ActiveSupport::Concern
# Perform `call` on the described class
#
# Automatically passes the current `project` value, if defined, to the context
# if none is provided.
#
# html - HTML String to pass to the filter's `call` method.
2015-12-23 02:04:40 +05:30
# context - Hash context for the filter. (default: {project: project})
2015-09-11 14:41:01 +05:30
#
# Returns a Nokogiri::XML::DocumentFragment
2015-12-23 02:04:40 +05:30
def filter(html, context = {})
2015-09-11 14:41:01 +05:30
if defined?(project)
2015-12-23 02:04:40 +05:30
context.reverse_merge!(project: project)
2015-09-11 14:41:01 +05:30
end
2018-05-09 12:01:36 +05:30
render_context = Banzai::RenderContext
.new(context[:project], context[:current_user])
context = context.merge(render_context: render_context)
2015-12-23 02:04:40 +05:30
described_class.call(html, context)
2015-09-11 14:41:01 +05:30
end
# Run text through HTML::Pipeline with the current filter and return the
# result Hash
#
# body - String text to run through the pipeline
2015-12-23 02:04:40 +05:30
# context - Hash context for the filter. (default: {project: project})
2015-09-11 14:41:01 +05:30
#
# Returns the Hash
2015-12-23 02:04:40 +05:30
def pipeline_result(body, context = {})
context.reverse_merge!(project: project) if defined?(project)
2015-09-11 14:41:01 +05:30
2015-12-23 02:04:40 +05:30
pipeline = HTML::Pipeline.new([described_class], context)
2015-09-11 14:41:01 +05:30
pipeline.call(body)
end
2015-12-23 02:04:40 +05:30
def reference_pipeline(context = {})
context.reverse_merge!(project: project) if defined?(project)
2015-10-24 18:46:33 +05:30
2015-12-23 02:04:40 +05:30
filters = [
Banzai::Filter::AutolinkFilter,
described_class
2015-12-23 02:04:40 +05:30
]
HTML::Pipeline.new(filters, context)
end
def reference_pipeline_result(body, context = {})
reference_pipeline(context).call(body)
end
def reference_filter(html, context = {})
reference_pipeline(context).to_document(html)
2015-10-24 18:46:33 +05:30
end
2015-09-11 14:41:01 +05:30
# Modify a String reference to make it invalid
#
# Commit SHAs get reversed, IDs get incremented by 1, all other Strings get
# their word characters reversed.
#
# reference - String reference to modify
#
# Returns a String
def invalidate_reference(reference)
2017-08-17 22:00:37 +05:30
if reference =~ /\A(.+)?[^\d]\d+\z/
2015-09-11 14:41:01 +05:30
# Integer-based reference with optional project prefix
2017-08-17 22:00:37 +05:30
reference.gsub(/\d+\z/) { |i| i.to_i + 10_000 }
2016-04-02 18:10:28 +05:30
elsif reference =~ /\A(.+@)?(\h{7,40}\z)/
2015-09-11 14:41:01 +05:30
# SHA-based reference with optional prefix
2016-04-02 18:10:28 +05:30
reference.gsub(/\h{7,40}\z/) { |v| v.reverse }
2015-09-11 14:41:01 +05:30
else
reference.gsub(/\w+\z/) { |v| v.reverse }
end
end
# Shortcut to Rails' auto-generated routes helpers, to avoid including the
# module
def urls
2016-06-02 11:05:42 +05:30
Gitlab::Routing.url_helpers
2015-09-11 14:41:01 +05:30
end
end