def test_page_signals_publish_unpublish_example(self):
        """
        The example in the docs provides the following example to the page publish and unpublish signals.
        """
        signal_hits = list()

        # Signal example
        @receiver(post_version_operation, sender=PageContent)
        def do_something_on_page_publish_unpublsh(*args, **kwargs):

            if (
                kwargs["operation"] == constants.OPERATION_PUBLISH
                or kwargs["operation"] == constants.OPERATION_UNPUBLISH
            ):
                # Storing the state of the operation and object at this moment to compare the state later
                obj = dict()
                obj["state"] = kwargs["obj"].state
                signal_hits.append(obj)

        version_1 = factories.PageVersionFactory(
            state=constants.DRAFT, content__template=""
        )
        version_2 = factories.PageVersionFactory(
            state=constants.DRAFT, content__template=""
        )
        version_1.publish(self.superuser)
        version_1.unpublish(self.superuser)
        version_2.archive(self.superuser)

        # Only the publish and unpublish signals should have had an affect
        self.assertEqual(len(signal_hits), 2)
        self.assertEqual(signal_hits[0].get("state"), constants.PUBLISHED)
        self.assertEqual(signal_hits[1].get("state"), constants.UNPUBLISHED)
    def setUp(self):
        self.wf = WorkflowFactory(name="Workflow Test")
        self.collection = ModerationCollectionFactory(
            author=self.user,
            name="Collection Admin Actions",
            workflow=self.wf,
            status=constants.IN_REVIEW,
        )

        pg1_version = factories.PageVersionFactory()
        pg2_version = factories.PageVersionFactory()

        self.mr1n = RootModerationRequestTreeNodeFactory(
            moderation_request__version=pg1_version,
            moderation_request__collection=self.collection,
            moderation_request__is_active=True,
        )
        self.mr1 = self.mr1n.moderation_request

        self.wfst = self.wf.steps.create(role=self.role2,
                                         is_required=True,
                                         order=1)

        # this moderation request is approved
        self.mr1.actions.create(to_user=self.user2,
                                by_user=self.user,
                                action=constants.ACTION_STARTED)
        self.mr1action2 = self.mr1.actions.create(
            by_user=self.user,
            to_user=self.user2,
            action=constants.ACTION_APPROVED,
            step_approved=self.wfst,
        )

        # this moderation request is not approved
        self.mr2n = RootModerationRequestTreeNodeFactory(
            moderation_request__version=pg2_version,
            moderation_request__collection=self.collection,
            moderation_request__is_active=True,
        )
        self.mr2 = self.mr2n.moderation_request
        self.mr2.actions.create(to_user=self.user2,
                                by_user=self.user,
                                action=constants.ACTION_STARTED)

        self.url = reverse(
            "admin:djangocms_moderation_moderationrequest_changelist")
        self.url_with_filter = "{}?moderation_request__collection__id={}".format(
            self.url, self.collection.pk)
        self.mr_tree_admin = ModerationRequestTreeAdmin(
            ModerationRequest, admin.AdminSite())
        self.mra = ModerationRequestAdmin(ModerationRequest, admin.AdminSite())
        self.mca = ModerationCollectionAdmin(ModerationCollection,
                                             admin.AdminSite())
    def test_add_plugins_from_placeholder(self):
        version = factories.PageVersionFactory()
        source_placeholder = factories.PlaceholderFactory(source=version.content)
        target_placeholder = factories.PlaceholderFactory(source=version.content)
        poll = factories.PollFactory()

        plugin = add_plugin(
            source_placeholder, "PollPlugin", version.content.language, poll=poll
        )

        dt = datetime(2016, 6, 6, tzinfo=timezone.utc)
        with freeze_time(dt):
            endpoint = self.get_copy_plugin_uri(plugin)
            data = {
                "source_language": version.content.language,
                "source_placeholder_id": source_placeholder.pk,
                "target_language": version.content.language,
                "target_placeholder_id": target_placeholder.pk,
            }

            with self.login_user_context(self.get_superuser()):
                response = self.client.post(endpoint, data)
                self.assertEqual(response.status_code, 200)

        version = Version.objects.get(pk=version.pk)
        self.assertEqual(version.modified, dt)
    def test_cut_plugin(self):
        version = factories.PageVersionFactory()
        placeholder = factories.PlaceholderFactory(source=version.content)
        poll = factories.PollFactory()
        user_settings = UserSettings.objects.create(
            language=version.content.language,
            user=self.get_superuser(),
            clipboard=Placeholder.objects.create(slot="clipboard"),
        )

        plugin = add_plugin(
            placeholder, "PollPlugin", version.content.language, poll=poll
        )

        dt = datetime(2016, 6, 6, tzinfo=timezone.utc)
        with freeze_time(dt):
            endpoint = self.get_move_plugin_uri(plugin)
            data = {
                "plugin_id": plugin.pk,
                "target_language": version.content.language,
                "placeholder_id": user_settings.clipboard_id,
            }

            with self.login_user_context(self.get_superuser()):
                response = self.client.post(endpoint, data)
                self.assertEqual(response.status_code, 200)

        version = Version.objects.get(pk=version.pk)
        self.assertEqual(version.modified, dt)
