kopia lustrzana https://github.com/wagtail/wagtail
Add tests for submitting translations of snippets with DraftStateMixin
rodzic
8aba5ecce4
commit
f87ad50cb3
|
@ -1,3 +1,4 @@
|
|||
from django.contrib.admin.utils import quote
|
||||
from django.contrib.auth.models import Group, Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.http import Http404
|
||||
|
@ -17,6 +18,7 @@ from wagtail.contrib.simple_translation.views import (
|
|||
from wagtail.models import Locale, Page
|
||||
from wagtail.test.i18n.models import TestPage
|
||||
from wagtail.test.snippets.models import TranslatableSnippet
|
||||
from wagtail.test.testapp.models import FullFeaturedSnippet
|
||||
from wagtail.test.utils import TestCase, WagtailTestUtils
|
||||
|
||||
|
||||
|
@ -291,6 +293,102 @@ class TestSubmitSnippetTranslationView(WagtailTestUtils, TestCase):
|
|||
)
|
||||
|
||||
|
||||
@override_settings(
|
||||
LANGUAGES=[
|
||||
("en", "English"),
|
||||
("fr", "French"),
|
||||
("de", "German"),
|
||||
],
|
||||
WAGTAIL_CONTENT_LANGUAGES=[
|
||||
("en", "English"),
|
||||
("fr", "French"),
|
||||
("de", "German"),
|
||||
],
|
||||
WAGTAIL_I18N_ENABLED=True,
|
||||
)
|
||||
class TestSubmitSnippetTranslationWithDraftState(WagtailTestUtils, TestCase):
|
||||
def setUp(self):
|
||||
self.login()
|
||||
self.en_locale = Locale.objects.first()
|
||||
self.fr_locale = Locale.objects.create(language_code="fr")
|
||||
self.de_locale = Locale.objects.create(language_code="de")
|
||||
|
||||
self.en_snippet = FullFeaturedSnippet.objects.create(
|
||||
text="Hello world", locale=self.en_locale, live=False
|
||||
)
|
||||
self.en_snippet.save_revision().publish()
|
||||
self.en_snippet.text = "It's edited"
|
||||
self.en_snippet.save_revision()
|
||||
self.en_snippet.refresh_from_db()
|
||||
|
||||
self.model_opts = self.en_snippet._meta
|
||||
self.app_label = self.model_opts.app_label
|
||||
self.model_name = self.model_opts.model_name
|
||||
|
||||
def get_submit_url(self):
|
||||
return reverse(
|
||||
"simple_translation:submit_snippet_translation",
|
||||
args=(self.app_label, self.model_name, quote(self.en_snippet.pk)),
|
||||
)
|
||||
|
||||
def get_snippet_url(self, view, snippet):
|
||||
return reverse(
|
||||
f"wagtailsnippets_{self.app_label}_{self.model_name}:{view}",
|
||||
args=(quote(snippet.pk),),
|
||||
)
|
||||
|
||||
def test_submit_snippet_translation_view_test_get(self):
|
||||
response = self.client.get(self.get_submit_url())
|
||||
self.assertIsInstance(response.context["form"], SubmitTranslationForm)
|
||||
|
||||
def test_submit_snippet_translation_view_test_post_invalid(self):
|
||||
response = self.client.post(self.get_submit_url(), {})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(
|
||||
response.context["form"].errors,
|
||||
{"locales": ["This field is required."]},
|
||||
)
|
||||
|
||||
def test_submit_snippet_translation_view_test_post_single_locale(self):
|
||||
data = {"locales": [self.de_locale.id], "include_subtree": True}
|
||||
response = self.client.post(self.get_submit_url(), data, follow=True)
|
||||
|
||||
translated_snippet = self.en_snippet.get_translation(self.de_locale.id)
|
||||
self.assertRedirects(response, self.get_snippet_url("edit", translated_snippet))
|
||||
|
||||
self.assertContains(response, "It's edited", count=1)
|
||||
self.assertContains(response, '<h3 id="status-sidebar-german"', count=1)
|
||||
self.assertContains(
|
||||
response,
|
||||
f'<a href="{self.get_snippet_url("edit", self.en_snippet)}"',
|
||||
count=1,
|
||||
)
|
||||
self.assertNotContains(
|
||||
response,
|
||||
f'<a href="{self.get_snippet_url("edit", translated_snippet)}"',
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
[msg.message for msg in response.context["messages"]],
|
||||
["Successfully created German for full-featured snippet 'It's edited'"],
|
||||
)
|
||||
|
||||
def test_submit_snippet_translation_view_test_post_multiple_locales(self):
|
||||
url = self.get_submit_url()
|
||||
data = {"locales": [self.de_locale.id, self.fr_locale.id]}
|
||||
|
||||
response = self.client.post(url, data)
|
||||
|
||||
self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(response.url, self.get_snippet_url("edit", self.en_snippet))
|
||||
|
||||
response = self.client.get(response.url) # follow the redirect
|
||||
self.assertEqual(
|
||||
[msg.message for msg in response.context["messages"]],
|
||||
["Successfully created 2 locales for full-featured snippet 'It's edited'"],
|
||||
)
|
||||
|
||||
|
||||
@override_settings(
|
||||
LANGUAGES=[
|
||||
("en", "English"),
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
# Generated by Django 5.0.dev20230127120405 on 2023-01-27 12:10
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("wagtailcore", "0083_workflowcontenttype"),
|
||||
("tests", "0018_alter_streampage_body"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="fullfeaturedsnippet",
|
||||
name="locale",
|
||||
field=models.ForeignKey(
|
||||
editable=False,
|
||||
on_delete=django.db.models.deletion.PROTECT,
|
||||
related_name="+",
|
||||
to="wagtailcore.locale",
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="fullfeaturedsnippet",
|
||||
name="translation_key",
|
||||
field=models.UUIDField(default=uuid.uuid4, editable=False),
|
||||
),
|
||||
migrations.AlterUniqueTogether(
|
||||
name="fullfeaturedsnippet",
|
||||
unique_together={("translation_key", "locale")},
|
||||
),
|
||||
]
|
|
@ -1129,6 +1129,7 @@ class FullFeaturedSnippet(
|
|||
DraftStateMixin,
|
||||
LockableMixin,
|
||||
RevisionMixin,
|
||||
TranslatableMixin,
|
||||
models.Model,
|
||||
):
|
||||
text = models.TextField()
|
||||
|
@ -1139,7 +1140,7 @@ class FullFeaturedSnippet(
|
|||
def get_preview_template(self, request, mode_name):
|
||||
return "tests/previewable_model.html"
|
||||
|
||||
class Meta:
|
||||
class Meta(TranslatableMixin.Meta):
|
||||
verbose_name = "full-featured snippet"
|
||||
verbose_name_plural = "full-featured snippets"
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue