0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 01:22:07 +01:00

Remove inline script usage for enabling commenting

- Fixes #11287
- Add basic unit tests for form element data attributes showing/not showing based on commenting configs
This commit is contained in:
Aman Pandey 2023-11-30 15:49:13 +05:30 committed by LB (Ben Johnston)
parent b93508f4ce
commit 3de0eca7e4
5 changed files with 83 additions and 14 deletions

View File

@ -338,6 +338,17 @@ window.comments = (() => {
updateCommentCount();
}
/** Add support for initializing comments via event dispatching. */
document.addEventListener(
'w-comments:init',
({ target }) => {
setTimeout(() => {
initCommentsInterface(target);
});
},
{ once: true },
);
return {
commentApp,
getContentPath,

View File

@ -4,10 +4,11 @@
{% block bodyclass %}editor-view{% endblock %}
{% block content %}
{% get_comments_enabled as comments_enabled %}
{% include 'wagtailadmin/shared/headers/page_create_header.html' with title=header_title %}
<form id="page-edit-form" action="{% url 'wagtailadmin_pages:add' content_type.app_label content_type.model parent_page.id %}" method="POST" novalidate{% if form.is_multipart %} enctype="multipart/form-data"{% endif %} data-edit-form>
<form id="page-edit-form" action="{% url 'wagtailadmin_pages:add' content_type.app_label content_type.model parent_page.id %}" method="POST" novalidate{% if form.is_multipart %} enctype="multipart/form-data"{% endif %} data-edit-form{% if comments_enabled %} data-controller="w-init" data-w-init-event-value="w-comments:init"{% endif %}>
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
@ -81,12 +82,6 @@
commentApp: window.comments.commentApp
}
);
{% get_comments_enabled as comments_enabled %}
{% if comments_enabled %}
// Initialise comments UI
window.comments.initCommentsInterface(document.getElementById('page-edit-form'));
{% endif %}
});
</script>
{% endblock %}

View File

@ -5,13 +5,14 @@
{% block bodyclass %}editor-view {% if page.live %}page-is-live{% endif %} {% if page_locked %}content-locked{% endif %}{% endblock %}
{% block content %}
{% get_comments_enabled as comments_enabled %}
{% page_permissions page as page_perms %}
{% include 'wagtailadmin/shared/headers/page_edit_header.html' with title=header_title %}
{% block form %}
<form id="page-edit-form" action="{% url 'wagtailadmin_pages:edit' page.id %}" method="POST" novalidate{% if form.is_multipart %} enctype="multipart/form-data"{% endif %} data-edit-form>
<form id="page-edit-form" action="{% url 'wagtailadmin_pages:edit' page.id %}" method="POST" novalidate{% if form.is_multipart %} enctype="multipart/form-data"{% endif %} data-edit-form{% if comments_enabled %} data-controller="w-init" data-w-init-event-value="w-comments:init"{% endif %}>
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}">
@ -81,12 +82,6 @@
commentApp: window.comments.commentApp
}
);
{% get_comments_enabled as comments_enabled %}
{% if comments_enabled %}
// Initialise comments UI
window.comments.initCommentsInterface(document.getElementById('page-edit-form'));
{% endif %}
});
</script>

View File

@ -1874,3 +1874,46 @@ class TestPageSubscriptionSettings(WagtailTestUtils, TestCase):
self.assertEqual(subscription.user, self.user)
self.assertFalse(subscription.comment_notifications)
class TestCommenting(WagtailTestUtils, TestCase):
"""
Tests the commenting related logic of the create page view.
"""
def setUp(self):
# Find root page
self.root_page = Page.objects.get(id=2)
# Login
self.user = self.login()
def test_commments_enabled_by_default(self):
response = self.client.get(
reverse(
"wagtailadmin_pages:add",
args=["tests", "simplepage", self.root_page.id],
)
)
self.assertEqual(response.status_code, 200)
self.assertContains(
response,
'data-edit-form data-controller="w-init" data-w-init-event-value="w-comments:init"',
)
@override_settings(WAGTAILADMIN_COMMENTS_ENABLED=False)
def test_commments_disabled(self):
response = self.client.get(
reverse(
"wagtailadmin_pages:add",
args=["tests", "simplepage", self.root_page.id],
)
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "data-edit-form")
self.assertNotContains(
response,
'data-controller="w-init" data-w-init-event-value="w-comments:init"',
)

View File

@ -3215,6 +3215,31 @@ class TestCommenting(WagtailTestUtils, TestCase):
[to for email in mail.outbox for to in email.to],
)
def test_commments_enabled_by_default(self):
response = self.client.get(
reverse("wagtailadmin_pages:edit", args=[self.child_page.id])
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "data-edit-form")
self.assertContains(
response,
'data-edit-form data-controller="w-init" data-w-init-event-value="w-comments:init"',
)
@override_settings(WAGTAILADMIN_COMMENTS_ENABLED=False)
def test_commments_disabled(self):
response = self.client.get(
reverse("wagtailadmin_pages:edit", args=[self.child_page.id])
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "data-edit-form")
self.assertNotContains(
response,
'data-controller="w-init" data-w-init-event-value="w-comments:init"',
)
def test_new_comment(self):
post_data = {
"title": "I've been edited!",