def test_verify_unknown_state_no_interface(self):
        """Create a document and call the view

        But do so with a workflow state that the Document does not have, so
        no marker interface is applied.
        """
        self.set_default_workflow()
        doc = api.content.create(
            container=self.portal,
            type='Document',
            id='doc'
        )
        doc.text = 'Document with fishy content'
        alsoProvides(doc, IStopWordsVerified)
        doc.reindexObject()

        self.assertFalse(
            IHasStopWords.providedBy(doc)
        )

        self.request.set('start', '0')
        self.request.set('size', '3')
        self.request.set('entries', 'fishy')
        self.request.set('states', 'non-existing')
        self._get_view()()

        self.assertFalse(
            IHasStopWords.providedBy(doc)
        )
    def test_verify_known_multiple_states_add_interface(self):
        """Create a document and call the view

        Specify multiple workflow states (including the Document one), to
        check that filtering by workflow state works.
        """
        self.set_default_workflow()
        doc = api.content.create(
            container=self.portal,
            type='Document',
            id='doc'
        )
        doc.text = 'Document with fishy content'
        alsoProvides(doc, IStopWordsVerified)

        doc.reindexObject()

        self.assertFalse(
            IHasStopWords.providedBy(doc)
        )

        self.request.set('start', '0')
        self.request.set('size', '3')
        self.request.set('entries', 'fishy')
        self.request.set('states', 'private,published')
        self._get_view()()

        self.assertTrue(
            IHasStopWords.providedBy(doc)
        )
    def test_object_verified(self):
        """Create a document and call the view. The document should have the
        IHasStopWords marker interface attached
        """
        doc = api.content.create(
            container=self.portal,
            type='Document',
            id='doc'
        )
        doc.text = 'Document with fishy content'
        alsoProvides(doc, IStopWordsVerified)
        doc.reindexObject()

        self.assertFalse(
            IHasStopWords.providedBy(doc)
        )

        self.request.set('start', '0')
        self.request.set('size', '3')
        self.request.set('entries', 'fishy')
        self._get_view()()

        self.assertTrue(
            IHasStopWords.providedBy(doc)
        )
    def test_verify_known_type_add_interface(self):
        """Create a document and call the view

        Specify a marker interface that Document provides, to check that
        filtering by marker interfaces works.
        """
        doc = api.content.create(
            container=self.portal,
            type='Document',
            id='doc'
        )
        doc.text = 'Document with fishy content'
        alsoProvides(doc, IStopWordsVerified)

        doc.reindexObject()

        self.assertFalse(
            IHasStopWords.providedBy(doc)
        )

        self.request.set('start', '0')
        self.request.set('size', '3')
        self.request.set('entries', 'fishy')
        self.request.set(
            'type',
            'Products.CMFCore.interfaces._content.IContentish',
        )
        self._get_view()()

        self.assertTrue(
            IHasStopWords.providedBy(doc)
        )
    def test_remove_interface_on_a_document(self):
        """Calling @@discard-alert on a document has the IHasStopWords removes
        it."""
        alsoProvides(self.document, IHasStopWords)
        self.assertTrue(IHasStopWords.providedBy(self.document))

        discard_view = api.content.get_view(name="discard-alert", context=self.document, request=self.request)
        discard_view()

        self.assertFalse(IHasStopWords.providedBy(self.document))
    def test_remove_interface_on_a_document(self):
        """Calling @@discard-alert on a document that has the IHasStopWords
        removes it and adds the IStopWordsVerified marker interface"""
        alsoProvides(self.document, IHasStopWords)
        self.assertTrue(IHasStopWords.providedBy(self.document))

        discard_view = api.content.get_view(
            name='discard-alert',
            context=self.document,
            request=self.request
        )
        discard_view()

        self.assertFalse(IHasStopWords.providedBy(self.document))
        self.assertTrue(IStopWordsVerified.providedBy(self.document))
    def test_object_without_verified_marker_interface(self):
        """Corner case: the object does not have the verified marker interface

        It should add the stop words marker interface without raising any
        exception.
        """
        self.assertFalse(IStopWordsVerified.providedBy(self.doc))
        self.assertFalse(IHasStopWords.providedBy(self.doc))

        catalog = api.portal.get_tool('portal_catalog')
        brain = catalog(id=self.doc.id)[0]

        verify_brain(brain, new_entries='random')

        self.assertFalse(IStopWordsVerified.providedBy(self.doc))
        self.assertTrue(IHasStopWords.providedBy(self.doc))
    def test_no_interface_no_problem(self):
        """Calling @@discard-alert on a document that does not have the
        IHasStopWords interface does not break.
        it."""
        discard_view = api.content.get_view(name="discard-alert", context=self.document, request=self.request)
        discard_view()

        self.assertFalse(IHasStopWords.providedBy(self.document))
    def test_remove_interface_on_a_comment(self):
        """Calling @@discard-alert on a comment has the IHasStopWords removes
        it."""
        comment = createObject("plone.Comment")
        comment.text = "something"
        comment.author_username = "******"
        comment.author_name = "Jim"
        comment.author_email = "*****@*****.**"
        conversation = IConversation(self.document)
        conversation.addComment(comment)

        alsoProvides(comment, IHasStopWords)
        self.assertTrue(IHasStopWords.providedBy(comment))

        discard_view = api.content.get_view(name="discard-alert", context=comment, request=self.request)
        discard_view()

        self.assertFalse(IHasStopWords.providedBy(comment))
    def test_no_stop_words_no_interface(self):
        comment = self._add_comment('no alert')
        condition = TextAlertCondition()
        condition.stop_words = u'one alert\nanother alert'

        executable = getMultiAdapter(
            (self.portal, condition, CommentDummyEvent(comment)),
            IExecutable
        )
        executable()
        self.assertFalse(IHasStopWords.providedBy(comment))
    def _apply_marker_interface(obj, has_stop_words):
        reindex = False
        if has_stop_words:
            alsoProvides(obj, IHasStopWords)
            reindex = True
        elif IHasStopWords.providedBy(obj):
            noLongerProvides(obj, IHasStopWords)
            reindex = True

        if reindex:
            obj.reindexObject(idxs=('object_provides', ))
    def test_remove_interface_on_a_comment(self):
        """Calling @@discard-alert on a comment that has the IHasStopWords
        removes it and adds the IStopWordsVerified marker interface"""
        comment = createObject('plone.Comment')
        comment.text = 'something'
        comment.author_username = '******'
        comment.author_name = 'Jim'
        comment.author_email = '*****@*****.**'
        conversation = IConversation(self.document)
        conversation.addComment(comment)

        alsoProvides(comment, IHasStopWords)
        self.assertTrue(IHasStopWords.providedBy(comment))

        discard_view = api.content.get_view(
            name='discard-alert',
            context=comment,
            request=self.request
        )
        discard_view()

        self.assertFalse(IHasStopWords.providedBy(comment))
        self.assertTrue(IStopWordsVerified.providedBy(comment))
    def test_add_and_remove_interface(self):
        comment = self._add_comment('one alert')
        condition = TextAlertCondition()
        condition.stop_words = u'one alert\nanother alert'

        # adds the marker interface
        executable = getMultiAdapter(
            (self.portal, condition, CommentDummyEvent(comment)),
            IExecutable
        )
        executable()

        comment.text = 'no longer creating an alert'
        executable()
        self.assertFalse(IHasStopWords.providedBy(comment))
    def test_stop_word_in_object(self):
        """Check that the verified marker interface is removed

        It should be removed if the stop words are found on the object's text.
        """
        alsoProvides(self.doc, IStopWordsVerified)
        self.doc.reindexObject()

        catalog = api.portal.get_tool('portal_catalog')
        brain = catalog(id=self.doc.id)[0]

        verify_brain(brain, new_entries='random')

        self.assertFalse(IStopWordsVerified.providedBy(self.doc))
        self.assertTrue(IHasStopWords.providedBy(self.doc))
    def __call__(self):
        if IHasStopWords.providedBy(self.context):
            noLongerProvides(self.context, IHasStopWords)
            self.context.reindexObject(idxs=('object_provides', ))

            api.portal.show_message(
                message=_(
                    u'stop_words_interface_removed_message',
                    default=u'The object no longer has an alert on it.'
                ),
                request=self.request,
            )

        return self.request.response.redirect(
            self.context.absolute_url(),
        )
    def test_has_stop_words_add_interface_document(self):
        document = api.content.create(
            container=self.portal,
            id='doc2',
            title='Document 2',
            type='Document'
        )
        document.setText('this gives one alert')
        condition = TextAlertCondition()
        condition.stop_words = u'one alert\nanother alert'

        executable = getMultiAdapter(
            (self.portal, condition, ContentTypeDummyEvent(document)),
            IExecutable
        )
        executable()
        self.assertTrue(IHasStopWords.providedBy(document))