-
Notifications
You must be signed in to change notification settings - Fork 80
/
test_cms_plugins_organizations_by_category.py
190 lines (156 loc) · 7.26 KB
/
test_cms_plugins_organizations_by_category.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""Unit tests for the Organizations by Category plugin and its model."""
import re
from django import forms
from cms.api import add_plugin
from cms.test_utils.testcases import CMSTestCase
from richie.apps.core.factories import PageFactory, UserFactory
from richie.apps.core.helpers import create_i18n_page
from richie.apps.courses.cms_plugins import OrganizationsByCategoryPlugin
from richie.apps.courses.factories import CategoryFactory, OrganizationFactory
from richie.apps.courses.models import OrganizationsByCategoryPluginModel
class OrganizationsByCategoryPluginTestCase(CMSTestCase):
"""
Test that OrganizationsByCategoryPlugin correctly displays a list of organizations related
to a category.
"""
def test_cms_plugins_organizations_by_category_form_page_choices(self):
"""
The form to create an organizations by category plugin should only list category pages
in the select box. There shouldn't be any duplicate because of published status.
"""
class OrganizationsByCategoryPluginModelForm(forms.ModelForm):
"""A form for testing the choices in the select box"""
class Meta:
model = OrganizationsByCategoryPluginModel
fields = ["page"]
category = CategoryFactory(should_publish=True)
PageFactory(title__title="other page", should_publish=True)
plugin_form = OrganizationsByCategoryPluginModelForm()
rendered_form = plugin_form.as_table()
self.assertEqual(rendered_form.count(category.extended_object.get_title()), 1)
self.assertNotIn("other", plugin_form.as_table())
def test_cms_plugins_organizations_by_category_render_on_public_page(self):
"""
The organizations by category plugin should render as expected on a public page.
"""
# Create a category
category = CategoryFactory(
page_title={"en": "category title", "fr": "titre catégorie"}
)
category_page = category.extended_object
# Create organizations
published_organization = OrganizationFactory(
page_title={"en": "public title", "fr": "titre public"},
fill_categories=[category],
fill_logo={"original_filename": "logo.jpg"},
should_publish=True,
)
OrganizationFactory(
page_title={"en": "private title", "fr": "titre privé"},
fill_categories=[category],
fill_logo={"original_filename": "logo.jpg"},
)
# Create a page to add the plugin to
page = create_i18n_page({"en": "A page", "fr": "Une page"})
placeholder = page.placeholders.get(slot="maincontent")
add_plugin(
placeholder, OrganizationsByCategoryPlugin, "en", **{"page": category_page}
)
add_plugin(
placeholder, OrganizationsByCategoryPlugin, "fr", **{"page": category_page}
)
category_page.publish("en")
category_page.publish("fr")
category.refresh_from_db()
page.publish("en")
page.publish("fr")
# Check the page content in English
url = page.get_absolute_url(language="en")
# The plugin should not be visible on the public page before it is published
category_page.unpublish("en")
response = self.client.get(url)
self.assertNotContains(response, "public title")
# # Republish the plugin
category_page.publish("en")
# Now modify the organization to have a draft different from the public version
title_obj = published_organization.extended_object.get_title_obj(language="en")
title_obj.title = "draft title"
title_obj.save()
# Publishing the page again should make the plugin public
page.publish("en")
# Check the page content in English
response = self.client.get(url)
# The organization's name should be present as a link to the cms page
# And CMS page title should be in title attribute of the link
self.assertIn(
'<a class="organization-glimpse" '
'href="/en/public-title/" title="public title"',
re.sub(" +", " ", str(response.content).replace("\\n", "")),
)
# The organization's title should be wrapped in a div
self.assertContains(
response,
'<div class="organization-glimpse__title">{:s}</div>'.format(
published_organization.public_extension.extended_object.get_title()
),
html=True,
)
self.assertNotContains(response, "draft")
self.assertNotContains(response, "private")
# Organization's logo should be present
pattern = (
r'<div class="organization-glimpse__logo">'
r'<img src="/media/filer_public_thumbnails/filer_public/.*logo\.jpg__200x113'
r'.*alt=""'
)
self.assertIsNotNone(re.search(pattern, str(response.content)))
# Same checks in French
url = page.get_absolute_url(language="fr")
response = self.client.get(url)
self.assertIn(
'<a class="organization-glimpse" '
'href="/fr/titre-public/" title="titre public"',
re.sub(" +", " ", str(response.content).replace("\\n", "")),
)
pattern = (
r'<div class="organization-glimpse__logo">'
r'<img src="/media/filer_public_thumbnails/filer_public/.*logo\.jpg__200x113'
r'.*alt=""'
)
self.assertIsNotNone(re.search(pattern, str(response.content)))
def test_cms_plugins_organizations_by_category_render_on_draft_page(self):
"""
The organization plugin should render as expected on a draft page.
"""
staff = UserFactory(is_staff=True, is_superuser=True)
self.client.login(username=staff.username, password="password")
# Create a category
category = CategoryFactory(page_title="public title")
category_page = category.extended_object
# Create organizations
organization = OrganizationFactory(
page_title={"en": "public title", "fr": "titre public"},
fill_categories=[category],
fill_logo={"original_filename": "logo.jpg"},
)
# Create a page to add the plugin to
page = create_i18n_page("A page")
placeholder = page.placeholders.get(slot="maincontent")
add_plugin(
placeholder, OrganizationsByCategoryPlugin, "en", **{"page": category_page}
)
category_page.publish("en")
category_page.unpublish("en")
category_page.refresh_from_db()
url = "{:s}?edit".format(page.get_absolute_url(language="en"))
# The organization plugin should still be visible on the draft page
response = self.client.get(url)
self.assertContains(response, "public title")
# Now modify the organization to have a draft different from the public version
title_obj = organization.extended_object.get_title_obj(language="en")
title_obj.title = "draft title"
title_obj.save()
# The draft version of the organization plugin should now be visible
response = self.client.get(url)
self.assertContains(response, "draft title")
self.assertNotContains(response, "public title")