Skip to content

Commit 94d3b2f

Browse files
committed
Add first unit test: Document
We need to explicitly allow the version's 'user' association to be optional. In Whitehall, this is globally set with: ``` config.active_record.belongs_to_required_by_default ``` Perhaps for now we ought to set this in the new standalone app also?
1 parent a90b9dd commit 94d3b2f

2 files changed

Lines changed: 240 additions & 1 deletion

File tree

app/models/content_block_manager/content_block/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Version < ApplicationRecord
55

66
belongs_to :item, polymorphic: true
77
validates :event, presence: true
8-
belongs_to :user, foreign_key: "whodunnit"
8+
belongs_to :user, foreign_key: "whodunnit", optional: true
99

1010
def field_diffs
1111
self[:field_diffs] ? ContentBlock::DiffItem.from_hash(self[:field_diffs]) : {}
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
require "test_helper"
2+
3+
class ContentBlockManager::ContentBlockDocumentTest < ActiveSupport::TestCase
4+
extend Minitest::Spec::DSL
5+
6+
it "exists with required data" do
7+
content_block_document = create(
8+
:content_block_document,
9+
:pension,
10+
content_id: "52084b2d-4a52-4e69-ba91-3052b07c7eb6",
11+
sluggable_string: "Title",
12+
created_at: Time.zone.local(2000, 12, 31, 23, 59, 59).utc,
13+
updated_at: Time.zone.local(2000, 12, 31, 23, 59, 59).utc,
14+
)
15+
16+
assert_equal "52084b2d-4a52-4e69-ba91-3052b07c7eb6", content_block_document.content_id
17+
assert_equal "Title", content_block_document.sluggable_string
18+
assert_equal "pension", content_block_document.block_type
19+
assert_equal Time.zone.local(2000, 12, 31, 23, 59, 59).utc, content_block_document.created_at
20+
assert_equal Time.zone.local(2000, 12, 31, 23, 59, 59).utc, content_block_document.updated_at
21+
assert_equal "title", content_block_document.content_id_alias
22+
end
23+
24+
it "does not allow the block type to be changed" do
25+
content_block_document = create(:content_block_document, :pension)
26+
27+
assert_raise ActiveRecord::ReadonlyAttributeError do
28+
content_block_document.update(block_type: "something_else")
29+
end
30+
end
31+
32+
it "can store the id of the latest edition" do
33+
content_block_document = create(:content_block_document, :pension)
34+
content_block_document.update!(latest_edition_id: 1)
35+
assert content_block_document.reload.latest_edition_id, 1
36+
end
37+
38+
it "can store the id of the live edition" do
39+
content_block_document = create(:content_block_document, :pension)
40+
content_block_document.update!(live_edition_id: 1)
41+
assert content_block_document.reload.live_edition_id, 1
42+
end
43+
44+
it "gets its version history from its editions" do
45+
document = create(:content_block_document, :pension)
46+
edition = create(
47+
:content_block_edition,
48+
document:,
49+
creator: create(:user),
50+
)
51+
document.update!(editions: [edition])
52+
53+
assert_equal document.versions.first.item.id, edition.id
54+
end
55+
56+
describe "embed_code" do
57+
let(:content_id) { SecureRandom.uuid }
58+
let(:content_id_alias) { "some-alias" }
59+
let(:document) { build(:content_block_document, :pension, content_id:, content_id_alias:) }
60+
61+
let(:strategy) { Flipflop::FeatureSet.current.test! }
62+
63+
before do
64+
strategy.switch!(:use_friendly_embed_codes, use_friendly_embed_codes)
65+
end
66+
67+
describe "when use_friendly_embed_codes? is false" do
68+
let(:use_friendly_embed_codes) { false }
69+
70+
it "returns embed code for the document" do
71+
assert_equal document.embed_code, "{{embed:content_block_pension:#{content_id}}}"
72+
end
73+
74+
it "returns embed code for a particular field" do
75+
assert_equal document.embed_code_for_field("rates/rate2/name"), "{{embed:content_block_pension:#{content_id}/rates/rate2/name}}"
76+
end
77+
end
78+
79+
describe "when use_friendly_embed_codes? is true" do
80+
let(:use_friendly_embed_codes) { true }
81+
82+
it "returns embed code for the document" do
83+
assert_equal document.embed_code, "{{embed:content_block_pension:#{content_id_alias}}}"
84+
end
85+
86+
it "returns embed code for a particular field" do
87+
assert_equal document.embed_code_for_field("rates/rate2/name"), "{{embed:content_block_pension:#{content_id_alias}/rates/rate2/name}}"
88+
end
89+
end
90+
91+
describe "when use_friendly_id is set manually" do
92+
let(:use_friendly_embed_codes) { false }
93+
94+
it "returns embed code for the document" do
95+
assert_equal document.embed_code(use_friendly_id: true), "{{embed:content_block_pension:#{content_id_alias}}}"
96+
assert_equal document.embed_code(use_friendly_id: false), "{{embed:content_block_pension:#{content_id}}}"
97+
end
98+
99+
it "returns embed code for a particular field" do
100+
assert_equal document.embed_code_for_field("rates/rate2/name", use_friendly_id: true), "{{embed:content_block_pension:#{content_id_alias}/rates/rate2/name}}"
101+
assert_equal document.embed_code_for_field("rates/rate2/name", use_friendly_id: false), "{{embed:content_block_pension:#{content_id}/rates/rate2/name}}"
102+
end
103+
end
104+
end
105+
106+
describe "latest_edition" do
107+
it "returns the latest edition" do
108+
document = create(:content_block_document, :pension)
109+
_first_edition = create(:content_block_edition, document:)
110+
second_edition = create(:content_block_edition, document:)
111+
112+
assert_equal second_edition, document.latest_edition
113+
end
114+
end
115+
116+
describe ".live" do
117+
it "only returns documents with a latest edition" do
118+
document_with_latest_edition = create(:content_block_document, :pension)
119+
latest_edition = create(:content_block_edition, document: document_with_latest_edition)
120+
document_with_latest_edition.latest_edition_id = latest_edition.id
121+
document_with_latest_edition.save!
122+
123+
create(:content_block_document, :pension, latest_edition_id: nil)
124+
125+
assert_equal [document_with_latest_edition], ContentBlockManager::ContentBlock::Document.live
126+
end
127+
end
128+
129+
describe "friendly_id" do
130+
it "generates a content_id_alias" do
131+
content_block_document = create(
132+
:content_block_document,
133+
:pension,
134+
sluggable_string: "This is a title",
135+
)
136+
137+
assert_equal "this-is-a-title", content_block_document.content_id_alias
138+
end
139+
140+
it "ensures content_id_aliases are unique" do
141+
content_block_documents = create_list(
142+
:content_block_document,
143+
2,
144+
:pension,
145+
sluggable_string: "This is a title",
146+
)
147+
148+
assert_equal "this-is-a-title", content_block_documents[0].content_id_alias
149+
assert_equal "this-is-a-title--2", content_block_documents[1].content_id_alias
150+
end
151+
152+
it "does not change the alias if the sluggable string changes" do
153+
content_block_document = create(
154+
:content_block_document,
155+
:pension,
156+
sluggable_string: "This is a title",
157+
)
158+
159+
content_block_document.sluggable_string = "Something else"
160+
content_block_document.save!
161+
162+
assert_equal "this-is-a-title", content_block_document.content_id_alias
163+
end
164+
end
165+
166+
describe "title" do
167+
it "returns the latest edition's title" do
168+
document = create(:content_block_document, :pension)
169+
_oldest_edition = create(:content_block_edition, document:)
170+
latest_edition = create(:content_block_edition, document:, title: "I am the latest edition")
171+
172+
assert_equal latest_edition.title, document.title
173+
end
174+
end
175+
176+
describe "#is_new_block?" do
177+
it "returns true when there is one associated edition" do
178+
document = create(:content_block_document, :pension, editions: create_list(:content_block_edition, 1, :pension))
179+
180+
assert document.is_new_block?
181+
end
182+
183+
it "returns false when there is more than one associated edition" do
184+
document = create(:content_block_document, :pension, editions: create_list(:content_block_edition, 2, :pension))
185+
186+
assert_not document.is_new_block?
187+
end
188+
end
189+
190+
describe "#has_newer_draft?" do
191+
let(:document) { create(:content_block_document, :pension) }
192+
193+
it "returns false when the newest edition is the same as the latest edition" do
194+
_older_edition = create(:content_block_edition, :pension, created_at: Time.zone.now - 2.days, document:)
195+
edition = create(:content_block_edition, :pension, created_at: Time.zone.now, document:)
196+
document.latest_edition_id = edition.id
197+
document.save!
198+
199+
assert_not document.has_newer_draft?
200+
end
201+
202+
it "returns true when the newest edition is not the same as the latest edition" do
203+
edition = create(:content_block_edition, :pension, created_at: Time.zone.now - 2.days, document:)
204+
_newer_edition = create(:content_block_edition, :pension, created_at: Time.zone.now, document:)
205+
document.latest_edition_id = edition.id
206+
document.save!
207+
208+
assert document.has_newer_draft?
209+
end
210+
end
211+
212+
describe "#latest_draft" do
213+
let(:document) { create(:content_block_document, :pension) }
214+
215+
it "returns the latest draft edition" do
216+
_older_draft = create(:content_block_edition, :pension, created_at: Time.zone.now - 2.days, document:, state: "draft")
217+
newest_draft = create(:content_block_edition, :pension, created_at: Time.zone.now - 1.day, document:, state: "draft")
218+
_newest_edition = create(:content_block_edition, :pension, created_at: Time.zone.now, document:, state: "published")
219+
220+
assert_equal newest_draft, document.latest_draft
221+
end
222+
end
223+
224+
describe "#schema" do
225+
let(:document) { build(:content_block_document, :pension) }
226+
let(:schema) { build(:content_block_schema) }
227+
228+
it "returns a schema object" do
229+
document.unstub(:schema)
230+
231+
ContentBlockManager::ContentBlock::Schema
232+
.expects(:find_by_block_type)
233+
.with(document.block_type)
234+
.returns(schema)
235+
236+
assert_equal document.schema, schema
237+
end
238+
end
239+
end

0 commit comments

Comments
 (0)