# Test if this actually works in Plone with real CMFEditions and real types
import os, sys, time

if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

from zope.app.tests import placelesssetup
from zope.interface import classImplements
from zope.app.annotation.interfaces import IAttributeAnnotatable
from Testing import ZopeTestCase
import Products.Five
from Products.Five import zcml
from Products.PloneTestCase import PloneTestCase
from Products.ATContentTypes.content.folder import ATFolder
from Products.ATContentTypes.content.document import ATDocument
import Products.ploneundelete
from Products.ploneundelete.interfaces import IUndeleteContainer
from Products.ploneundelete.interfaces import IUndeleteSupport
from Products.ploneundelete.interfaces import ICanBeUndeleted

ZopeTestCase.installProduct('CMFUid')
ZopeTestCase.installProduct('CMFEditions')
ZopeTestCase.installProduct('ploneundelete')

PloneTestCase.setupPloneSite(products=('CMFEditions',))

# Turn on container events, this is off by default because it breaks the
# entire universe in zope 2.8, but it's required for testing.
ENABLE_EVENTS=\
'''<configure xmlns="http://namespaces.zope.org/zope"
           xmlns:five="http://namespaces.zope.org/five">
    <five:containerEvents />
</configure>'''


class TestUndeletion(PloneTestCase.PloneTestCase):

    def afterSetUp(self):
        placelesssetup.setUp()
        # This is all about admin-ish tasks
        self.setRoles(['Manager'])
        zcml.load_config('meta.zcml', Products.Five)
        zcml.load_config('configure.zcml', Products.ploneundelete)
        zcml.load_string(ENABLE_EVENTS)
        # Make normal folders UndeleteContainers, and Documents Undeletable
        classImplements(ATFolder, IAttributeAnnotatable, IUndeleteContainer)
        classImplements(ATDocument, ICanBeUndeleted)
        repo = self.portal.portal_repository
        repo.setVersionableContentTypes(['Document'])
        self.portal.invokeFactory('Folder', 'my_folder', title='My Title')
        self.container = self.portal.my_folder
        self.container.invokeFactory('Document', 'my_doc', title='Doc Title')
        self.doc = self.container.my_doc

    def beforeTearDown(self):
        placelesssetup.tearDown()

    def testBasicDeleteAndUndelete(self):
        print IUndeleteContainer.providedBy(self.container)
        undeleter = IUndeleteSupport(self.container)
        self.assertEqual(len(undeleter.listDeleted()), 0)
        self.container.manage_delObjects(['my_doc',])
        self.assertEqual(getattr(self.container, 'my_doc', None), None)
        deleted = undeleter.listDeleted()
        self.assertEqual(len(deleted), 1)
        self.assertEqual(deleted[0]['id'], 'my_doc')
        self.assertEqual(deleted[0]['title'], 'Doc Title')
        # Undelete
        undeleter.undelete(deleted[0]['uid'])
        self.failIf(getattr(self.container, 'my_doc') is None)
        doc = self.container.my_doc
        self.assertEqual(doc.getId(), 'my_doc')
        self.assertEqual(doc.Title(), 'Doc Title')


def test_suite():
    from unittest import TestSuite, makeSuite
    suite = TestSuite()
    suite.addTest(makeSuite(TestUndeletion))
    return suite


if __name__ == '__main__':
    framework()