Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 1d01aad

Browse files
author
Greg Turner
committed
Documenting create_content_instance, and an example Fluent Page test creation.
1 parent 2e6a545 commit 1d01aad

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

docs/contributing/testing.md

+51
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,54 @@ with migrations applied. It will be restored to the test database in
4747

4848
`pg_dump -O -x -f test_initial_data.sql -d foo`
4949

50+
51+
# To create fluent pages in tests.
52+
53+
We do this by using dynamic fixture:
54+
55+
from django.contrib.auth import get_user_model
56+
from django_webtest import WebTest
57+
from django_dynamic_fixture import G
58+
from django.contrib.contenttypes.models import ContentType
59+
from django.contrib.sites.models import Site
60+
from icekit.models import Layout
61+
from icekit.page_types.layout_page.models import LayoutPage
62+
63+
User = get_user_model()
64+
65+
66+
class FluentPageTestCase(WebTest):
67+
def setUp(self):
68+
self.layout_1 = G(
69+
Layout,
70+
template_name='layout_page/layoutpage/layouts/default.html',
71+
)
72+
self.layout_1.content_types.add(ContentType.objects.get_for_model(LayoutPage))
73+
self.layout_1.save()
74+
self.staff_1 = User.objects.create(
75+
76+
is_staff=True,
77+
is_active=True,
78+
is_superuser=True,
79+
)
80+
self.page_1 = LayoutPage.objects.create(
81+
title='Test Page',
82+
slug='test-page',
83+
parent_site=Site.objects.first(),
84+
layout=self.layout_1,
85+
author=self.staff_1,
86+
status='p', # Publish the page
87+
)
88+
89+
90+
There is also a helper function to add content items:
91+
92+
from icekit.utils.fluent_contents import create_content_instance
93+
94+
...
95+
self.child_page_1 = create_content_instance(
96+
models.ContentItem,
97+
page=self.page_1,
98+
placeholder_name="main", # default
99+
**kwargs # arguments for initialising the ContentItem model
100+
)

icekit/utils/fluent_contents.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
# USEFUL FUNCTIONS FOR FLUENT CONTENTS #############################################################
55

66
# Fluent Contents Helper Functions #################################################################
7-
def create_content_instance(content_plugin_class, test_page, placeholder_name='main', **kwargs):
7+
def create_content_instance(content_plugin_class, page, placeholder_name='main', **kwargs):
88
"""
99
Creates a content instance from a content plugin class.
1010
1111
:param content_plugin_class: The class of the content plugin.
12-
:param test_page: The fluent_page instance to create the content
12+
:param page: The fluent_page instance to create the content
1313
instance one.
1414
:param placeholder_name: The placeholder name defined in the
1515
template. [DEFAULT: main]
@@ -18,23 +18,23 @@ def create_content_instance(content_plugin_class, test_page, placeholder_name='m
1818
:return: The content instance created.
1919
"""
2020
# Get the placeholders that are currently available for the slot.
21-
placeholders = test_page.get_placeholder_by_slot(placeholder_name)
21+
placeholders = page.get_placeholder_by_slot(placeholder_name)
2222

2323
# If a placeholder exists for the placeholder_name use the first one provided otherwise create
2424
# a new placeholder instance.
2525
if placeholders.exists():
2626
placeholder = placeholders[0]
2727
else:
28-
placeholder = test_page.create_placeholder(placeholder_name)
28+
placeholder = page.create_placeholder(placeholder_name)
2929

3030
# Obtain the content type for the page instance class.
31-
ct = ContentType.objects.get_for_model(type(test_page))
31+
ct = ContentType.objects.get_for_model(type(page))
3232

3333
# Create the actual plugin instance.
3434
try:
3535
content_instance = content_plugin_class.objects.create(
3636
parent_type=ct,
37-
parent_id=test_page.id,
37+
parent_id=page.id,
3838
placeholder=placeholder,
3939
**kwargs
4040
)

0 commit comments

Comments
 (0)