Browse Source

Add section to testing docs about working with pages

- Closes #11244
Mariana Bedran Lesche 1 year ago
parent
commit
ca8a87077b
4 changed files with 72 additions and 0 deletions
  1. 1 0
      CHANGELOG.txt
  2. 1 0
      CONTRIBUTORS.md
  3. 69 0
      docs/advanced_topics/testing.md
  4. 1 0
      docs/releases/6.0.md

+ 1 - 0
CHANGELOG.txt

@@ -25,6 +25,7 @@ Changelog
  * Fix: Ensure image focal point box can be removed (Gunnar Scherf)
  * Docs: Document, for contributors, the use of translate string literals passed as arguments to tags and filters using `_()` within templates (Chiemezuo Akujobi)
  * Docs: Document all features for the Documents app in one location (Neeraj Yetheendran)
+ * Docs: Add section to testing docs about creating pages and working with page content (Mariana Bedran Lesche)
  * Maintenance: Update BeautifulSoup upper bound to 4.12.x (scott-8)
  * Maintenance: Migrate initialization of classes (such as `body.ready`) from multiple JavaScript implementations to one Stimulus controller `w-init` (Chiemezuo Akujobi)
  * Maintenance: Adopt the usage of of translate string literals using `arg=_('...')` in all `wagtailadmin` module templates (Chiemezuo Akujobi)

+ 1 - 0
CONTRIBUTORS.md

@@ -770,6 +770,7 @@
 * Joe Tsoi
 * Cassidy Pittman
 * Gunnar Scherf
+* Mariana Bedran Lesche
 
 ## Translators
 

+ 69 - 0
docs/advanced_topics/testing.md

@@ -214,6 +214,75 @@ def test_content_page_subpages(self):
    .. autofunction:: inline_formset
 ```
 
+## Creating Page objects within tests
+
+If you want to create page objects within tests, you will need to go through some steps before actually creating the page you want to test.
+
+-   Pages can't be created directly with `MyPage.objects.create()` as you would do with a regular Django model, they need to be added as children to a parent page with `parent.add_child(instance=child)`.
+-   To start the page tree, you need a root page that can be created with `Page.get_first_root_node()`.
+-   You also need a `Site` set up with the correct `hostname` and a `root_page`.
+
+```python
+from wagtail.models import Page, Site
+from wagtail.rich_text import RichText
+from wagtail.test.utils import WagtailPageTestCase
+
+from home.models import HomePage, MyPage
+
+
+class MyPageTest(WagtailPageTestCase):
+    @classmethod
+    def setUpTestData(cls):
+        root = Page.get_first_root_node()
+        Site.objects.create(
+            hostname="testserver",
+            root_page=root,
+            is_default_site=True,
+            site_name="testserver",
+        )
+        home = HomePage(title="Home")
+        root.add_child(instance=home)
+        cls.page = MyPage(
+            title="My Page",
+            slug="mypage",
+        )
+        home.add_child(instance=cls.page)
+
+    def test_get(self):
+        response = self.client.get(self.page.url)
+        self.assertEqual(response.status_code, 200)
+```
+
+### Working with Page content
+
+You will likely want to test the content of your page. If it includes a `StreamField`, you will need to set its content as a list of tuples with the block's name and content. For `RichTextBlock`, the content has to be an instance of `RichText`.
+
+```python
+...
+from wagtail.rich_text import RichText
+
+class MyPageTest(WagtailPageTestCase):
+    @classmethod
+    def setUpTestData(cls):
+        ...
+        # Create page instance here
+        cls.page.body.extend(
+            [
+                ("heading", "Just a CharField Heading"),
+                ("paragraph", RichText("<p>First paragraph</p>")),
+                ("paragraph", RichText("<p>Second paragraph</p>")),
+            ]
+        )
+        cls.page.save()
+
+    def test_page_content(self):
+        response = self.client.get(self.page.url)
+        self.assertEqual(response.status_code, 200)
+        self.assertContains(response, "Just a CharField Heading")
+        self.assertContains(response, "<p>First paragraph</p>")
+        self.assertContains(response, "<p>Second paragraph</p>")
+```
+
 ## Fixtures
 
 ### Using `dumpdata`

+ 1 - 0
docs/releases/6.0.md

@@ -41,6 +41,7 @@ depth: 1
 
  * Document, for contributors, the use of translate string literals passed as arguments to tags and filters using `_()` within templates (Chiemezuo Akujobi)
  * Document all features for the Documents app in one location, see [](../advanced_topics/documents/index) (Neeraj Yetheendran)
+ * Add section to [testing docs](../docs/advanced_topics/testing) about creating pages and working with page content (Mariana Bedran Lesche)
 
 ### Maintenance