summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-08-20 17:52:44 +0200
committererdgeist <erdgeist@erdgeist.org>2026-08-20 17:52:44 +0200
commitc635b44d8139d0738d2caa6461c9946659d56c45 (patch)
tree75c1279aefd54d6a895c0408ff48f8959147cc80
parenta1794d3087e6cbd228f3641e7313815d8c59695f (diff)
Add tests for the redirect mechanics
-rw-r--r--test/models/node_test.rb87
-rw-r--r--test/models/page_test.rb25
2 files changed, 112 insertions, 0 deletions
diff --git a/test/models/node_test.rb b/test/models/node_test.rb
index 39be6d32..bd91d560 100644
--- a/test/models/node_test.rb
+++ b/test/models/node_test.rb
@@ -1157,4 +1157,91 @@ class NodeTest < ActiveSupport::TestCase
1157 assert_raises(ActiveRecord::RecordInvalid) { node.reload.publish_draft! } 1157 assert_raises(ActiveRecord::RecordInvalid) { node.reload.publish_draft! }
1158 assert_nil node.reload.head 1158 assert_nil node.reload.head
1159 end 1159 end
1160
1161 test "publishing a redirect to a page that itself redirects is refused" do
1162 final = Node.root.children.create!(:slug => "chain_final")
1163 final.publish_draft!
1164
1165 middle = Node.root.children.create!(:slug => "chain_middle")
1166 middle.draft.update!(:redirect => "temporary", :redirect_node_id => final.id)
1167 middle.publish_draft!
1168
1169 first = Node.root.children.create!(:slug => "chain_first")
1170 first.draft.update!(:redirect => "temporary", :redirect_node_id => middle.id)
1171
1172 assert_raises(ActiveRecord::RecordInvalid) { first.reload.publish_draft! }
1173 assert_nil first.reload.head
1174 end
1175
1176 test "publishing a redirect is refused when something already redirects here" do
1177 target = Node.root.children.create!(:slug => "chain_target")
1178 target.publish_draft!
1179
1180 source = Node.root.children.create!(:slug => "chain_source")
1181 source.draft.update!(:redirect => "temporary", :redirect_node_id => target.id)
1182 source.publish_draft!
1183
1184 onward = Node.root.children.create!(:slug => "chain_onward")
1185 onward.publish_draft!
1186
1187 target.reload
1188 find_or_create_draft(target, @user1)
1189 target.draft.update!(:redirect => "temporary", :redirect_node_id => onward.id)
1190
1191 assert_raises(ActiveRecord::RecordInvalid) { target.reload.publish_draft! }
1192 assert_nil target.reload.head.redirect
1193 end
1194
1195 test "a draft redirect elsewhere does not block publishing a redirect here" do
1196 target = Node.root.children.create!(:slug => "draft_chain_target")
1197 target.publish_draft!
1198
1199 onward = Node.root.children.create!(:slug => "draft_chain_onward")
1200 onward.publish_draft!
1201
1202 source = Node.root.children.create!(:slug => "draft_chain_source")
1203 source.draft.update!(:redirect => "temporary", :redirect_node_id => target.id)
1204 # deliberately not published: only live redirects count
1205
1206 target.reload
1207 find_or_create_draft(target, @user1)
1208 target.draft.update!(:redirect => "temporary", :redirect_node_id => onward.id)
1209 target.reload.publish_draft!
1210
1211 assert_equal "temporary", target.reload.head.redirect
1212 end
1213
1214 test "publishing a page that redirects to itself is refused" do
1215 node = Node.root.children.create!(:slug => "self_redirect")
1216 node.draft.update!(:redirect => "temporary", :redirect_node_id => node.id)
1217
1218 assert_raises(ActiveRecord::RecordInvalid) { node.reload.publish_draft! }
1219 assert_nil node.reload.head
1220 end
1221
1222 test "publishing a redirect to a node that no longer exists is refused" do
1223 doomed = Node.root.children.create!(:slug => "doomed_redirect_target")
1224 node = Node.root.children.create!(:slug => "dangling_redirect")
1225 node.draft.update!(:redirect => "temporary", :redirect_node_id => doomed.id)
1226 doomed.destroy!
1227
1228 assert_raises(ActiveRecord::RecordInvalid) { node.reload.publish_draft! }
1229 assert_nil node.reload.head
1230 end
1231
1232 test "the public search excludes a redirecting page" do
1233 found = Node.root.children.create!(:slug => "search_visible")
1234 found.draft.update!(:title => "Wachhund")
1235 found.publish_draft!
1236
1237 hidden = Node.root.children.create!(:slug => "search_hidden")
1238 hidden.draft.update!(:title => "Wachhund", :redirect => "temporary",
1239 :redirect_node_id => found.id)
1240 hidden.publish_draft!
1241
1242 results = Node.search("Wachhund")
1243
1244 assert_includes results, found
1245 assert_not_includes results, hidden
1246 end
1160end 1247end
diff --git a/test/models/page_test.rb b/test/models/page_test.rb
index f095a7e1..85098ebd 100644
--- a/test/models/page_test.rb
+++ b/test/models/page_test.rb
@@ -466,4 +466,29 @@ class PageTest < ActiveSupport::TestCase
466 :order_by => "slug" }).map { |p| p.node.slug } 466 :order_by => "slug" }).map { |p| p.node.slug }
467 assert_equal %w[alpha Mike zulu], names 467 assert_equal %w[alpha Mike zulu], names
468 end 468 end
469
470 test "redirect_target prefers an internal node over an external url" do
471 target = Node.root.children.create!(:slug => "redirect_precedence_target")
472 target.publish_draft!
473
474 node = Node.root.children.create!(:slug => "redirect_precedence")
475 page = node.draft
476 page.update!(:redirect => "temporary", :redirect_node_id => target.id,
477 :external_url => "https://example.org/")
478
479 resolved = page.redirect_target
480 assert resolved.internal?
481 assert_equal target, resolved.node
482 end
483
484 test "redirect_target is nil when the target has no head" do
485 target = Node.root.children.create!(:slug => "redirect_unpublished_target")
486 assert_nil target.head
487
488 node = Node.root.children.create!(:slug => "redirect_to_unpublished")
489 page = node.draft
490 page.update!(:redirect => "temporary", :redirect_node_id => target.id)
491
492 assert_nil page.redirect_target
493 end
469end 494end