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

Code before:
        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
            tags = TagField(tag_model=RestaurantTag)

        form = RestaurantTagForm({"tags": "Italian, delicious"})
        self.assertTrue(form.is_valid())
        self.assertEqual(form.cleaned_data["tags"], ["Italian"])

    def test_override_free_tagging(self):
        class RestaurantTagForm(forms.Form):
            tags = TagField(tag_model=RestaurantTag, free_tagging=True)

        form = RestaurantTagForm({"tags": "Italian, delicious"})
        self.assertTrue(form.is_valid())
        self.assertEqual(set(form.cleaned_data["tags"]), {"Italian", "delicious"})

    def test_tag_over_one_hundred_characters(self):

Review comment (suggestion):
This can be simplified with `self.assertWarns()`. We also want to assert the warning message – we can use `self.assertWarnsMessage()` for this. And the deprecation is only raised when `render()` is called, so let's minimise the scope of the context manager's block to be more specific. Finally, I think we should check that the setting is still reflected to the JS init params:

```suggestion
    @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."
        ):
            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},
            ],
        )
```

Code after:
        ):
            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")

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

        form = RestaurantTagForm({"tags": "Italian, delicious"})
        self.assertTrue(form.is_valid())
        self.assertEqual(form.cleaned_data["tags"], ["Italian"])

    def test_override_free_tagging(self):
        class RestaurantTagForm(forms.Form):
            tags = TagField(tag_model=RestaurantTag, free_tagging=True)

        form = RestaurantTagForm({"tags": "Italian, delicious"})