Esempio n. 5
0
    def test_notify_version_author_version_unlocked_email_contents_users_full_name_used(
            self):
        """
        The email contains the full name of the author
        """
        user = self.user_has_unlock_perms
        user.first_name = "Firstname"
        user.last_name = "Lastname"
        user.save()
        draft_version = factories.PageVersionFactory(
            content__template="", created_by=self.user_author)
        draft_unlock_url = self.get_admin_url(
            self.versionable.version_model_proxy, 'unlock', draft_version.pk)

        # Check that no emails exist
        self.assertEqual(len(mail.outbox), 0)

        # Unlock the version with a different user with unlock permissions
        with self.login_user_context(user):
            self.client.post(draft_unlock_url, follow=True)

        expected_body = "The following draft version has been unlocked by {by_user} for their use.".format(
            by_user=user.get_full_name())

        self.assertEqual(len(mail.outbox), 1)
        self.assertTrue(expected_body in mail.outbox[0].body)
Esempio n. 6
0
    def test_notify_version_author_version_unlocked_email_sent_for_different_user(
            self):
        """
        The user unlocking a version that is authored buy a different user
        should be sent a notification email
        """
        draft_version = factories.PageVersionFactory(
            content__template="", created_by=self.user_author)
        draft_unlock_url = self.get_admin_url(
            self.versionable.version_model_proxy, 'unlock', draft_version.pk)

        # Check that no emails exist
        self.assertEqual(len(mail.outbox), 0)

        # Unlock the version with a different user with unlock permissions
        with self.login_user_context(self.user_has_unlock_perms):
            self.client.post(draft_unlock_url, follow=True)

        site = get_current_site()
        expected_subject = "[Django CMS] ({site_name}) {title} - {description}".format(
            site_name=site.name,
            title=draft_version.content,
            description=_("Unlocked"),
        )
        expected_body = "The following draft version has been unlocked by {by_user} for their use.".format(
            by_user=self.user_has_unlock_perms)
        expected_version_url = get_absolute_url(
            get_object_preview_url(draft_version.content))

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, expected_subject)
        self.assertEqual(mail.outbox[0].to[0], self.user_author.email)
        self.assertTrue(expected_body in mail.outbox[0].body)
        self.assertTrue(expected_version_url in mail.outbox[0].body)
    def test_placeholders_are_copied(self):
        """The implementation of versioning for PageContent correctly
        copies placeholders
        """
        original_version = factories.PageVersionFactory()
        original_placeholders = factories.PlaceholderFactory.create_batch(
            2, source=original_version.content)
        original_version.content.placeholders.add(*original_placeholders)
        user = factories.UserFactory()

        new_version = original_version.copy(user)

        new_placeholders = new_version.content.placeholders.all()
        self.assertEqual(new_placeholders.count(), 2)
        self.assertNotEqual(new_placeholders[0].pk,
                            original_placeholders[0].pk)
        self.assertEqual(new_placeholders[0].slot,
                         original_placeholders[0].slot)
        self.assertEqual(new_placeholders[0].default_width,
                         original_placeholders[0].default_width)
        self.assertEqual(new_placeholders[0].source, new_version.content)
        self.assertNotEqual(new_placeholders[1].pk,
                            original_placeholders[1].pk)
        self.assertEqual(new_placeholders[1].slot,
                         original_placeholders[1].slot)
        self.assertEqual(new_placeholders[1].default_width,
                         original_placeholders[1].default_width)
        self.assertEqual(new_placeholders[1].source, new_version.content)
 def test_grouper_selector_non_default_label_unpublished(self):
     """
     Grouper selector shows the PageContent label format when PageContent is set
     """
     version = factories.PageVersionFactory()
     form_class = grouper_form_factory(PageContent,
                                       version.content.language)
     label = "No available title (Unpublished)"
     self.assertIn((version.content.page.pk, label),
                   form_class.base_fields["page"].choices)
    def test_copy_plugins_method_used(self):
        original_version = factories.PageVersionFactory()
        placeholder = factories.PlaceholderFactory(
            source=original_version.content)
        original_version.content.placeholders.add(placeholder)
        user = factories.UserFactory()

        with patch("djangocms_versioning.cms_config.Placeholder.copy_plugins"
                   ) as mocked_copy:
            new_version = original_version.copy(user)

        new_placeholder = new_version.content.placeholders.get()
        mocked_copy.assert_called_once_with(new_placeholder)
    def test_clear_placeholder(self):
        version = factories.PageVersionFactory()
        placeholder = factories.PlaceholderFactory(source=version.content)

        dt = datetime(2016, 6, 6, tzinfo=timezone.utc)
        with freeze_time(dt):
            endpoint = self.get_clear_placeholder_url(placeholder)

            with self.login_user_context(self.get_superuser()):
                response = self.client.post(endpoint, {"test": 0})
                self.assertEqual(response.status_code, 302)

        version = Version.objects.get(pk=version.pk)
        self.assertEqual(version.modified, dt)
    def test_text_plugins_are_copied(self):
        """The implementation of versioning for PageContent correctly
        copies text plugins
        """
        original_version = factories.PageVersionFactory()
        placeholder = factories.PlaceholderFactory(
            source=original_version.content)
        original_version.content.placeholders.add(placeholder)
        with freeze_time("2017-07-07"):
            # Make sure created in the past
            original_plugins = factories.TextPluginFactory.create_batch(
                2, placeholder=placeholder)
        user = factories.UserFactory()

        new_version = original_version.copy(user)

        new_plugins = new_version.content.placeholders.get().cmsplugin_set.all(
        )
        self.assertEqual(new_plugins.count(), 2)
        self.assertNotEqual(new_plugins[0].pk, original_plugins[0].pk)
        self.assertEqual(new_plugins[0].language, original_plugins[0].language)
        self.assertIsNone(new_plugins[0].parent)
        self.assertEqual(new_plugins[0].position, original_plugins[0].position)
        self.assertEqual(new_plugins[0].plugin_type,
                         original_plugins[0].plugin_type)
        self.assertEqual(
            new_plugins[0].djangocms_text_ckeditor_text.body,
            original_plugins[0].djangocms_text_ckeditor_text.body,
        )
        self.assertEqual(new_plugins[0].creation_date,
                         original_plugins[0].creation_date)
        self.assertEqual(new_plugins[0].changed_date, now())
        self.assertNotEqual(new_plugins[1].pk, original_plugins[1].pk)
        self.assertEqual(new_plugins[1].language, original_plugins[1].language)
        self.assertIsNone(new_plugins[1].parent)
        self.assertEqual(new_plugins[1].position, original_plugins[1].position)
        self.assertEqual(new_plugins[1].plugin_type,
                         original_plugins[1].plugin_type)
        self.assertEqual(
            new_plugins[1].djangocms_text_ckeditor_text.body,
            original_plugins[1].djangocms_text_ckeditor_text.body,
        )
        self.assertEqual(new_plugins[1].creation_date,
                         original_plugins[1].creation_date)
        self.assertEqual(new_plugins[1].changed_date, now())
    def test_add_plugin(self):
        version = factories.PageVersionFactory()
        placeholder = factories.PlaceholderFactory(source=version.content)
        poll = factories.PollFactory()
        dt = datetime(2016, 6, 6, tzinfo=timezone.utc)
        with freeze_time(dt):
            endpoint = self.get_add_plugin_uri(
                placeholder=placeholder,
                plugin_type="PollPlugin",
                language=version.content.language,
            )
            data = {"poll": poll.pk}

            with self.login_user_context(self.get_superuser()):
                response = self.client.post(endpoint, data)
                self.assertEqual(response.status_code, 200)
        version = Version.objects.get(pk=version.pk)
        self.assertEqual(version.modified, dt)
