diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-30 12:55:27 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-30 12:55:27 +0200 |
| commit | 5ef13e5aa5ea565b07441616ab1eece565aa2f53 (patch) | |
| tree | b34276c2537061e55d2cfdf7824fc713619f9230 /test | |
| parent | 60c3671b6f09f1f4d4a7d30216c94a80dbee6baa (diff) | |
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.
Diffstat (limited to 'test')
| -rw-r--r-- | test/controllers/content_controller_test.rb | 40 |
1 files changed, 40 insertions, 0 deletions
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 | |||
| 190 | FileUtils.rm_rf(Rails.root.join("tmp", "test_uploads", asset.id.to_s)) | 190 | FileUtils.rm_rf(Rails.root.join("tmp", "test_uploads", asset.id.to_s)) |
| 191 | end | 191 | end |
| 192 | end | 192 | end |
| 193 | |||
| 194 | test "a translated page declares hreflang alternates and canonicalises per locale" do | ||
| 195 | node = create_node_under_root "og_hreflang_test" | ||
| 196 | draft = find_or_create_draft(node, @user1) | ||
| 197 | draft.title = "Zweisprachig" | ||
| 198 | draft.save | ||
| 199 | node.publish_draft! | ||
| 200 | node.reload | ||
| 201 | Globalize.with_locale(:en) { node.head.update!(:title => "Bilingual") } | ||
| 202 | |||
| 203 | get :render_page, params: { :locale => "de", :page_path => ["og_hreflang_test"] } | ||
| 204 | |||
| 205 | assert_response :success | ||
| 206 | assert_select "link[rel=alternate][hreflang=de][href=?]", | ||
| 207 | "http://test.host/og_hreflang_test" | ||
| 208 | assert_select "link[rel=alternate][hreflang=en][href=?]", | ||
| 209 | "http://test.host/en/og_hreflang_test" | ||
| 210 | assert_select "link[rel=alternate][hreflang='x-default'][href=?]", | ||
| 211 | "http://test.host/og_hreflang_test" | ||
| 212 | assert_select "link[rel=canonical][href=?]", | ||
| 213 | "http://test.host/og_hreflang_test" | ||
| 214 | end | ||
| 215 | |||
| 216 | test "an untranslated page declares no alternates and canonicalises to the default locale" do | ||
| 217 | node = create_node_under_root "og_single_locale_test" | ||
| 218 | draft = find_or_create_draft(node, @user1) | ||
| 219 | draft.title = "Nur Deutsch" | ||
| 220 | draft.save | ||
| 221 | node.publish_draft! | ||
| 222 | |||
| 223 | get :render_page, params: { :locale => "en", :page_path => ["og_single_locale_test"] } | ||
| 224 | |||
| 225 | assert_response :success | ||
| 226 | assert_select "link[rel=alternate][hreflang]", false, | ||
| 227 | "one translation is nothing to declare" | ||
| 228 | # Served German through the fallback chain, so the German URL is | ||
| 229 | # canonical rather than the /en/ address that was requested. | ||
| 230 | assert_select "link[rel=canonical][href=?]", | ||
| 231 | "http://test.host/og_single_locale_test" | ||
| 232 | end | ||
| 193 | 233 | ||
| 194 | protected | 234 | protected |
| 195 | 235 | ||
