mentionable_shared_examples.rb 4.57 KB
Newer Older
1 2
# Specifications for behavior common to all Mentionable implementations.
# Requires a shared context containing:
3
# - subject { "the mentionable implementation" }
4 5 6 7
# - let(:backref_text) { "the way that +subject+ should refer to itself in backreferences " }
# - let(:set_mentionable_text) { lambda { |txt| "block that assigns txt to the subject's mentionable_text" } }

def common_mentionable_setup
Douwe Maan's avatar
Douwe Maan committed
8
  let(:project) { subject.project }
9 10 11
  let(:author)  { subject.author }

  let(:mentioned_issue)  { create(:issue, project: project) }
12
  let!(:mentioned_mr)     { create(:merge_request, :simple, source_project: project) }
Douwe Maan's avatar
Douwe Maan committed
13
  let(:mentioned_commit) { project.commit("HEAD~1") }
14 15 16 17

  let(:ext_proj)   { create(:project, :public) }
  let(:ext_issue)  { create(:issue, project: ext_proj) }
  let(:ext_mr)     { create(:merge_request, :simple, source_project: ext_proj) }
Douwe Maan's avatar
Douwe Maan committed
18
  let(:ext_commit) { ext_proj.commit("HEAD~2") }
19

20 21 22 23 24 25
  # Override to add known commits to the repository stub.
  let(:extra_commits) { [] }

  # A string that mentions each of the +mentioned_.*+ objects above. Mentionables should add a self-reference
  # to this string and place it in their +mentionable_text+.
  let(:ref_string) do
26 27
    <<-MSG.strip_heredoc
      These references are new:
28 29 30
        Issue:  #{mentioned_issue.to_reference}
        Merge:  #{mentioned_mr.to_reference}
        Commit: #{mentioned_commit.to_reference}
31 32

      This reference is a repeat and should only be mentioned once:
33
        Repeat: #{mentioned_issue.to_reference}
34 35

      These references are cross-referenced:
36 37 38
        Issue:  #{ext_issue.to_reference(project)}
        Merge:  #{ext_mr.to_reference(project)}
        Commit: #{ext_commit.to_reference(project)}
39 40 41 42

      This is a self-reference and should not be mentioned at all:
        Self: #{backref_text}
    MSG
43 44 45
  end

  before do
46 47
    # Wire the project's repository to return the mentioned commit, and +nil+
    # for any unrecognized commits.
Douwe Maan's avatar
Douwe Maan committed
48 49 50 51 52
    allow_any_instance_of(::Repository).to receive(:commit).and_call_original
    allow_any_instance_of(::Repository).to receive(:commit).with(mentioned_commit.short_id).and_return(mentioned_commit)
    extra_commits.each do |commit|
      allow_any_instance_of(::Repository).to receive(:commit).with(commit.short_id).and_return(commit)
    end
53

54 55 56 57 58 59 60 61
    set_mentionable_text.call(ref_string)
  end
end

shared_examples 'a mentionable' do
  common_mentionable_setup

  it 'generates a descriptive back-reference' do
62
    expect(subject.gfm_reference).to eq(backref_text)
63 64 65 66
  end

  it "extracts references from its reference property" do
    # De-duplicate and omit itself
67
    refs = subject.referenced_mentionables
68 69 70 71 72 73 74
    expect(refs.size).to eq(6)
    expect(refs).to include(mentioned_issue)
    expect(refs).to include(mentioned_mr)
    expect(refs).to include(mentioned_commit)
    expect(refs).to include(ext_issue)
    expect(refs).to include(ext_mr)
    expect(refs).to include(ext_commit)
75 76 77
  end

  it 'creates cross-reference notes' do
78 79 80 81
    mentioned_objects = [mentioned_issue, mentioned_mr, mentioned_commit,
                         ext_issue, ext_mr, ext_commit]

    mentioned_objects.each do |referenced|
82
      expect(SystemNoteService).to receive(:cross_reference).
83
        with(referenced, subject.local_reference, author)
84 85
    end

86
    subject.create_cross_references!
87 88 89 90 91 92 93 94
  end
end

shared_examples 'an editable mentionable' do
  common_mentionable_setup

  it_behaves_like 'a mentionable'

95 96 97 98
  let(:new_issues) do
    [create(:issue, project: project), create(:issue, project: ext_proj)]
  end

99
  it 'creates new cross-reference notes when the mentionable text is edited' do
Douwe Maan's avatar
Douwe Maan committed
100 101
    subject.save

Douwe Maan's avatar
Douwe Maan committed
102
    new_text = <<-MSG.strip_heredoc
103
      These references already existed:
Douwe Maan's avatar
Douwe Maan committed
104 105 106 107 108 109

      Issue:  #{mentioned_issue.to_reference}

      Commit: #{mentioned_commit.to_reference}

      ---
110 111

      This cross-project reference already existed:
Douwe Maan's avatar
Douwe Maan committed
112 113 114 115

      Issue:  #{ext_issue.to_reference(project)}

      ---
116 117

      These two references are introduced in an edit:
Douwe Maan's avatar
Douwe Maan committed
118 119 120 121

      Issue: #{new_issues[0].to_reference}

      Cross: #{new_issues[1].to_reference(project)}
122
    MSG
123

124 125
    # These three objects were already referenced, and should not receive new
    # notes
126
    [mentioned_issue, mentioned_commit, ext_issue].each do |oldref|
127
      expect(SystemNoteService).not_to receive(:cross_reference).
128
        with(oldref, any_args)
129 130
    end

131 132
    # These two issues are new and should receive reference notes
    new_issues.each do |newref|
133
      expect(SystemNoteService).to receive(:cross_reference).
134
        with(newref, subject.local_reference, author)
135
    end
136 137

    set_mentionable_text.call(new_text)
138
    subject.create_new_cross_references!(author)
139 140
  end
end