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

cache_markdown_field_spec.rb « concerns « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f85f636ebe5c15b0432bf26af5bc9e17755d3677 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe CacheMarkdownField, :clean_gitlab_redis_cache do
  let(:ar_class) do
    Class.new(ActiveRecord::Base) do
      self.table_name = 'issues'
      include CacheMarkdownField
      cache_markdown_field :title, pipeline: :single_line
      cache_markdown_field :description

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

  let(:other_class) do
    Class.new do
      include CacheMarkdownField

      def initialize(args = {})
        @title = args[:title]
        @description = args[:description]
        @cached_markdown_version = args[:cached_markdown_version]
        @title_html = args[:title_html]
        @description_html = args[:description_html]
        @author = args[:author]
        @project = args[:project]
        @parent_user = args[:parent_user]
      end

      attr_accessor :title, :description, :cached_markdown_version

      cache_markdown_field :title, pipeline: :single_line
      cache_markdown_field :description

      def cache_key
        "cache-key"
      end
    end
  end

  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>' }

  let(:cache_version) { Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16 }

  def thing_subclass(klass, *extra_attributes)
    Class.new(klass) { attr_accessor(*extra_attributes) }
  end

  shared_examples 'a class with cached markdown fields' do
    describe '#cached_html_up_to_date?' do
      let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }

      subject { thing.cached_html_up_to_date?(:title) }

      it 'returns false when the version is absent' do
        thing.cached_markdown_version = nil

        is_expected.to be_falsy
      end

      it 'returns false when the version is too early' do
        thing.cached_markdown_version -= 1

        is_expected.to be_falsy
      end

      it 'returns false when the version is too late' do
        thing.cached_markdown_version += 1

        is_expected.to be_falsy
      end

      it 'returns false when the local version was bumped' do
        stub_application_setting(local_markdown_version: 2)
        thing.cached_markdown_version = cache_version

        is_expected.to be_falsy
      end

      it 'returns true when the local version is default' do
        thing.cached_markdown_version = cache_version

        is_expected.to be_truthy
      end

      it 'returns true when the cached version is just right' do
        stub_application_setting(local_markdown_version: 2)
        thing.cached_markdown_version = cache_version + 2

        is_expected.to be_truthy
      end
    end

    describe '#latest_cached_markdown_version' do
      let(:thing) { klass.new }

      subject { thing.latest_cached_markdown_version }

      it 'returns default version' do
        thing.cached_markdown_version = nil
        is_expected.to eq(cache_version)
      end
    end

    describe '#refresh_markdown_cache' do
      let(:thing) { klass.new(description: markdown, description_html: html, cached_markdown_version: cache_version) }

      before do
        thing.description = updated_markdown
      end

      it 'fills all html fields' do
        thing.refresh_markdown_cache

        expect(thing.description_html).to eq(updated_html)
      end

      it 'does not save the result' do
        expect(thing).not_to receive(:save_markdown)

        thing.refresh_markdown_cache
      end

      it 'updates the markdown cache version' do
        thing.cached_markdown_version = nil
        thing.refresh_markdown_cache

        expect(thing.cached_markdown_version).to eq(cache_version)
      end
    end

    describe '#refresh_markdown_cache!' do
      let(:thing) { klass.new(description: markdown, description_html: html, cached_markdown_version: cache_version) }

      before do
        thing.description = updated_markdown
      end

      it 'fills all html fields' do
        thing.refresh_markdown_cache!

        expect(thing.description_html).to eq(updated_html)
      end

      it 'saves the changes' do
        expect(thing)
          .to receive(:save_markdown)
          .with({ "description_html" => updated_html, "title_html" => "", "cached_markdown_version" => cache_version })

        thing.refresh_markdown_cache!
      end
    end

    describe '#banzai_render_context' do
      let(:thing) { klass.new(title: markdown, title_html: html, cached_markdown_version: cache_version) }

      subject(:context) { thing.banzai_render_context(:title) }

      it 'sets project to nil if the object lacks a project' do
        is_expected.to have_key(:project)
        expect(context[:project]).to be_nil
      end

      it 'excludes author if the object lacks an author' do
        is_expected.not_to have_key(:author)
      end

      it 'raises if the context for an unrecognised field is requested' do
        expect { thing.banzai_render_context(:not_found) }.to raise_error(ArgumentError)
      end

      it 'includes the pipeline' do
        title_context = thing.banzai_render_context(:title)

        expect(title_context[:pipeline]).to eq(:single_line)
      end

      it 'returns copies of the context template' do
        template = thing.cached_markdown_fields[:description]
        copy = thing.banzai_render_context(:description)

        expect(copy).not_to be(template)
      end

      context 'with a project' do
        let(:project) { build(:project, group: create(:group)) }
        let(:thing) { thing_subclass(klass, :project).new(title: markdown, title_html: html, project: project) }

        it 'sets the project in the context' do
          is_expected.to have_key(:project)
          expect(context[:project]).to eq(project)
        end
      end

      context 'with an author' do
        let(:user) { build(:user) }
        let(:thing) { thing_subclass(klass, :author).new(title: markdown, title_html: html, author: user) }

        it 'sets the author in the context' do
          is_expected.to have_key(:author)
          expect(context[:author]).to eq(user)
        end
      end

      context 'with a parent_user' do
        let(:user) { build(:user) }
        let(:thing) { thing_subclass(klass, :author, :parent_user).new(title: markdown, title_html: html, parent_user: user, author: user) }

        it 'sets the user in the context' do
          is_expected.to have_key(:user)
          expect(context[:user]).to eq(user)
        end

        context 'when the personal_snippet_reference_filters flag is disabled' do
          before do
            stub_feature_flags(personal_snippet_reference_filters: false)
          end

          it 'does not set the user in the context' do
            is_expected.not_to have_key(:user)
            expect(context[:user]).to be_nil
          end
        end
      end
    end

    describe '#updated_cached_html_for' do
      let(:thing) { klass.new(project_id: project.id, namespace_id: project.project_namespace_id, description: markdown, description_html: html, cached_markdown_version: cache_version) }

      context 'when the markdown cache is outdated' do
        before do
          thing.cached_markdown_version += 1
        end

        it 'calls #refresh_markdown_cache!' do
          expect(thing).to receive(:refresh_markdown_cache)

          expect(thing.updated_cached_html_for(:description)).to eq(html)
        end
      end

      context 'when the markdown field does not exist' do
        it 'returns nil' do
          expect(thing.updated_cached_html_for(:something)).to eq(nil)
        end
      end

      context 'when the markdown cache is up to date' do
        before do
          thing.try(:save)
        end

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

          expect(thing.updated_cached_html_for(:description)).to eq(html)
        end
      end
    end

    describe '#rendered_field_content' do
      let(:thing) { klass.new(description: markdown, description_html: nil, cached_markdown_version: cache_version) }

      context 'when a field can be cached' do
        it 'returns the html' do
          thing.description = updated_markdown

          expect(thing.rendered_field_content(:description)).to eq updated_html
        end
      end

      context 'when a field cannot be cached' do
        it 'returns nil' do
          allow(thing).to receive(:can_cache_field?).with(:description).and_return false

          expect(thing.rendered_field_content(:description)).to eq nil
        end
      end
    end
  end

  shared_examples 'a class with mentionable markdown fields' do
    let(:mentionable) { klass.new(project_id: project.id, namespace_id: project.project_namespace_id, description: markdown, description_html: html, title: markdown, title_html: html, cached_markdown_version: cache_version) }

    context 'when klass is a Mentionable', :aggregate_failures do
      before do
        klass.send(:include, Mentionable)
        klass.send(:attr_mentionable, :description)
      end

      describe '#mentionable_attributes_changed?' do
        message = Struct.new(:text)

        let(:changes) do
          msg = message.new('test')

          changes = {}
          changes[msg] = ['', 'some message']
          changes[:random_sym_key] = ['', 'some message']
          changes["description"] = ['', 'some message']
          changes
        end

        it 'returns true with key string' do
          changes["description_html"] = ['', 'some message']

          allow(mentionable).to receive(:saved_changes).and_return(changes)

          expect(mentionable.send(:mentionable_attributes_changed?)).to be true
        end

        it 'returns false with key symbol' do
          changes[:description_html] = ['', 'some message']
          allow(mentionable).to receive(:saved_changes).and_return(changes)

          expect(mentionable.send(:mentionable_attributes_changed?)).to be false
        end

        it 'returns false when no attr_mentionable keys' do
          allow(mentionable).to receive(:saved_changes).and_return(changes)

          expect(mentionable.send(:mentionable_attributes_changed?)).to be false
        end
      end

      describe '#save' do
        context 'when cache is outdated' do
          before do
            thing.cached_markdown_version += 1
          end

          context 'when the markdown field also a mentionable attribute' do
            let(:thing) { klass.new(project_id: project.id, namespace_id: project.project_namespace_id, description: markdown, description_html: html, cached_markdown_version: cache_version) }

            it 'calls #store_mentions!' do
              expect(thing).to receive(:mentionable_attributes_changed?).and_return(true)
              expect(thing).to receive(:store_mentions!)

              thing.try(:save)

              expect(thing.description_html).to eq(html)
            end
          end

          context 'when the markdown field is not mentionable attribute' do
            let(:thing) { klass.new(project_id: project.id, namespace_id: project.project_namespace_id, title: markdown, title_html: html, cached_markdown_version: cache_version) }

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

              thing.try(:save)

              expect(thing.title_html).to eq(html)
            end
          end
        end

        context 'when the markdown field does not exist' do
          let(:thing) { klass.new(project_id: project.id, namespace_id: project.project_namespace_id, cached_markdown_version: cache_version) }

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

            thing.try(:save)
          end
        end
      end
    end
  end

  context 'for Active record classes' do
    let_it_be(:project) { create(:project) }

    let(:klass) { ar_class }

    it_behaves_like 'a class with cached markdown fields'
    it_behaves_like 'a class with mentionable markdown fields'

    describe '#attribute_invalidated?' do
      let(:thing) { klass.create!(project_id: project.id, namespace_id: project.project_namespace_id, description: markdown, description_html: html, cached_markdown_version: cache_version) }

      it 'returns true when cached_markdown_version is different' do
        thing.cached_markdown_version += 1

        expect(thing.attribute_invalidated?(:description_html)).to eq(true)
      end

      it 'returns true when markdown is changed' do
        thing.description = updated_markdown

        expect(thing.attribute_invalidated?(:description_html)).to eq(true)
      end

      it 'returns true when both markdown and HTML are changed' do
        thing.description = updated_markdown
        thing.description_html = updated_html

        expect(thing.attribute_invalidated?(:description_html)).to eq(true)
      end

      it 'returns false when there are no changes' do
        expect(thing.attribute_invalidated?(:description_html)).to eq(false)
      end

      it 'returns false if skip_markdown_cache_validation is true' do
        # invalidates the attribute
        thing.cached_markdown_version += 1
        thing.description = updated_markdown

        thing.skip_markdown_cache_validation = true

        expect(thing.attribute_invalidated?(:description_html)).to eq(false)
      end
    end

    context 'when cache version is updated' do
      let(:old_version) { cache_version - 1 }
      let(:old_html) { '<p data-sourcepos="1:1-1:5" dir="auto" class="some-old-class"><code>Foo</code></p>' }

      let(:thing) do
        # This forces the record to have outdated HTML. We can't use `create` because the `before_create` hook
        # would re-render the HTML to the latest version
        klass.create!(project_id: project.id, namespace_id: project.project_namespace_id).tap do |thing|
          thing.update_columns(description: markdown, description_html: old_html, cached_markdown_version: old_version)
        end
      end

      it 'correctly updates cached HTML even if refresh_markdown_cache is called before updating the attribute' do
        thing.refresh_markdown_cache

        thing.update!(description: updated_markdown)

        expect(thing.description_html).to eq(updated_html)
      end
    end
  end

  context 'for other classes' do
    let_it_be(:project) { create(:project) }

    let(:klass) { other_class }

    it_behaves_like 'a class with cached markdown fields'
  end
end