Ejemplo n.º 1
0
    def test_new_published_version_doesnt_change_status_of_versions_with_other_states(
        self
    ):
        """When versions relating to the same grouper have non-published
        states, these should not change upon creating a new published version
        """
        poll = factories.PollFactory()
        factories.PollVersionFactory(
            state=constants.DRAFT, content__poll=poll, content__language="en"
        )
        factories.PollVersionFactory(
            state=constants.ARCHIVED, content__poll=poll, content__language="en"
        )
        user = factories.UserFactory()
        version = factories.PollVersionFactory(
            state=constants.DRAFT, content__poll=poll, content__language="en"
        )

        version.publish(user)

        # Nothing has an unpublished state cause there were no published versions
        self.assertEqual(
            Version.objects.exclude(pk=version.pk)
            .filter(state=constants.UNPUBLISHED)
            .count(),
            0,
        )
Ejemplo n.º 2
0
    def test_new_published_version_doesnt_change_status_of_other_content_types(
            self):
        """Regression test for a bug in which filtering byt content_type
        was missed in the query that chooses versions to unpublish,
        thereby unpublishing all versions with a certain object_id, not
        just the versions we want to unpublish.
        """
        pv = factories.PollVersionFactory(state=constants.PUBLISHED,
                                          content__id=11,
                                          content__language='en')
        bv = factories.BlogPostVersionFactory(state=constants.PUBLISHED,
                                              content__id=11)
        version = factories.PollVersionFactory(
            state=constants.DRAFT,
            content__poll=pv.content.poll,
            content__language='en',
        )
        user = factories.UserFactory()

        version.publish(user)

        # Only poll version was changed
        pv_ = Version.objects.get(pk=pv.pk)
        self.assertEqual(pv_.state, constants.UNPUBLISHED)
        bv_ = Version.objects.get(pk=bv.pk)
        self.assertEqual(bv_.state, constants.PUBLISHED)
Ejemplo n.º 3
0
    def test_new_published_version_doesnt_change_status_of_published_versions_from_other_groupers(
            self):
        """When versions relating to different groupers have a new
        published version created, then this should not change the other
        published versions' status to unpublished.
        """
        factories.PollVersionFactory(state=constants.PUBLISHED)
        user = factories.UserFactory()
        version = factories.PollVersionFactory(state=constants.DRAFT)

        version.publish(user)

        # Both are still published versions because they relate to different groupers
        self.assertEqual(
            Version.objects.filter(state=constants.PUBLISHED).count(), 2)
 def test_modified_date(self):
     pv = factories.PollVersionFactory()
     dt = datetime(2016, 6, 6, tzinfo=timezone.utc)
     with freeze_time(dt):
         pv.content.save()
     pv = Version.objects.get(pk=pv.pk)
     self.assertEqual(pv.modified, dt)
Ejemplo n.º 5
0
    def test_new_published_version_causes_old_published_versions_to_change_to_unpublished(
        self
    ):
        """When versions relating to the same grouper have a new published
        version created, all old published version should be marked unpublished
        """
        poll = factories.PollFactory()
        factories.PollVersionFactory.create_batch(
            3, state=constants.PUBLISHED, content__poll=poll, content__language="en"
        )
        user = factories.UserFactory()
        version = factories.PollVersionFactory(
            state=constants.DRAFT, content__poll=poll, content__language="en"
        )

        version.publish(user)

        # Only one published version
        self.assertEqual(Version.objects.filter(state=constants.PUBLISHED).count(), 1)
        # Everything other than the last published version has status unpublished
        self.assertEqual(
            Version.objects.exclude(pk=version.pk)
            .filter(state=constants.UNPUBLISHED)
            .count(),
            3,
        )
    def test_content_object_gets_duplicated_with_default_copy(self):
        """When copying, the new version object should have a new
        related content object. The default copy method will copy all
        content fields (other than the pk) exactly as they were.
        """
        original_version = factories.PollVersionFactory()
        user = factories.UserFactory()
        versioning_app_ext = apps.get_app_config(
            "djangocms_versioning").cms_extension
        versionables_mock = self._create_versionables_mock(default_copy)

        with patch.object(versioning_app_ext, "versionables_by_content",
                          versionables_mock):
            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.text,
                         new_version.content.text)
        self.assertEqual(original_version.content.language,
                         new_version.content.language)
        self.assertEqual(original_version.content.poll,
                         new_version.content.poll)
