|
| 1 | +import unittest |
| 2 | + |
| 3 | +from cms.api import add_plugin, create_page |
| 4 | +from cms.models import PageContent |
| 5 | +from django.apps import apps |
| 6 | +from django.contrib.contenttypes.models import ContentType |
| 7 | +from rest_framework.reverse import reverse |
| 8 | + |
| 9 | +from tests.base import BaseCMSRestTestCase |
| 10 | +from tests.types import PLACEHOLDER_FIELD_TYPES |
| 11 | +from tests.utils import assert_field_types |
| 12 | + |
| 13 | +try: |
| 14 | + from djangocms_alias.models import Alias, AliasContent, Category |
| 15 | + |
| 16 | + HAS_ALIAS = True |
| 17 | +except ImportError: |
| 18 | + HAS_ALIAS = False |
| 19 | + |
| 20 | + |
| 21 | +@unittest.skipUnless(HAS_ALIAS and apps.is_installed("djangocms_alias"), "djangocms_alias is not installed") |
| 22 | +class AliasAPITestCase(BaseCMSRestTestCase): |
| 23 | + @classmethod |
| 24 | + def setUpClass(cls): |
| 25 | + """ |
| 26 | + Add alias with plugin content to a test page placeholder. |
| 27 | + """ |
| 28 | + super().setUpClass() |
| 29 | + |
| 30 | + cls.page = create_page( |
| 31 | + title="Alias Test Page", |
| 32 | + template="INHERIT", |
| 33 | + language="en", |
| 34 | + in_navigation=True, |
| 35 | + ) |
| 36 | + cls.page_content = PageContent.objects.get(page=cls.page, language="en") |
| 37 | + cls.page_content_type = ContentType.objects.get(model="pagecontent") |
| 38 | + cls.page_placeholder = cls.page.get_placeholders(language="en").get(slot="content") |
| 39 | + |
| 40 | + cls.category = Category.objects.create() |
| 41 | + cls.category.set_current_language("en") |
| 42 | + cls.category.name = "Alias Category" |
| 43 | + cls.category.save() |
| 44 | + |
| 45 | + cls.alias = Alias.objects.create(category=cls.category, static_code="test-alias") |
| 46 | + cls.alias_content = AliasContent.objects.create( |
| 47 | + alias=cls.alias, |
| 48 | + name="Alias Content", |
| 49 | + language="en", |
| 50 | + ) |
| 51 | + cls.alias_placeholder = cls.alias_content.placeholder |
| 52 | + |
| 53 | + add_plugin( |
| 54 | + placeholder=cls.alias_placeholder, |
| 55 | + plugin_type="TextPlugin", |
| 56 | + language="en", |
| 57 | + body="<p>Alias text</p>", |
| 58 | + ) |
| 59 | + |
| 60 | + add_plugin( |
| 61 | + placeholder=cls.page_placeholder, |
| 62 | + plugin_type="Alias", |
| 63 | + language="en", |
| 64 | + alias=cls.alias, |
| 65 | + ) |
| 66 | + |
| 67 | + def test_get(self): |
| 68 | + """ |
| 69 | + Tests alias plugin rendering via the placeholder detail endpoint. |
| 70 | +
|
| 71 | + Verifies: |
| 72 | + - Endpoint returns 200 OK for valid requests |
| 73 | + - Response structure matches placeholder field types |
| 74 | + - Alias plugin resolves its content with nested plugins |
| 75 | + """ |
| 76 | + |
| 77 | + type_checks = PLACEHOLDER_FIELD_TYPES |
| 78 | + |
| 79 | + plugin_type_checks = { |
| 80 | + "plugin_type": str, |
| 81 | + "content": list, |
| 82 | + } |
| 83 | + |
| 84 | + # GET request |
| 85 | + response = self.client.get( |
| 86 | + reverse( |
| 87 | + "placeholder-detail", |
| 88 | + kwargs={ |
| 89 | + "language": "en", |
| 90 | + "content_type_id": self.page_content_type.id, |
| 91 | + "object_id": self.page_content.id, |
| 92 | + "slot": "content", |
| 93 | + }, |
| 94 | + ) |
| 95 | + ) |
| 96 | + self.assertEqual(response.status_code, 200) |
| 97 | + placeholder = response.json() |
| 98 | + |
| 99 | + # Placeholder Validation |
| 100 | + for field, expected_type in type_checks.items(): |
| 101 | + assert_field_types( |
| 102 | + self, |
| 103 | + placeholder, |
| 104 | + field, |
| 105 | + expected_type, |
| 106 | + ) |
| 107 | + |
| 108 | + # Plugin Type Validation |
| 109 | + for plugin in placeholder["content"]: |
| 110 | + for field, expected_type in plugin_type_checks.items(): |
| 111 | + assert_field_types( |
| 112 | + self, |
| 113 | + plugin, |
| 114 | + field, |
| 115 | + expected_type, |
| 116 | + ) |
| 117 | + |
| 118 | + # Alias content validation |
| 119 | + alias_plugin = placeholder["content"][0] |
| 120 | + self.assertTrue(alias_plugin["content"]) |
| 121 | + self.assertEqual(alias_plugin["content"][0]["plugin_type"], "TextPlugin") |
| 122 | + self.assertIn("Alias text", alias_plugin["content"][0]["body"]) |
| 123 | + |
| 124 | + def test_circular_alias(self): |
| 125 | + """ |
| 126 | + Tests that a circular alias reference returns empty content |
| 127 | + instead of recursing infinitely. |
| 128 | + """ |
| 129 | + # Add the alias as a plugin inside its own placeholder |
| 130 | + add_plugin( |
| 131 | + placeholder=self.alias_placeholder, |
| 132 | + plugin_type="Alias", |
| 133 | + language="en", |
| 134 | + alias=self.alias, |
| 135 | + ) |
| 136 | + |
| 137 | + response = self.client.get( |
| 138 | + reverse( |
| 139 | + "placeholder-detail", |
| 140 | + kwargs={ |
| 141 | + "language": "en", |
| 142 | + "content_type_id": self.page_content_type.id, |
| 143 | + "object_id": self.page_content.id, |
| 144 | + "slot": "content", |
| 145 | + }, |
| 146 | + ) |
| 147 | + ) |
| 148 | + self.assertEqual(response.status_code, 200) |
| 149 | + placeholder = response.json() |
| 150 | + |
| 151 | + # The top-level alias plugin should resolve its content |
| 152 | + alias_plugin = placeholder["content"][0] |
| 153 | + self.assertTrue(alias_plugin["content"]) |
| 154 | + |
| 155 | + # The nested self-referencing alias plugin should have empty content |
| 156 | + nested_alias = next( |
| 157 | + plugin for plugin in alias_plugin["content"] if plugin.get("plugin_type") in ("Alias", "AliasPlugin") |
| 158 | + ) |
| 159 | + self.assertEqual(nested_alias["content"], []) |
0 commit comments