-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for custom tag is_wagtail_version_gte
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import unittest | ||
|
||
from unittest.mock import patch | ||
|
||
from django import forms | ||
from django.http import HttpResponse | ||
from django.template import Context, Template | ||
from django.test import TestCase, override_settings | ||
from django.urls import path | ||
from django.views import View | ||
from wagtail import VERSION as WAGTAIL_VERSION | ||
|
||
|
||
class DummyForm(forms.Form): | ||
media_file = forms.FileField(label="Change media file:", required=False) | ||
|
||
|
||
class DummyFileFieldView(View): | ||
template_string = """ | ||
{% load media_tags %} | ||
{% is_wagtail_version_gte 6 0 as wagtail60 %} # Update version as needed | ||
{% if wagtail60 %} | ||
{% include "wagtailmedia/media/_file_field.html" %} | ||
{% else %} | ||
{% include "wagtailmedia/media/_file_field_legacy.html" %} | ||
{% endif %} | ||
""" | ||
|
||
def get(self, request, *args, **kwargs): | ||
template = Template(self.template_string) | ||
form = DummyForm() # Create an instance of the form | ||
context_data = self.get_context_data(form=form, **kwargs) | ||
return HttpResponse(template.render(Context(context_data))) | ||
|
||
def get_context_data(self, **kwargs): | ||
# Mock 'media' and 'field' as they are used in the templates | ||
return { | ||
"media": { | ||
"url": "http://example.com/media/file.mp3", | ||
"filename": "file.mp3", | ||
}, | ||
"field": kwargs["form"]["media_file"], # Passing the actual form field | ||
**kwargs, | ||
} | ||
|
||
|
||
urlpatterns = [ | ||
# Temporary URL for testing | ||
path( | ||
"test-filefield-template/", | ||
DummyFileFieldView.as_view(), | ||
name="test-filefield-template", | ||
), | ||
] | ||
|
||
|
||
@override_settings(ROOT_URLCONF=__name__) # Override ROOT_URLCONF during this test | ||
class WagtailVersionFileFieldTemplateTests(TestCase): | ||
@patch( | ||
"wagtailmedia.templatetags.media_tags.WAGTAIL_VERSION", new=(5, 2) | ||
) # Version for legacy template | ||
def test_legacy_template_loaded(self): | ||
response = self.client.get("/test-filefield-template/") | ||
self.assertTemplateUsed(response, "wagtailmedia/media/_file_field_legacy.html") | ||
|
||
@unittest.skipIf(WAGTAIL_VERSION < (6.0), "Requires Wagtail 6.0 or higher") | ||
@patch( | ||
"wagtailmedia.templatetags.media_tags.WAGTAIL_VERSION", new=(6, 0) | ||
) # Version for new template | ||
def test_new_template_loaded(self): | ||
response = self.client.get("/test-filefield-template/") | ||
self.assertTemplateUsed(response, "wagtailmedia/media/_file_field.html") |