Ejemplo n.º 7
0
    def test_new_draft_doesnt_change_status_of_drafts_of_other_content_types(
            self):
        """Regression test for a bug in which filtering by content_type
        was missed in the query that chooses versions to archive,
        thereby archiving all versions with a certain object_id, not
        just the versions we want to archive.
        """
        pv = factories.PollVersionFactory(
            state=constants.DRAFT,
            content__id=11,
            content__language='en',
        )
        bv = factories.BlogPostVersionFactory(state=constants.DRAFT,
                                              content__id=11)

        Version.objects.create(content=factories.PollContentFactory(
            poll=pv.content.poll,
            language='en',
        ),
                               created_by=factories.UserFactory(),
                               state=constants.DRAFT)

        # Only poll version was changed
        pv_ = Version.objects.get(pk=pv.pk)
        self.assertEqual(pv_.state, constants.ARCHIVED)
        bv_ = Version.objects.get(pk=bv.pk)
        self.assertEqual(bv_.state, constants.DRAFT)
    def test_archive_signals_fired(self):
        """
        When a version is changed to archived the correct signals are fired!
        """
        poll = factories.PollFactory()
        version = factories.PollVersionFactory(
            state=constants.DRAFT, content__poll=poll
        )

        with signal_tester(pre_version_operation, post_version_operation) as env:

            version.archive(self.superuser)

            self.assertEqual(env.call_count, 2)

            pre_call_kwargs = env.calls[0][1]
            post_call_kwargs = env.calls[1][1]

            # pre call
            self.assertTrue(pre_call_kwargs["token"] == post_call_kwargs["token"])
            self.assertEqual(pre_call_kwargs["operation"], constants.OPERATION_ARCHIVE)
            self.assertEqual(
                pre_call_kwargs["sender"], version.content_type.model_class()
            )
            self.assertEqual(pre_call_kwargs["obj"], version)
            # post call
            self.assertTrue("token" in post_call_kwargs)
            self.assertEqual(post_call_kwargs["operation"], constants.OPERATION_ARCHIVE)
            self.assertEqual(
                post_call_kwargs["sender"], version.content_type.model_class()
            )
            self.assertEqual(post_call_kwargs["obj"], version)
Ejemplo n.º 9
0
    def test_versioned_queryset_return_full_with_helper_method(self):
        """With an extra helper method we can return the full queryset"""
        factories.PollVersionFactory()
        normal_count = PollContent.objects.all()
        full_count = remove_published_where(PollContent.objects.all())

        self.assertEqual(normal_count.count(), 0)
        self.assertEqual(full_count.count(), 1)
Ejemplo n.º 10
0
    def test_new_draft_doesnt_change_status_of_drafts_with_other_states(self):
        """When versions relating to the same grouper have non-draft
        states, these should not change upon creating a new draft
        """
        poll = factories.PollFactory()
        factories.PollVersionFactory(state=constants.UNPUBLISHED,
                                     content__poll=poll)
        factories.PollVersionFactory(state=constants.PUBLISHED,
                                     content__poll=poll)

        version = Version.objects.create(
            content=factories.PollContentFactory(poll=poll),
            created_by=factories.UserFactory(),
            state=constants.DRAFT)

        # Nothing has an archived state cause there were no drafts
        self.assertEqual(
            Version.objects.exclude(pk=version.pk).filter(
                state=constants.ARCHIVED).count(), 0)
