From 86882d0c1e10d04e4d8b1cb39f5cd9fd01266baa Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Sun, 31 May 2009 23:54:24 -0400 Subject: [PATCH] port phpt tests to phpunit. I could use some help breaking up the recurrence tests into something meaningful --- framework/Date/test/Horde/Date/DateTest.php | 148 ++++++ framework/Date/test/Horde/Date/RecurrenceTest.php | 584 ++++++++++++++++++++++ framework/Date/test/Horde/Date/UtilsTest.php | 23 + 3 files changed, 755 insertions(+) create mode 100644 framework/Date/test/Horde/Date/UtilsTest.php diff --git a/framework/Date/test/Horde/Date/DateTest.php b/framework/Date/test/Horde/Date/DateTest.php index 6221d9f69..ca54e6355 100644 --- a/framework/Date/test/Horde/Date/DateTest.php +++ b/framework/Date/test/Horde/Date/DateTest.php @@ -12,6 +12,30 @@ */ class Horde_Date_DateTest extends PHPUnit_Framework_TestCase { + public function testConstructor() + { + $oldTimezone = date_default_timezone_get(); + date_default_timezone_set('Europe/Berlin'); + + $date = new stdClass(); + $date->year = 2001; + $date->month = 2; + $date->mday = 3; + $date->hour = 4; + $date->min = 5; + $date->sec = 6; + + $this->assertEquals('2001-02-03 04:05:06', (string)new Horde_Date($date)); + $this->assertEquals('2001-02-03 04:05:06', (string)new Horde_Date((array)$date)); + $this->assertEquals('2001-02-03 04:05:06', (string)new Horde_Date(array('year' => 2001, 'month' => 2, 'day' => 3, 'hour' => 4, 'minute' => 5, 'sec' => 6))); + $this->assertEquals('2001-02-03 04:05:06', (string)new Horde_Date('20010203040506')); + $this->assertEquals('2001-02-03 05:05:06', (string)new Horde_Date('20010203T040506Z')); + $this->assertEquals('2001-02-03 04:05:06', (string)new Horde_Date('2001-02-03 04:05:06')); + $this->assertEquals('2001-02-03 04:05:06', (string)new Horde_Date(981169506)); + + date_default_timezone_set($oldTimezone); + } + public function testDateCorrection() { $d = new Horde_Date('2008-01-01 00:00:00'); @@ -35,6 +59,115 @@ class Horde_Date_DateTest extends PHPUnit_Framework_TestCase $this->assertEquals(4, $d->hour); } + public function testTimestamp() + { + date_default_timezone_set('America/New_York'); + + $date = new Horde_Date(array('mday' => 1, 'month' => 10, 'year' => 2004)); + $this->assertEquals('1096603200', $date->timestamp()); + $this->assertEquals('1096603200', mktime(0, 0, 0, $date->month, $date->mday, $date->year)); + + $date = new Horde_Date(array('mday' => 1, 'month' => 5, 'year' => 1948)); + $this->assertEquals('-683841600', $date->timestamp()); + $this->assertEquals('-683841600', mktime(0, 0, 0, $date->month, $date->mday, $date->year)); + } + + public function testStrftime() + { + setlocale(LC_TIME, 'en_US.UTF-8'); + + $date = new Horde_Date('2001-02-03 16:05:06'); + $expected = array( + '20', + '03', + '02/03/01', + ' 3', + '16', + '04', + '02', + '05', + '16:05', + '06', + "\t", + '16:05:06', + '01', + '2001', + '%', + ); + $format = '%C%n%d%n%D%n%e%n%H%n%I%n%m%n%M%n%R%n%S%n%t%n%T%n%y%n%Y%n%%'; + $this->assertEquals($expected, explode("\n", strftime($format, $date->timestamp()))); + $this->assertEquals($expected, explode("\n", $date->strftime($format))); + + $expected = array( + 'Feb', + 'February', + 'PM', + '04:05:06 PM', + '02/03/2001', + '04:05:06 PM', + ); + $format = '%b%n%B%n%p%n%r%n%x%n%X'; + $this->assertEquals($expected, explode("\n", strftime($format, $date->timestamp()))); + $this->assertEquals($expected, explode("\n", $date->strftime($format))); + + $date->year = 1899; + $expected = array( + '18', + '03', + '02/03/99', + ' 3', + '16', + '04', + '02', + '05', + '16:05', + '06', + "\t", + '16:05:06', + '99', + '1899', + '%', + ); + $format = '%C%n%d%n%D%n%e%n%H%n%I%n%m%n%M%n%R%n%S%n%t%n%T%n%y%n%Y%n%%'; + $this->assertEquals($expected, explode("\n", $date->strftime($format))); + } + + public function testStrftimeDe() + { + setlocale(LC_TIME, 'de_DE'); + + $date = new Horde_Date('2001-02-03 16:05:06'); + + $expected = array( + 'Feb', + 'Februar', + '', + '04:05:06', + '03.02.2001', + '16:05:06', + ); + $format = '%b%n%B%n%p%n%r%n%x%n%X'; + $this->assertEquals($expected, explode("\n", strftime($format, $date->timestamp()))); + $this->assertEquals($expected, explode("\n", $date->strftime($format))); + } + + public function testSetTimezone() + { + date_default_timezone_set('America/New_York'); + + $date = new Horde_Date('20010203040506'); + $this->assertEquals('2001-02-03 04:05:06', (string)$date); + + $date->setTimezone('Europe/Berlin'); + $this->assertEquals('2001-02-03 10:05:06', (string)$date); + + $date = new Horde_Date('20010203040506', 'UTC'); + $this->assertEquals('2001-02-03 04:05:06', (string)$date); + + $date->setTimezone('Europe/Berlin'); + $this->assertEquals('2001-02-03 05:05:06', (string)$date); + } + public function testDateMath() { $d = new Horde_Date('2008-01-01 00:00:00'); @@ -47,4 +180,19 @@ class Horde_Date_DateTest extends PHPUnit_Framework_TestCase $this->assertEquals('2006-04-24 12:30:00', (string)$span->begin->add($span->width() / 2)); } + public function testSetNthWeekday() + { + $date = new Horde_Date('2004-10-01'); + + $date->setNthWeekday(Horde_Date::DATE_SATURDAY); + $this->assertEquals(2, $date->mday); + + $date->setNthWeekday(Horde_Date::DATE_SATURDAY, 2); + $this->assertEquals(9, $date->mday); + + $date = new Horde_Date('2007-04-01'); + $date->setNthWeekday(Horde_Date::DATE_THURSDAY); + $this->assertEquals(5, $date->mday); + } + } diff --git a/framework/Date/test/Horde/Date/RecurrenceTest.php b/framework/Date/test/Horde/Date/RecurrenceTest.php index 6f5c5347f..497d8b504 100644 --- a/framework/Date/test/Horde/Date/RecurrenceTest.php +++ b/framework/Date/test/Horde/Date/RecurrenceTest.php @@ -112,4 +112,588 @@ class Horde_Date_RecurrenceTest extends PHPUnit_Framework_TestCase $this->assertEquals(0, count($s->getCompletions())); } + public function testBug2813RecurrenceEndFromIcalendar() + { + $iCal = new Horde_iCalendar(); + $iCal->parsevCalendar(file_get_contents(dirname(__FILE__) . '/fixtures/bug2813.ics')); + $components = $iCal->getComponents(); + + date_default_timezone_set('US/Eastern'); + + foreach ($components as $content) { + if ($content instanceof Horde_iCalendar_vevent) { + $start = new Horde_Date($content->getAttribute('DTSTART')); + $end = new Horde_Date($content->getAttribute('DTEND')); + $rrule = $content->getAttribute('RRULE'); + $recurrence = new Horde_Date_Recurrence($start, $end); + $recurrence->fromRRule20($rrule); + break; + } + } + + $after = array('year' => 2006, 'month' => 6); + + $after['mday'] = 16; + $this->assertEquals('2006-06-16 18:00:00', (string)$recurrence->nextRecurrence($after)); + + $after['mday'] = 17; + $this->assertEquals('2006-06-17 18:00:00', (string)$recurrence->nextRecurrence($after)); + + $after['mday'] = 18; + $this->assertEquals('', (string)$recurrence->nextRecurrence($after)); + } + + public function testBug4626MonthlyByDayRRule() + { + $rrule = new Horde_Date_Recurrence('2008-04-05 00:00:00'); + $rrule->setRecurType(Horde_Date_Recurrence::RECUR_MONTHLY_WEEKDAY); + $rrule->setRecurOnDay(Horde_Date::MASK_SATURDAY); + + $this->assertEquals('MP1 1+ SA #0', $rrule->toRRule10(new Horde_iCalendar())); + $this->assertEquals('FREQ=MONTHLY;INTERVAL=1;BYDAY=1SA', $rrule->toRRule20(new Horde_iCalendar())); + } + +} + +/* +function recur($r) +{ + $ical = new Horde_iCalendar(); + echo $r->toRRule10($ical) . "\n"; + echo $r->toRRule20($ical) . "\n"; + $protect = 0; + $next = new Horde_Date('2007-03-01 00:00:00'); + while ($next = $r->nextRecurrence($next)) { + if (++$protect > 10) { + die('Infinite loop'); + } + echo (string)$next . "\n"; + $next->mday++; + } + var_dump($next); + echo "\n"; +} + +function dump($rrule, $version) +{ + $r = new Horde_Date_Recurrence('2007-03-01 10:00:00'); + if ($version == 1) { + $r->fromRRule10($rrule); + } else { + $r->fromRRule20($rrule); + } + + var_dump($r->getRecurType()); + var_dump((int)$r->getRecurInterval()); + var_dump($r->getRecurOnDays()); + var_dump($r->getRecurCount()); + if ($r->hasRecurEnd()) { + echo (string)$r->recurEnd . "\n"; + } + echo "\n"; +} + +$r = new Horde_Date_Recurrence('2007-03-01 10:00:00'); + +$r->setRecurType(Horde_Date_Recurrence::RECUR_DAILY); +$r->setRecurInterval(2); +$r->setRecurEnd(new Horde_Date('2007-03-07 10:00:00')); +recur($r); +$r->setRecurCount(4); +recur($r); + +$r->setRecurType(Horde_Date_Recurrence::RECUR_WEEKLY); +$r->setRecurOnDay(Horde_Date::MASK_THURSDAY); +$r->setRecurInterval(1); +$r->setRecurEnd(new Horde_Date('2007-03-29 10:00:00')); +recur($r); +$r->setRecurCount(4); +recur($r); +$r->setRecurInterval(2); +recur($r); + +$r->setRecurType(Horde_Date_Recurrence::RECUR_MONTHLY_DATE); +$r->setRecurInterval(1); +$r->setRecurEnd(new Horde_Date('2007-05-01 10:00:00')); +recur($r); +$r->setRecurCount(4); +recur($r); +$r->setRecurInterval(2); +recur($r); + +$r->setRecurType(Horde_Date_Recurrence::RECUR_MONTHLY_WEEKDAY); +$r->setRecurInterval(1); +$r->setRecurEnd(new Horde_Date('2007-05-01 10:00:00')); +recur($r); +$r->setRecurCount(4); +recur($r); + +$r->setRecurType(Horde_Date_Recurrence::RECUR_YEARLY_DATE); +$r->setRecurEnd(new Horde_Date('2009-03-01 10:00:00')); +recur($r); +$r->setRecurCount(4); +recur($r); + +$r->setRecurType(Horde_Date_Recurrence::RECUR_YEARLY_DAY); +$r->setRecurEnd(new Horde_Date('2009-03-01 10:00:00')); +recur($r); +$r->setRecurCount(4); +recur($r); + +$r->setRecurType(Horde_Date_Recurrence::RECUR_YEARLY_WEEKDAY); +$r->setRecurEnd(new Horde_Date('2009-03-01 10:00:00')); +recur($r); +$r->setRecurCount(4); +recur($r); + +$r = new Horde_Date_Recurrence('2007-04-25 12:00:00'); +$r->setRecurType(Horde_Date_Recurrence::RECUR_YEARLY_DATE); +$r->setRecurEnd(new Horde_Date('2011-04-25 23:00:00')); +$r->setRecurInterval(2); +$next = new Horde_Date('2009-03-30 00:00:00'); +$next = $r->nextRecurrence($next); +echo (string)$next . "\n\n"; + +$r = new Horde_Date_Recurrence('2008-02-29 00:00:00'); +$r->setRecurType(Horde_Date_Recurrence::RECUR_YEARLY_DATE); +$r->setRecurInterval(1); +$next = new Horde_Date('2008-03-01 00:00:00'); +$next = $r->nextRecurrence($next); +echo (string)$next . "\n\n"; + +$r = new Horde_Date_Recurrence('2008-03-14 12:00:00'); +$r->setRecurType(Horde_Date_Recurrence::RECUR_MONTHLY_WEEKDAY); +$r->setRecurCount(2); +$ical = new Horde_iCalendar(); +echo $r->toRRule10($ical) . "\n"; +echo $r->toRRule20($ical) . "\n\n"; + +$rrule1 = array('D2 20070307', + 'D2 20070308T090000Z', + 'D2 #4', + 'W1 TH 20070329', + 'W1 TH 20070330T080000Z', + 'W1 SU MO TU WE TH FR SA 20070603T235959', + 'W1 TH #4', + 'W2 TH #4', + 'MD1 1 20070501', + 'MD1 1 20070502T080000Z', + 'MD1 1 #4', + 'MD2 1 #4', + 'MP1 1+ TH 20070501', + 'MP1 1+ TH 20070502T080000Z', + 'MP1 1+ TH #4', + 'YM1 3 20090301', + 'YM1 3 20090302T090000Z', + 'YM1 3 #4', + 'YD1 60 20090301', + 'YD1 60 20090302T090000Z', + 'YD1 60 #4'); +foreach ($rrule1 as $rrule) { + dump($rrule, 1); +} +$rrule2 = array('FREQ=DAILY;INTERVAL=2;UNTIL=20070307', + 'FREQ=DAILY;INTERVAL=2;UNTIL=20070308T090000Z', + 'FREQ=DAILY;INTERVAL=2;COUNT=4', + 'FREQ=WEEKLY;INTERVAL=1;BYDAY=TH;UNTIL=20070329', + 'FREQ=WEEKLY;INTERVAL=1;BYDAY=TH;UNTIL=20070330T080000Z', + 'FREQ=WEEKLY;INTERVAL=1;BYDAY=TH;COUNT=4', + 'FREQ=WEEKLY;INTERVAL=2;BYDAY=TH;COUNT=4', + 'FREQ=MONTHLY;INTERVAL=1;UNTIL=20070501', + 'FREQ=MONTHLY;INTERVAL=1;UNTIL=20070502T080000Z', + 'FREQ=MONTHLY;INTERVAL=1;COUNT=4', + 'FREQ=MONTHLY;INTERVAL=2;COUNT=4', + 'FREQ=MONTHLY;INTERVAL=1;BYDAY=1TH;UNTIL=20070501', + 'FREQ=MONTHLY;INTERVAL=1;BYDAY=1TH;UNTIL=20070502T080000Z', + 'FREQ=MONTHLY;INTERVAL=1;BYDAY=1TH;COUNT=4', + 'FREQ=YEARLY;INTERVAL=1;UNTIL=20090301', + 'FREQ=YEARLY;INTERVAL=1;UNTIL=20090302T090000Z', + 'FREQ=YEARLY;INTERVAL=1;COUNT=4', + 'FREQ=YEARLY;INTERVAL=1;BYYEARDAY=60;UNTIL=20090301', + 'FREQ=YEARLY;INTERVAL=1;BYYEARDAY=60;UNTIL=20090302T090000Z', + 'FREQ=YEARLY;INTERVAL=1;BYYEARDAY=60;COUNT=4', + 'FREQ=YEARLY;INTERVAL=1;BYDAY=1TH;BYMONTH=3;UNTIL=20090301', + 'FREQ=YEARLY;INTERVAL=1;BYDAY=1TH;BYMONTH=3;UNTIL=20090302T090000Z', + 'FREQ=YEARLY;INTERVAL=1;BYDAY=1TH;BYMONTH=3;COUNT=4'); +foreach ($rrule2 as $rrule) { + dump($rrule, 2); } + +?> +--EXPECT-- +D2 20070308T090000Z +FREQ=DAILY;INTERVAL=2;UNTIL=20070308T090000Z +2007-03-01 10:00:00 +2007-03-03 10:00:00 +2007-03-05 10:00:00 +2007-03-07 10:00:00 +bool(false) + +D2 #4 +FREQ=DAILY;INTERVAL=2;COUNT=4 +2007-03-01 10:00:00 +2007-03-03 10:00:00 +2007-03-05 10:00:00 +2007-03-07 10:00:00 +bool(false) + +W1 TH 20070330T080000Z +FREQ=WEEKLY;INTERVAL=1;BYDAY=TH;UNTIL=20070330T080000Z +2007-03-01 10:00:00 +2007-03-08 10:00:00 +2007-03-15 10:00:00 +2007-03-22 10:00:00 +2007-03-29 10:00:00 +bool(false) + +W1 TH #4 +FREQ=WEEKLY;INTERVAL=1;BYDAY=TH;COUNT=4 +2007-03-01 10:00:00 +2007-03-08 10:00:00 +2007-03-15 10:00:00 +2007-03-22 10:00:00 +bool(false) + +W2 TH #4 +FREQ=WEEKLY;INTERVAL=2;BYDAY=TH;COUNT=4 +2007-03-01 10:00:00 +2007-03-15 10:00:00 +2007-03-29 10:00:00 +2007-04-12 10:00:00 +bool(false) + +MD1 1 20070502T080000Z +FREQ=MONTHLY;INTERVAL=1;UNTIL=20070502T080000Z +2007-03-01 10:00:00 +2007-04-01 10:00:00 +2007-05-01 10:00:00 +bool(false) + +MD1 1 #4 +FREQ=MONTHLY;INTERVAL=1;COUNT=4 +2007-03-01 10:00:00 +2007-04-01 10:00:00 +2007-05-01 10:00:00 +2007-06-01 10:00:00 +bool(false) + +MD2 1 #4 +FREQ=MONTHLY;INTERVAL=2;COUNT=4 +2007-03-01 10:00:00 +2007-05-01 10:00:00 +2007-07-01 10:00:00 +2007-09-01 10:00:00 +bool(false) + +MP1 1+ TH 20070502T080000Z +FREQ=MONTHLY;INTERVAL=1;BYDAY=1TH;UNTIL=20070502T080000Z +2007-03-01 10:00:00 +2007-04-05 10:00:00 +bool(false) + +MP1 1+ TH #4 +FREQ=MONTHLY;INTERVAL=1;BYDAY=1TH;COUNT=4 +2007-03-01 10:00:00 +2007-04-05 10:00:00 +2007-05-03 10:00:00 +2007-06-07 10:00:00 +bool(false) + +YM1 3 20090302T090000Z +FREQ=YEARLY;INTERVAL=1;UNTIL=20090302T090000Z +2007-03-01 10:00:00 +2008-03-01 10:00:00 +2009-03-01 10:00:00 +bool(false) + +YM1 3 #4 +FREQ=YEARLY;INTERVAL=1;COUNT=4 +2007-03-01 10:00:00 +2008-03-01 10:00:00 +2009-03-01 10:00:00 +2010-03-01 10:00:00 +bool(false) + +YD1 60 20090302T090000Z +FREQ=YEARLY;INTERVAL=1;BYYEARDAY=60;UNTIL=20090302T090000Z +2007-03-01 10:00:00 +2008-02-29 10:00:00 +2009-03-01 10:00:00 +bool(false) + +YD1 60 #4 +FREQ=YEARLY;INTERVAL=1;BYYEARDAY=60;COUNT=4 +2007-03-01 10:00:00 +2008-02-29 10:00:00 +2009-03-01 10:00:00 +2010-03-01 10:00:00 +bool(false) + + +FREQ=YEARLY;INTERVAL=1;BYDAY=1TH;BYMONTH=3;UNTIL=20090302T090000Z +2007-03-01 10:00:00 +2008-03-06 10:00:00 +bool(false) + + +FREQ=YEARLY;INTERVAL=1;BYDAY=1TH;BYMONTH=3;COUNT=4 +2007-03-01 10:00:00 +2008-03-06 10:00:00 +2009-03-05 10:00:00 +2010-03-04 10:00:00 +bool(false) + +2009-04-25 12:00:00 + +2012-02-29 00:00:00 + +MP1 2+ FR #2 +FREQ=MONTHLY;INTERVAL=1;BYDAY=2FR;COUNT=2 + +int(1) +int(2) +NULL +NULL +2007-03-07 00:00:00 + +int(1) +int(2) +NULL +NULL +2007-03-08 00:00:00 + +int(1) +int(2) +NULL +int(4) + +int(2) +int(1) +int(16) +NULL +2007-03-29 00:00:00 + +int(2) +int(1) +int(16) +NULL +2007-03-30 00:00:00 + +int(2) +int(1) +int(127) +NULL +2007-06-03 00:00:00 + +int(2) +int(1) +int(16) +int(4) + +int(2) +int(2) +int(16) +int(4) + +int(3) +int(1) +NULL +NULL +2007-05-01 00:00:00 + +int(3) +int(1) +NULL +NULL +2007-05-02 00:00:00 + +int(3) +int(1) +NULL +int(4) + +int(3) +int(2) +NULL +int(4) + +int(4) +int(1) +NULL +NULL +2007-05-01 00:00:00 + +int(4) +int(1) +NULL +NULL +2007-05-02 00:00:00 + +int(4) +int(1) +NULL +int(4) + +int(5) +int(1) +NULL +NULL +2009-03-01 00:00:00 + +int(5) +int(1) +NULL +NULL +2009-03-02 00:00:00 + +int(5) +int(1) +NULL +int(4) + +int(6) +int(1) +NULL +NULL +2009-03-01 00:00:00 + +int(6) +int(1) +NULL +NULL +2009-03-02 00:00:00 + +int(6) +int(1) +NULL +int(4) + +int(1) +int(2) +NULL +NULL +2007-03-07 00:00:00 + +int(1) +int(2) +NULL +NULL +2007-03-08 00:00:00 + +int(1) +int(2) +NULL +int(4) + +int(2) +int(1) +int(16) +NULL +2007-03-29 00:00:00 + +int(2) +int(1) +int(16) +NULL +2007-03-30 00:00:00 + +int(2) +int(1) +int(16) +int(4) + +int(2) +int(2) +int(16) +int(4) + +int(3) +int(1) +NULL +NULL +2007-05-01 00:00:00 + +int(3) +int(1) +NULL +NULL +2007-05-02 00:00:00 + +int(3) +int(1) +NULL +int(4) + +int(3) +int(2) +NULL +int(4) + +int(4) +int(1) +NULL +NULL +2007-05-01 00:00:00 + +int(4) +int(1) +NULL +NULL +2007-05-02 00:00:00 + +int(4) +int(1) +NULL +int(4) + +int(5) +int(1) +NULL +NULL +2009-03-01 00:00:00 + +int(5) +int(1) +NULL +NULL +2009-03-02 00:00:00 + +int(5) +int(1) +NULL +int(4) + +int(6) +int(1) +NULL +NULL +2009-03-01 00:00:00 + +int(6) +int(1) +NULL +NULL +2009-03-02 00:00:00 + +int(6) +int(1) +NULL +int(4) + +int(7) +int(1) +NULL +NULL +2009-03-01 00:00:00 + +int(7) +int(1) +NULL +NULL +2009-03-02 00:00:00 + +int(7) +int(1) +NULL +int(4) +*/ diff --git a/framework/Date/test/Horde/Date/UtilsTest.php b/framework/Date/test/Horde/Date/UtilsTest.php new file mode 100644 index 000000000..d3f31da60 --- /dev/null +++ b/framework/Date/test/Horde/Date/UtilsTest.php @@ -0,0 +1,23 @@ +assertEquals('2006-01-02', Horde_Date::firstDayOfWeek(1, 2006)->format('Y-m-d')); + $this->assertEquals('2007-01-01', Horde_Date::firstDayOfWeek(1, 2007)->format('Y-m-d')); + $this->assertEquals('2007-12-31', Horde_Date::firstDayOfWeek(1, 2008)->format('Y-m-d')); + $this->assertEquals('2010-01-04', Horde_Date::firstDayOfWeek(1, 2010)->format('Y-m-d')); + } + +} -- 2.11.0