govuk_content_models 42.0.0 → 42.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/app/traits/attachable.rb +2 -2
  4. data/govuk_content_models.gemspec +1 -2
  5. data/lib/govuk_content_models/version.rb +1 -1
  6. metadata +3 -83
  7. data/test/fixtures/uploads/image.jpg +0 -0
  8. data/test/models/action_test.rb +0 -13
  9. data/test/models/artefact_action_test.rb +0 -130
  10. data/test/models/artefact_external_link_test.rb +0 -32
  11. data/test/models/artefact_test.rb +0 -482
  12. data/test/models/business_support/business_size_test.rb +0 -25
  13. data/test/models/business_support/business_type_test.rb +0 -25
  14. data/test/models/business_support/location_test.rb +0 -25
  15. data/test/models/business_support/purpose_test.rb +0 -29
  16. data/test/models/business_support/sector_test.rb +0 -25
  17. data/test/models/business_support/stage_test.rb +0 -25
  18. data/test/models/business_support/support_type_test.rb +0 -25
  19. data/test/models/business_support_edition_test.rb +0 -282
  20. data/test/models/campaign_edition_test.rb +0 -91
  21. data/test/models/completed_transaction_edition_test.rb +0 -56
  22. data/test/models/downtime_test.rb +0 -93
  23. data/test/models/edition_scheduled_for_publishing_test.rb +0 -91
  24. data/test/models/edition_test.rb +0 -1185
  25. data/test/models/help_page_edition_test.rb +0 -43
  26. data/test/models/licence_edition_test.rb +0 -105
  27. data/test/models/local_service_test.rb +0 -6
  28. data/test/models/local_transaction_edition_test.rb +0 -37
  29. data/test/models/overview_dashboard_test.rb +0 -46
  30. data/test/models/parted_test.rb +0 -26
  31. data/test/models/prerendered_entity_tests.rb +0 -46
  32. data/test/models/rendered_manual_test.rb +0 -10
  33. data/test/models/simple_smart_answer_edition_test.rb +0 -211
  34. data/test/models/simple_smart_answer_node_test.rb +0 -134
  35. data/test/models/simple_smart_answer_option_test.rb +0 -97
  36. data/test/models/time_zone_test.rb +0 -48
  37. data/test/models/transaction_edition_test.rb +0 -36
  38. data/test/models/travel_advice_edition_test.rb +0 -469
  39. data/test/models/user_test.rb +0 -155
  40. data/test/models/video_edition_test.rb +0 -64
  41. data/test/models/workflow_test.rb +0 -498
  42. data/test/test_helper.rb +0 -61
  43. data/test/traits/attachable_test.rb +0 -244
  44. data/test/validators/link_validator_test.rb +0 -86
  45. data/test/validators/safe_html_validator_test.rb +0 -83
  46. data/test/validators/slug_validator_test.rb +0 -109