Ejemplo n.º 11
0
    def test_grouper_selector_default_label(self):
        """
        Grouper selector shows the default label format when no override is set
        """
        version = factories.PollVersionFactory()
        form_class = grouper_form_factory(PollContent, language='en')

        self.assertIn(
            (version.content.poll.pk, str(version.content.poll)),
            form_class.base_fields['grouper'].choices,
        )
    def test_version_number_for_sequentially_created_versions(self):
        """
        An object being updated in sequential order without any other object versions
        increments for each version
        """
        poll_1 = factories.PollFactory()

        p1_version_1 = factories.PollVersionFactory(content__poll=poll_1,
                                                    content__language="en")
        p1_version_2 = factories.PollVersionFactory(content__poll=poll_1,
                                                    content__language="en")
        p1_version_3 = factories.PollVersionFactory(content__poll=poll_1,
                                                    content__language="en")
        p1_version_4 = factories.PollVersionFactory(content__poll=poll_1,
                                                    content__language="en")

        self.assertEqual(p1_version_1.number, 1)
        self.assertEqual(p1_version_2.number, 2)
        self.assertEqual(p1_version_3.number, 3)
        self.assertEqual(p1_version_4.number, 4)
Ejemplo n.º 13
0
    def test_factory(self):
        pv = factories.PollVersionFactory()
        form_class = grouper_form_factory(PollContent, language='en')

        self.assertIn(forms.Form, form_class.mro())
        self.assertEqual(form_class.__name__, 'PollContentGrouperForm')
        self.assertIn('grouper', form_class.base_fields)
        self.assertIn(
            (pv.content.poll.pk, str(pv.content.poll)),
            form_class.base_fields['grouper'].choices,
        )
Ejemplo n.º 14
0
    def test_filter_by_grouper(self):
        poll = factories.PollFactory()
        versions = factories.PollVersionFactory.create_batch(
            2, content__poll=poll)  # same grouper
        factories.PollVersionFactory()  # different grouper

        versions_for_grouper = Version.objects.filter_by_grouper(poll)

        self.assertQuerysetEqual(versions_for_grouper,
                                 [versions[0].pk, versions[1].pk],
                                 transform=lambda o: o.pk,
                                 ordered=False)
Ejemplo n.º 15
0
    def test_published_change_to_unpublished_is_logged(self):
        version = factories.PollVersionFactory(state=constants.PUBLISHED)
        user = factories.UserFactory()

        version.unpublish(user)

        tracking = StateTracking.objects.get()
        self.assertEqual(tracking.version, version)
        self.assertEqual(tracking.date, now())
        self.assertEqual(tracking.old_state, constants.PUBLISHED)
        self.assertEqual(tracking.new_state, constants.UNPUBLISHED)
        self.assertEqual(tracking.user, user)
