/
category_tag_spec.rb
872 lines (790 loc) · 30 KB
/
category_tag_spec.rb
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# encoding: UTF-8
# frozen_string_literal: true
RSpec.describe "category tag restrictions" do
def filter_allowed_tags(opts = {})
DiscourseTagging.filter_allowed_tags(Guardian.new(user), opts)
end
fab!(:tag1) { Fabricate(:tag, name: "tag1") }
fab!(:tag2) { Fabricate(:tag, name: "tag2") }
fab!(:tag3) { Fabricate(:tag, name: "tag3") }
fab!(:tag4) { Fabricate(:tag, name: "tag4") }
let(:tag_with_colon) { Fabricate(:tag, name: "with:colon") }
fab!(:user)
fab!(:admin) { Fabricate(:admin, refresh_auto_groups: true) }
before do
SiteSetting.tagging_enabled = true
SiteSetting.create_tag_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
end
context "with tags restricted to one category" do
fab!(:category_with_tags) { Fabricate(:category) }
fab!(:other_category) { Fabricate(:category) }
before { category_with_tags.tags = [tag1, tag2] }
it "tags belonging to that category can only be used there" do
msg =
I18n.t(
"tags.forbidden.category_does_not_allow_tags",
count: 1,
tags: tag3.name,
category: category_with_tags.name,
)
expect {
create_post(category: category_with_tags, tags: [tag1.name, tag2.name, tag3.name])
}.to raise_error(StandardError, msg)
msg =
I18n.t(
"tags.forbidden.restricted_tags_cannot_be_used_in_category",
count: 2,
tags: [tag1, tag2].map(&:name).sort.join(", "),
category: other_category.name,
)
expect {
create_post(category: other_category, tags: [tag1.name, tag2.name, tag3.name])
}.to raise_error(StandardError, msg)
end
it "search can show only permitted tags" do
expect(filter_allowed_tags.count).to eq(Tag.count)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category_with_tags),
[tag1, tag2],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag3, tag4])
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category_with_tags,
selected_tags: [tag1.name],
),
[tag2],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category_with_tags,
selected_tags: [tag1.name],
term: "tag",
),
[tag2],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category, selected_tags: [tag3.name]),
[tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: other_category,
selected_tags: [tag3.name],
term: "tag",
),
[tag4],
)
end
it "search can handle colons in tag names" do
tag_with_colon
expect_same_tag_names(filter_allowed_tags(for_input: true, term: "with:c"), [tag_with_colon])
end
it "can't create new tags in a restricted category" do
msg =
I18n.t(
"tags.forbidden.category_does_not_allow_tags",
count: 1,
tags: "newtag",
category: category_with_tags.name,
)
expect {
create_post(category: category_with_tags, tags: [tag1.name, "newtag"])
}.to raise_error(StandardError, msg)
expect {
create_post(category: category_with_tags, tags: [tag1.name, "newtag"], user: admin)
}.to raise_error(StandardError, msg)
end
it "can create new tags in a non-restricted category" do
post = create_post(category: other_category, tags: [tag3.name, "newtag"])
expect_same_tag_names(post.topic.tags, [tag3.name, "newtag"])
end
it "can create tags when changing category settings" do
expect { other_category.update(allowed_tags: ["newtag"]) }.to change { Tag.count }.by(1)
expect {
other_category.update(allowed_tags: [tag1.name, "tag-stuff", tag2.name, "another-tag"])
}.to change { Tag.count }.by(2)
end
context "with required tags from tag group" do
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1, tag3]) }
before do
category_with_tags.update!(
category_required_tag_groups: [
CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1),
],
)
end
it "search only returns the allowed tags" do
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category_with_tags),
[tag1],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category_with_tags,
selected_tags: [tag1.name],
),
[tag2],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category_with_tags,
selected_tags: [tag2.name],
),
[tag1],
)
end
end
context "when category allows other tags to be used" do
before { category_with_tags.update!(allow_global_tags: true) }
it "search can show the permitted tags" do
expect(filter_allowed_tags.count).to eq(Tag.count)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category_with_tags),
[tag1, tag2, tag3, tag4],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag3, tag4])
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category_with_tags,
selected_tags: [tag1.name],
),
[tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category_with_tags,
selected_tags: [tag1.name],
term: "tag",
),
[tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: other_category,
selected_tags: [tag3.name],
),
[tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: other_category,
selected_tags: [tag3.name],
term: "tag",
),
[tag4],
)
end
it "works if no tags are restricted to the category" do
other_category.update!(allow_global_tags: true)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: other_category,
selected_tags: [tag3.name],
),
[tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: other_category,
selected_tags: [tag3.name],
term: "tag",
),
[tag4],
)
end
context "with required tags from tag group" do
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1, tag3]) }
before do
category_with_tags.update!(
category_required_tag_groups: [
CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1),
],
)
end
it "search only returns the allowed tags" do
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category_with_tags),
[tag1, tag3],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category_with_tags,
selected_tags: [tag1.name],
),
[tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category_with_tags,
selected_tags: [tag2.name],
),
[tag1, tag3],
)
end
end
end
end
context "with tag groups restricted to a category" do
fab!(:tag_group1) { Fabricate(:tag_group) }
fab!(:category)
fab!(:other_category) { Fabricate(:category) }
before do
tag_group1.tags = [tag1, tag2]
category.allowed_tag_groups = [tag_group1.name]
category.reload
end
it "tags in the group are used by category tag restrictions" do
expect_same_tag_names(filter_allowed_tags(for_input: true, category: category), [tag1, tag2])
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag3, tag4])
tag_group1.tags = [tag2, tag3, tag4]
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag2, tag3, tag4],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag1])
expect_same_tag_names(filter_allowed_tags(for_input: true, category: other_category), [tag1])
end
it "groups and individual tags can be mixed" do
category.allowed_tags = [tag4.name]
category.reload
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag1, tag2, tag4],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag3])
expect_same_tag_names(filter_allowed_tags(for_input: true, category: other_category), [tag3])
end
it "enforces restrictions when creating a topic" do
msg =
I18n.t(
"tags.forbidden.category_does_not_allow_tags",
count: 1,
tags: "newtag",
category: category.name,
)
expect { create_post(category: category, tags: [tag1.name, "newtag"]) }.to raise_error(
StandardError,
msg,
)
end
it "handles colons" do
tag_with_colon
expect_same_tag_names(filter_allowed_tags(for_input: true, term: "with:c"), [tag_with_colon])
end
context "with required tags from tag group" do
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1, tag3]) }
before do
category.update!(
category_required_tag_groups: [
CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1),
],
)
end
it "search only returns the allowed tags" do
expect_same_tag_names(filter_allowed_tags(for_input: true, category: category), [tag1])
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category, selected_tags: [tag1.name]),
[tag2],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category, selected_tags: [tag2.name]),
[tag1],
)
end
end
context "when category allows other tags to be used" do
before { category.update!(allow_global_tags: true) }
it "filters tags correctly" do
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag1, tag2, tag3, tag4],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag3, tag4])
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag3, tag4],
)
tag_group1.tags = [tag2, tag3, tag4]
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag1, tag2, tag3, tag4],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag1])
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag1],
)
end
it "works if no tags are restricted to the category" do
other_category.update!(allow_global_tags: true)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag3, tag4],
)
tag_group1.tags = [tag2, tag3, tag4]
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag1],
)
end
context "with required tags from tag group" do
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1, tag3]) }
before do
category.update!(
category_required_tag_groups: [
CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1),
],
)
end
it "search only returns the allowed tags" do
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag1, tag3],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category, selected_tags: [tag1.name]),
[tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category, selected_tags: [tag2.name]),
[tag1, tag3],
)
end
end
context "when another category has restricted tags using groups" do
fab!(:category2) { Fabricate(:category) }
fab!(:tag_group2) { Fabricate(:tag_group) }
before do
tag_group2.tags = [tag2, tag3]
category2.allowed_tag_groups = [tag_group2.name]
category2.reload
end
it "filters tags correctly" do
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category2),
[tag2, tag3],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag4])
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag1, tag2, tag4],
)
end
it "doesn't care about tags in a group that isn't used in a category" do
unused_tag_group = Fabricate(:tag_group)
unused_tag_group.tags = [tag4]
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category2),
[tag2, tag3],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag4])
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag1, tag2, tag4],
)
end
end
context "when another category has restricted tags" do
fab!(:category2) { Fabricate(:category) }
it "doesn't filter tags that are also restricted in another category" do
category2.tags = [tag2, tag3]
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category2),
[tag2, tag3],
)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag4])
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag1, tag2, tag4],
)
end
end
end
end
context "with tag groups with parent tag" do
it "for input field, filter_allowed_tags returns results based on whether parent tag is present or not" do
tag_group = Fabricate(:tag_group, parent_tag_id: tag1.id)
tag_group.tags = [tag3, tag4]
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag1, tag2])
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag1.name]),
[tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag1.name, tag3.name]),
[tag2, tag4],
)
end
it "for tagging a topic, filter_allowed_tags allows tags without parent tag" do
tag_group = Fabricate(:tag_group, parent_tag_id: tag1.id)
tag_group.tags = [tag3, tag4]
expect_same_tag_names(filter_allowed_tags(for_topic: true), [tag1, tag2, tag3, tag4])
expect_same_tag_names(
filter_allowed_tags(for_topic: true, selected_tags: [tag1.name]),
[tag1, tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_topic: true, selected_tags: [tag1.name, tag3.name]),
[tag1, tag2, tag3, tag4],
)
end
it "filter_allowed_tags returns tags common to more than one tag group with parent tag" do
common = Fabricate(:tag, name: "common")
tag_group = Fabricate(:tag_group, parent_tag_id: tag1.id)
tag_group.tags = [tag2, common]
tag_group = Fabricate(:tag_group, parent_tag_id: tag3.id)
tag_group.tags = [tag4]
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag1, tag3])
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag1.name]),
[tag2, tag3, common],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag3.name]),
[tag4, tag1],
)
tag_group.tags = [tag4, common]
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag1, tag3])
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag1.name]),
[tag2, tag3, common],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag3.name]),
[tag4, tag1, common],
)
parent_tag_group = Fabricate(:tag_group, tags: [tag1, tag3])
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag1, tag3])
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag1.name]),
[tag2, tag3, common],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag3.name]),
[tag4, tag1, common],
)
parent_tag_group.update!(one_per_topic: true)
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag1, tag3])
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag1.name]),
[tag2, common],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, selected_tags: [tag3.name]),
[tag4, common],
)
end
context "with required tags from tag group" do
fab!(:tag_group) { Fabricate(:tag_group, tags: [tag1, tag2]) }
fab!(:category) do
Fabricate(
:category,
category_required_tag_groups: [
CategoryRequiredTagGroup.new(tag_group: tag_group, min_count: 1),
],
)
end
it "search only returns the allowed tags" do
tag_group_with_parent = Fabricate(:tag_group, parent_tag_id: tag1.id, tags: [tag3, tag4])
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category),
[tag1, tag2],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category, selected_tags: [tag2.name]),
[tag1],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: category, selected_tags: [tag1.name]),
[tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: category,
selected_tags: [tag1.name, tag2.name],
),
[tag3, tag4],
)
end
end
context "with category restrictions" do
fab!(:car_category) { Fabricate(:category) }
fab!(:other_category) { Fabricate(:category) }
fab!(:makes) { Fabricate(:tag_group, name: "Makes") }
fab!(:honda_group) { Fabricate(:tag_group, name: "Honda Models") }
fab!(:ford_group) { Fabricate(:tag_group, name: "Ford Models") }
before do
@tags = {}
%w[honda ford civic accord mustang taurus].each do |name|
@tags[name] = Fabricate(:tag, name: name)
end
makes.tags = [@tags["honda"], @tags["ford"]]
honda_group.parent_tag_id = @tags["honda"].id
honda_group.save
honda_group.tags = [@tags["civic"], @tags["accord"]]
ford_group.parent_tag_id = @tags["ford"].id
ford_group.save
ford_group.tags = [@tags["mustang"], @tags["taurus"]]
car_category.allowed_tag_groups = [makes.name, honda_group.name, ford_group.name]
end
it "handles all those rules" do
# car tags can't be used outside of car category:
expect_same_tag_names(filter_allowed_tags(for_input: true), [tag1, tag2, tag3, tag4])
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: other_category),
[tag1, tag2, tag3, tag4],
)
# in car category, a make tag must be given first:
expect(
sorted_tag_names(filter_allowed_tags(for_input: true, category: car_category)),
).to eq(%w[ford honda])
# model tags depend on which make is chosen:
expect(
sorted_tag_names(
filter_allowed_tags(for_input: true, category: car_category, selected_tags: ["honda"]),
),
).to eq(%w[accord civic ford])
expect(
sorted_tag_names(
filter_allowed_tags(for_input: true, category: car_category, selected_tags: ["ford"]),
),
).to eq(%w[honda mustang taurus])
makes.update!(one_per_topic: true)
expect(
sorted_tag_names(
filter_allowed_tags(for_input: true, category: car_category, selected_tags: ["honda"]),
),
).to eq(%w[accord civic])
expect(
sorted_tag_names(
filter_allowed_tags(for_input: true, category: car_category, selected_tags: ["ford"]),
),
).to eq(%w[mustang taurus])
honda_group.update!(one_per_topic: true)
ford_group.update!(one_per_topic: true)
expect(
sorted_tag_names(
filter_allowed_tags(for_input: true, category: car_category, selected_tags: ["honda"]),
),
).to eq(%w[accord civic])
expect(
sorted_tag_names(
filter_allowed_tags(for_input: true, category: car_category, selected_tags: ["ford"]),
),
).to eq(%w[mustang taurus])
car_category.update!(allow_global_tags: true)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: car_category),
["ford", "honda", tag1, tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(for_input: true, category: car_category, selected_tags: ["ford"]),
["mustang", "taurus", tag1, tag2, tag3, tag4],
)
expect_same_tag_names(
filter_allowed_tags(
for_input: true,
category: car_category,
selected_tags: %w[ford mustang],
),
[tag1, tag2, tag3, tag4],
)
end
it "can apply the tags to a topic" do
post = create_post(category: car_category, tags: %w[ford mustang])
expect(post.topic.tags.map(&:name).sort).to eq(%w[ford mustang])
end
context "with limit one tag from each group" do
before do
makes.update(one_per_topic: true)
honda_group.update(one_per_topic: true)
ford_group.update(one_per_topic: true)
end
it "can restrict one tag from each group" do
expect(
sorted_tag_names(filter_allowed_tags(for_input: true, category: car_category)),
).to eq(%w[ford honda])
expect(
sorted_tag_names(
filter_allowed_tags(
for_input: true,
category: car_category,
selected_tags: ["honda"],
),
),
).to eq(%w[accord civic])
expect(
sorted_tag_names(
filter_allowed_tags(for_input: true, category: car_category, selected_tags: ["ford"]),
),
).to eq(%w[mustang taurus])
expect(
sorted_tag_names(
filter_allowed_tags(
for_input: true,
category: car_category,
selected_tags: %w[ford mustang],
),
),
).to eq([])
end
it "can apply the tags to a topic" do
post = create_post(category: car_category, tags: %w[ford mustang])
expect(post.topic.tags.map(&:name).sort).to eq(%w[ford mustang])
end
it "can remove extra tags from the same group" do
# A weird case that input field wouldn't allow.
# Only one tag from car makers is allowed, but we're saying that two have been selected.
names =
filter_allowed_tags(
for_input: true,
category: car_category,
selected_tags: %w[honda ford],
).map(&:name)
expect(names.include?("honda") || names.include?("ford")).to eq(false)
expect(names).to include("civic")
expect(names).to include("mustang")
end
end
end
end
end
RSpec.describe "tag topic counts per category" do
fab!(:admin)
fab!(:category)
fab!(:category2) { Fabricate(:category) }
fab!(:tag1) { Fabricate(:tag) }
fab!(:tag2) { Fabricate(:tag) }
fab!(:tag3) { Fabricate(:tag) }
before do
SiteSetting.tagging_enabled = true
SiteSetting.create_tag_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0]
end
it "counts when a topic is created with tags" do
expect { Fabricate(:topic, category: category, tags: [tag1, tag2]) }.to change {
CategoryTagStat.count
}.by(2)
expect(CategoryTagStat.where(category: category, tag: tag1).sum(:topic_count)).to eq(1)
expect(CategoryTagStat.where(category: category, tag: tag2).sum(:topic_count)).to eq(1)
end
it "counts when tag is added to an existing topic" do
user = Fabricate(:user, refresh_auto_groups: true)
topic = Fabricate(:topic, user: user, category: category)
post = Fabricate(:post, user: user, topic: topic)
expect(CategoryTagStat.where(category: category).count).to eq(0)
expect {
PostRevisor.new(post).revise!(topic.user, raw: post.raw, tags: [tag1.name, tag2.name])
}.to change { CategoryTagStat.count }.by(2)
expect(CategoryTagStat.where(category: category, tag: tag1).sum(:topic_count)).to eq(1)
expect(CategoryTagStat.where(category: category, tag: tag2).sum(:topic_count)).to eq(1)
end
context "with topic with 2 tags" do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:topic) { Fabricate(:topic, user: user, category: category, tags: [tag1, tag2]) }
fab!(:post) { Fabricate(:post, user: user, topic: topic) }
it "has correct counts after tag is removed from a topic" do
post
topic2 = Fabricate(:topic, category: category, tags: [tag2])
expect(CategoryTagStat.where(category: category, tag: tag2).sum(:topic_count)).to eq(2)
PostRevisor.new(post).revise!(topic.user, raw: post.raw, tags: [])
expect(CategoryTagStat.where(category: category, tag: tag2).sum(:topic_count)).to eq(1)
expect(CategoryTagStat.where(category: category, tag: tag1).sum(:topic_count)).to eq(0)
end
it "has correct counts after a topic's category changes" do
PostRevisor.new(post).revise!(
topic.user,
category_id: category2.id,
raw: post.raw,
tags: [tag1.name, tag2.name],
)
expect(CategoryTagStat.where(category: category, tag: tag1).sum(:topic_count)).to eq(0)
expect(CategoryTagStat.where(category: category, tag: tag2).sum(:topic_count)).to eq(0)
expect(CategoryTagStat.where(category: category2, tag: tag1).sum(:topic_count)).to eq(1)
expect(CategoryTagStat.where(category: category2, tag: tag2).sum(:topic_count)).to eq(1)
end
it "has correct counts after topic's category AND tags changed" do
PostRevisor.new(post).revise!(
topic.user,
raw: post.raw,
tags: [tag2.name, tag3.name],
category_id: category2.id,
)
expect(CategoryTagStat.where(category: category, tag: tag1).sum(:topic_count)).to eq(0)
expect(CategoryTagStat.where(category: category, tag: tag2).sum(:topic_count)).to eq(0)
expect(CategoryTagStat.where(category: category, tag: tag3).sum(:topic_count)).to eq(0)
expect(CategoryTagStat.where(category: category2, tag: tag1).sum(:topic_count)).to eq(0)
expect(CategoryTagStat.where(category: category2, tag: tag2).sum(:topic_count)).to eq(1)
expect(CategoryTagStat.where(category: category2, tag: tag3).sum(:topic_count)).to eq(1)
end
end
context "with topic with one tag" do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:topic) { Fabricate(:topic, user: user, tags: [tag1], category: category) }
fab!(:post) { Fabricate(:post, user: user, topic: topic) }
it "counts after topic becomes uncategorized" do
PostRevisor.new(post).revise!(
user,
raw: post.raw,
tags: [tag1.name],
category_id: SiteSetting.uncategorized_category_id,
)
expect(
CategoryTagStat.where(
category: Category.find(SiteSetting.uncategorized_category_id),
tag: tag1,
).sum(:topic_count),
).to eq(1)
expect(CategoryTagStat.where(category: category, tag: tag1).sum(:topic_count)).to eq(0)
end
it "updates counts after topic is deleted" do
PostDestroyer.new(admin, post).destroy
expect(CategoryTagStat.where(category: category, tag: tag1).sum(:topic_count)).to eq(0)
end
it "updates counts after topic is recovered" do
PostDestroyer.new(admin, post).destroy
PostDestroyer.new(admin, post).recover
expect(CategoryTagStat.where(category: category, tag: tag1).sum(:topic_count)).to eq(1)
end
end
end