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

extension_spec.rb « active_record « markdown_cache « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 57f2b1cfd966e7e8fc452b4dae33d6c05473e300 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe Gitlab::MarkdownCache::ActiveRecord::Extension do
  let(:klass) do
    Class.new(ActiveRecord::Base) do
      self.table_name = 'issues'
      include CacheMarkdownField
      cache_markdown_field :title, whitelisted: true
      cache_markdown_field :description, pipeline: :single_line

      attribute :author
      attribute :project

      before_validation -> { self.work_item_type_id = ::WorkItems::Type.default_issue_type.id }
    end
  end

  let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 }
  let(:thing) { klass.create!(title: markdown, title_html: html, cached_markdown_version: cache_version) }

  let(:markdown) { '`Foo`' }
  let(:html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Foo</code></p>' }

  let(:updated_markdown) { '`Bar`' }
  let(:updated_html) { '<p data-sourcepos="1:1-1:5" dir="auto"><code>Bar</code></p>' }

  context 'an unchanged markdown field' do
    let(:thing) { klass.new(title: markdown) }

    before do
      thing.title = thing.title
      thing.save!
    end

    it { expect(thing.title).to eq(markdown) }
    it { expect(thing.title_html).to eq(html) }
    it { expect(thing.title_html_changed?).not_to be_truthy }
    it { expect(thing.cached_markdown_version).to eq(cache_version) }
  end

  context 'a changed markdown field' do
    let(:thing) { klass.create!(title: markdown, title_html: html, cached_markdown_version: cache_version) }

    before do
      thing.title = updated_markdown
      thing.save!
    end

    it { expect(thing.title_html).to eq(updated_html) }
    it { expect(thing.cached_markdown_version).to eq(cache_version) }
  end

  context 'when a markdown field is set repeatedly to an empty string' do
    it do
      expect(thing).to receive(:refresh_markdown_cache).once
      thing.title = ''
      thing.save!
      thing.title = ''
      thing.save!
    end
  end

  context 'when a markdown field is set repeatedly to a string which renders as empty html' do
    it do
      expect(thing).to receive(:refresh_markdown_cache).once
      thing.title = '[//]: # (This is also a comment.)'
      thing.save!
      thing.title = '[//]: # (This is also a comment.)'
      thing.save!
    end
  end

  context 'a non-markdown field changed' do
    let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }

    before do
      thing.state_id = 2
      thing.save!
    end

    it { expect(thing.state_id).to eq(2) }
    it { expect(thing.title).to eq(markdown) }
    it { expect(thing.title_html).to eq(html) }
    it { expect(thing.cached_markdown_version).to eq(cache_version) }
  end

  context 'version is out of date' do
    let(:thing) { klass.new(title: updated_markdown, title_html: html, cached_markdown_version: nil) }

    before do
      thing.save!
    end

    it { expect(thing.title_html).to eq(updated_html) }
    it { expect(thing.cached_markdown_version).to eq(cache_version) }
  end

  context 'when an invalidating field is changed' do
    it 'invalidates the cache when project changes' do
      thing.project = :new_project
      allow(Banzai::Renderer).to receive(:cacheless_render_field).and_return(updated_html)

      thing.save!

      expect(thing.title_html).to eq(updated_html)
      expect(thing.description_html).to eq(updated_html)
      expect(thing.cached_markdown_version).to eq(cache_version)
    end

    it 'invalidates the cache when author changes' do
      thing.author = :new_author
      allow(Banzai::Renderer).to receive(:cacheless_render_field).and_return(updated_html)

      thing.save!

      expect(thing.title_html).to eq(updated_html)
      expect(thing.description_html).to eq(updated_html)
      expect(thing.cached_markdown_version).to eq(cache_version)
    end
  end

  describe '.attributes' do
    it 'excludes cache attributes that is blacklisted by default' do
      expect(thing.attributes.keys.sort).not_to include(%w[description_html])
    end
  end

  describe '#cached_html_up_to_date?' do
    let(:thing) { klass.create!(title: updated_markdown, title_html: html, cached_markdown_version: nil) }

    subject { thing.cached_html_up_to_date?(:title) }

    it 'returns false if markdown has been changed but html has not' do
      thing.title = "changed!"

      is_expected.to be_falsy
    end

    it 'returns true if markdown has not been changed but html has' do
      thing.title_html = updated_html

      is_expected.to be_truthy
    end

    it 'returns true if markdown and html have both been changed' do
      thing.title = updated_markdown
      thing.title_html = updated_html

      is_expected.to be_truthy
    end

    it 'returns false if the markdown field is set but the html is not' do
      thing.title_html = nil

      is_expected.to be_falsy
    end
  end

  describe '#refresh_markdown_cache!' do
    before do
      thing.title = updated_markdown
    end

    it 'skips saving if not persisted' do
      expect(thing).to receive(:persisted?).and_return(false)
      expect(thing).not_to receive(:update_columns)

      thing.refresh_markdown_cache!
    end

    it 'saves the changes' do
      expect(thing).to receive(:persisted?).and_return(true)

      expect(thing).to receive(:update_columns)
                         .with({ "title_html" => updated_html,
                                 "description_html" => "",
                                 "cached_markdown_version" => cache_version })

      thing.refresh_markdown_cache!
    end
  end

  context 'with note' do
    let(:klass) do
      Class.new(ActiveRecord::Base) do
        self.table_name = 'notes'
        include CacheMarkdownField
        include Importable
        include Mentionable

        attr_mentionable :note, pipeline: :note
        cache_markdown_field :note, pipeline: :note
      end
    end

    let(:thing) { klass.new(note: markdown) }

    before do
      thing.note = "hello world"
    end

    it 'calls store_mentions!' do
      expect(thing).to receive(:store_mentions!).and_call_original

      thing.save!
    end

    context 'during import' do
      before do
        thing.importing = true
      end

      it 'does not call store_mentions!' do
        expect(thing).not_to receive(:store_mentions!)

        thing.save!
      end
    end
  end

  context 'when persisted cache is newer than current version' do
    before do
      thing.update_column(:cached_markdown_version, thing.cached_markdown_version + 1)
    end

    it 'does not save the generated HTML' do
      expect(thing).not_to receive(:update_columns)

      thing.refresh_markdown_cache!
    end
  end

  context 'when persisted cache is nil' do
    before do
      thing.update_column(:cached_markdown_version, nil)
    end

    it 'does not save the generated HTML' do
      expect(thing).to receive(:update_columns)

      thing.refresh_markdown_cache!
    end
  end
end