Esempio n. 13
0
    def test_notify_version_author_version_unlocked_email_not_sent_for_different_user(self):
        """
        The user unlocking a version that authored the version should not be
        sent a notification email
        """
        draft_version = factories.PageVersionFactory(content__template="", created_by=self.user_author)
        draft_unlock_url = self.get_admin_url(self.versionable.version_model_proxy,
                                              'unlock', draft_version.pk)

        # Check that no emails exist
        self.assertEqual(len(mail.outbox), 0)

        # Unlock the version the same user who authored it
        with self.login_user_context(self.user_author):
            self.client.post(draft_unlock_url, follow=True)

        # Check that no emails still exist
        self.assertEqual(len(mail.outbox), 0)
Esempio n. 14
0
    def setUp(self):
        self.site = Site.objects.get_current()
        self.user = self.get_superuser()
        self.language = 'en'
        self.title = 'test page'

        self.version = factories.PageVersionFactory(
            content__language='en',
            state=DRAFT,
        )
        factories.PageUrlFactory(
            page=self.version.content.page,
            language='en',
            path=slugify(self.title),
            slug=slugify(self.title),
        )

        self.page = self.version.content.page
        self.content = self.version.content
Esempio n. 15
0
 def test_grouper_selector_non_default_label(self):
     """
     Grouper selector shows the PageContent label format when PageContent is set
     """
     version = factories.PageVersionFactory()
     PageUrl.objects.create(
         page=version.content.page,
         language=version.content.language,
         path='test',
         slug='test',
     )
     version.publish(version.created_by)
     form_class = grouper_form_factory(PageContent, version.content.language)
     label = "{title} (/{path}/)".format(
         title=version.content.title,
         path=version.content.page.get_path(version.content.language))
     self.assertIn(
         (version.content.page.pk, label),
         form_class.base_fields['grouper'].choices,
     )
    def test_page_content_object_gets_duplicated(self):
        """The implementation of versioning for PageContent correctly
        copies the PageContent object
        """
        with freeze_time("2017-07-07"):
            # Make sure created in the past
            original_version = factories.PageVersionFactory()
        user = factories.UserFactory()

        new_version = original_version.copy(user)

        # Created a new content record
        self.assertNotEqual(original_version.content.pk,
                            new_version.content.pk)
        # Has the same fields as the original version
        self.assertEqual(original_version.content.title,
                         new_version.content.title)
        self.assertEqual(original_version.content.language,
                         new_version.content.language)
        # new content should have a future date
        self.assertGreater(new_version.content.creation_date,
                           original_version.content.creation_date)
        self.assertEqual(original_version.content.created_by,
                         new_version.content.created_by)
        self.assertEqual(new_version.content.changed_date, now())
        self.assertEqual(original_version.content.changed_by,
                         new_version.content.changed_by)
        self.assertEqual(original_version.content.in_navigation,
                         new_version.content.in_navigation)
        self.assertEqual(original_version.content.soft_root,
                         new_version.content.soft_root)
        self.assertEqual(original_version.content.template,
                         new_version.content.template)
        self.assertEqual(
            original_version.content.limit_visibility_in_menu,
            new_version.content.limit_visibility_in_menu,
        )
        self.assertEqual(original_version.content.xframe_options,
                         new_version.content.xframe_options)
        self.assertEqual(original_version.content.page,
                         new_version.content.page)