View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\date_recur\Unit;
use Drupal\date_recur\Exception\DateRecurHelperArgumentException;
use Drupal\date_recur\Rl\RlHelper;
use Drupal\Tests\UnitTestCase;
class DateRecurRlHelperUnitTest extends UnitTestCase {
public function testOccurrence() {
$helper = $this
->createHelper('FREQ=DAILY;COUNT=1', new \DateTime('2am 14 April 2014'), new \DateTime('4am 14 April 2014'));
$occurrences = $helper
->getOccurrences(new \DateTime('1am 14 April 2014'), new \DateTime('1:30am 14 April 2014'));
$this
->assertCount(0, $occurrences);
$occurrences = $helper
->getOccurrences(new \DateTime('4:30am 14 April 2014'), new \DateTime('5am 14 April 2014'));
$this
->assertCount(0, $occurrences);
$occurrences = $helper
->getOccurrences(new \DateTime('1am 14 April 2014'), new \DateTime('3am 14 April 2014'));
$this
->assertCount(1, $occurrences);
$occurrences = $helper
->getOccurrences(new \DateTime('2am 14 April 2014'), new \DateTime('4am 14 April 2014'));
$this
->assertCount(1, $occurrences);
$occurrences = $helper
->getOccurrences(new \DateTime('2:30am 14 April 2014'), new \DateTime('3:30am 14 April 2014'));
$this
->assertCount(1, $occurrences);
$occurrences = $helper
->getOccurrences(new \DateTime('3am 14 April 2014'), new \DateTime('5am 14 April 2014'));
$this
->assertCount(1, $occurrences);
$occurrences = $helper
->getOccurrences(new \DateTime('1am 14 April 2014'), new \DateTime('3am 14 April 2014'), 0);
$this
->assertCount(0, $occurrences);
}
public function testInvalidLimit() {
$helper = $this
->createHelper('FREQ=DAILY;COUNT=10', new \DateTime('2am 14 April 2014'), new \DateTime('4am 14 April 2014'));
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Invalid count limit.');
$helper
->getOccurrences(new \DateTime('1am 14 April 2014'), new \DateTime('3am 14 April 2014'), -1);
}
public function testFrequency() {
$dtStart = new \DateTime('9am 16 June 2014');
$rrule = 'FREQ=DAILY;COUNT=10';
$instance = $this
->createHelper($rrule, $dtStart);
$rules = $instance
->getRules();
$this
->assertCount(1, $rules);
$rule = $rules[0];
$this
->assertEquals('DAILY', $rule
->getFrequency());
}
public function testExdate() {
$timeZone = new \DateTimeZone('Asia/Singapore');
$dtStart = new \DateTime('10am 16 June 2014', $timeZone);
$rrule = 'RRULE:FREQ=DAILY;COUNT=6
EXDATE:20140617T020000Z';
$instance = $this
->createHelper($rrule, $dtStart);
$occurrences = $instance
->getOccurrences();
$this
->assertCount(5, $occurrences);
$this
->assertEquals('Mon, 16 Jun 2014 10:00:00 +0800', $occurrences[0]
->getStart()
->format('r'));
$this
->assertEquals('Wed, 18 Jun 2014 10:00:00 +0800', $occurrences[1]
->getStart()
->format('r'));
$this
->assertEquals('Thu, 19 Jun 2014 10:00:00 +0800', $occurrences[2]
->getStart()
->format('r'));
$this
->assertEquals('Fri, 20 Jun 2014 10:00:00 +0800', $occurrences[3]
->getStart()
->format('r'));
$this
->assertEquals('Sat, 21 Jun 2014 10:00:00 +0800', $occurrences[4]
->getStart()
->format('r'));
$exDates = $instance
->getRlRuleset()
->getExDates();
$this
->assertCount(1, $exDates);
$this
->assertEquals('Tue, 17 Jun 2014 02:00:00 +0000', $exDates[0]
->format('r'));
}
public function testExdateMultiple() {
$timeZone = new \DateTimeZone('Asia/Singapore');
$dtStart = new \DateTime('10am 16 June 2014', $timeZone);
$rrule = 'RRULE:FREQ=DAILY;COUNT=6
EXDATE:20140617T020000Z,20140619T020000Z';
$instance = $this
->createHelper($rrule, $dtStart);
$occurrences = $instance
->getOccurrences();
$this
->assertCount(4, $occurrences);
$this
->assertEquals('Mon, 16 Jun 2014 10:00:00 +0800', $occurrences[0]
->getStart()
->format('r'));
$this
->assertEquals('Wed, 18 Jun 2014 10:00:00 +0800', $occurrences[1]
->getStart()
->format('r'));
$this
->assertEquals('Fri, 20 Jun 2014 10:00:00 +0800', $occurrences[2]
->getStart()
->format('r'));
$this
->assertEquals('Sat, 21 Jun 2014 10:00:00 +0800', $occurrences[3]
->getStart()
->format('r'));
$exDates = $instance
->getRlRuleset()
->getExDates();
$this
->assertCount(2, $exDates);
$this
->assertEquals('Tue, 17 Jun 2014 02:00:00 +0000', $exDates[0]
->format('r'));
$this
->assertEquals('Thu, 19 Jun 2014 02:00:00 +0000', $exDates[1]
->format('r'));
}
public function testExdateTimezone() {
$timeZone = new \DateTimeZone('Asia/Singapore');
$dtStart = new \DateTime('9am 16 June 2014', $timeZone);
$rrule = 'RRULE:FREQ=DAILY;COUNT=6
EXDATE:20140617T000000Z,20140618T000000Z';
$instance = $this
->createHelper($rrule, $dtStart);
$occurrences = $instance
->getOccurrences();
$this
->assertCount(6, $occurrences);
}
public function testRdate() {
$timeZone = new \DateTimeZone('Asia/Singapore');
$dtStart = new \DateTime('11am 4 Oct 2012', $timeZone);
$rrule = 'RRULE:FREQ=WEEKLY;COUNT=3
RDATE:20121006T120000Z';
$instance = $this
->createHelper($rrule, $dtStart);
$occurrences = $instance
->getOccurrences();
$this
->assertCount(4, $occurrences);
$this
->assertEquals('Thu, 04 Oct 2012 11:00:00 +0800', $occurrences[0]
->getStart()
->format('r'));
$this
->assertEquals('Sat, 06 Oct 2012 12:00:00 +0000', $occurrences[1]
->getStart()
->format('r'));
$this
->assertEquals('Thu, 11 Oct 2012 11:00:00 +0800', $occurrences[2]
->getStart()
->format('r'));
$this
->assertEquals('Thu, 18 Oct 2012 11:00:00 +0800', $occurrences[3]
->getStart()
->format('r'));
$rDates = $instance
->getRlRuleset()
->getDates();
$this
->assertCount(1, $rDates);
$this
->assertEquals('Sat, 06 Oct 2012 12:00:00 +0000', $rDates[0]
->format('r'));
}
public function testRdateMultiple() {
$timeZone = new \DateTimeZone('Asia/Singapore');
$dtStart = new \DateTime('11am 4 Oct 2012', $timeZone);
$rrule = 'RRULE:FREQ=WEEKLY;COUNT=3
RDATE:20121006T120000Z,20121013T120000Z';
$instance = $this
->createHelper($rrule, $dtStart);
$occurrences = $instance
->getOccurrences();
$this
->assertCount(5, $occurrences);
$this
->assertEquals('Thu, 04 Oct 2012 11:00:00 +0800', $occurrences[0]
->getStart()
->format('r'));
$this
->assertEquals('Sat, 06 Oct 2012 12:00:00 +0000', $occurrences[1]
->getStart()
->format('r'));
$this
->assertEquals('Thu, 11 Oct 2012 11:00:00 +0800', $occurrences[2]
->getStart()
->format('r'));
$this
->assertEquals('Sat, 13 Oct 2012 12:00:00 +0000', $occurrences[3]
->getStart()
->format('r'));
$this
->assertEquals('Thu, 18 Oct 2012 11:00:00 +0800', $occurrences[4]
->getStart()
->format('r'));
$rDates = $instance
->getRlRuleset()
->getDates();
$this
->assertCount(2, $rDates);
$this
->assertEquals('Sat, 06 Oct 2012 12:00:00 +0000', $rDates[0]
->format('r'));
$this
->assertEquals('Sat, 13 Oct 2012 12:00:00 +0000', $rDates[1]
->format('r'));
}
public function testRedundantPartsOmitted() {
$dtStart = new \DateTime('9am 16 June 2014');
$rrule = 'FREQ=DAILY;COUNT=10';
$instance = $this
->createHelper($rrule, $dtStart);
$rules = $instance
->getRules();
$this
->assertCount(1, $rules);
$rule = $rules[0];
$parts = $rule
->getParts();
$this
->assertArrayHasKey('DTSTART', $parts);
$this
->assertArrayHasKey('COUNT', $parts);
$this
->assertArrayNotHasKey('BYMONTHDAY', $parts);
}
public function testMultilineMissingColon() {
$rrule = 'RRULE:FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR;COUNT=3
EXDATE:19960402T010000Z
foobar';
$this
->expectException(DateRecurHelperArgumentException::class);
$this
->expectExceptionMessage('Multiline RRULE must be prefixed with either: RRULE, EXDATE, EXRULE, or RDATE. Missing for line 3');
$this
->createHelper($rrule, new \DateTime());
}
public function testGetExcluded() {
$tz = new \DateTimeZone('Asia/Singapore');
$dtStart = new \DateTime('9am 4 September 2018', $tz);
$string = 'RRULE:FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR;COUNT=3
EXDATE:20180906T010000Z';
$helper = $this
->createHelper($string, $dtStart);
$excluded = $helper
->getExcluded();
$this
->assertCount(1, $excluded);
$expectedDate = new \DateTime('9am 6 September 2018', $tz);
$this
->assertEquals($expectedDate, $excluded[0]);
}
protected function createHelper(...$args) {
return RlHelper::createInstance(...func_get_args());
}
}