diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-13 19:10:55 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-13 19:10:55 +0200 |
| commit | 4072efdc912ecb3b79621fb1838434f792e3a531 (patch) | |
| tree | af2cafbec6068a6ee05ac70a064e2ffb2bf369fe /test | |
| parent | 1234371b084356c7542e53d20c9b816332a945d3 (diff) | |
clone_attributes_from: update translations in place, not delete+recreate
Every promotion (autosave creation, draft creation, draft save) ran
every locale's translation through delete-all-then-recreate
unconditionally, giving every locale a fresh created_at/updated_at
regardless of whether its content actually changed. Confirmed via
Node.find(546).draft.translations -- both DE and EN shared one
identical timestamp despite only EN having been edited.
This silently defeated Page.find_with_outdated_translations' whole
staleness comparison: every save reset every locale back into
lockstep, so timestamps could never actually drift apart the way
the feature depends on. The existing test for it only ever passed
by manually backdating a timestamp with record_timestamps = false,
bypassing normal save flow entirely -- not something that happens
through ordinary editing.
Now updates a matching locale's translation only when its own
attributes actually differ, creates one genuinely new, and removes
one genuinely gone from the source -- same end state, but real
per-locale timestamps survive an unrelated save. search_vector is
excluded from both the comparison and the copied attributes: it's
DB-trigger-maintained from title/abstract, not real content, and
comparing a precomputed tsvector risked a false 'changed' from
representation noise alone.
Also reloads the source's translations association, not just
self's -- clone_attributes_from previously only guaranteed a fresh
self, leaving any caller holding a page reference across an earlier
mutation vulnerable to reading a stale cached association.
Diffstat (limited to 'test')
| -rw-r--r-- | test/models/page_test.rb | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/test/models/page_test.rb b/test/models/page_test.rb index bcddc89..24bc7c5 100644 --- a/test/models/page_test.rb +++ b/test/models/page_test.rb | |||
| @@ -177,6 +177,56 @@ class PageTest < ActiveSupport::TestCase | |||
| 177 | assert first_page.published_at.present? | 177 | assert first_page.published_at.present? |
| 178 | end | 178 | end |
| 179 | 179 | ||
| 180 | test "clone_attributes_from preserves an unchanged locale's original timestamp" do | ||
| 181 | n = Node.root.children.create!(:slug => "clone_preserve_timestamp_test") | ||
| 182 | source = n.draft | ||
| 183 | Globalize.with_locale(:de) { source.update!(:title => "Deutscher Titel") } | ||
| 184 | Globalize.with_locale(:en) { source.update!(:title => "English Title") } | ||
| 185 | |||
| 186 | target = Page.create! | ||
| 187 | target.clone_attributes_from(source) | ||
| 188 | original_en_updated_at = target.translations.find_by(:locale => :en).updated_at | ||
| 189 | |||
| 190 | Globalize.with_locale(:de) { source.update!(:title => "Deutscher Titel (bearbeitet)") } | ||
| 191 | target.clone_attributes_from(source) | ||
| 192 | |||
| 193 | en_translation = target.translations.find_by(:locale => :en) | ||
| 194 | assert_equal "English Title", en_translation.title | ||
| 195 | assert_equal original_en_updated_at, en_translation.updated_at | ||
| 196 | end | ||
| 197 | |||
| 198 | test "clone_attributes_from gives a genuinely changed locale a fresh timestamp" do | ||
| 199 | n = Node.root.children.create!(:slug => "clone_fresh_timestamp_test") | ||
| 200 | source = n.draft | ||
| 201 | Globalize.with_locale(:de) { source.update!(:title => "Erste Version") } | ||
| 202 | |||
| 203 | target = Page.create! | ||
| 204 | target.clone_attributes_from(source) | ||
| 205 | original_de_updated_at = target.translations.find_by(:locale => :de).updated_at | ||
| 206 | |||
| 207 | Globalize.with_locale(:de) { source.update!(:title => "Zweite Version") } | ||
| 208 | target.clone_attributes_from(source) | ||
| 209 | |||
| 210 | de_translation = target.translations.find_by(:locale => :de) | ||
| 211 | assert_equal "Zweite Version", de_translation.title | ||
| 212 | assert_operator de_translation.updated_at, :>, original_de_updated_at | ||
| 213 | end | ||
| 214 | |||
| 215 | test "clone_attributes_from removes a locale no longer present in the source" do | ||
| 216 | n = Node.root.children.create!(:slug => "clone_removed_locale_test") | ||
| 217 | source = n.draft | ||
| 218 | Globalize.with_locale(:en) { source.update!(:title => "English Title") } | ||
| 219 | |||
| 220 | target = Page.create! | ||
| 221 | target.clone_attributes_from(source) | ||
| 222 | assert_includes target.translations.map(&:locale), :en | ||
| 223 | |||
| 224 | source.translations.where(:locale => :en).delete_all | ||
| 225 | target.clone_attributes_from(source) | ||
| 226 | |||
| 227 | assert_not_includes target.reload.translations.map(&:locale), :en | ||
| 228 | end | ||
| 229 | |||
| 180 | def test_diff_against_inline_keeps_tags_and_marks_only_the_changed_word | 230 | def test_diff_against_inline_keeps_tags_and_marks_only_the_changed_word |
| 181 | n = Node.root.children.create! :slug => "diff_against_test" | 231 | n = Node.root.children.create! :slug => "diff_against_test" |
| 182 | d = n.find_or_create_draft @user1 | 232 | d = n.find_or_create_draft @user1 |
| @@ -271,4 +321,53 @@ class PageTest < ActiveSupport::TestCase | |||
| 271 | assert_equal [added_asset], diff[:assets][:added] | 321 | assert_equal [added_asset], diff[:assets][:added] |
| 272 | assert_equal [removed_asset], diff[:assets][:removed] | 322 | assert_equal [removed_asset], diff[:assets][:removed] |
| 273 | end | 323 | end |
| 324 | |||
| 325 | test "diff_against with an explicit locale compares that locale's own translation on each side" do | ||
| 326 | n = Node.root.children.create!(:slug => "diff_locale_test") | ||
| 327 | d = n.find_or_create_draft(@user1) | ||
| 328 | Globalize.with_locale(:en) { d.update!(:title => "Old English") } | ||
| 329 | d.save! | ||
| 330 | n.publish_draft! | ||
| 331 | |||
| 332 | d2 = n.find_or_create_draft(@user1) | ||
| 333 | Globalize.with_locale(:en) { d2.update!(:title => "New English") } | ||
| 334 | d2.save! | ||
| 335 | |||
| 336 | diff = d2.diff_against(n.head, :locale => :en) | ||
| 337 | |||
| 338 | assert_match "<del>Old</del>", diff[:title] | ||
| 339 | assert_match "<ins>New</ins>", diff[:title] | ||
| 340 | end | ||
| 341 | |||
| 342 | test "diff_against with an explicit locale ignores content in other locales entirely" do | ||
| 343 | n = Node.root.children.create!(:slug => "diff_locale_isolation_test") | ||
| 344 | d = n.find_or_create_draft(@user1) | ||
| 345 | d.save! | ||
| 346 | n.publish_draft! | ||
| 347 | |||
| 348 | d2 = n.find_or_create_draft(@user1) | ||
| 349 | Globalize.with_locale(:de) { d2.update!(:title => "Nur Deutsch geƤndert") } | ||
| 350 | d2.save! | ||
| 351 | |||
| 352 | diff = d2.diff_against(n.head, :locale => :en) | ||
| 353 | |||
| 354 | assert_no_match(/Deutsch/, diff[:title]) | ||
| 355 | end | ||
| 356 | |||
| 357 | test "locale_diff_summary flags a locale that only exists on one side as changed" do | ||
| 358 | n = Node.root.children.create!(:slug => "diff_locale_summary_test") | ||
| 359 | d = n.find_or_create_draft(@user1) | ||
| 360 | d.save! | ||
| 361 | n.publish_draft! | ||
| 362 | |||
| 363 | d2 = n.find_or_create_draft(@user1) | ||
| 364 | Globalize.with_locale(:en) { d2.update!(:title => "New English translation") } | ||
| 365 | d2.save! | ||
| 366 | |||
| 367 | summary = d2.locale_diff_summary(n.head) | ||
| 368 | en_entry = summary.find { |s| s[:locale] == :en } | ||
| 369 | |||
| 370 | assert en_entry[:changed] | ||
| 371 | refute en_entry[:exists_there] | ||
| 372 | end | ||
| 274 | end | 373 | end |
