wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
222 stars 84 forks source link

Adds failing test for cleared foreignkey behaviour #744

Open alexalligator opened 9 months ago

alexalligator commented 9 months ago

This PR adds a failing test to demonstrate the issue described below.

Ref slack discussion.

Wagtail localize currently skips extracting segments for optional foreign keys that have no instance set. I.e. cleared foreign keys are treated as a lack of meaningful data, rather than None being treated as a meaningful value in and of itself.

# From extract.py

elif isinstance(field, models.ForeignKey):
    # ... 

    related_instance = getattr(instance, field.name)
    if related_instance:
        segments.append(
            RelatedObjectSegmentValue.from_instance(
                field.name, related_instance
            )
        )

This means that once a page is translated, clearing the field on the source page and retranslating leaves the translation with a value still selected:

zerolab commented 9 months ago

The tests are failing with

test_post_update_page_translation_with_publish_translations_and_cleared_foreignkey
      self.assertEqual(self.fr_blog_post.test_snippet, None)
  AssertionError: <TestSnippet: TestSnippet object (2)> != None

as intended to show in the PR