diff --git a/wagtail/tests/models.py b/wagtail/tests/models.py index bbf91d278f..e061a4bfeb 100644 --- a/wagtail/tests/models.py +++ b/wagtail/tests/models.py @@ -255,18 +255,42 @@ FormPage.content_panels = [ ] +# Snippets + # Snippets class Advert(models.Model): - url = models.URLField(null=True, blank=True) - text = models.CharField(max_length=255) + url = models.URLField(null=True, blank=True) + text = models.CharField(max_length=255) - panels = [ - FieldPanel('url'), - FieldPanel('text'), - ] + panels = [ + FieldPanel('url'), + FieldPanel('text'), + ] + + def __unicode__(self): + return self.text - def __unicode__(self): - return self.text register_snippet(Advert) + + +# AlphaSnippet and ZuluSnippet are for testing ordering of +# snippets when registering. They are named as such to ensure +# thier ordering is clear. They are registered during testing +# to ensure specific [in]correct register ordering + +# AlphaSnippet is registered during TestSnippetOrdering +class AlphaSnippet(models.Model): + text = models.CharField(max_length=255) + + def __unicode__(self): + return self.text + + +# ZuluSnippet is registered during TestSnippetOrdering +class ZuluSnippet(models.Model): + text = models.CharField(max_length=255) + + def __unicode__(self): + return self.text diff --git a/wagtail/wagtailsnippets/models.py b/wagtail/wagtailsnippets/models.py index b929f06aa2..8af6266555 100644 --- a/wagtail/wagtailsnippets/models.py +++ b/wagtail/wagtailsnippets/models.py @@ -19,3 +19,4 @@ def get_snippet_content_types(): def register_snippet(model): if model not in SNIPPET_MODELS: SNIPPET_MODELS.append(model) + SNIPPET_MODELS.sort(key=lambda x: x._meta.verbose_name) diff --git a/wagtail/wagtailsnippets/tests.py b/wagtail/wagtailsnippets/tests.py index 727e5e52df..de5e60b9b3 100644 --- a/wagtail/wagtailsnippets/tests.py +++ b/wagtail/wagtailsnippets/tests.py @@ -3,7 +3,8 @@ from django.core.urlresolvers import reverse from django.contrib.auth.models import User from wagtail.tests.utils import login, unittest -from wagtail.tests.models import Advert +from wagtail.tests.models import Advert, AlphaSnippet, ZuluSnippet +from wagtail.wagtailsnippets.models import register_snippet, SNIPPET_MODELS from wagtail.wagtailsnippets.views.snippets import get_content_type_from_url_params, get_snippet_edit_handler from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel @@ -168,3 +169,16 @@ class TestSnippetChooserPanel(TestCase): def test_render_js(self): self.assertTrue("createSnippetChooser(fixPrefix('id_text'), 'contenttypes/contenttype');" in self.snippet_chooser_panel.render_js()) + + +class TestSnippetOrdering(TestCase): + def setUp(self): + register_snippet(ZuluSnippet) + register_snippet(AlphaSnippet) + + def test_snippets_ordering(self): + # Ensure AlphaSnippet is before ZuluSnippet + # Cannot check first and last position as other snippets + # may get registered elsewhere during test + self.assertLess(SNIPPET_MODELS.index(AlphaSnippet), + SNIPPET_MODELS.index(ZuluSnippet))