# Calendaring is a simple CMF/Plone calendaring implementation.
# Copyright (C) 2004 Enfold Systems
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
"""
$Id: test_traversal.py,v 1.6 2005/01/25 01:03:48 dreamcatcher Exp $
"""
import os, sys
from os import curdir
from os.path import join, abspath, dirname, split
from StringIO import StringIO

try:
    __file__
except NameError:
    # Test was called directly, so no __file__ global exists.
    _prefix = abspath(curdir)
else:
    # Test was called by another test.
    _prefix = abspath(dirname(__file__))

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

# Load fixture
import unittest
from Testing import ZopeTestCase

# Install our product
ZopeTestCase.installProduct('MimetypesRegistry')
ZopeTestCase.installProduct('PortalTransforms')
ZopeTestCase.installProduct('Archetypes')
ZopeTestCase.installProduct('Calendaring')
ZopeTestCase.installProduct('ATContentTypes')

import datetime
from DateTime import DateTime
from Products.CMFPlone.tests import PloneTestCase
from Products.Calendaring.tests.utils import installType

class TestViewTraversal(PloneTestCase.PloneTestCase):

    def _setupCalendar(self):
        self.qi = self.portal.portal_quickinstaller
        self.qi.installProduct('PortalTransforms')
        self.qi.installProduct('Archetypes')
        self.qi.installProduct('Calendaring')
        self.ct = self.portal.portal_calendar
        self.wf = self.portal.portal_workflow

        # Install ATEvent as Event
        installType(self.portal,
                    'ATEvent',
                    'ATContentTypes',
                    'Event',
                    ignore_actions=True)

        self.ct.setMapping('atct')
        self.portal._v_skindata = None
        self.portal.setupCurrentSkin()

    def afterSetUp(self):
        self.loginPortalOwner()
        self._setupCalendar()
        self.cat = self.portal.portal_catalog
        self.mt = self.portal.portal_membership
        self.wt = self.portal.portal_workflow
        self.portal.invokeFactory('Calendar', id='test_calendar')
        self.cal = self.portal.test_calendar
        self.file = open(join(_prefix, 'data', 'tasks.ics'), 'r')
        self.items = self.cal.fromFile(self.file)
        self.file.close()

    def _check_day_view(self, dt, view, evcount=(1, 1)):
        self.assertEquals(view.start, dt)
        self.assertEquals(view.end, dt + view.resolution)
        gef = self.ct.getEventsForThisDay
        self.assertEquals(len(gef(DateTime(2004, 4, 26))), 1)
        parts = list(view.parts)
        self.assertEquals(len(parts), 24)
        hour = parts[9]
        events = list(hour.events)
        self.assertEquals(len(events), evcount[0])
        if evcount[0]:
            ev0 = events[0].getObject()
            review_state = self.wf.getInfoFor(ev0, 'review_state')
            self.assertEquals(review_state, 'private')
        hour = parts[15]
        events = list(hour.events)
        self.assertEquals(len(events), evcount[1])
        if evcount[1]:
            ev0 = events[0].getObject()
            review_state = self.wf.getInfoFor(ev0, 'review_state')
            self.assertEquals(review_state, 'published')

    def test_traversal_day_view(self):
        cal = self.cal
        ct = self.ct
        uid = cal.UID()
        request = cal.REQUEST
        dt = datetime.datetime(2004, 4, 26)
        request.set('cyear', 2004)
        request.set('cmonth', 4)
        request.set('cday', 26)
        path = '%s/day_view' % uid
        view = ct.restrictedTraverse(path)
        self._check_day_view(dt, view)

    def test_anon_traversal_day_view(self):
        self.logout()
        cal = self.cal
        ct = self.ct
        uid = cal.UID()
        request = cal.REQUEST
        dt = datetime.datetime(2004, 4, 26)
        request.set('cyear', 2004)
        request.set('cmonth', 4)
        request.set('cday', 26)
        path = '%s/day_view' % uid
        view = ct.restrictedTraverse(path)
        self._check_day_view(dt, view, evcount=(0, 1))

    def test_traversal_calendar_day_view(self):
        cal = self.cal
        request = cal.REQUEST
        dt = datetime.datetime(2004, 4, 26)
        request.set('cyear', 2004)
        request.set('cmonth', 4)
        request.set('cday', 26)
        path = 'day_view'
        view = cal.restrictedTraverse(path)
        self._check_day_view(dt, view)

    def test_anon_traversal_calendar_day_view(self):
        self.logout()
        cal = self.cal
        request = cal.REQUEST
        dt = datetime.datetime(2004, 4, 26)
        request.set('cyear', 2004)
        request.set('cmonth', 4)
        request.set('cday', 26)
        path = 'day_view'
        view = cal.restrictedTraverse(path)
        self._check_day_view(dt, view, evcount=(0,1))

    def _check_month_view(self, dt, view):
        self.assertEquals(view.start, dt)
        self.assertEquals(view.end, dt + view.resolution)
        gef = self.ct.getEventsForThisDay
        self.assertEquals(len(gef(DateTime(2004, 4, 26))), 1)
        parts = list(view.parts)
        self.assertEquals(len(parts), 5)

    def test_traversal_month_view(self):
        cal = self.cal
        ct = self.ct
        uid = cal.UID()
        request = cal.REQUEST
        dt = datetime.datetime(2004, 4, 1)
        request.set('cyear', 2004)
        request.set('cmonth', 4)
        request.set('cday', 26)
        path = '%s/month_view' % uid
        view = ct.restrictedTraverse(path)
        self._check_month_view(dt, view)

    def test_traversal_calendar_month_view(self):
        cal = self.cal
        request = cal.REQUEST
        dt = datetime.datetime(2004, 4, 1)
        request.set('cyear', 2004)
        request.set('cmonth', 4)
        request.set('cday', 26)
        path = 'month_view'
        view = cal.restrictedTraverse(path)
        self._check_month_view(dt, view)

    def _check_week_view(self, dt, view):
        self.assertEquals(view.start, dt)
        self.assertEquals(view.end, dt + view.resolution)
        gef = self.ct.getEventsForThisDay
        self.assertEquals(len(gef(DateTime(2004, 4, 26))), 1)
        parts = list(view.parts)
        self.assertEquals(len(parts), 7)

    def test_traversal_week_view(self):
        cal = self.cal
        ct = self.ct
        uid = cal.UID()
        request = cal.REQUEST
        dt = datetime.datetime(2004, 4, 26)
        request.set('cyear', 2004)
        request.set('cmonth', 4)
        request.set('cday', 26)
        path = '%s/week_view' % uid
        view = ct.restrictedTraverse(path)
        self._check_week_view(dt, view)

    def test_traversal_calendar_week_view(self):
        cal = self.cal
        request = cal.REQUEST
        dt = datetime.datetime(2004, 4, 26)
        request.set('cyear', 2004)
        request.set('cmonth', 4)
        request.set('cday', 26)
        path = 'week_view'
        view = cal.restrictedTraverse(path)
        self._check_week_view(dt, view)

    def test_HEAD(self):
        cal = self.cal
        request = cal.REQUEST
        response = request.RESPONSE
        self.assertEquals(cal.HEAD(request, response).status, 200)

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

if __name__ == '__main__':
    framework(descriptions=1, verbosity=1)
