From 5ef13e5aa5ea565b07441616ab1eece565aa2f53 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Thu, 30 Jul 2026 12:55:27 +0200 Subject: Canonicalise per locale and declare hreflang alternates German content is reachable at both /updates/foo and /de/updates/foo, and a page with no English translation renders German at /en/updates/foo through the fallback chain, so three URLs each claimed to be canonical. Both tags now derive from one locale-to-URL function, which points every duplicate at the address of the version actually served, the unprefixed German URL, matching default_url_options. Alternates are declared only for locales in which the page genuinely has a translation, since /en/ serving German through the fallback is not an English version of the page. x-default points at German, the site's primary language. --- test/controllers/content_controller_test.rb | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'test/controllers/content_controller_test.rb') diff --git a/test/controllers/content_controller_test.rb b/test/controllers/content_controller_test.rb index 304fd921..5bb02f3d 100644 --- a/test/controllers/content_controller_test.rb +++ b/test/controllers/content_controller_test.rb @@ -190,6 +190,46 @@ class ContentControllerTest < ActionController::TestCase FileUtils.rm_rf(Rails.root.join("tmp", "test_uploads", asset.id.to_s)) end end + + test "a translated page declares hreflang alternates and canonicalises per locale" do + node = create_node_under_root "og_hreflang_test" + draft = find_or_create_draft(node, @user1) + draft.title = "Zweisprachig" + draft.save + node.publish_draft! + node.reload + Globalize.with_locale(:en) { node.head.update!(:title => "Bilingual") } + + get :render_page, params: { :locale => "de", :page_path => ["og_hreflang_test"] } + + assert_response :success + assert_select "link[rel=alternate][hreflang=de][href=?]", + "http://test.host/og_hreflang_test" + assert_select "link[rel=alternate][hreflang=en][href=?]", + "http://test.host/en/og_hreflang_test" + assert_select "link[rel=alternate][hreflang='x-default'][href=?]", + "http://test.host/og_hreflang_test" + assert_select "link[rel=canonical][href=?]", + "http://test.host/og_hreflang_test" + end + + test "an untranslated page declares no alternates and canonicalises to the default locale" do + node = create_node_under_root "og_single_locale_test" + draft = find_or_create_draft(node, @user1) + draft.title = "Nur Deutsch" + draft.save + node.publish_draft! + + get :render_page, params: { :locale => "en", :page_path => ["og_single_locale_test"] } + + assert_response :success + assert_select "link[rel=alternate][hreflang]", false, + "one translation is nothing to declare" + # Served German through the fallback chain, so the German URL is + # canonical rather than the /en/ address that was requested. + assert_select "link[rel=canonical][href=?]", + "http://test.host/og_single_locale_test" + end protected -- cgit v1.3