Ejemplo n.º 16
0
    def test_draft_change_to_archived_is_logged(self):
        version = factories.PollVersionFactory(state=constants.DRAFT)
        user = factories.UserFactory()

        version.archive(user)

        tracking = StateTracking.objects.get()
        self.assertEqual(tracking.version, version)
        self.assertEqual(tracking.date, now())
        self.assertEqual(tracking.old_state, constants.DRAFT)
        self.assertEqual(tracking.new_state, constants.ARCHIVED)
        self.assertEqual(tracking.user, user)
    def test_version_number_for_multiple_sources_created_versions_with_languages(
            self):
        """
        The same object with different laguages being updated at similar times
        makes no difference to the version numbers
        """
        poll_1 = factories.PollFactory()
        language_1 = "en"
        language_2 = "it"

        lang1_version_1 = factories.PollVersionFactory(
            content__poll=poll_1, content__language=language_1)
        lang1_version_2 = factories.PollVersionFactory(
            content__poll=poll_1, content__language=language_1)
        lang2_version_1 = factories.PollVersionFactory(
            content__poll=poll_1, content__language=language_2)
        lang1_version_3 = factories.PollVersionFactory(
            content__poll=poll_1, content__language=language_1)
        lang2_version_2 = factories.PollVersionFactory(
            content__poll=poll_1, content__language=language_2)
        lang1_version_4 = factories.PollVersionFactory(
            content__poll=poll_1, content__language=language_1)

        # Language 1 checks
        self.assertEqual(lang1_version_1.number, 1)
        self.assertEqual(lang1_version_2.number, 2)
        self.assertEqual(lang1_version_3.number, 3)
        self.assertEqual(lang1_version_4.number, 4)
        # Language 2 checks
        self.assertEqual(lang2_version_1.number, 1)
        self.assertEqual(lang2_version_2.number, 2)
    def test_version_number_for_multiple_sources_created_versions(self):
        """
        Different objects being updated at similar times makes no difference to the version numbers
        P1 and P2 checks are mixed because versions can be created for different groupers at different times
        """
        poll_1 = factories.PollFactory()
        poll_2 = factories.PollFactory()
        language = "en"

        p1_version_1 = factories.PollVersionFactory(content__poll=poll_1,
                                                    content__language=language)
        p1_version_2 = factories.PollVersionFactory(content__poll=poll_1,
                                                    content__language=language)
        p2_version_1 = factories.PollVersionFactory(content__poll=poll_2,
                                                    content__language=language)
        p1_version_3 = factories.PollVersionFactory(content__poll=poll_1,
                                                    content__language=language)
        p2_version_2 = factories.PollVersionFactory(content__poll=poll_2,
                                                    content__language=language)
        p1_version_4 = factories.PollVersionFactory(content__poll=poll_1,
                                                    content__language=language)

        # Poll 1 checks
        self.assertEqual(p1_version_1.number, 1)
        self.assertEqual(p1_version_2.number, 2)
        self.assertEqual(p1_version_3.number, 3)
        self.assertEqual(p1_version_4.number, 4)
        # Poll 2 checks
        self.assertEqual(p2_version_1.number, 1)
        self.assertEqual(p2_version_2.number, 2)
Ejemplo n.º 19
0
    def test_deleting_last_version_deletes_grouper_as_well(self):
        """
        Deleting the last version deletes the grouper as well.
        """
        poll_1 = factories.PollFactory()
        language_1 = "en"
        language_2 = "it"

        lang1_version_1 = factories.PollVersionFactory(
            content__poll=poll_1, content__language=language_1)
        lang2_version_1 = factories.PollVersionFactory(
            content__poll=poll_1, content__language=language_2)

        first_delete = lang1_version_1.delete()
        poll_exists = Poll.objects.filter(pk=poll_1.pk).exists()
        second_delete = lang2_version_1.delete()
        poll_removed = not Poll.objects.filter(pk=poll_1.pk).exists()

        self.assertEqual(first_delete[1]['last'], False)
        self.assertEqual(second_delete[1]['last'], True)
        self.assertEqual(poll_exists, True)
        self.assertEqual(poll_removed, True)
    def test_convert_to_proxy(self):
        version = factories.PollVersionFactory()
        proxied_version = version.convert_to_proxy()

        cms_extension = apps.get_app_config(
            "djangocms_versioning").cms_extension
        versionable = cms_extension.versionables_by_content[PollContent]
        proxy_model = versionable.version_model_proxy

        self.assertNotEqual(version.__class__, proxied_version.__class__)
        # assert it's still a subclass of Version
        self.assertTrue(isinstance(proxied_version, Version))
        self.assertTrue(isinstance(proxied_version, proxy_model))
 def test_version_list_url(self):
     pv = factories.PollVersionFactory(content__language="en")
     url = version_list_url(pv.content)
     parsed = urlparse(url)
     self.assertEqual(parsed.path,
                      "/en/admin/djangocms_versioning/pollcontentversion/")
     self.assertEqual(
         {k: v[0]
          for k, v in parse_qs(parsed.query).items()},
         {
             "poll": "1",
             "language": "en"
         },
     )
    def test_filter_by_grouper_doesnt_include_other_content_types(self):
        """Regression test for a bug in which filtering by content_type
        field was missed in the query
        """
        pv = factories.PollVersionFactory(content__id=11)
        factories.BlogPostVersionFactory(content__id=11)

        versions_for_grouper = Version.objects.filter_by_grouper(
            pv.content.poll)

        # Only poll version included
        self.assertQuerysetEqual(versions_for_grouper, [pv.pk],
                                 transform=lambda o: o.pk,
                                 ordered=False)
