summaryrefslogtreecommitdiff
path: root/test/models
diff options
context:
space:
mode:
Diffstat (limited to 'test/models')
-rw-r--r--test/models/asset_test.rb12
-rw-r--r--test/models/helpers/content_helper_test.rb46
-rw-r--r--test/models/node_action_test.rb20
-rw-r--r--test/models/node_attach_asset_test.rb107
-rw-r--r--test/models/node_test.rb45
-rw-r--r--test/models/page_test.rb8
-rw-r--r--test/models/related_asset_test.rb16
7 files changed, 223 insertions, 31 deletions
diff --git a/test/models/asset_test.rb b/test/models/asset_test.rb
index d246abe..ab1cc5d 100644
--- a/test/models/asset_test.rb
+++ b/test/models/asset_test.rb
@@ -41,4 +41,16 @@ class AssetTest < ActiveSupport::TestCase
41 test "license_key may be blank" do 41 test "license_key may be blank" do
42 assert Asset.new(:license_key => nil).valid? 42 assert Asset.new(:license_key => nil).valid?
43 end 43 end
44
45 test "pdf? is true only for application/pdf" do
46 assert Asset.new(:upload_content_type => "application/pdf").pdf?
47 assert_not Asset.new(:upload_content_type => "image/png").pdf?
48 end
49
50 test "show_credit? is false for a PDF even with every credit field present" do
51 asset = Asset.new(:name => "demo", :upload_content_type => "application/pdf",
52 :creator => "Jane Doe", :source_url => "https://example.org", :license_key => "cc_by_4")
53 assert asset.has_credit?
54 assert_not asset.show_credit?
55 end
44end 56end
diff --git a/test/models/helpers/content_helper_test.rb b/test/models/helpers/content_helper_test.rb
index d6c7b43..c69ef4e 100644
--- a/test/models/helpers/content_helper_test.rb
+++ b/test/models/helpers/content_helper_test.rb
@@ -79,4 +79,50 @@ class ContentHelperTest < ActionView::TestCase
79 79
80 I18n.with_locale(:en) { assert_match t(:open_gallery), headline_image } 80 I18n.with_locale(:en) { assert_match t(:open_gallery), headline_image }
81 end 81 end
82
83 test "headline_image renders a document card for a PDF headline, not a lightbox image" do
84 node = Node.root.children.create!(:slug => "headline_image_pdf_test")
85 asset = Asset.create!(:name => "Expert Opinion", :upload_content_type => "application/pdf")
86 node.draft.assets << asset
87 node.draft.related_assets.find_by(:asset_id => asset.id).update!(:headline => true)
88 @page = node.draft
89
90 result = headline_image
91
92 assert_match "headline_document_card", result
93 assert_match "Expert Opinion", result
94 assert_no_match "data-gallery", result
95 end
96
97 test "headline_image lists other attached PDFs below the headline" do
98 node = Node.root.children.create!(:slug => "headline_image_multi_pdf_test")
99 headline_pdf = Asset.create!(:name => "Main Filing", :upload_content_type => "application/pdf")
100 other_pdf = Asset.create!(:name => "Supplementary Exhibit", :upload_content_type => "application/pdf")
101 node.draft.assets << headline_pdf
102 node.draft.assets << other_pdf
103 node.draft.related_assets.find_by(:asset_id => headline_pdf.id).update!(:headline => true)
104 @page = node.draft
105
106 result = headline_image
107
108 assert_match "Main Filing", result
109 assert_match "Supplementary Exhibit", result
110 assert_match "headline_document_card", result
111 assert_match "related_documents_list", result
112 end
113
114 test "headline_image lists attached PDFs even with no headline chosen" do
115 node = Node.root.children.create!(:slug => "headline_image_pdf_no_headline_test")
116 pdf_a = Asset.create!(:name => "Document A", :upload_content_type => "application/pdf")
117 pdf_b = Asset.create!(:name => "Document B", :upload_content_type => "application/pdf")
118 node.draft.assets << pdf_a
119 node.draft.assets << pdf_b
120 @page = node.draft
121
122 result = headline_image
123
124 assert_no_match "headline_document_card", result
125 assert_match "Document A", result
126 assert_match "Document B", result
127 end
82end 128end
diff --git a/test/models/node_action_test.rb b/test/models/node_action_test.rb
index b177cca..849b36f 100644
--- a/test/models/node_action_test.rb
+++ b/test/models/node_action_test.rb
@@ -118,11 +118,29 @@ class NodeActionTest < ActiveSupport::TestCase
118 118
119 test "record! passes provenance and historical timestamps through" do 119 test "record! passes provenance and historical timestamps through" do
120 long_ago = 2.years.ago 120 long_ago = 2.years.ago
121 action = NodeAction.record!(:node => nil, :action => "publish", 121 node = Node.root.children.create!(:slug => "provenance_backfill_test")
122 action = NodeAction.record!(:node => node, :action => "publish",
122 :occurred_at => long_ago, 123 :occurred_at => long_ago,
123 :inferred_from => "from_page_revision") 124 :inferred_from => "from_page_revision")
124 125
125 assert_equal "from_page_revision", action.inferred_from 126 assert_equal "from_page_revision", action.inferred_from
126 assert_in_delta long_ago.to_f, action.occurred_at.to_f, 1.0 127 assert_in_delta long_ago.to_f, action.occurred_at.to_f, 1.0
127 end 128 end
129
130
131 test "record! with participants: writes one action_participant per subject" do
132 n1, n2 = Node.root.children.create!(:slug => "participant_a"), Node.root.children.create!(:slug => "participant_b")
133 action = NodeAction.record!(:participants => [n1, n2], :action => "trash", :user => users(:quentin))
134 assert_equal [n1, n2], action.action_participants.map(&:subject)
135 end
136
137 test "record! with node: alone still writes exactly one participant" do
138 node = Node.root.children.create!(:slug => "participant_sugar")
139 action = NodeAction.record!(:node => node, :action => "create", :user => users(:quentin))
140 assert_equal [node], action.action_participants.map(&:subject)
141 end
142
143 test "record! refuses an empty participant set" do
144 assert_raises(ArgumentError) { NodeAction.record!(:action => "create", :user => users(:quentin)) }
145 end
128end 146end
diff --git a/test/models/node_attach_asset_test.rb b/test/models/node_attach_asset_test.rb
new file mode 100644
index 0000000..cb5b60f
--- /dev/null
+++ b/test/models/node_attach_asset_test.rb
@@ -0,0 +1,107 @@
1require "test_helper"
2
3class NodeAttachAssetTest < ActiveSupport::TestCase
4
5 def setup
6 @user = users(:quentin)
7 @other = users(:aaron)
8 @node = Node.root.children.create!(:slug => "attach_asset_test")
9 @image = create_image_asset
10 end
11
12 test "attaches to a draft-only node" do
13 result = @node.attach_asset!(@image, :user => @user)
14 assert_equal 1, result[:attached]
15 assert_includes @node.draft.assets, @image
16 end
17
18 test "attaches to head when no draft is pending" do
19 @node.publish_draft!(@user)
20 result = @node.attach_asset!(@image, :user => @user)
21 assert_equal 1, result[:attached]
22 assert_includes @node.head.assets, @image
23 end
24
25 test "attaches to head and pending draft alike" do
26 @node.publish_draft!(@user)
27 @node.lock_for_editing!(@user)
28 @node.create_new_draft(@user)
29 result = @node.attach_asset!(@image, :user => @user)
30 assert_equal 2, result[:attached]
31 assert_includes @node.head.assets, @image
32 assert_includes @node.draft.assets, @image
33 end
34
35 test "attaches to all three lifecycle rows" do
36 @node.publish_draft!(@user)
37 @node.lock_for_editing!(@user)
38 @node.create_new_draft(@user)
39 @node.autosave!({ :title => "wip" }, @user)
40 result = @node.attach_asset!(@image, :user => @user)
41 assert_equal 3, result[:attached]
42 [@node.head, @node.draft, @node.autosave].each do |row|
43 assert_includes row.assets, @image
44 end
45 end
46
47 test "skips rows that already carry the asset" do
48 @node.draft.related_assets.create!(:asset => @image)
49 @node.publish_draft!(@user)
50 @node.lock_for_editing!(@user)
51 @node.create_new_draft(@user)
52 result = @node.attach_asset!(@image, :user => @user)
53 assert_equal 0, result[:attached]
54 assert_equal 2, result[:already]
55 assert_equal 1, @node.draft.related_assets.where(:asset_id => @image.id).count
56 end
57
58 test "refuses when another user holds the lock and writes nothing" do
59 @node.lock_for_editing!(@other)
60 assert_raises(LockedByAnotherUser) { @node.attach_asset!(@image, :user => @user) }
61 assert_empty @node.draft.assets.reload
62 end
63
64 test "proceeds when the attaching user holds the lock" do
65 @node.lock_for_editing!(@user)
66 result = @node.attach_asset!(@image, :user => @user)
67 assert_equal 1, result[:attached]
68 end
69
70 test "sets the headline when none exists" do
71 result = @node.attach_asset!(@image, :user => @user, :headline => true)
72 assert_equal :set, result[:headline]
73 assert_equal @image, @node.draft.headline_asset
74 end
75
76 test "keeps an existing headline and reports it" do
77 incumbent = create_image_asset
78 @node.draft.related_assets.create!(:asset => incumbent, :headline => true)
79 result = @node.attach_asset!(@image, :user => @user, :headline => true)
80 assert_equal :kept_existing, result[:headline]
81 assert_equal incumbent, @node.draft.reload.headline_asset
82 assert_includes @node.draft.assets, @image
83 end
84
85 test "declines the headline flag for ineligible asset types" do
86 plain = create_plain_asset
87 result = @node.attach_asset!(plain, :user => @user, :headline => true)
88 assert_equal :not_eligible, result[:headline]
89 assert_includes @node.draft.assets.reload, plain
90 assert_nil @node.draft.headline_asset
91 end
92
93 test "refuses nodes in the Trash" do
94 @node.trash!(@user)
95 assert_raises(ActiveRecord::RecordInvalid) { @node.attach_asset!(@image, :user => @user) }
96 end
97
98 private
99
100 def create_image_asset
101 Asset.create!(:name => "attach test image", :upload_content_type => "image/png")
102 end
103
104 def create_plain_asset
105 Asset.create!(:name => "attach test note", :upload_content_type => "text/plain")
106 end
107end
diff --git a/test/models/node_test.rb b/test/models/node_test.rb
index ab66f81..0083b08 100644
--- a/test/models/node_test.rb
+++ b/test/models/node_test.rb
@@ -268,7 +268,6 @@ class NodeTest < ActiveSupport::TestCase
268 assert_equal "quentin", node.head.user.login 268 assert_equal "quentin", node.head.user.login
269 end 269 end
270 270
271
272 test "update?" do 271 test "update?" do
273 Node.root.descendants.delete_all 272 Node.root.descendants.delete_all
274 updates = Node.root.children.create!( :slug => "updates" ) 273 updates = Node.root.children.create!( :slug => "updates" )
@@ -359,6 +358,16 @@ class NodeTest < ActiveSupport::TestCase
359 assert_equal @user1, node.reload.lock_owner 358 assert_equal @user1, node.reload.lock_owner
360 end 359 end
361 360
361 test "title reads from autosave when neither draft nor head exists yet" do
362 node = Node.root.children.create!(:slug => "title_autosave_only_test")
363 node.draft.destroy
364 node.update_column(:draft_id, nil)
365 node.lock_for_editing!(users(:quentin))
366 node.autosave!({ :title => "autosave-only title" }, users(:quentin))
367
368 assert_equal "autosave-only title", node.reload.title
369 end
370
362 test "revert! is a safe no-op on a fresh node with only a draft" do 371 test "revert! is a safe no-op on a fresh node with only a draft" do
363 node = create_node_with_draft 372 node = create_node_with_draft
364 node.lock_for_editing!(@user1) 373 node.lock_for_editing!(@user1)
@@ -537,30 +546,6 @@ class NodeTest < ActiveSupport::TestCase
537 assert result.index(mine) < result.index(someone_elses_newer) 546 assert result.index(mine) < result.index(someone_elses_newer)
538 end 547 end
539 548
540 test "recently_changed includes a node whose head was recently published" do
541 node = Node.root.children.create!(:slug => "recent_changed_published")
542 find_or_create_draft(node, @user1)
543 node.autosave!({:title => "v1"}, @user1)
544 node.save_draft!(@user1)
545 node.publish_draft!
546
547 assert_includes Node.recently_changed, node
548 end
549
550 test "recently_changed excludes a node only touched by locking or unlocking after an old publish" do
551 node = Node.root.children.create!(:slug => "recent_changed_lock_only")
552 find_or_create_draft(node, @user1)
553 node.autosave!({:title => "v1"}, @user1)
554 node.save_draft!(@user1)
555 node.publish_draft!
556 node.head.update_column(:updated_at, 20.days.ago)
557
558 node.lock_for_editing!(@user1)
559 node.unlock!
560
561 assert_not_includes Node.recently_changed, node
562 end
563
564 test "autosave! carries over the current related assets to the newly created autosave row" do 549 test "autosave! carries over the current related assets to the newly created autosave row" do
565 node = Node.root.children.create!(:slug => "autosave_asset_carryover_test") 550 node = Node.root.children.create!(:slug => "autosave_asset_carryover_test")
566 user = User.find_by_login("quentin") 551 user = User.find_by_login("quentin")
@@ -771,6 +756,16 @@ class NodeTest < ActiveSupport::TestCase
771 assert node.valid? 756 assert node.valid?
772 end 757 end
773 758
759 test "trash! records every subtree node as a participant, not just the root" do
760 parent = Node.root.children.create!(:slug => "trash_participants_parent")
761 child = parent.children.create!(:slug => "trash_participants_child")
762
763 parent.trash!(users(:quentin))
764
765 action = parent.node_actions.where(:action => "trash").last
766 assert_includes action.action_participants.map(&:subject), child
767 end
768
774 test "destroying a node with children is refused" do 769 test "destroying a node with children is refused" do
775 parent = Node.root.children.create!(:slug => "destroy_guard_parent") 770 parent = Node.root.children.create!(:slug => "destroy_guard_parent")
776 parent.children.create!(:slug => "destroy_guard_child") 771 parent.children.create!(:slug => "destroy_guard_child")
diff --git a/test/models/page_test.rb b/test/models/page_test.rb
index 1f924f9..98a00d2 100644
--- a/test/models/page_test.rb
+++ b/test/models/page_test.rb
@@ -309,11 +309,15 @@ class PageTest < ActiveSupport::TestCase
309 309
310 kept_asset = Asset.create!(:upload_file_name => "kept.png", :upload_content_type => "image/png", :upload_file_size => 1) 310 kept_asset = Asset.create!(:upload_file_name => "kept.png", :upload_content_type => "image/png", :upload_file_size => 1)
311 removed_asset = Asset.create!(:upload_file_name => "removed.pdf", :upload_content_type => "application/pdf", :upload_file_size => 1) 311 removed_asset = Asset.create!(:upload_file_name => "removed.pdf", :upload_content_type => "application/pdf", :upload_file_size => 1)
312 n.head.update_assets([kept_asset.id, removed_asset.id]) 312 n.head.related_assets.delete_all
313 n.head.related_assets.create!(:asset_id => kept_asset.id, :position => 1)
314 n.head.related_assets.create!(:asset_id => removed_asset.id, :position => 2)
313 315
314 d2 = find_or_create_draft(n, @user1) 316 d2 = find_or_create_draft(n, @user1)
315 added_asset = Asset.create!(:upload_file_name => "added.png", :upload_content_type => "image/png", :upload_file_size => 1) 317 added_asset = Asset.create!(:upload_file_name => "added.png", :upload_content_type => "image/png", :upload_file_size => 1)
316 d2.update_assets([kept_asset.id, added_asset.id]) 318 d2.related_assets.delete_all
319 d2.related_assets.create!(:asset_id => kept_asset.id, :position => 1)
320 d2.related_assets.create!(:asset_id => added_asset.id, :position => 2)
317 d2.save! 321 d2.save!
318 322
319 diff = d2.diff_against(n.head) 323 diff = d2.diff_against(n.head)
diff --git a/test/models/related_asset_test.rb b/test/models/related_asset_test.rb
index 710b4cc..bb86ddb 100644
--- a/test/models/related_asset_test.rb
+++ b/test/models/related_asset_test.rb
@@ -11,15 +11,25 @@ class RelatedAssetTest < ActiveSupport::TestCase
11 assert related.valid? 11 assert related.valid?
12 end 12 end
13 13
14 test "headline cannot be set on a non-image asset" do 14 test "headline can be set on a PDF asset" do
15 node = Node.root.children.create!(:slug => "related_asset_headline_pdf_test") 15 node = Node.root.children.create!(:slug => "related_asset_headline_pdf_test")
16 asset = Asset.create!(:name => "programme", :upload_content_type => "application/pdf") 16 asset = Asset.create!(:name => "expert opinion", :upload_content_type => "application/pdf")
17 node.draft.assets << asset
18 related = node.draft.related_assets.find_by(:asset_id => asset.id)
19
20 related.headline = true
21 assert related.valid?
22 end
23
24 test "headline cannot be set on a non-image, non-PDF asset" do
25 node = Node.root.children.create!(:slug => "related_asset_headline_text_test")
26 asset = Asset.create!(:name => "programme", :upload_content_type => "text/plain")
17 node.draft.assets << asset 27 node.draft.assets << asset
18 related = node.draft.related_assets.find_by(:asset_id => asset.id) 28 related = node.draft.related_assets.find_by(:asset_id => asset.id)
19 29
20 related.headline = true 30 related.headline = true
21 assert_not related.valid? 31 assert_not related.valid?
22 assert_includes related.errors[:headline], "can only be set on image assets" 32 assert_includes related.errors[:headline], "can only be set on image or PDF assets"
23 end 33 end
24 34
25 test "the headline validation does not raise when asset is missing" do 35 test "the headline validation does not raise when asset is missing" do