@@ -1,91 +0,0 @@
1
- require "test_helper"
2
-
3
- class CampaignEditionTest < ActiveSupport::TestCase
4
- setup do
5
- @artefact = FactoryGirl.create(:artefact, :kind => 'campaign', :slug => "start-all-the-campaigns")
6
- end
7
-
8
- should "have correct extra fields" do
9
- c = FactoryGirl.create(
10
- :campaign_edition,
11
- panopticon_id: @artefact.id,
12
- body: "Start all the campaigns!",
13
- large_image_id: "large-image-id-from-the-asset-manager",
14
- medium_image_id: "medium-image-id-from-the-asset-manager",
15
- small_image_id: "small-image-id-from-the-asset-manager",
16
- organisation_formatted_name: "Driver & Vehicle\nLicensing\nAgency",
17
- organisation_url: "/government/organisations/driver-and-vehicle-licensing-agency",
18
- organisation_brand_colour: "department-for-transport",
19
- organisation_crest: "single-identity",
20
- )
21
-
22
- assert_equal "Start all the campaigns!", c.body
23
- assert_equal "large-image-id-from-the-asset-manager", c.large_image_id
24
- assert_equal "medium-image-id-from-the-asset-manager", c.medium_image_id
25
- assert_equal "small-image-id-from-the-asset-manager", c.small_image_id
26
- assert_equal "Driver & Vehicle\nLicensing\nAgency", c.organisation_formatted_name
27
- assert_equal "/government/organisations/driver-and-vehicle-licensing-agency", c.organisation_url
28
- assert_equal "department-for-transport", c.organisation_brand_colour
29
- assert_equal "single-identity", c.organisation_crest
30
- end
31
-
32
- should "give a friendly (legacy supporting) description of its format" do
33
- campaign = CampaignEdition.new
34
- assert_equal "Campaign", campaign.format
35
- end
36
-
37
- should "return the body as whole_body" do
38
- campaign = FactoryGirl.build(:campaign_edition,
39
- :panopticon_id => @artefact.id,
40
- :body => "Something")
41
- assert_equal campaign.body, campaign.whole_body
42
- end
43
-
44
- should "clone extra fields when cloning edition" do
45
- campaign = FactoryGirl.create(:campaign_edition,
46
- :panopticon_id => @artefact.id,
47
- :state => "published",
48
- :body => "I'm very campaignful",
49
- :large_image_id => "large-image",
50
- :medium_image_id => "medium-image",
51
- :small_image_id => "small-image",
52
- :organisation_formatted_name => "Driver & Vehicle\nLicensing\nAgency",
53
- :organisation_url => "/government/organisations/driver-and-vehicle-licensing-agency",
54
- :organisation_brand_colour => "department-for-transport",
55
- :organisation_crest => "single-identity" )
56
-
57
- new_campaign = campaign.build_clone
58
- assert_equal campaign.body, new_campaign.body
59
- assert_equal campaign.large_image_id, new_campaign.large_image_id
60
- assert_equal campaign.medium_image_id, new_campaign.medium_image_id
61
- assert_equal campaign.small_image_id, new_campaign.small_image_id
62
- assert_equal campaign.organisation_formatted_name, new_campaign.organisation_formatted_name
63
- assert_equal campaign.organisation_url, new_campaign.organisation_url
64
- assert_equal campaign.organisation_brand_colour, new_campaign.organisation_brand_colour
65
- assert_equal campaign.organisation_crest, new_campaign.organisation_crest
66
- end
67
-
68
- should "be not valid with an organisation brand colour from outside the list" do
69
- campaign = FactoryGirl.build(:campaign_edition, :panopticon_id => @artefact.id)
70
- campaign.organisation_brand_colour = "something-else"
71
-
72
- refute campaign.valid?
73
- assert campaign.errors.has_key?(:organisation_brand_colour)
74
- end
75
-
76
- should "be not valid with an organisation crest from outside the list" do
77
- campaign = FactoryGirl.build(:campaign_edition, :panopticon_id => @artefact.id)
78
- campaign.organisation_crest = "something-else"
79
-
80
- refute campaign.valid?
81
- assert campaign.errors.has_key?(:organisation_crest)
82
- end
83
-
84
- should "be valid with a blank organisation crest and brand colour" do
85
- campaign = FactoryGirl.build(:campaign_edition, :panopticon_id => @artefact.id)
86
- campaign.organisation_crest = ''
87
- campaign.organisation_brand_colour = ''
88
-
89
- assert campaign.valid?
90
- end
91
- end
@@ -1,56 +0,0 @@
1
- require "test_helper"
2
-
3
- class CompletedTransactionEditionTest < ActiveSupport::TestCase
4
- test "controls whether organ donor registration promotion should be displayed on a completed transaction page" do
5
- completed_transaction_edition = FactoryGirl.create(:completed_transaction_edition)
6
- refute completed_transaction_edition.promotes_something?
7
-
8
- completed_transaction_edition.promotion_choice = "organ_donor"
9
- completed_transaction_edition.promotion_choice_url = "https://www.organdonation.nhs.uk/registration/"
10
-
11
- completed_transaction_edition.save!
12
- assert completed_transaction_edition.reload.promotes_something?
13
-
14
- completed_transaction_edition.promotion_choice = ''
15
- completed_transaction_edition.save!
16
- refute completed_transaction_edition.reload.promotes_something?
17
- end
18
-
19
- test "invalid if promotion_choice_url is not specified when a promotion choice is made" do
20
- completed_transaction_edition = FactoryGirl.build(:completed_transaction_edition,
21
- promotion_choice: 'organ_donor', promotion_choice_url: "")
22
-
23
- assert completed_transaction_edition.invalid?
24
- assert_includes completed_transaction_edition.errors[:promotion_choice_url], "can't be blank"
25
- end
26
-
27
- test "invalid if promotion_choice is not one of the allowed ones" do
28
- completed_transaction_edition = FactoryGirl.build(:completed_transaction_edition, promotion_choice: 'cheese')
29
-
30
- assert completed_transaction_edition.invalid?
31
- assert_includes completed_transaction_edition.errors[:promotion_choice], "is not included in the list"
32
- end
33
-
34
- test "stores promotion choice and URL" do
35
- completed_transaction_edition = FactoryGirl.build(:completed_transaction_edition)
36
-
37
- completed_transaction_edition.promotion_choice = "none"
38
- completed_transaction_edition.save!
39
-
40
- assert_equal "none", completed_transaction_edition.reload.promotion_choice
41
-
42
- completed_transaction_edition.promotion_choice = "organ_donor"
43
- completed_transaction_edition.promotion_choice_url = "https://www.organdonation.nhs.uk/registration/"
44
- completed_transaction_edition.save!
45
-
46
- assert_equal "organ_donor", completed_transaction_edition.reload.promotion_choice
47
- assert_equal "https://www.organdonation.nhs.uk/registration/", completed_transaction_edition.promotion_choice_url
48
-
49
- completed_transaction_edition.promotion_choice = "register_to_vote"
50
- completed_transaction_edition.promotion_choice_url = "https://www.gov.uk/register-to-vote"
51
- completed_transaction_edition.save!
52
-
53
- assert_equal "register_to_vote", completed_transaction_edition.reload.promotion_choice
54
- assert_equal "https://www.gov.uk/register-to-vote", completed_transaction_edition.promotion_choice_url
55
- end
56
- end
@@ -1,93 +0,0 @@
1
- require "test_helper"
2
-
3
- class DowntimeTest < ActiveSupport::TestCase
4
- context "validations" do
5
- should "validate presence of message" do
6
- downtime = FactoryGirl.build(:downtime, message: nil)
7
-
8
- refute downtime.valid?
9
- assert_includes downtime.errors[:message], "can't be blank"
10
- end
11
-
12
- should "validate presence of start time" do
13
- downtime = FactoryGirl.build(:downtime, start_time: nil)
14
-
15
- refute downtime.valid?
16
- assert_includes downtime.errors[:start_time], "can't be blank"
17
- end
18
-
19
- should "validate presence of end time" do
20
- downtime = FactoryGirl.build(:downtime, end_time: nil)
21
-
22
- refute downtime.valid?
23
- assert_includes downtime.errors[:end_time], "can't be blank"
24
- end
25
-
26
- should "validate presence of artefact" do
27
- downtime = FactoryGirl.build(:downtime, artefact: nil)
28
-
29
- refute downtime.valid?
30
- assert_includes downtime.errors[:artefact], "can't be blank"
31
- end
32
-
33
- should "validate end time is in future" do
34
- downtime = FactoryGirl.build(:downtime, end_time: Date.today - 1)
35
-
36
- refute downtime.valid?
37
- assert_includes downtime.errors[:end_time], 'must be in the future'
38
- end
39
-
40
- should "validate end time is in future only on create" do
41
- downtime = FactoryGirl.create(:downtime)
42
- downtime.assign_attributes(start_time: Date.today - 3, end_time: Date.today - 1)
43
-
44
- assert downtime.valid?
45
- end
46
-
47
- should "validate start time is earlier than end time" do
48
- downtime = FactoryGirl.build(:downtime, start_time: Date.today + 2, end_time: Date.today + 1)
49
-
50
- refute downtime.valid?
51
- assert_includes downtime.errors[:start_time], "must be earlier than end time"
52
- end
53
- end
54
-
55
- context "for an artefact" do
56
- should "be returned if found" do
57
- downtime = FactoryGirl.create(:downtime)
58
- assert_equal downtime, Downtime.for(downtime.artefact)
59
- end
60
-
61
- should "be nil if not found" do
62
- assert_nil Downtime.for(FactoryGirl.build(:artefact))
63
- end
64
- end
65
-
66
- context "publicising downtime" do
67
- should "start at midnight a day before it is scheduled" do
68
- now = Time.zone.parse("2015-01-01")
69
- Timecop.freeze(now) do
70
- downtime = FactoryGirl.build(:downtime)
71
-
72
- downtime.start_time = Time.zone.parse("2015-01-02 03:00")
73
- assert downtime.publicise?
74
-
75
- downtime.start_time = Time.zone.parse("2015-01-01 21:00")
76
- assert downtime.publicise?
77
-
78
- downtime.start_time = Time.zone.parse("2015-01-03 00:00")
79
- refute downtime.publicise?
80
- end
81
- end
82
-
83
- should "stop after scheduled end time" do
84
- Timecop.freeze(Time.zone.now + 10) do
85
- downtime = FactoryGirl.build(:downtime)
86
-
87
- downtime.end_time = Time.zone.now - 1.minute
88
- refute downtime.publicise?
89
- end
90
- end
91
- end
92
-
93
- end
@@ -1,91 +0,0 @@
1
- require "test_helper"
2
-
3
- class EditionScheduledForPublishingTest < ActiveSupport::TestCase
4
- context "#schedule_for_publishing" do
5
- context "when publish_at is not specified" do
6
- setup do
7
- @edition = FactoryGirl.create(:edition, state: 'ready')
8
- @edition.schedule_for_publishing
9
- end
10
-
11
- should "return an error" do
12
- assert_includes @edition.errors[:publish_at], "can't be blank"
13
- end
14
-
15
- should "not complete the transition to scheduled_for_publishing" do
16
- assert_equal 'ready', @edition.state
17
- end
18
- end
19
-
20
- context "when publish_at is specified" do
21
- setup do
22
- @edition = FactoryGirl.create(:edition, state: 'ready')
23
- @publish_when = 1.day.from_now
24
- @edition.schedule_for_publishing(@publish_when)
25
- @edition.reload
26
- end
27
-
28
- should "save publish_at against the edition" do
29
- assert_equal @publish_when.to_i, @edition.publish_at.to_i
30
- end
31
-
32
- should "complete the transition to scheduled_for_publishing" do
33
- assert_equal 'scheduled_for_publishing', @edition.state
34
- end
35
- end
36
-
37
- should "not allow scheduling at a time in the past" do
38
- edition = FactoryGirl.create(:edition, state: 'ready')
39
-
40
- edition.schedule_for_publishing(1.hour.ago)
41
-
42
- assert_includes edition.errors[:publish_at], "can't be a time in the past"
43
- end
44
- end
45
-
46
- context "when scheduled_for_publishing" do
47
- should "not allow editing fields like title" do
48
- edition = FactoryGirl.create(:edition, :scheduled_for_publishing)
49
-
50
- edition.title = 'a new title'
51
-
52
- refute edition.valid?
53
- assert_includes edition.errors.full_messages, "Editions scheduled for publishing can't be edited"
54
- end
55
-
56
- should "return false for #can_destroy?" do
57
- edition = FactoryGirl.build(:edition, :scheduled_for_publishing)
58
- refute edition.can_destroy?
59
- end
60
-
61
- should "return false for #can_create_new_edition?" do
62
- edition = FactoryGirl.build(:edition, :scheduled_for_publishing)
63
- refute edition.can_create_new_edition?
64
- end
65
-
66
- should "allow transition to published state" do
67
- edition = FactoryGirl.build(:edition, :scheduled_for_publishing)
68
- assert edition.can_publish?
69
- end
70
- end
71
-
72
- context "#cancel_scheduled_publishing" do
73
- should "remove the publish_at stored against the edition and transition back to ready" do
74
- edition = FactoryGirl.create(:edition, :scheduled_for_publishing)
75
- edition.cancel_scheduled_publishing
76
- edition.reload
77
-
78
- assert_nil edition.publish_at
79
- assert_equal 'ready', edition.state
80
- end
81
-
82
- should "work with editions that have passed publish_at time" do
83
- edition = FactoryGirl.create(:edition, :scheduled_for_publishing)
84
- edition.update_attribute :publish_at, 2.days.ago
85
-
86
- edition.cancel_scheduled_publishing
87
-
88
- assert_equal 'ready', edition.reload.state
89
- end
90
- end
91
- end
@@ -1,1185 +0,0 @@
1
- require "test_helper"
2
-
3
- class Edition
4
- def update_in_search_index
5
- end
6
- end
7
-
8
- class EditionTest < ActiveSupport::TestCase
9
- def setup
10
- @artefact = FactoryGirl.create(:artefact)
11
- end
12
-
13
- def template_answer(version_number = 1)
14
- artefact = FactoryGirl.create(:artefact,
15
- kind: "answer",
16
- name: "Foo bar",
17
- owning_app: "publisher")
18
-
19
- AnswerEdition.create(state: "ready", slug: "childcare", panopticon_id: artefact.id,
20
- title: "Child care stuff", body: "Lots of info", version_number: version_number)
21
- end
22
-
23
- def template_published_answer(version_number = 1)
24
- answer = template_answer(version_number)
25
- answer.publish!
26
- answer.save!
27
- answer
28
- end
29
-
30
- def template_transaction
31
- artefact = FactoryGirl.create(:artefact)
32
- TransactionEdition.create(title: "One", introduction: "introduction",
33
- more_information: "more info", panopticon_id: @artefact.id, slug: "childcare")
34
- end
35
-
36
- def template_unpublished_answer(version_number = 1)
37
- template_answer(version_number)
38
- end
39
-
40
- def draft_second_edition_from(published_edition)
41
- published_edition.build_clone(AnswerEdition).tap { |edition|
42
- edition.body = "Test Body 2"
43
- edition.save
44
- edition.reload
45
- }
46
- end
47
-
48
- test "it must have a title" do
49
- a = LocalTransactionEdition.new
50
- refute a.valid?
51
- assert a.errors[:title].any?
52
- end
53
-
54
- test "it is not in beta by default" do
55
- refute FactoryGirl.build(:guide_edition).in_beta?
56
- end
57
-
58
- test "it can be in beta" do
59
- assert FactoryGirl.build(:guide_edition, in_beta: true).in_beta?
60
- end
61
-
62
- test "it should give a friendly (legacy supporting) description of its format" do
63
- a = LocalTransactionEdition.new
64
- assert_equal "LocalTransaction", a.format
65
- end
66
-
67
- test "it should be able to find its siblings" do
68
- artefact2 = FactoryGirl.create(:artefact)
69
- g1 = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, version_number: 1)
70
- g2 = FactoryGirl.create(:guide_edition, panopticon_id: artefact2.id, version_number: 1)
71
- g3 = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, version_number: 2)
72
- assert_equal [], g2.siblings.to_a
73
- assert_equal [g3], g1.siblings.to_a
74
- end
75
-
76
- test "it should be able to find its previous siblings" do
77
- artefact2 = FactoryGirl.create(:artefact)
78
- g1 = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, version_number: 1)
79
- g2 = FactoryGirl.create(:guide_edition, panopticon_id: artefact2.id, version_number: 1)
80
- g3 = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, version_number: 2)
81
-
82
- assert_equal [], g1.previous_siblings.to_a
83
- assert_equal [g1], g3.previous_siblings.to_a
84
- end
85
-
86
- test "subsequent and previous siblings are in order" do
87
- g4 = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, version_number: 4)
88
- g2 = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, version_number: 2)
89
- g1 = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, version_number: 1)
90
- g3 = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, version_number: 3)
91
-
92
- assert_equal [g2, g3, g4], g1.subsequent_siblings.to_a
93
- assert_equal [g1, g2, g3], g4.previous_siblings.to_a
94
- end
95
-
96
- test "A programme should have default parts" do
97
- programme = FactoryGirl.create(:programme_edition, panopticon_id: @artefact.id)
98
- assert_equal programme.parts.count, ProgrammeEdition::DEFAULT_PARTS.length
99
- end
100
-
101
- context "link validation" do
102
- should "not be done when the edition is created" do
103
- assert_difference 'AnswerEdition.count', 1 do
104
- FactoryGirl.create(:answer_edition, body: 'abc [foobar](http://foobar.com "hover")')
105
- end
106
- end
107
-
108
- should "be done when an existing edition is updated" do
109
- edition = FactoryGirl.create(:answer_edition, body: 'abc [foobar](http://foobar.com "hover")')
110
-
111
- edition.body += "some update"
112
-
113
- refute edition.valid?
114
- assert_includes edition.errors.full_messages, %q<Body ["Don't include hover text in links. Delete the text in quotation marks eg \\"This appears when you hover over the link.\\""]>
115
- end
116
-
117
- should "allow archiving an edition with invalid links" do
118
- edition = FactoryGirl.create(:answer_edition, state: 'published', body: 'abc [foobar](http://foobar.com "hover")')
119
-
120
- assert_difference 'AnswerEdition.archived.count', 1 do
121
- edition.archive!
122
- end
123
- end
124
- end
125
-
126
- context "change note" do
127
- should "be a minor change by default" do
128
- refute AnswerEdition.new.major_change
129
- end
130
- should "not be valid for major changes with a blank change note" do
131
- edition = AnswerEdition.new(major_change: true, change_note: "")
132
- refute edition.valid?
133
- assert edition.errors.has_key?(:change_note)
134
- end
135
- should "be valid for major changes with a change note" do
136
- edition = AnswerEdition.new(title: "Edition", version_number: 1, panopticon_id: 123, major_change: true, change_note: "Changed")
137
- assert edition.valid?
138
- end
139
- should "be valid when blank for minor changes" do
140
- edition = AnswerEdition.new(title: "Edition", version_number: 1, panopticon_id: 123, change_note: "")
141
- assert edition.valid?
142
- end
143
- should "be valid when populated for minor changes" do
144
- edition = AnswerEdition.new(title: "Edition", version_number: 1, panopticon_id: 123, change_note: "Changed")
145
- assert edition.valid?
146
- end
147
- end
148
-
149
- test "reviewer cannot be the assignee" do
150
- user = FactoryGirl.create(:user)
151
- edition = AnswerEdition.new(title: "Edition", version_number: 1, panopticon_id: 123,
152
- state: "in_review", review_requested_at: Time.zone.now, assigned_to: user)
153
- edition.reviewer = user.name
154
- refute edition.valid?
155
- assert edition.errors.has_key?(:reviewer)
156
- end
157
-
158
- test "it should build a clone" do
159
- edition = FactoryGirl.create(:guide_edition,
160
- state: "published",
161
- panopticon_id: @artefact.id,
162
- version_number: 1,
163
- overview: "I am a test overview")
164
- clone_edition = edition.build_clone
165
- assert_equal "I am a test overview", clone_edition.overview
166
- assert_equal 2, clone_edition.version_number
167
- end
168
-
169
- test "cloning can only occur from a published edition" do
170
- edition = FactoryGirl.create(:guide_edition,
171
- panopticon_id: @artefact.id,
172
- version_number: 1)
173
- assert_raise(RuntimeError) do
174
- edition.build_clone
175
- end
176
- end
177
-
178
- test "cloning can only occur from a published edition with no subsequent in progress siblings" do
179
- edition = FactoryGirl.create(:guide_edition,
180
- panopticon_id: @artefact.id,
181
- state: "published",
182
- version_number: 1)
183
-
184
- FactoryGirl.create(:guide_edition,
185
- panopticon_id: @artefact.id,
186
- state: "draft",
187
- version_number: 2)
188
-
189
- assert_raise(RuntimeError) do
190
- edition.build_clone
191
- end
192
- end
193
-
194
- test "cloning from an earlier edition should give you a safe version number" do
195
- edition = FactoryGirl.create(:guide_edition,
196
- state: "published",
197
- panopticon_id: @artefact.id,
198
- version_number: 1)
199
- edition_two = FactoryGirl.create(:guide_edition,
200
- state: "published",
201
- panopticon_id: @artefact.id,
202
- version_number: 2)
203
-
204
- clone1 = edition.build_clone
205
- assert_equal 3, clone1.version_number
206
- end
207
-
208
- # test cloning into different edition types
209
- Edition.subclasses.permutation(2).each do |source_class, destination_class|
210
- test "it should be possible to clone from a #{source_class} to a #{destination_class}" do
211
- # Note that the new edition won't necessarily be valid - for example the
212
- # new type might have required fields that the old just doesn't have.
213
- # This is OK because when Publisher saves the clone, it already skips
214
- # validations. The user will then be required to populate those values
215
- # before they save the edition again.
216
- source_edition = FactoryGirl.create(:edition, _type: source_class.to_s, state: "published")
217
-
218
- assert_nothing_raised do
219
- new_edition = source_edition.build_clone(destination_class)
220
- end
221
- end
222
- end
223
-
224
- test "Cloning from GuideEdition into AnswerEdition" do
225
- edition = FactoryGirl.create(
226
- :guide_edition,
227
- state: "published",
228
- panopticon_id: @artefact.id,
229
- version_number: 1,
230
- overview: "I am a test overview",
231
- video_url: "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
232
- )
233
- new_edition = edition.build_clone AnswerEdition
234
-
235
- assert_equal AnswerEdition, new_edition.class
236
- assert_equal 2, new_edition.version_number
237
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
238
- assert_equal "draft", new_edition.state
239
- assert_equal "I am a test overview", new_edition.overview
240
- assert_equal edition.whole_body, new_edition.whole_body
241
- end
242
-
243
- test "Cloning from LicenceEdition into AnswerEdition" do
244
- edition = FactoryGirl.create(
245
- :licence_edition,
246
- state: "published",
247
- panopticon_id: @artefact.id,
248
- version_number: 1,
249
- licence_overview: "I am a test overview",
250
- licence_identifier: "Test identifier",
251
- licence_short_description: "I am a test short description",
252
- will_continue_on: "test will continue on",
253
- continuation_link: "https://github.com/alphagov/"
254
- )
255
- new_edition = edition.build_clone AnswerEdition
256
-
257
- assert_equal AnswerEdition, new_edition.class
258
- assert_equal 2, new_edition.version_number
259
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
260
- assert_equal "draft", new_edition.state
261
- assert_match /#{edition.licence_overview}/, new_edition.body
262
- assert_match /#{edition.licence_short_description}/, new_edition.body
263
- assert_equal edition.whole_body, new_edition.body
264
- end
265
-
266
- test "Cloning from ProgrammeEdition into AnswerEdition" do
267
- edition = FactoryGirl.create(
268
- :programme_edition,
269
- state: "published",
270
- panopticon_id: @artefact.id,
271
- version_number: 1,
272
- overview: "I am a test overview"
273
- )
274
- new_edition = edition.build_clone AnswerEdition
275
-
276
- assert_equal AnswerEdition, new_edition.class
277
- assert_equal 2, new_edition.version_number
278
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
279
- assert_equal "draft", new_edition.state
280
- assert_equal "I am a test overview", new_edition.overview
281
- assert_equal edition.whole_body, new_edition.whole_body
282
- end
283
-
284
- test "Cloning from TransactionEdition into AnswerEdition" do
285
- edition = FactoryGirl.create(
286
- :transaction_edition,
287
- state: "published",
288
- panopticon_id: @artefact.id,
289
- version_number: 1,
290
- overview: "I am a test overview",
291
- more_information: "More information",
292
- alternate_methods: "Alternate methods"
293
- )
294
- new_edition = edition.build_clone AnswerEdition
295
-
296
- assert_equal AnswerEdition, new_edition.class
297
- assert_equal 2, new_edition.version_number
298
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
299
- assert_equal "draft", new_edition.state
300
- assert_equal "I am a test overview", new_edition.overview
301
- assert_equal edition.whole_body, new_edition.whole_body
302
- end
303
-
304
- test "Cloning from SimpleSmartAnswerEdition into AnswerEdition" do
305
- edition = FactoryGirl.create(
306
- :simple_smart_answer_edition,
307
- state: "published",
308
- panopticon_id: @artefact.id,
309
- version_number: 1,
310
- overview: "I am a test overview",
311
- )
312
- new_edition = edition.build_clone AnswerEdition
313
-
314
- assert_equal AnswerEdition, new_edition.class
315
- assert_equal 2, new_edition.version_number
316
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
317
- assert_equal "draft", new_edition.state
318
- assert_equal "I am a test overview", new_edition.overview
319
- assert_equal edition.whole_body, new_edition.whole_body
320
- end
321
-
322
- test "Cloning from AnswerEdition into TransactionEdition" do
323
- edition = FactoryGirl.create(
324
- :answer_edition,
325
- state: "published",
326
- panopticon_id: @artefact.id,
327
- version_number: 1,
328
- overview: "I am a test overview",
329
- body: "Test body"
330
- )
331
- new_edition = edition.build_clone TransactionEdition
332
-
333
- assert_equal TransactionEdition, new_edition.class
334
- assert_equal 2, new_edition.version_number
335
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
336
- assert_equal "draft", new_edition.state
337
- assert_equal "I am a test overview", new_edition.overview
338
- assert_equal "Test body", new_edition.more_information
339
- end
340
-
341
- test "Cloning from AnswerEdition into SimpleSmartAnswerEdition" do
342
- edition = FactoryGirl.create(
343
- :answer_edition,
344
- state: "published",
345
- panopticon_id: @artefact.id,
346
- version_number: 1,
347
- overview: "I am a test overview",
348
- body: "Test body"
349
- )
350
- new_edition = edition.build_clone SimpleSmartAnswerEdition
351
-
352
- assert_equal SimpleSmartAnswerEdition, new_edition.class
353
- assert_equal 2, new_edition.version_number
354
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
355
- assert_equal "draft", new_edition.state
356
- assert_equal "I am a test overview", new_edition.overview
357
- assert_equal "Test body", new_edition.body
358
- end
359
-
360
- test "Cloning from GuideEdition into TransactionEdition" do
361
- edition = FactoryGirl.create(
362
- :guide_edition,
363
- state: "published",
364
- panopticon_id: @artefact.id,
365
- version_number: 1,
366
- overview: "I am a test overview",
367
- video_url: "http://www.youtube.com/watch?v=dQw4w9WgXcQ"
368
- )
369
- new_edition = edition.build_clone TransactionEdition
370
-
371
- assert_equal TransactionEdition, new_edition.class
372
- assert_equal 2, new_edition.version_number
373
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
374
- assert_equal "draft", new_edition.state
375
- assert_equal "I am a test overview", new_edition.overview
376
- assert_equal edition.whole_body, new_edition.more_information
377
- end
378
-
379
- test "Cloning from ProgrammeEdition into TransactionEdition" do
380
- edition = FactoryGirl.create(
381
- :programme_edition,
382
- state: "published",
383
- panopticon_id: @artefact.id,
384
- version_number: 1,
385
- overview: "I am a test overview",
386
- )
387
- new_edition = edition.build_clone TransactionEdition
388
-
389
- assert_equal TransactionEdition, new_edition.class
390
- assert_equal 2, new_edition.version_number
391
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
392
- assert_equal "draft", new_edition.state
393
- assert_equal "I am a test overview", new_edition.overview
394
- assert_equal edition.whole_body, new_edition.more_information
395
- end
396
-
397
- test "Cloning from AnswerEdition into GuideEdition" do
398
- edition = FactoryGirl.create(
399
- :answer_edition,
400
- state: "published",
401
- panopticon_id: @artefact.id,
402
- version_number: 1,
403
- overview: "I am a test overview",
404
- )
405
- new_edition = edition.build_clone GuideEdition
406
-
407
- assert_equal GuideEdition, new_edition.class
408
- assert_equal 2, new_edition.version_number
409
- assert_equal @artefact.id.to_s, new_edition.panopticon_id
410
- assert_equal "draft", new_edition.state
411
- assert_equal "I am a test overview", new_edition.overview
412
- end
413
-
414
- test "Cloning between types with parts" do
415
- edition = FactoryGirl.create(:programme_edition_with_multiple_parts,
416
- panopticon_id: @artefact.id,
417
- state: "published",
418
- version_number: 1,
419
- overview: "I am a shiny programme",
420
- )
421
- new_edition = edition.build_clone GuideEdition
422
-
423
- assert_equal(new_edition.parts.map {|part| part.title },
424
- edition.parts.map {|part| part.title })
425
- assert_equal 7, new_edition.parts.size #there are 5 'default' parts plus an additional two created by the factory
426
- end
427
-
428
- test "knows the common fields of two edition subclasses" do
429
- to_copy = Set.new([:introduction, :need_to_know, :more_information])
430
- result = Set.new(TransactionEdition.new.fields_to_copy(PlaceEdition))
431
- assert to_copy.proper_subset?(result)
432
- end
433
-
434
- # Mongoid 2.x marks array fields as dirty whenever they are accessed.
435
- # See https://github.com/mongoid/mongoid/issues/2311
436
- # This behaviour has been patched in lib/mongoid/monkey_patches.rb
437
- # in order to prevent workflow validation failures for editions
438
- # with array fields.
439
- #
440
- test "editions with array fields should accurately track changes" do
441
- bs = FactoryGirl.create(:business_support_edition, sectors: [])
442
- assert_empty bs.changes
443
- bs.sectors
444
- assert_empty bs.changes
445
- bs.sectors << 'manufacturing'
446
- assert_equal ['sectors'], bs.changes.keys
447
- end
448
-
449
- test "edition finder should return the published edition when given an empty edition parameter" do
450
- dummy_publication = template_published_answer
451
- second_publication = template_unpublished_answer(2)
452
-
453
- assert dummy_publication.published?
454
- assert_equal dummy_publication, Edition.find_and_identify("childcare", "")
455
- end
456
-
457
- test "edition finder should return the latest edition when asked" do
458
- dummy_publication = template_published_answer
459
- second_publication = template_unpublished_answer(2)
460
-
461
- assert_equal 2, Edition.where(slug: dummy_publication.slug).count
462
- found_edition = Edition.find_and_identify("childcare", "latest")
463
- assert_equal second_publication.version_number, found_edition.version_number
464
- end
465
-
466
- test "a publication should not have a video" do
467
- dummy_publication = template_published_answer
468
- assert !dummy_publication.has_video?
469
- end
470
-
471
- test "should create a publication based on data imported from panopticon" do
472
- artefact = FactoryGirl.create(:artefact,
473
- slug: "foo-bar",
474
- kind: "answer",
475
- name: "Foo bar",
476
- owning_app: "publisher",
477
- )
478
- artefact.save!
479
-
480
- a = Artefact.find(artefact.id)
481
- user = User.create
482
-
483
- publication = Edition.find_or_create_from_panopticon_data(artefact.id, user)
484
-
485
- assert_kind_of AnswerEdition, publication
486
- assert_equal artefact.name, publication.title
487
- assert_equal artefact.id.to_s, publication.panopticon_id.to_s
488
- end
489
-
490
- test "should not change edition metadata if archived" do
491
- artefact = FactoryGirl.create(:artefact,
492
- slug: "foo-bar",
493
- kind: "answer",
494
- name: "Foo bar",
495
- owning_app: "publisher",
496
- )
497
-
498
- guide = FactoryGirl.create(:guide_edition,
499
- panopticon_id: artefact.id,
500
- title: "Original title",
501
- slug: "original-title",
502
- state: "archived"
503
- )
504
- artefact.slug = "new-slug"
505
- artefact.save
506
-
507
- assert_not_equal "new-slug", guide.reload.slug
508
- end
509
-
510
- test "should scope publications by assignee" do
511
- stub_request(:get, %r{http://panopticon\.test\.gov\.uk/artefacts/.*\.js}).
512
- to_return(status: 200, body: "{}", headers: {})
513
-
514
- a, b = 2.times.map { FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id) }
515
-
516
- alice, bob, charlie = %w[ alice bob charlie ].map { |s|
517
- FactoryGirl.create(:user, name: s)
518
- }
519
- alice.assign(a, bob)
520
- alice.assign(a, charlie)
521
- alice.assign(b, bob)
522
-
523
- assert_equal [b], Edition.assigned_to(bob).to_a
524
- end
525
-
526
- test "cannot delete a publication that has been published" do
527
- dummy_answer = template_published_answer
528
- loaded_answer = AnswerEdition.where(slug: "childcare").first
529
-
530
- assert_equal loaded_answer, dummy_answer
531
- assert ! dummy_answer.can_destroy?
532
- assert_raise (Workflow::CannotDeletePublishedPublication) do
533
- dummy_answer.destroy
534
- end
535
- end
536
-
537
- test "cannot delete a published publication with a new draft edition" do
538
- dummy_answer = template_published_answer
539
-
540
- new_edition = dummy_answer.build_clone
541
- new_edition.body = "Two"
542
- dummy_answer.save
543
-
544
- assert_raise (Edition::CannotDeletePublishedPublication) do
545
- dummy_answer.destroy
546
- end
547
- end
548
-
549
- test "can delete a publication that has not been published" do
550
- dummy_answer = template_unpublished_answer
551
- dummy_answer.destroy
552
-
553
- loaded_answer = AnswerEdition.where(slug: dummy_answer.slug).first
554
- assert_nil loaded_answer
555
- end
556
-
557
- test "deleting a newer draft of a published edition removes sibling information" do
558
- user1 = FactoryGirl.create(:user)
559
- edition = AnswerEdition.find_or_create_from_panopticon_data(@artefact.id, user1)
560
- edition.update_attribute(:state, "published")
561
- second_edition = edition.build_clone
562
- second_edition.save!
563
- edition.reload
564
-
565
- assert edition.sibling_in_progress
566
-
567
- second_edition.destroy
568
- edition.reload
569
-
570
- assert_nil edition.sibling_in_progress
571
- end
572
-
573
- test "the latest edition should remove sibling_in_progress details if it is present" do
574
- user1 = FactoryGirl.create(:user)
575
- edition = AnswerEdition.find_or_create_from_panopticon_data(@artefact.id, user1)
576
- edition.update_attribute(:state, "published")
577
-
578
- # simulate a document having a newer edition destroyed (previous behaviour).
579
- edition.sibling_in_progress = 2
580
- edition.save(validate: false)
581
-
582
- assert edition.can_create_new_edition?
583
- end
584
-
585
- test "should also delete associated artefact" do
586
- user1 = FactoryGirl.create(:user)
587
- edition = AnswerEdition.find_or_create_from_panopticon_data(@artefact.id, user1)
588
-
589
- assert_difference "Artefact.count", -1 do
590
- edition.destroy
591
- end
592
- end
593
-
594
- test "should not delete associated artefact if there are other editions of this publication" do
595
- user1 = FactoryGirl.create(:user)
596
- edition = AnswerEdition.find_or_create_from_panopticon_data(@artefact.id, user1)
597
- edition.update_attribute(:state, "published")
598
-
599
- edition.reload
600
- second_edition = edition.build_clone
601
- second_edition.save!
602
-
603
- assert_no_difference "Artefact.count" do
604
- second_edition.destroy
605
- end
606
- end
607
-
608
- test "should scope publications assigned to nobody" do
609
- stub_request(:get, %r{http://panopticon\.test\.gov\.uk/artefacts/.*\.js}).
610
- to_return(status: 200, body: "{}", headers: {})
611
-
612
- a, b = 2.times.map { |i| FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id) }
613
-
614
- alice, bob, charlie = %w[ alice bob charlie ].map { |s|
615
- FactoryGirl.create(:user, name: s)
616
- }
617
-
618
- alice.assign(a, bob)
619
- a.reload
620
- assert_equal bob, a.assigned_to
621
-
622
- alice.assign(a, charlie)
623
- a.reload
624
- assert_equal charlie, a.assigned_to
625
-
626
- assert_equal 2, Edition.count
627
- assert_equal [b], Edition.assigned_to(nil).to_a
628
- assert_equal [], Edition.assigned_to(bob).to_a
629
- assert_equal [a], Edition.assigned_to(charlie).to_a
630
- end
631
-
632
- test "given multiple editions, can return the most recent published edition" do
633
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, slug: "hedgehog-topiary", state: "published")
634
-
635
- second_edition = edition.build_clone
636
- edition.update_attribute(:state, "archived")
637
- second_edition.update_attribute(:state, "published")
638
-
639
- third_edition = second_edition.build_clone
640
- third_edition.update_attribute(:state, "draft")
641
-
642
- assert_equal edition.published_edition, second_edition
643
- end
644
-
645
- test "editions, by default, return their title for use in the admin-interface lists of publications" do
646
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
647
- assert_equal edition.title, edition.admin_list_title
648
- end
649
-
650
- test "editions can have notes stored for the history tab" do
651
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
652
- user = User.new
653
- assert edition.new_action(user, "note", comment: "Something important")
654
- end
655
-
656
- test "status should not be affected by notes" do
657
- user = User.create(name: "bob")
658
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
659
- edition.new_action(user, Action::APPROVE_REVIEW)
660
- edition.new_action(user, Action::NOTE, comment: "Something important")
661
-
662
- assert_equal Action::APPROVE_REVIEW, edition.latest_status_action.request_type
663
- end
664
-
665
- test "should have no assignee by default" do
666
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
667
- assert_nil edition.assigned_to
668
- end
669
-
670
- test "should be assigned to the last assigned recipient" do
671
- alice = FactoryGirl.create(:user, name: "alice")
672
- bob = FactoryGirl.create(:user, name: "bob")
673
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
674
- alice.assign(edition, bob)
675
- assert_equal bob, edition.assigned_to
676
- end
677
-
678
- test "new edition should have an incremented version number" do
679
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "published")
680
- new_edition = edition.build_clone
681
- assert_equal edition.version_number + 1, new_edition.version_number
682
- end
683
-
684
- test "new edition should have an empty list of actions" do
685
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "published")
686
- new_edition = edition.build_clone
687
- assert_equal [], new_edition.actions
688
- end
689
-
690
- test "new editions should have the same text when created" do
691
- edition = FactoryGirl.create(:guide_edition_with_two_parts, panopticon_id: @artefact.id, state: "published")
692
- new_edition = edition.build_clone
693
- original_text = edition.parts.map {|p| p.body }.join(" ")
694
- new_text = new_edition.parts.map {|p| p.body }.join(" ")
695
- assert_equal original_text, new_text
696
- end
697
-
698
- test "changing text in a new edition should not change text in old edition" do
699
- edition = FactoryGirl.create(:guide_edition_with_two_parts, panopticon_id: @artefact.id, state: "published")
700
- new_edition = edition.build_clone
701
- new_edition.parts.first.body = "Some other version text"
702
- original_text = edition.parts.map {|p| p.body }.join(" ")
703
- new_text = new_edition.parts.map {|p| p.body }.join(" ")
704
- assert_not_equal original_text, new_text
705
- end
706
-
707
- test "a new guide has no published edition" do
708
- guide = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
709
- assert_nil GuideEdition.where(state: "published", panopticon_id: guide.panopticon_id).first
710
- end
711
-
712
- test "an edition of a guide can be published" do
713
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
714
- edition.publish
715
- assert_not_nil GuideEdition.where(state: "published", panopticon_id: edition.panopticon_id).first
716
- end
717
-
718
- test "should archive older editions, even if there are validation errors, when a new edition is published" do
719
- edition = FactoryGirl.create(:guide_edition_with_two_parts, panopticon_id: @artefact.id, state: "ready")
720
-
721
- user = User.create name: "bob"
722
- publish(user, edition, "First publication")
723
-
724
- second_edition = edition.build_clone
725
- second_edition.state = "ready"
726
- second_edition.save!
727
-
728
- publish(user, second_edition, "Second publication")
729
-
730
- # simulate link validation errors in published edition
731
- second_edition.parts.first.update_attribute(:body, "[register your vehicle](registering-an-imported-vehicle)")
732
-
733
- third_edition = second_edition.build_clone
734
- # fix link validation error in cloned edition by appending a '/' to the relative url
735
- third_edition.parts.first.body = "[register your vehicle](/registering-an-imported-vehicle)"
736
- third_edition.state = "ready"
737
- third_edition.save!
738
-
739
- publish(user, third_edition, "Third publication")
740
-
741
- edition.reload
742
- assert edition.archived?
743
-
744
- second_edition.reload
745
- assert second_edition.archived?
746
-
747
- assert_equal 2, GuideEdition.where(panopticon_id: edition.panopticon_id, state: "archived").count
748
- end
749
-
750
- test "when an edition is published, publish_at is cleared" do
751
- user = FactoryGirl.create(:user)
752
- edition = FactoryGirl.create(:edition, :scheduled_for_publishing)
753
-
754
- publish(user, edition, "First publication")
755
-
756
- assert_nil edition.reload.publish_at
757
- end
758
-
759
- test "edition can return latest status action of a specified request type" do
760
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "draft")
761
- user = User.create(name: "George")
762
- request_review(user, edition)
763
-
764
- assert_equal edition.actions.size, 1
765
- assert edition.latest_status_action(Action::REQUEST_REVIEW).present?
766
- end
767
-
768
- test "a published edition can't be edited" do
769
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "published")
770
- edition.title = "My New Title"
771
-
772
- assert ! edition.save
773
- assert_equal ["Published editions can't be edited"], edition.errors[:base]
774
- end
775
-
776
- test "edition's publish history is recorded" do
777
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
778
-
779
- user = User.create name: "bob"
780
- publish(user, edition, "First publication")
781
-
782
- second_edition = edition.build_clone
783
- second_edition.update_attribute(:state, "ready")
784
- second_edition.save!
785
- publish(user, second_edition, "Second publication")
786
-
787
- third_edition = second_edition.build_clone
788
- third_edition.update_attribute(:state, "ready")
789
- third_edition.save!
790
- publish(user, third_edition, "Third publication")
791
-
792
- edition.reload
793
- assert edition.actions.where("request_type" => "publish")
794
-
795
- second_edition.reload
796
- assert second_edition.actions.where("request_type" => "publish")
797
-
798
- third_edition.reload
799
- assert third_edition.actions.where("request_type" => "publish")
800
- assert third_edition.published?
801
- end
802
-
803
-
804
- test "a series with all editions published should not have siblings in progress" do
805
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
806
-
807
- user = User.create name: "bob"
808
- publish(user, edition, "First publication")
809
-
810
- new_edition = edition.build_clone
811
- new_edition.state = "ready"
812
- new_edition.save!
813
- publish(user, new_edition, "Second publication")
814
-
815
- edition = edition.reload
816
-
817
- assert_nil edition.sibling_in_progress
818
- end
819
-
820
- test "a series with one published and one draft edition should have a sibling in progress" do
821
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
822
- edition.save!
823
-
824
- user = User.create name: "bob"
825
- publish(user, edition, "First publication")
826
-
827
- new_edition = edition.build_clone
828
- new_edition.save!
829
-
830
- edition = edition.reload
831
-
832
- assert_not_nil edition.sibling_in_progress
833
- assert_equal new_edition.version_number, edition.sibling_in_progress
834
- end
835
-
836
- test "a part's slug must be of the correct format" do
837
- edition_one = GuideEdition.new(title: "One", slug: "one", panopticon_id: @artefact.id)
838
- edition_one.parts.build title: "Part One", body:"Never gonna give you up", slug: "part-One-1"
839
- edition_one.save!
840
-
841
- edition_one.parts[0].slug = "part one"
842
- assert_raise (Mongoid::Errors::Validations) do
843
- edition_one.save!
844
- end
845
- end
846
-
847
- test "parts can be sorted by the order field using a scope" do
848
- edition = GuideEdition.new(title: "One", slug: "one", panopticon_id: @artefact.id)
849
- edition.parts.build title: "Biscuits", body:"Never gonna give you up", slug: "biscuits", order: 2
850
- edition.parts.build title: "Cookies", body:"NYAN NYAN NYAN NYAN", slug: "cookies", order: 1
851
- edition.save!
852
- edition.reload
853
-
854
- assert_equal "Cookies", edition.parts.in_order.first.title
855
- assert_equal "Biscuits", edition.parts.in_order.last.title
856
- end
857
-
858
- test "user should not be able to review an edition they requested review for" do
859
- user = User.create(name: "Mary")
860
-
861
- edition = ProgrammeEdition.create(title: "Childcare", slug: "childcare", panopticon_id: @artefact.id)
862
- assert edition.can_request_review?
863
- request_review(user, edition)
864
- refute request_amendments(user, edition)
865
- end
866
-
867
- test "a published publication with a draft edition is in progress" do
868
- dummy_answer = template_published_answer
869
- assert !dummy_answer.has_sibling_in_progress?
870
-
871
- edition = dummy_answer.build_clone
872
- edition.save
873
-
874
- dummy_answer.reload
875
- assert dummy_answer.has_sibling_in_progress?
876
- end
877
-
878
- test "a draft edition cannot be published" do
879
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "draft")
880
- refute edition.can_publish?
881
- end
882
-
883
- # test denormalisation
884
-
885
- test "should denormalise an edition with an assigned user and action requesters" do
886
- user1 = FactoryGirl.create(:user, name: "Morwenna")
887
- user2 = FactoryGirl.create(:user, name: "John")
888
- user3 = FactoryGirl.create(:user, name: "Nick")
889
-
890
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "archived")
891
-
892
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "archived", assigned_to_id: user1.id)
893
- edition.actions.create request_type: Action::CREATE, requester: user2
894
- edition.actions.create request_type: Action::PUBLISH, requester: user3
895
- edition.actions.create request_type: Action::ARCHIVE, requester: user1
896
- edition.save! and edition.reload
897
-
898
- assert_equal user1.name, edition.assignee
899
- assert_equal user2.name, edition.creator
900
- assert_equal user3.name, edition.publisher
901
- assert_equal user1.name, edition.archiver
902
- end
903
-
904
- test "should denormalise an assignee's name when an edition is assigned" do
905
- user1 = FactoryGirl.create(:user)
906
- user2 = FactoryGirl.create(:user)
907
-
908
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "draft")
909
- user1.assign edition, user2
910
-
911
- assert_equal user2, edition.assigned_to
912
- assert_equal user2.name, edition.assignee
913
- end
914
-
915
- test "should denormalise a creator's name when an edition is created" do
916
- user = FactoryGirl.create(:user)
917
- artefact = FactoryGirl.create(:artefact,
918
- slug: "foo-bar",
919
- kind: "answer",
920
- name: "Foo bar",
921
- owning_app: "publisher",
922
- )
923
-
924
- edition = AnswerEdition.find_or_create_from_panopticon_data(artefact.id, user)
925
-
926
- assert_equal user.name, edition.creator
927
- end
928
-
929
- test "should denormalise a publishing user's name when an edition is published" do
930
- user = FactoryGirl.create(:user)
931
-
932
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
933
- publish(user, edition, "First publication")
934
-
935
- assert_equal user.name, edition.publisher
936
- end
937
-
938
- test "should set siblings in progress to nil for new editions" do
939
- user = FactoryGirl.create(:user)
940
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "ready")
941
- published_edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "published")
942
- assert_equal 1, edition.version_number
943
- assert_nil edition.sibling_in_progress
944
- end
945
-
946
- test "should update previous editions when new edition is added" do
947
- user = FactoryGirl.create(:user)
948
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "archived")
949
- published_edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "published")
950
- new_edition = published_edition.build_clone
951
- new_edition.save!
952
- published_edition.reload
953
-
954
- assert_equal 3, new_edition.version_number
955
- assert_equal 3, published_edition.sibling_in_progress
956
- end
957
-
958
- test "should update previous editions when new edition is published" do
959
- user = FactoryGirl.create(:user)
960
- edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "archived")
961
- published_edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "published")
962
-
963
- new_edition = published_edition.build_clone
964
- new_edition.save!
965
- new_edition.update_attribute(:state, "ready")
966
- publish(user, new_edition, "First publication")
967
-
968
- assert_equal 3, new_edition.version_number
969
- assert_nil new_edition.sibling_in_progress
970
- assert_nil published_edition.reload.sibling_in_progress
971
- end
972
-
973
- test "all subclasses should provide a working whole_body method for diffing" do
974
- Edition.subclasses.each do |klass|
975
- assert klass.instance_methods.include?(:whole_body), "#{klass} doesn't provide a whole_body"
976
- assert_nothing_raised do
977
- klass.new.whole_body
978
- end
979
- end
980
- end
981
-
982
- test "should convert a GuideEdition to an AnswerEdition" do
983
- guide_edition = FactoryGirl.create(:guide_edition, panopticon_id: @artefact.id, state: "published")
984
- answer_edition = guide_edition.build_clone(AnswerEdition)
985
-
986
- assert_equal guide_edition.whole_body, answer_edition.whole_body
987
- end
988
-
989
- test "should convert an AnswerEdition to a GuideEdition" do
990
- answer_edition = template_published_answer
991
- guide_edition = answer_edition.build_clone(GuideEdition)
992
-
993
- expected = "# Part One\n\n" + answer_edition.whole_body
994
-
995
- assert_equal expected, guide_edition.whole_body
996
- end
997
-
998
- test "should not allow any changes to an edition with an archived artefact" do
999
- artefact = FactoryGirl.create(:artefact)
1000
- guide_edition = FactoryGirl.create(:guide_edition, state: 'draft', panopticon_id: artefact.id)
1001
- artefact.state = 'archived'
1002
- artefact.save
1003
-
1004
- assert_raise(RuntimeError) do
1005
- guide_edition.title = "Error this"
1006
- guide_edition.save!
1007
- end
1008
- end
1009
-
1010
- test "should return the artefact" do
1011
- assert_equal "Foo bar", template_published_answer.artefact.name
1012
- end
1013
-
1014
- context "validating version_number" do
1015
- should "be required" do
1016
- ed = FactoryGirl.build(:edition, :panopticon_id => @artefact.id)
1017
- ed.version_number = nil
1018
- assert ! ed.valid?, "Expected edition not to be valid with no version_number"
1019
- end
1020
-
1021
- should "be unique" do
1022
- ed1 = FactoryGirl.create(:edition, :panopticon_id => @artefact.id)
1023
- ed2 = FactoryGirl.build(:edition, :panopticon_id => @artefact.id)
1024
- ed2.version_number = ed1.version_number
1025
-
1026
- assert ! ed2.valid?, "Expected edition not to be valid with conflicting version_number"
1027
- end
1028
-
1029
- should "allow editions belonging to different artefacts to have matching version_numbers" do
1030
- ed1 = FactoryGirl.create(:edition, :panopticon_id => @artefact.id)
1031
- ed2 = FactoryGirl.build(:edition, :panopticon_id => FactoryGirl.create(:artefact).id)
1032
- ed2.version_number = ed1.version_number
1033
-
1034
- assert ed2.valid?, "Expected edition to be valid"
1035
- end
1036
-
1037
- should "have a database-level constraint on the uniqueness" do
1038
- ed1 = FactoryGirl.create(:edition, :panopticon_id => @artefact.id)
1039
- ed2 = FactoryGirl.build(:edition, :panopticon_id => @artefact.id)
1040
- ed2.version_number = ed1.version_number
1041
-
1042
- assert_raises Mongo::Error::OperationFailure do
1043
- ed2.save! validate: false
1044
- end
1045
- end
1046
- end
1047
-
1048
- context "indexable_content" do
1049
- context "editions with a 'body'" do
1050
- should "include the body with markup removed" do
1051
- edition = FactoryGirl.create(:answer_edition, body: "## Title", panopticon_id: FactoryGirl.create(:artefact).id)
1052
- assert_equal "Title", edition.indexable_content
1053
- end
1054
- end
1055
-
1056
- context "for a single part thing" do
1057
- should "have an empty string for an edition with no body" do
1058
- edition = FactoryGirl.create(:guide_edition, :state => 'ready', :title => 'one part thing', panopticon_id: FactoryGirl.create(:artefact).id)
1059
- edition.publish
1060
- assert_equal "", edition.indexable_content
1061
- end
1062
- end
1063
-
1064
- context "for a multi part thing" do
1065
- should "have the normalised content of all parts" do
1066
- edition = FactoryGirl.create(:guide_edition_with_two_parts, :state => 'ready', panopticon_id: FactoryGirl.create(:artefact).id)
1067
- edition.publish
1068
- assert_equal "PART ! This is some version text. PART !! This is some more version text.", edition.indexable_content
1069
- end
1070
- end
1071
-
1072
- context "indexable_content would contain govspeak" do
1073
- should "convert it to plaintext" do
1074
- edition = FactoryGirl.create(:guide_edition_with_two_govspeak_parts, :state => 'ready', panopticon_id: FactoryGirl.create(:artefact).id)
1075
- edition.publish
1076
-
1077
- expected = "Some Part Title! This is some version text. Another Part Title This is link text."
1078
- assert_equal expected, edition.indexable_content
1079
- end
1080
- end
1081
- end
1082
-
1083
- context "#latest_major_update" do
1084
- should 'return the most recent published edition with a major change' do
1085
- edition1 = FactoryGirl.create(:answer_edition, major_change: true,
1086
- change_note: 'published',
1087
- state: 'published',
1088
- version_number: 1)
1089
- edition2 = edition1.build_clone
1090
-
1091
- edition2.update_attributes!(major_change: true, change_note: 'changed', state: 'published')
1092
- edition1.update_attributes!(state: 'archived')
1093
-
1094
- edition3 = edition2.build_clone
1095
-
1096
- assert_equal edition2.id, edition3.latest_major_update.id
1097
- end
1098
- end
1099
-
1100
- context "#latest_change_note" do
1101
- should 'return the change note of the latest major update' do
1102
- edition1 = FactoryGirl.create(:answer_edition, major_change: true,
1103
- change_note: 'a change note',
1104
- state: 'published')
1105
- edition2 = edition1.build_clone
1106
-
1107
- assert_equal 'a change note', edition2.latest_change_note
1108
- end
1109
-
1110
- should 'return nil if there is no major update in the edition series' do
1111
- edition1 = FactoryGirl.create(:answer_edition, major_change: false,
1112
- state: 'published')
1113
- assert_equal nil, edition1.latest_change_note
1114
- end
1115
- end
1116
-
1117
- context '#public_updated_at' do
1118
- should 'return the updated_at timestamp of the latest major update' do
1119
- edition1 = FactoryGirl.create(:answer_edition, major_change: true,
1120
- change_note: 'a change note',
1121
- updated_at: 1.minute.ago,
1122
- state: 'published')
1123
- edition2 = edition1.build_clone
1124
-
1125
- assert_in_delta edition1.updated_at, edition2.public_updated_at, 1.second
1126
- end
1127
-
1128
- should 'return the timestamp of the first published edition when there are no major updates' do
1129
- edition1 = FactoryGirl.create(:answer_edition, major_change: false,
1130
- updated_at: 2.minute.ago,
1131
- state: 'published')
1132
- edition2 = edition1.build_clone
1133
- Timecop.freeze(1.minute.ago) do
1134
- #added to allow significant amount of time between edition updated_at values
1135
- edition2.update_attributes!(state: 'published', major_change: false)
1136
- end
1137
- edition1.update_attributes!(state: 'archived', major_change: false)
1138
-
1139
- assert_in_delta edition1.updated_at, edition2.public_updated_at, 1.second
1140
- assert_not_in_delta edition2.updated_at, edition2.public_updated_at, 1.second
1141
- end
1142
-
1143
- should 'return nil if there are no major updates and no published editions' do
1144
- edition1 = FactoryGirl.create(:answer_edition, major_change: false,
1145
- updated_at: 1.minute.ago,
1146
- state: 'draft')
1147
-
1148
- assert_equal nil, edition1.public_updated_at
1149
- end
1150
- end
1151
-
1152
- context '#has_ever_been_published?' do
1153
- should 'return true if any edition has a published state' do
1154
- edition1 = FactoryGirl.create(:answer_edition, major_change: false,
1155
- updated_at: 2.minute.ago,
1156
- state: 'published')
1157
- edition2 = edition1.build_clone
1158
- edition2.update_attributes!(state: 'archived', major_change: false)
1159
- edition4 = FactoryGirl.create(:answer_edition, major_change: false,
1160
- updated_at: 2.minute.ago,
1161
- state: 'draft')
1162
-
1163
- assert_equal true, edition1.has_ever_been_published?
1164
- assert_equal true, edition2.has_ever_been_published?
1165
- assert_equal false, edition4.has_ever_been_published?
1166
- end
1167
- end
1168
-
1169
- context '#first_edition_of_published' do
1170
- should 'return the first edition of a series that has at least one edition state published' do
1171
- edition1 = FactoryGirl.create(:answer_edition, major_change: false,
1172
- updated_at: 2.minute.ago,
1173
- state: 'published')
1174
- edition2 = edition1.build_clone
1175
- edition1.update_attributes!(state: 'archived', major_change: false)
1176
- edition2.update_attributes!(state: 'published', major_change: false)
1177
- edition3 = edition2.build_clone
1178
- edition3.update_attributes!(state: 'archived', major_change: false)
1179
-
1180
- assert_equal edition1, edition1.first_edition_of_published
1181
- assert_equal edition1, edition2.first_edition_of_published
1182
- assert_equal edition1, edition3.first_edition_of_published
1183
- end
1184
- end
1185
- end