1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
require 'test_helper'
class RelatedAssetsControllerTest < ActionController::TestCase
test "search finds assets by name, excluding ones already attached" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_search_test")
attached = Asset.create!(:name => "biometrics-scan", :upload_content_type => "image/png")
node.draft.assets << attached
Asset.create!(:name => "biometrics-poster", :upload_content_type => "image/png")
Asset.create!(:name => "chaostreff-flyer", :upload_content_type => "image/png")
get :search, params: { :node_id => node.id, :search_term => "biometrics" }
json = JSON.parse(response.body)
names = json.map { |a| a["name"] }
assert_includes names, "biometrics-poster"
assert_not_includes names, "biometrics-scan"
assert_not_includes names, "chaostreff-flyer"
end
test "search with a blank term returns the most recently created assets" do
Asset.delete_all
RelatedAsset.delete_all
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_recent_test")
Asset.create!(:name => "older-photo", :upload_content_type => "image/png", :created_at => 2.days.ago)
Asset.create!(:name => "newer-photo", :upload_content_type => "image/png", :created_at => 1.hour.ago)
get :search, params: { :node_id => node.id, :search_term => "" }
json = JSON.parse(response.body)
assert_equal ["newer-photo", "older-photo"], json.map { |a| a["name"] }
end
test "create attaches an asset to the node's editable page" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_create_test")
asset = Asset.create!(:name => "erfa-photo", :upload_content_type => "image/png")
post :create, params: { :node_id => node.id, :asset_id => asset.id }
assert_response :success
assert_includes node.draft.reload.related_assets.map(&:asset_id), asset.id
json = JSON.parse(response.body)
assert json["url"].present?
end
test "create does not duplicate an already-attached asset" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_dup_test")
asset = Asset.create!(:name => "erfa-photo-2", :upload_content_type => "image/png")
node.draft.assets << asset
post :create, params: { :node_id => node.id, :asset_id => asset.id }
assert_response :success
assert_equal 1, node.draft.reload.related_assets.count
end
test "destroy removes the attached asset" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_destroy_test")
asset = Asset.create!(:name => "old-photo", :upload_content_type => "image/png")
node.draft.assets << asset
related = node.draft.related_assets.first
delete :destroy, params: { :node_id => node.id, :id => related.id }
assert_response :success
assert_equal 0, node.draft.reload.related_assets.count
end
test "update reorders the attached assets" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_reorder_test")
first = Asset.create!(:name => "first-photo", :upload_content_type => "image/png")
second = Asset.create!(:name => "second-photo", :upload_content_type => "image/png")
node.draft.assets << first
node.draft.assets << second
second_related = node.draft.related_assets.find_by(:asset_id => second.id)
patch :update, params: { :node_id => node.id, :id => second_related.id, :position => 1 }
assert_response :success
ordered_asset_ids = node.draft.reload.related_assets.map(&:asset_id)
assert_equal [second.id, first.id], ordered_asset_ids
end
test "update sets the headline flag" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_headline_test")
asset = Asset.create!(:name => "headline-photo", :upload_content_type => "image/png")
node.draft.assets << asset
related = node.draft.related_assets.find_by(:asset_id => asset.id)
patch :update, params: { :node_id => node.id, :id => related.id, :headline => "true" }
assert_response :success
assert related.reload.headline?
end
test "update with headline=true clears any previous headline on the same page" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_headline_swap_test")
first = Asset.create!(:name => "first-headline", :upload_content_type => "image/png")
second = Asset.create!(:name => "second-headline", :upload_content_type => "image/png")
node.draft.assets << first
node.draft.assets << second
first_related = node.draft.related_assets.find_by(:asset_id => first.id)
second_related = node.draft.related_assets.find_by(:asset_id => second.id)
first_related.update!(:headline => true)
patch :update, params: { :node_id => node.id, :id => second_related.id, :headline => "true" }
assert_response :success
assert_not first_related.reload.headline?
assert second_related.reload.headline?
end
test "update with headline=false clears the headline" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_headline_unset_test")
asset = Asset.create!(:name => "unset-headline", :upload_content_type => "image/png")
node.draft.assets << asset
related = node.draft.related_assets.find_by(:asset_id => asset.id)
related.update!(:headline => true)
patch :update, params: { :node_id => node.id, :id => related.id, :headline => "false" }
assert_response :success
assert_not related.reload.headline?
end
test "search includes PDF assets as headline-eligible candidates" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_search_pdf_test")
asset = Asset.create!(:name => "expert-opinion-searchable", :upload_content_type => "application/pdf")
get :search, params: { :node_id => node.id, :search_term => "expert-opinion-searchable" }
assert_response :success
ids = JSON.parse(response.body).map { |r| r["id"] }
assert_includes ids, asset.id
end
test "search matches by filename as well as name" do
login_as :quentin
node = Node.root.children.create!(:slug => "related_assets_search_filename_test")
asset = Asset.create!(:name => "Untitled", :upload_content_type => "application/pdf",
:upload_file_name => "Stellungnahme_Patientendaten_Schutz.pdf")
get :search, params: { :node_id => node.id, :search_term => "Patientendaten" }
assert_response :success
ids = JSON.parse(response.body).map { |r| r["id"] }
assert_includes ids, asset.id
end
end
|