def test_cms_wizards_blogpost_submit_form_max_lengths(self):
        """
        Check that the form correctly raises an error when the slug is too long. The path built
        by combining the slug of the page with the slug of its parent page, should not exceed
        255 characters in length.
        """
        # A parent page with a very long slug
        create_page(
            "y" * 200,
            "richie/single_column.html",
            "en",
            reverse_id=BlogPost.PAGE["reverse_id"],
        )

        # A blogpost with a slug at the limit length should work
        user = UserFactory(is_staff=True, is_superuser=True)
        form = BlogPostWizardForm(
            data={
                "title": "t" * 255,
                "slug": "s" * 54
            },
            wizard_language="en",
            wizard_user=user,
        )
        self.assertTrue(form.is_valid())
        form.save()

        # A blogpost with a slug too long with regards to the parent's one should raise an error
        form = BlogPostWizardForm(
            data={
                "title": "t" * 255,
                "slug": "s" * 55
            },
            wizard_language="en",
            wizard_user=user,
        )
        self.assertFalse(form.is_valid())
        self.assertEqual(
            form.errors["slug"][0],
            ("This slug is too long. The length of the path built by prepending the slug of "
             "the parent page would be 256 characters long and it should be less than 255"
             ),
        )
    def test_cms_wizards_blogpost_submit_form(self):
        """
        A user with the required permissions submitting a valid BlogPostWizardForm should be able
        to create a BlogPost page extension and its related page.
        """
        any_page = create_page("Any page", "richie/single_column.html", "en")

        # A parent page should pre-exist
        create_page(
            "News",
            "richie/single_column.html",
            "en",
            reverse_id=BlogPost.PAGE["reverse_id"],
        )

        # Create a user with just the required permissions
        user = UserFactory(
            is_staff=True,
            permissions=[
                "courses.add_blogpost", "cms.add_page", "cms.change_page"
            ],
        )

        # We can submit a form with just the title set
        form = BlogPostWizardForm(
            data={"title": "My title"},
            wizard_language="en",
            wizard_user=user,
            wizard_page=any_page,
        )
        self.assertTrue(form.is_valid())
        blog_page = form.save()

        # Related page should have been created as draft
        Page.objects.drafts().get(id=blog_page.id)
        BlogPost.objects.get(id=blog_page.blogpost.id,
                             extended_object=blog_page)

        self.assertEqual(blog_page.get_title(), "My title")
        # The slug should have been automatically set
        self.assertEqual(blog_page.get_slug(), "my-title")
    def test_cms_wizards_blogpost_submit_form_slugify_long_title(self):
        """
        When generating the slug from the title, we should respect the slug's "max_length"
        """
        # A parent page should pre-exist
        create_page(
            "News",
            "richie/single_column.html",
            "en",
            reverse_id=BlogPost.PAGE["reverse_id"],
        )

        # Submit a title at max length
        data = {"title": "t" * 255}
        user = UserFactory(is_staff=True, is_superuser=True)
        form = BlogPostWizardForm(data=data, wizard_language="en", wizard_user=user)

        self.assertTrue(form.is_valid())
        page = form.save()
        # Check that the slug has been truncated
        self.assertEqual(page.get_slug(), "t" * 200)