1from cms.api import create_page
2from cms.test_utils.testcases import CMSTestCase
3from cms.test_utils.project.sampleapp.models import (
4    PageOnDeleteCascade, PageOnDeleteSetNull,
5    PlaceholderOnDeleteCascade, PlaceholderOnDeleteSetNull,
6)
7
8
9class PageFieldOnDeleteTestCase(CMSTestCase):
10    def setUp(self):
11        super(PageFieldOnDeleteTestCase, self).setUp()
12        self.page = create_page(
13            'on delete test page',
14            template='nav_playground.html',
15            language='en',
16            published=True,
17        )
18        self.page.reload()
19
20    def test_page_field_on_delete_cascade(self):
21        """
22        PageField.on_delete defaults to CASCADE
23        """
24        on_delete_model = PageOnDeleteCascade.objects.create(page=self.page)
25        on_delete_model.page.delete()
26        self.assertFalse(PageOnDeleteCascade.objects.filter(pk=on_delete_model.pk).exists())
27
28    def test_page_field_on_delete_set_null(self):
29        """
30        PageField uses on_delete override
31        """
32        on_delete_model = PageOnDeleteSetNull.objects.create(page=self.page)
33        on_delete_model.page.delete()
34        on_delete_model.refresh_from_db()
35        self.assertIsNotNone(on_delete_model)
36        self.assertIsNone(on_delete_model.page)
37
38
39class PlaceholderFieldOnDeleteTestCase(CMSTestCase):
40    def setUp(self):
41        super(PlaceholderFieldOnDeleteTestCase, self).setUp()
42        self.page = create_page(
43            'on delete test page',
44            template='nav_playground.html',
45            language='en',
46            published=True,
47        )
48        self.page.reload()
49        self.placeholder = self.page.get_placeholders().get(slot='body')
50
51    def test_placeholder_field_on_delete_cascade(self):
52        """
53        PlaceholderField.on_delete defaults to CASCADE
54        """
55        on_delete_model = PlaceholderOnDeleteCascade.objects.create(placeholder=self.placeholder)
56        on_delete_model.placeholder.delete()
57        self.assertFalse(PlaceholderOnDeleteCascade.objects.filter(pk=on_delete_model.pk).exists())
58
59    def test_placeholder_field_on_delete_set_null(self):
60        """
61        PlaceholderField uses on_delete override
62        """
63        on_delete_model = PlaceholderOnDeleteSetNull.objects.create(placeholder=self.placeholder)
64        on_delete_model.placeholder.delete()
65        on_delete_model.refresh_from_db()
66        self.assertIsNotNone(on_delete_model)
67        self.assertIsNone(on_delete_model.placeholder)
68