Welcome to mirror list, hosted at ThFree Co, Russian Federation.

filter_spec_helper.rb « helpers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7beed9c775577299e8a68a140766e97c6b122398 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true

# Helper methods for Banzai filter specs
#
# 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.
  # context - Hash context for the filter. (default: {project: project})
  #
  # Returns a Nokogiri::XML::DocumentFragment
  def filter(html, context = {}, result = nil)
    if defined?(project)
      context.reverse_merge!(project: project)
    end

    render_context = Banzai::RenderContext
      .new(context[:project], context[:current_user])

    context = context.merge(render_context: render_context)

    described_class.call(html, context, result)
  end

  # Get an instance of the Filter class
  #
  # Use this for testing instance methods, but remember to test the result of
  # the full pipeline by calling #call using the other methods in this helper.
  def filter_instance
    context = { project: project, current_user: current_user, render_context: render_context }

    described_class.new(input_text, context)
  end

  def render_context
    Banzai::RenderContext.new(project, current_user)
  end

  # Run text through HTML::Pipeline with the current filter and return the
  # result Hash
  #
  # body     - String text to run through the pipeline
  # context - Hash context for the filter. (default: {project: project})
  #
  # Returns the Hash
  def pipeline_result(body, context = {})
    context.reverse_merge!(project: project) if defined?(project)

    pipeline = HTML::Pipeline.new([described_class], context)
    pipeline.call(body)
  end

  def reference_pipeline(filter: described_class, **context)
    context.reverse_merge!(project: project) if defined?(project)
    context.reverse_merge!(current_user: current_user) if defined?(current_user)

    filters = [Banzai::Filter::AutolinkFilter, filter].compact

    redact = context.delete(:redact)
    filters.push(Banzai::Filter::ReferenceRedactorFilter) if redact

    HTML::Pipeline.new(filters, context)
  end

  def reference_pipeline_result(body, context = {})
    reference_pipeline(context).call(body)
  end

  def reference_filter(text, context = {})
    reference_pipeline(**context).to_document(text)
  end

  # Use to test no-ops
  def null_filter(text, context = {})
    reference_pipeline(filter: nil, **context).to_document(text)
  end

  # 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)
    case reference
    when /\A(.+)?[^\d]\d+\z/
      # Integer-based reference with optional project prefix
      reference.gsub(/\d+\z/) { |i| i.to_i + 10_000 }
    when /\A(.+@)?(\h{7,40}\z)/
      # SHA-based reference with optional prefix
      reference.gsub(/\h{7,40}\z/) { |v| v.reverse }
    else
      reference.gsub(/\w+\z/) { |v| v.reverse }
    end
  end

  # Shortcut to Rails' auto-generated routes helpers, to avoid including the
  # module
  def urls
    Gitlab::Routing.url_helpers
  end
end