Repository: wagtail/wagtail
File: wagtail/admin/tests/test_widgets.py (Python)

Code before:
        self.assertEqual(
            help_text, "Tags can only consist of a single word, no spaces allowed."
        )

        self.assertIn(
            """<p class="help">%s</p>""" % escape(help_text),
            html,
        )

    @override_settings(WAGTAIL_TAG_SPACES_ALLOWED=False, WAGTAIL_TAG_LIMIT=3)
    def test_render_js_init_with_legacy_settings(self):
        """Checks that the 'w-tag' controller attributes are correctly added to the tag widgets using legacy TAG_* settings"""
        widget = widgets.AdminTagWidget()

        html = widget.render("tags", None, attrs={"id": "alpha"})
        params = self.get_js_init_params(html)

        self.assertEqual(
            params,
            [
                "alpha",
                "/admin/tag-autocomplete/",
                {"allowSpaces": False, "tagLimit": 3, "autocompleteOnly": False},
            ],
        )

    @override_settings(TAG_SPACES_ALLOWED=False)
    def test_tag_spaces_allowed_deprecation_warning(self):
        """Checks that a deprecation warning is raised for TAG_SPACES_ALLOWED"""
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")

            widget = widgets.AdminTagWidget()
            widget.render("tags", None, attrs={"id": "alpha"})

            self.assertTrue(
                any(
                    issubclass(warning.category, RemovedInWagtail70Warning)
                    for warning in w
                )
            )


class TestTagField(TestCase):
    def setUp(self):
        RestaurantTag.objects.create(name="Italian", slug="italian")
        RestaurantTag.objects.create(name="Indian", slug="indian")

    def test_tag_whitelisting(self):
        class RestaurantTagForm(forms.Form):
            # RestaurantTag sets free_tagging=False at the model level

Review comment (suggestion):
The settings applied doesn't seem to match what the docstring suggests (i.e. they are the new settings, not the legacy ones).

I think rather than separating the methods by the output, i.e. one test for deprecation and one test for the rendered params, we should separate them by setting. This ensures we test the complete expected behaviour: a warning is raised _but_ it still affects the params. Otherwise, one of the tests could still pass if you for example only raise a warning but not use the legacy setting's value.

So, this should be specific to the `TAG_LIMIT` setting (since we already have one for `TAG_SPACES_ALLOWED` below).

```suggestion
    @override_settings(TAG_LIMIT=3)
    def test_legacy_tag_limit_setting(self):
        widget = widgets.AdminTagWidget()
        with self.assertWarnsMessage(
            RemovedInWagtail70Warning,
            "The setting 'TAG_LIMIT' is deprecated. "
            "Please use 'WAGTAIL_TAG_LIMIT' instead."
        ):
            widget.render("tags", None, attrs={"id": "alpha"})
        params = self.get_js_init_params(html)
        self.assertEqual(
            params,
            [
                "alpha",
                "/admin/tag-autocomplete/",
                {"allowSpaces": True, "tagLimit": 3, "autocompleteOnly": False},
            ],
        )
```

Code after:
            help_text, "Tags can only consist of a single word, no spaces allowed."
        )

        self.assertIn(
            """<p class="help">%s</p>""" % escape(help_text),
            html,
        )

    @override_settings(TAG_LIMIT=3)
    def test_legacy_tag_limit_setting(self):
        widget = widgets.AdminTagWidget()
        with self.assertWarnsMessage(
            RemovedInWagtail70Warning,
            "The setting 'TAG_LIMIT' is deprecated. "
            "Please use 'WAGTAIL_TAG_LIMIT' instead.",
        ):
            html = widget.render("tags", None, attrs={"id": "alpha"})
        params = self.get_js_init_params(html)
        self.assertEqual(
            params,
            [
                "alpha",
                "/admin/tag-autocomplete/",
                {"allowSpaces": True, "tagLimit": 3, "autocompleteOnly": False},
            ],
        )

    @override_settings(TAG_SPACES_ALLOWED=False)
    def test_legacy_tag_spaces_allowed_setting(self):
        widget = widgets.AdminTagWidget()
        with self.assertWarnsMessage(
            RemovedInWagtail70Warning,
            "The setting 'TAG_SPACES_ALLOWED' is deprecated. "
            "Please use 'WAGTAIL_TAG_SPACES_ALLOWED' instead.",
        ):
            html = widget.render("tags", None, attrs={"id": "alpha"})
        params = self.get_js_init_params(html)
        self.assertEqual(
            params,
            [
                "alpha",
                "/admin/tag-autocomplete/",
                {"allowSpaces": False, "tagLimit": None, "autocompleteOnly": False},
            ],
        )


class TestTagField(TestCase):
    def setUp(self):
        RestaurantTag.objects.create(name="Italian", slug="italian")
        RestaurantTag.objects.create(name="Indian", slug="indian")