Ejemplo n.º 23
0
    def test_new_draft_doesnt_change_status_of_drafts_from_other_groupers(self):
        """When versions relating to different groupers have a new draft
        created, then this should not change the other draft's status to
        archived.
        """
        factories.PollVersionFactory(state=constants.DRAFT)

        Version.objects.create(
            content=factories.PollContentFactory(),
            state=constants.DRAFT,
            created_by=factories.UserFactory(),
        )

        # Both are still drafts because they relate to different groupers
        self.assertEqual(Version.objects.filter(state=constants.DRAFT).count(), 2)
    def test_content_object_gets_duplicated_with_source(self):
        """When copying, the new version object should have a new
        related content object. The default copy method will copy all
        content fields (other than the pk) exactly as they were with source.
        """
        original_version = factories.PollVersionFactory()
        user = factories.UserFactory()
        versioning_app_ext = apps.get_app_config(
            "djangocms_versioning").cms_extension
        versionables_mock = self._create_versionables_mock(default_copy)

        with patch.object(versioning_app_ext, "versionables_by_content",
                          versionables_mock):
            new_version = original_version.copy(user)

        self.assertEqual(original_version, new_version.source)
    def test_new_version_object_gets_created(self):
        """When copying, a new version object should get created
        """
        with freeze_time("2017-07-07"):
            # Make sure created in the past
            original_version = factories.PollVersionFactory(state=PUBLISHED)
        user = factories.UserFactory()

        new_version = original_version.copy(user)

        # Created a new version record
        self.assertNotEqual(original_version.pk, new_version.pk)
        self.assertEqual(new_version.created, now())
        self.assertEqual(new_version.created_by, user)
        # The state should always be DRAFT no matter what the original
        # state was
        self.assertEqual(new_version.state, DRAFT)
Ejemplo n.º 26
0
    def test_the_copy_method_is_configurable(self):
        """When copying, the new version object should have a new
        related content object. How the content object will be
        copied can be configured.
        """
        original_version = factories.PollVersionFactory()
        user = factories.UserFactory()
        new_content = factories.PollContentFactory(
            poll=original_version.content.poll)
        mocked_copy = Mock(return_value=new_content)
        versionables_mock = self._create_versionables_mock(mocked_copy)
        versioning_app_ext = apps.get_app_config(
            'djangocms_versioning').cms_extension

        with patch.object(versioning_app_ext, 'versionables_by_content', versionables_mock):
            new_version = original_version.copy(user)

        self.assertEqual(new_version.content.pk, new_content.pk)
Ejemplo n.º 27
0
 def test_unpublished_cant_be_published(self):
     version = factories.PollVersionFactory(state=constants.UNPUBLISHED)
     self.assertFalse(version.can_be_published())
Ejemplo n.º 28
0
 def test_unpublished_cant_change_to_unpublished(self):
     version = factories.PollVersionFactory(state=constants.UNPUBLISHED)
     user = factories.UserFactory()
     with self.assertRaises(TransitionNotAllowed):
         version.unpublish(user)
Ejemplo n.º 29
0
 def test_published_can_change_to_unpublished(self):
     version = factories.PollVersionFactory(state=constants.PUBLISHED)
     user = factories.UserFactory()
     version.unpublish(user)
     self.assertEqual(version.state, constants.UNPUBLISHED)
Ejemplo n.º 30
0
 def test_archived_cant_be_published(self):
     version = factories.PollVersionFactory(state=constants.ARCHIVED)
     self.assertFalse(version.can_be_published())