"""
$Id: __init__.py,v 1.2 2005/02/26 17:56:10 sidnei Exp $
"""

import os, sys
from cStringIO import StringIO

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

from base import BaseTest
from zope.interface import directlyProvides, directlyProvidedBy
from zope.component import getView
from zope.configuration.exceptions import ConfigurationError

from Products.CMFCore.utils import getToolByName
from Products.Fate.tests.utils import load_string, makeContent, order_keeper
from Products.Fate.tests.sample import content
from Products.Fate.tests.sample.interfaces import IAdding

decl = """
<configure xmlns="http://namespaces.zope.org/zope"
  xmlns:fate="http://namespaces.plone.org/fate"
  package="Products.Fate.tests">

  <subscriber
      for="Products.Fate.event.IReferenceAddedEvent"
      factory=".utils.reference_add"
      />

  <subscriber
      for="Products.Fate.event.IReferenceRemovedEvent"
      factory=".utils.reference_remove"
      />

</configure>
"""

from base import register_zcml

class ReferencesTest(BaseTest):

    def afterSetUp(self):
        register_zcml(with_setup=True)
        load_string(decl)
        self.request = self.portal.REQUEST
        qi = getToolByName(self.portal, 'portal_quickinstaller')
        qi.installProduct('Archetypes')

        from Products.Archetypes.public import listTypes
        from Products.Archetypes.Extensions.utils import installTypes
        types = listTypes(content.PROJECTNAME)
        out = StringIO()
        installTypes(self.portal, out, types, content.PROJECTNAME)
        order_keeper.clear()

    def beforeTearDown(self):
        order_keeper.clear()

    def test_contained_ordering_refs_field_api(self):
        # Make a box and a couple things
        box = makeContent(self.folder, 'Box', 'box')
        things = []
        for i in range(0, 4):
            things.append(
                makeContent(self.folder, 'Thing', 'thing%s' % i)
                )
        expected = [t.UID() for t in things]
        self.failIf(expected == order_keeper.get(box.UID()))
        box.setContents(things)
        self.assertEquals(expected, order_keeper.get(box.UID()))

        # Delete one thing, set refs again, check that it got removed.
        del things[2]
        expected = [t.UID() for t in things]
        box.setContents(things)
        self.assertEquals(expected, order_keeper.get(box.UID()))

        # Now make a couple more boxes and add things to them.
        boxes = []
        for i in range(0, 4):
            boxes.append(
                makeContent(self.folder, 'Box', 'box%s' % i)
                )

        thing = things[0]

        expected = [t.UID() for t in boxes]
        self.failIf(expected == order_keeper.get(thing.UID()))
        thing.setContainers(boxes)
        self.assertEquals(expected, order_keeper.get(thing.UID()))

        # Delete two boxes, set refs again, check that they got removed.
        del boxes[0]
        del boxes[-2]
        expected = [t.UID() for t in boxes]
        self.failIf(expected == order_keeper.get(thing.UID()))
        thing.setContainers(boxes)
        self.assertEquals(expected, order_keeper.get(thing.UID()))



    def test_contained_ordering_refs_engine_api(self):
        tool = getToolByName(self.portal, 'reference_catalog')
        # Make a box and a couple things
        box = makeContent(self.folder, 'Box', 'box')
        things = []
        for i in range(0, 4):
            things.append(
                makeContent(self.folder, 'Thing', 'thing%s' % i)
                )
        expected = [t.UID() for t in things]
        self.failIf(expected == order_keeper.get(box.UID()))
        for t in things:
            tool.addReference(box, t, 'contains')
        self.assertEquals(expected, order_keeper.get(box.UID()))

        # Delete one thing, set refs again, check that it got removed.
        delete = things[2]
        del things[2]
        expected = [t.UID() for t in things]
        tool.deleteReference(box, delete, 'contains')
        self.assertEquals(expected, order_keeper.get(box.UID()))

        # Clear all refs
        for t in things:
            tool.deleteReference(box, t, 'contains')
        self.assertEquals([], order_keeper.get(box.UID()))

        # Now make a couple more boxes and add things to them.
        boxes = []
        for i in range(0, 4):
            boxes.append(
                makeContent(self.folder, 'Box', 'box%s' % i)
                )

        thing = things[0]

        expected = [t.UID() for t in boxes]
        self.failIf(expected == order_keeper.get(thing.UID()))
        for b in boxes:
            tool.addReference(thing, b, 'containers')
        self.assertEquals(expected, order_keeper.get(thing.UID()))

        # Delete two boxes, set refs again, check that they got removed.
        b1 = boxes[0]
        b2 = boxes[-2]
        del boxes[0]
        del boxes[-2]
        expected = [t.UID() for t in boxes]
        self.failIf(expected == order_keeper.get(thing.UID()))
        for b in (b1, b2):
            tool.deleteReference(thing, b, 'containers')
        self.assertEquals(expected, order_keeper.get(thing.UID()))

        for b in boxes:
            tool.deleteReference(thing, b, 'containers')
        self.assertEquals([], order_keeper.get(thing.UID()))


def test_suite():
    import unittest
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(ReferencesTest))
    return suite

if __name__ == '__main__':
    framework()
