1# -*- coding: utf-8 -*-
2
3from django.contrib.admin.sites import site
4from django.template import Context
5from django.template.base import Template
6from django.utils import six
7
8from cms.api import add_plugin
9from cms.models import StaticPlaceholder, Placeholder, UserSettings
10from cms.tests.test_plugins import PluginsTestBaseCase
11from cms.utils.urlutils import admin_reverse
12
13
14class StaticPlaceholderTestCase(PluginsTestBaseCase):
15    @property
16    def admin_class(self):
17        return site._registry[StaticPlaceholder]
18
19    def fill_placeholder(self, placeholder=None):
20        if placeholder is None:
21            placeholder = Placeholder(slot=u"some_slot")
22            placeholder.save()  # a good idea, if not strictly necessary
23
24
25        # plugin in placeholder
26        plugin_1 = add_plugin(placeholder, u"TextPlugin", u"en",
27                              body=u"01",
28        )
29        plugin_1.save()
30
31        # IMPORTANT: plugins must be reloaded, before they can be assigned
32        # as a parent. Otherwise, the MPTT structure doesn't seem to rebuild
33        # properly.
34
35        # child of plugin_1
36        plugin_2 = add_plugin(placeholder, u"TextPlugin", u"en",
37                              body=u"02",
38        )
39        plugin_1 = self.reload(plugin_1)
40        plugin_2.parent = plugin_1
41        plugin_2.save()
42        return placeholder
43
44    def get_admin(self):
45        usr = self._create_user("admin", True, True)
46        return usr
47
48    def test_template_creation(self):
49        self.assertObjectDoesNotExist(StaticPlaceholder.objects.all(), code='foobar')
50        self.assertObjectDoesNotExist(Placeholder.objects.all(), slot='foobar')
51        t = Template('{% load cms_tags %}{% static_placeholder "foobar" %}')
52        t.render(self.get_context('/'))
53        self.assertObjectExist(StaticPlaceholder.objects.all(), code='foobar',
54                               creation_method=StaticPlaceholder.CREATION_BY_TEMPLATE)
55        self.assertEqual(Placeholder.objects.filter(slot='foobar').count(), 2)
56
57    def test_empty(self):
58        self.assertObjectDoesNotExist(StaticPlaceholder.objects.all(), code='foobar')
59        self.assertObjectDoesNotExist(Placeholder.objects.all(), slot='foobar')
60        t = Template('{% load cms_tags %}{% static_placeholder "foobar" or %}No Content{% endstatic_placeholder %}')
61        rendered = t.render(self.get_context('/'))
62        self.assertIn("No Content", rendered)
63
64        t = Template('{% load cms_tags %}{% static_placeholder "" %}')
65        rendered = t.render(self.get_context('/'))
66        self.assertEqual("", rendered)
67
68        t = Template('{% load cms_tags %}{% static_placeholder code or %}No Content{% endstatic_placeholder %}')
69        rendered = t.render(Context({'code': StaticPlaceholder.objects.all()[0]}))
70        self.assertIn("No Content", rendered)
71
72        for p in Placeholder.objects.all():
73            add_plugin(p, 'TextPlugin', 'en', body='test')
74        t = Template('{% load cms_tags %}{% static_placeholder "foobar" or %}No Content{% endstatic_placeholder %}')
75        rendered = t.render(self.get_context('/'))
76        self.assertNotIn("No Content", rendered)
77        self.assertEqual(StaticPlaceholder.objects.filter(site_id__isnull=True, code='foobar').count(), 1)
78
79    def test_local(self):
80        self.assertObjectDoesNotExist(StaticPlaceholder.objects.all(), code='foobar')
81        self.assertObjectDoesNotExist(Placeholder.objects.all(), slot='foobar')
82        t = Template('{% load cms_tags %}{% static_placeholder "foobar" site or %}No Content{% endstatic_placeholder %}')
83        rendered = t.render(self.get_context('/'))
84        self.assertIn("No Content", rendered)
85        for p in Placeholder.objects.all():
86            add_plugin(p, 'TextPlugin', 'en', body='test')
87        rendered = t.render(self.get_context('/'))
88        self.assertNotIn("No Content", rendered)
89        self.assertEqual(StaticPlaceholder.objects.filter(site_id__isnull=False, code='foobar').count(), 1)
90
91    def test_publish_stack(self):
92        static_placeholder = StaticPlaceholder.objects.create(name='foo', code='bar', site_id=1)
93        self.fill_placeholder(static_placeholder.draft)
94        static_placeholder.dirty = True
95        static_placeholder.save()
96        self.assertEqual(static_placeholder.draft.cmsplugin_set.all().count(), 2)
97        self.assertEqual(static_placeholder.public.cmsplugin_set.all().count(), 0)
98        with self.login_user_context(self.get_superuser()):
99            response = self.client.post('%s?statics=%s' % (admin_reverse("cms_page_publish_page", args=[1, 'en']), static_placeholder.pk))
100            self.assertEqual(response.status_code, 302)
101
102    def test_permissions(self):
103        static_placeholder = StaticPlaceholder.objects.create(name='foo', code='bar', site_id=1)
104        request = self.get_request()
105
106        request.user = self._create_user('user_a', is_staff=True, is_superuser=False, permissions=['change_staticplaceholder'])
107        self.assertTrue( static_placeholder.has_change_permission(request) )
108        self.assertFalse( static_placeholder.has_publish_permission(request) )
109
110        request.user = self._create_user('user_b', is_staff=True, is_superuser=False, permissions=['change_staticplaceholder', 'publish_page'])
111        self.assertTrue( static_placeholder.has_change_permission(request) )
112        self.assertTrue( static_placeholder.has_publish_permission(request) )
113
114        request.user = self.get_superuser()
115        self.assertTrue( static_placeholder.has_change_permission(request) )
116        self.assertTrue( static_placeholder.has_publish_permission(request) )
117
118    def test_move_plugin(self):
119        static_placeholder_source = StaticPlaceholder.objects.create(name='foobar', code='foobar', site_id=1)
120        static_placeholder_target = StaticPlaceholder.objects.create(name='foofoo', code='foofoo', site_id=1)
121        sourceplugin = add_plugin(static_placeholder_source.draft, 'TextPlugin', 'en', body='test')
122
123        admin = self.get_admin()
124
125        with self.login_user_context(admin):
126            endpoint = self.get_move_plugin_uri(sourceplugin, container=StaticPlaceholder)
127            data = {
128                'plugin_id': sourceplugin.pk,
129                'placeholder_id': static_placeholder_target.draft.id,
130                'plugin_parent': '',
131                'target_language': 'en',
132            }
133            response = self.client.post(endpoint, data)
134            self.assertEqual(response.status_code, 200)
135            source = StaticPlaceholder.objects.get(pk=static_placeholder_source.pk)
136            target = StaticPlaceholder.objects.get(pk=static_placeholder_target.pk)
137            self.assertTrue(source.dirty)
138            self.assertTrue(target.dirty)
139
140    def test_paste_plugin(self):
141        admin = self.get_admin()
142        user_settings = UserSettings.objects.create(
143            language="en",
144            user=admin,
145            clipboard=Placeholder.objects.create(slot='clipboard'),
146        )
147        clipboard = user_settings.clipboard
148        static_placeholder_target = StaticPlaceholder.objects.create(name='foofoo', code='foofoo', site_id=1)
149
150        plugin = add_plugin(clipboard, 'TextPlugin', 'en', body='test source')
151
152        endpoint = self.get_move_plugin_uri(plugin)
153
154        data = {
155            'plugin_id': plugin.pk,
156            'placeholder_id': static_placeholder_target.draft.pk,
157            'move_a_copy': 'true',
158            'target_language': 'en',
159            'plugin_order[]': ['__COPY__'],
160        }
161
162        with self.login_user_context(admin):
163            response = self.client.post(endpoint, data)
164            static_placeholder_target = self.reload(static_placeholder_target)
165
166            self.assertEqual(response.status_code, 200)
167            self.assertTrue(static_placeholder_target.dirty)
168            self.assertTrue(static_placeholder_target.draft.get_plugins(plugin.language).exists())
169
170    def test_create_by_admin(self):
171        url = admin_reverse("cms_staticplaceholder_add")
172        with self.login_user_context(self.get_superuser()):
173            response = self.client.post(url, data={'name': 'Name', 'code': 'content'})
174            self.assertEqual(response.status_code, 302)
175
176    def test_display_name(self):
177        without_name = StaticPlaceholder.objects.create(code='foobar', site_id=1)
178        without_code = StaticPlaceholder.objects.create(site_id=1)
179
180        self.assertEqual(without_name.get_name(), without_name.code)
181        self.assertEqual(without_code.get_name(), six.text_type(without_code.pk))
182