govuk_content_models 41.1.1 → 42.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,188 +0,0 @@
1
- require "test_helper"
2
-
3
- class TagTest < ActiveSupport::TestCase
4
- test "should return a hash of the fields" do
5
- tag = FactoryGirl.build(:live_tag,
6
- tag_id: "crime",
7
- tag_type: "section",
8
- title: "Crime",
9
- )
10
- expected_hash = {
11
- id: "crime",
12
- title: "Crime",
13
- type: "section",
14
- description: nil,
15
- short_description: nil
16
- }
17
- assert_equal expected_hash, tag.as_json
18
- end
19
-
20
- setup do
21
- %w(crime business housing).each do |section|
22
- FactoryGirl.create(:live_tag, :tag_id => section, :title => section.capitalize)
23
- end
24
-
25
- %w(pie mash chips).each do |keyword|
26
- FactoryGirl.create(
27
- :live_tag,
28
- :tag_id => keyword,
29
- :title => keyword.capitalize,
30
- :tag_type => "keyword"
31
- )
32
- end
33
- end
34
-
35
- test "should load by tag ID" do
36
- assert_equal "Crime", Tag.by_tag_id("crime").title
37
- end
38
-
39
- test "should load by tag ID and type" do
40
- # This form is deprecated in favour of providing the type as an option
41
- assert_equal "Crime", Tag.by_tag_id("crime", "section").title
42
- end
43
-
44
- test "accepts the tag type as an option" do
45
- assert_equal "Crime", Tag.by_tag_id("crime", type: "section").title
46
- end
47
-
48
- test "should not load an incorrectly-typed tag" do
49
- assert_nil Tag.by_tag_id("crime", "keyword")
50
- end
51
-
52
- test "should return nil if tag does not exist" do
53
- assert_nil Tag.by_tag_id("batman")
54
- end
55
-
56
- test "should return multiple tags" do
57
- assert_equal(
58
- %w(Crime Business),
59
- Tag.by_tag_ids(%w(crime business)).map(&:title)
60
- )
61
- end
62
-
63
- test "should not return missing tags" do
64
- tag_ids = %w(crime business not_a_real_tag housing)
65
- tags = Tag.by_tag_ids(tag_ids)
66
-
67
- assert_equal %w(crime business housing), tags.map(&:tag_id)
68
- end
69
-
70
- test "should not return draft tags unless requested" do
71
- draft_tag = FactoryGirl.create(:draft_tag,
72
- tag_id: "draft-tag",
73
- tag_type: "section",
74
- title: "A draft tag",
75
- )
76
-
77
- tag_ids = %w(crime business draft-tag housing)
78
-
79
- assert_equal %w(crime business housing).to_set, Tag.by_tag_ids(tag_ids).map(&:tag_id).to_set
80
- assert_equal %w(crime business draft-tag housing).to_set, Tag.by_tag_ids(tag_ids, draft: true).map(&:tag_id).to_set
81
-
82
- assert_nil Tag.by_tag_id('draft-tag')
83
- assert_equal draft_tag, Tag.by_tag_id('draft-tag', draft: true)
84
- end
85
-
86
- test "should return nil for tags of the wrong type" do
87
- tag_ids = %w(crime business pie batman)
88
- tags = Tag.by_tag_ids(tag_ids, "section")
89
- [2, 3].each do |i| assert_nil tags[i] end
90
- [0, 1].each do |i| assert_equal tag_ids[i], tags[i].tag_id end
91
- end
92
-
93
- test "should raise an exception if any tags are missing" do
94
- assert_raises Tag::MissingTags do
95
- Tag.validate_tag_ids(%w(crime business batman))
96
- end
97
- end
98
-
99
- test "should raise an exception with the wrong tag type" do
100
- assert_raises Tag::MissingTags do
101
- Tag.validate_tag_ids(%w(crime business pie chips), "section")
102
- end
103
- end
104
-
105
- test "should return tags given a list of tag ids and tag types" do
106
- tag_types_and_ids = [
107
- { tag_type: "section", tag_id: "crime" },
108
- { tag_type: "section", tag_id: "business" },
109
- { tag_type: "keyword", tag_id: "pie" },
110
- { tag_type: "keyword", tag_id: "chips" }
111
- ]
112
- tags = Tag.by_tag_types_and_ids(tag_types_and_ids)
113
-
114
- assert_equal %w{Business Chips Crime Pie}, tags.map(&:title).sort
115
- end
116
-
117
- test "should be invalid when tag id already exists for the tag type" do
118
- Tag.create!(tag_id: "cars", tag_type: "vehicles", title: "Cars")
119
- Tag.create!(tag_id: "cars", tag_type: "gary-numan-songs", title: "Cars")
120
-
121
- tag = Tag.new(tag_id: "cars", tag_type: "vehicles")
122
-
123
- refute tag.valid?
124
- assert tag.errors.has_key?(:tag_id)
125
- end
126
-
127
- test "should validate with TagIdValidator" do
128
- assert_includes Tag.validators.map(&:class), TagIdValidator
129
- end
130
-
131
- test "#parent returns the parent even if the parent is draft" do
132
- parent = FactoryGirl.create(:tag, state: 'draft')
133
- child = FactoryGirl.create(:tag, state: 'draft', parent_id: parent.tag_id)
134
-
135
- assert_equal parent, child.parent
136
- end
137
-
138
- context "state" do
139
- setup do
140
- @atts = { tag_type: 'section', tag_id: 'test', title: 'Test' }
141
- end
142
-
143
- should "be created in draft state by default" do
144
- tag = Tag.create(@atts)
145
-
146
- assert tag.persisted?
147
- assert_equal 'draft', tag.state
148
- end
149
-
150
- should "be able to be set to live" do
151
- tag = Tag.new(@atts)
152
- tag.state = 'live'
153
- tag.save
154
-
155
- tag.reload
156
- assert_equal 'live', tag.state
157
- end
158
-
159
- should "not be created in another state" do
160
- tag = Tag.new(@atts)
161
- tag.state = 'foo'
162
-
163
- assert !tag.valid?
164
- assert tag.errors.has_key?(:state)
165
- end
166
-
167
- should "be set to live when published" do
168
- tag = Tag.create(@atts)
169
-
170
- assert_equal 'draft', tag.state
171
- tag.publish!
172
-
173
- tag.reload
174
- assert_equal 'live', tag.state
175
- end
176
-
177
- should "not be published more than once" do
178
- tag = Tag.create(@atts)
179
-
180
- tag.publish!
181
- tag.reload
182
-
183
- assert_raises StateMachines::InvalidTransition do
184
- tag.publish!
185
- end
186
- end
187
- end
188
- end
@@ -1,132 +0,0 @@
1
- require 'test_helper'
2
-
3
- # This test relies on the fact that artefact uses the taggable module
4
- class TaggableTest < ActiveSupport::TestCase
5
-
6
- TEST_KEYWORDS = [['cheese', 'Cheese'], ['bacon', 'Bacon']]
7
-
8
- setup do
9
- @parent_section = FactoryGirl.create(:live_tag, :tag_id => 'crime', :tag_type => 'section', :title => 'Crime')
10
- FactoryGirl.create(:live_tag, :tag_id => 'crime/the-police', :tag_type => 'section', :title => 'The Police', :parent_id => @parent_section.id)
11
- FactoryGirl.create(:live_tag, :tag_id => 'crime/batman', :tag_type => 'section', :title => 'Batman', :parent_id => @parent_section.id)
12
- @draft_section = FactoryGirl.create(:draft_tag, parent_id: @parent_section.id)
13
-
14
- TEST_KEYWORDS.each do |tag_id, title|
15
- FactoryGirl.create(:live_tag, :tag_id => tag_id, :tag_type => 'keyword', :title => title)
16
- end
17
-
18
- @item = FactoryGirl.create(:artefact)
19
- end
20
-
21
- test "can set sections" do
22
- @item.sections = ['crime', 'crime/the-police']
23
-
24
- assert_equal ['crime', 'crime/the-police'], @item.tag_ids, 'Mismatched tags'
25
- assert_equal ['crime', 'crime/the-police'], @item.sections.collect(&:tag_id), 'Mismatched sections'
26
-
27
- assert_equal 'Crime', @item.primary_section.title
28
- end
29
-
30
- test "can set sections and primary section separately" do
31
- @item.sections = ['crime', 'crime/the-police']
32
- @item.primary_section = 'crime'
33
-
34
- assert_equal ['crime', 'crime/the-police'], @item.tag_ids, 'Mismatched tags'
35
- assert_equal ['crime', 'crime/the-police'], @item.sections.collect(&:tag_id), 'Mismatched sections'
36
-
37
- assert_equal 'Crime', @item.primary_section.title
38
- end
39
-
40
- test "can set subsection as primary section" do
41
- @item.sections = ['crime/the-police', 'crime']
42
- @item.primary_section = 'crime/the-police'
43
- assert_equal 'The Police', @item.primary_section.title
44
- end
45
-
46
- test "cannot set non-existent sections" do
47
- assert_raise Tag::MissingTags do
48
- @item.sections = ['weevils']
49
- end
50
- end
51
-
52
- test "cannot set non-section tags" do
53
- assert_raise Tag::MissingTags do
54
- @item.sections = ['crime', 'bacon']
55
- end
56
- end
57
-
58
- test "can set no sections" do
59
- @item.sections = ['crime', 'crime/the-police']
60
- @item.sections = []
61
- assert_equal [], @item.sections
62
-
63
- refute @item.primary_section
64
- end
65
-
66
- test "setting sections doesn't break other tags" do
67
- @item.keywords = ['cheese', 'bacon']
68
- @item.sections = ['crime']
69
- @item.primary_section = 'crime'
70
-
71
- assert_equal ['bacon', 'cheese', 'crime'], @item.tag_ids.sort
72
- assert_equal 'Crime', @item.primary_section.title
73
- end
74
-
75
- test "setting primary section adds section to tags" do
76
- @item.sections = ['crime', 'crime/the-police']
77
- @item.primary_section = 'crime/batman'
78
-
79
- assert_includes @item.sections.collect(&:tag_id), 'crime/batman'
80
- end
81
-
82
- test "setting primary section to existing section works" do
83
- @item.sections = ['crime', 'crime/the-police']
84
- @item.primary_section = 'crime/the-police'
85
- # Note: not testing the order of the sections in this test, just testing
86
- # that the section is still present and not duplicated
87
- assert_equal ['crime', 'crime/the-police'], @item.sections.collect(&:tag_id).sort
88
- end
89
-
90
- test "setting primary section to existing section reorders correctly" do
91
- @item.sections = ['crime', 'crime/the-police']
92
- @item.save!
93
-
94
- @item.primary_section = 'crime/batman'
95
- @item.save!
96
-
97
- assert_equal 'crime/batman', @item.primary_section.tag_id
98
- assert_equal ['crime/batman', 'crime', 'crime/the-police'], @item.sections.collect(&:tag_id)
99
- end
100
-
101
- test "can set tags using foo_ids= type method" do
102
- @item.keyword_ids = ['bacon']
103
- @item.save!
104
-
105
- @item.reload
106
-
107
- assert_equal ['bacon'], @item.keyword_ids
108
- end
109
-
110
- test "can set tags of type to be nil" do
111
- @item.section_ids = nil
112
- @item.save!
113
-
114
- assert_equal [], @item.section_ids
115
-
116
- @item.sections = nil
117
- @item.save!
118
-
119
- assert_equal [], @item.section_ids
120
- end
121
-
122
- test "returns draft tags only if requested" do
123
- @item.section_ids = [@parent_section.tag_id, @draft_section.tag_id]
124
- @item.save!
125
-
126
- assert_equal [@parent_section.tag_id], @item.section_ids
127
- assert_equal [@parent_section.tag_id, @draft_section.tag_id], @item.section_ids(draft: true)
128
-
129
- assert_equal [@parent_section], @item.sections
130
- assert_equal [@parent_section, @draft_section], @item.sections(draft: true)
131
- end
132
- end
@@ -1,34 +0,0 @@
1
- require 'test_helper'
2
- require 'browse_page_validator'
3
-
4
- class BrowsePageValidatorTest < ActiveSupport::TestCase
5
- class Record
6
- include Mongoid::Document
7
-
8
- field :browse_pages, type: Array
9
-
10
- validates_with BrowsePageValidator
11
- end
12
-
13
- should "allow tagging to a variety of unique browse pages" do
14
- record = Record.new(
15
- browse_pages: [
16
- 'business/tax',
17
- 'housing/safety-environment'
18
- ]
19
- )
20
-
21
- assert record.valid?
22
- end
23
-
24
- should "be invalid if there's duplicates in the browse page list" do
25
- record = Record.new(
26
- browse_pages: [
27
- 'housing/safety-environment',
28
- 'housing/safety-environment'
29
- ]
30
- )
31
-
32
- refute record.valid?
33
- end
34
- end
@@ -1,66 +0,0 @@
1
- require 'test_helper'
2
- require 'tag_id_validator'
3
-
4
- class TagIdValidatorTest < ActiveSupport::TestCase
5
- class Dummy
6
- include Mongoid::Document
7
-
8
- field :tag_id, type: String
9
- field :parent_id, type: String
10
-
11
- validates_with TagIdValidator
12
- end
13
-
14
- should "permit a lower-case alphanumeric tag id" do
15
- dummy = Dummy.new(tag_id: "a-good-tag-id")
16
- assert dummy.valid?
17
- end
18
-
19
- should "not permit a tag id with spaces" do
20
- dummy = Dummy.new(tag_id: "this tag has spaces")
21
- refute dummy.valid?
22
- assert dummy.errors.has_key?(:tag_id)
23
- end
24
-
25
- should "not permit a tag id with uppercase characters" do
26
- dummy = Dummy.new(tag_id: "CLEAN-ALL-THE-THINGS")
27
- refute dummy.valid?
28
- assert dummy.errors.has_key?(:tag_id)
29
- end
30
-
31
- should "not permit a tag id with non-alphanumeric characters" do
32
- dummy = Dummy.new(tag_id: "a-t@g-!d")
33
- refute dummy.valid?
34
- assert dummy.errors.has_key?(:tag_id)
35
- end
36
-
37
- should "not permit a tag id with underscores" do
38
- dummy = Dummy.new(tag_id: "tag_id_with_underscores")
39
- refute dummy.valid?
40
- assert dummy.errors.has_key?(:tag_id)
41
- end
42
-
43
- should "permit a child tag id containing a slash" do
44
- dummy = Dummy.new(tag_id: "parent-tag-id/child-tag-id", parent_id: 1)
45
- assert dummy.valid?
46
- end
47
-
48
- should "not permit a parent tag id containing a slash" do
49
- dummy = Dummy.new(tag_id: "an-invalid/parent-tag-id", parent_id: nil)
50
- refute dummy.valid?
51
- assert dummy.errors.has_key?(:tag_id)
52
- end
53
-
54
- should "not permit a child tag id with more than one slash" do
55
- dummy = Dummy.new(tag_id: "parent-tag-id/two/slashes", parent_id: 1)
56
- refute dummy.valid?
57
- assert dummy.errors.has_key?(:tag_id)
58
- end
59
-
60
- should "not permit a slash at the end of a tag id" do
61
- dummy = Dummy.new(tag_id: "parent-tag-id/")
62
- refute dummy.valid?
63
- assert dummy.errors.has_key?(:tag_id)
64
- end
65
-
66
- end
@@ -1,48 +0,0 @@
1
- require 'test_helper'
2
- require 'topic_validator'
3
-
4
- class TopicValidatorTest < ActiveSupport::TestCase
5
- class Record
6
- include Mongoid::Document
7
-
8
- field :primary_topic, type: String
9
- field :additional_topics, type: Array
10
-
11
- validates_with TopicValidator
12
- end
13
-
14
- should "allow tagging to a variety of unique topics" do
15
- record = Record.new(
16
- primary_topic: 'oil-and-gas/exploration',
17
- additional_topics: [
18
- 'oil-and-gas/fields-and-wells',
19
- 'oil-and-gas/licensing'
20
- ]
21
- )
22
-
23
- assert record.valid?
24
- end
25
-
26
- should "be invalid if there's duplicates in the additional topic list" do
27
- record = Record.new(
28
- additional_topics: [
29
- 'oil-and-gas/fields-and-wells',
30
- 'oil-and-gas/fields-and-wells'
31
- ]
32
- )
33
-
34
- refute record.valid?
35
- end
36
-
37
- should "be invalid if the primary topic is in the additional topic list" do
38
- record = Record.new(
39
- primary_topic: 'oil-and-gas/fields-and-wells',
40
- additional_topics: [
41
- 'oil-and-gas/fields-and-wells',
42
- 'oil-and-gas/licensing'
43
- ]
44
- )
45
-
46
- refute record.valid?
47
- end
48
- end