[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/calendar/tests/ -> rrule_manager_tests.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Defines test class to test manage rrule during ical imports.
  19   *
  20   * @package core_calendar
  21   * @category test
  22   * @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  class core_calendar_rrule_manager_testcase extends advanced_testcase {
  26  
  27      /** @var stdClass a dummy event */
  28      protected $event;
  29  
  30      /**
  31       * Set up method.
  32       */
  33      protected function setUp() {
  34          global $DB, $CFG;
  35          $this->resetAfterTest();
  36  
  37          $this->setTimezone('Australia/Perth');
  38  
  39          $user = $this->getDataGenerator()->create_user();
  40          $sub = new stdClass();
  41          $sub->url = '';
  42          $sub->courseid = 0;
  43          $sub->groupid = 0;
  44          $sub->userid = $user->id;
  45          $sub->pollinterval = 0;
  46          $subid = $DB->insert_record('event_subscriptions', $sub, true);
  47  
  48          $event = new stdClass();
  49          $event->name = 'Event name';
  50          $event->description = '';
  51          $event->timestart = 1385913700; // A 2013-12-2 Monday event.
  52          $event->timeduration = 3600;
  53          $event->uuid = 'uuid';
  54          $event->subscriptionid = $subid;
  55          $event->userid = $user->id;
  56          $event->groupid = 0;
  57          $event->courseid = 0;
  58          $event->eventtype = 'user';
  59          $eventobj = calendar_event::create($event, false);
  60          $DB->set_field('event', 'repeatid', $eventobj->id, array('id' => $eventobj->id));
  61          $eventobj->repeatid = $eventobj->id;
  62          $this->event = $eventobj;
  63      }
  64  
  65      /**
  66       * Test parse_rrule() method.
  67       */
  68      public function test_parse_rrule() {
  69  
  70          $rrule = "FREQ=DAILY;COUNT=3;INTERVAL=4;BYSECOND=20,40;BYMINUTE=2,30;BYHOUR=3,4;BYDAY=MO,TH;BYMONTHDAY=20,
  71                  30;BYYEARDAY=300,-20;BYWEEKNO=22,33;BYMONTH=3,4";
  72          $mang = new core_tests_calendar_rrule_manager($rrule);
  73          $mang->parse_rrule();
  74          $this->assertEquals(\core_calendar\rrule_manager::FREQ_DAILY, $mang->freq);
  75          $this->assertEquals(3, $mang->count);
  76          $this->assertEquals(4, $mang->interval);
  77          $this->assertEquals(array(20, 40), $mang->bysecond);
  78          $this->assertEquals(array(2, 30), $mang->byminute);
  79          $this->assertEquals(array(3, 4), $mang->byhour);
  80          $this->assertEquals(array('MO', 'TH'), $mang->byday);
  81          $this->assertEquals(array(20, 30), $mang->bymonthday);
  82          $this->assertEquals(array(300, -20), $mang->byyearday);
  83          $this->assertEquals(array(22, 33), $mang->byweekno);
  84          $this->assertEquals(array(3, 4), $mang->bymonth);
  85      }
  86  
  87      /**
  88       * Test exception is thrown for invalid property.
  89       *
  90       * @expectedException moodle_exception
  91       */
  92      public function test_parse_rrule_validation() {
  93  
  94          $rrule = "RANDOM=PROPERTY;";
  95          $mang = new core_tests_calendar_rrule_manager($rrule);
  96          $mang->parse_rrule();
  97      }
  98  
  99      /**
 100       * Test exception is thrown for invalid frequency.
 101       *
 102       * @expectedException moodle_exception
 103       */
 104      public function test_freq_validation() {
 105  
 106          $rrule = "FREQ=RANDOMLY;";
 107          $mang = new core_tests_calendar_rrule_manager($rrule);
 108          $mang->parse_rrule();
 109      }
 110  
 111      /**
 112       * Test recurrence rules for daily frequency.
 113       */
 114      public function test_daily_events() {
 115          global $DB;
 116  
 117          $rrule = 'FREQ=DAILY;COUNT=3'; // This should generate 2 child events + 1 parent.
 118          $mang = new \core_calendar\rrule_manager($rrule);
 119          $mang->parse_rrule();
 120          $mang->create_events($this->event);
 121          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 122          $this->assertEquals(3, $count);
 123          $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 124                  'timestart' => ($this->event->timestart + DAYSECS)));
 125          $this->assertTrue($result);
 126          $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 127                  'timestart' => ($this->event->timestart + 2 * DAYSECS)));
 128          $this->assertTrue($result);
 129  
 130          $until = $this->event->timestart + DAYSECS * 2;
 131          $until = date('Y-m-d', $until);
 132          $rrule = "FREQ=DAILY;UNTIL=$until"; // This should generate 1 child event + 1 parent,since by then until bound would be hit.
 133          $mang = new \core_calendar\rrule_manager($rrule);
 134          $mang->parse_rrule();
 135          $mang->create_events($this->event);
 136          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 137          $this->assertEquals(2, $count);
 138          $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 139                  'timestart' => ($this->event->timestart + DAYSECS)));
 140          $this->assertTrue($result);
 141  
 142          $rrule = 'FREQ=DAILY;COUNT=3;INTERVAL=3'; // This should generate 2 child events + 1 parent, every 3rd day.
 143          $mang = new \core_calendar\rrule_manager($rrule);
 144          $mang->parse_rrule();
 145          $mang->create_events($this->event);
 146          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 147          $this->assertEquals(3, $count);
 148          $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 149                  'timestart' => ($this->event->timestart + 3 * DAYSECS)));
 150          $this->assertTrue($result);
 151          $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 152                  'timestart' => ($this->event->timestart + 6 * DAYSECS)));
 153          $this->assertTrue($result);
 154  
 155          // Forever event. This should generate events for time() + 10 year period, every 300th day.
 156          $rrule = 'FREQ=DAILY;INTERVAL=300';
 157          $mang = new \core_calendar\rrule_manager($rrule);
 158          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 159          $mang->parse_rrule();
 160          $mang->create_events($this->event);
 161          for ($i = 0, $time = $this->event->timestart; $time < $until; $i++, $time = $this->event->timestart + 300 * DAYSECS * $i) {
 162              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 163                      'timestart' => ($time)));
 164              $this->assertTrue($result);
 165          }
 166      }
 167  
 168      /**
 169       * Test recurrence rules for weekly frequency.
 170       */
 171      public function test_weekly_events() {
 172          global $DB;
 173  
 174          $rrule = 'FREQ=WEEKLY;COUNT=1'; // This should generate 7 events in total, one for each day.
 175          $mang = new \core_calendar\rrule_manager($rrule);
 176          $mang->parse_rrule();
 177          $mang->create_events($this->event);
 178          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 179          $this->assertEquals(7, $count);
 180          for ($i = 0; $i < 7; $i++) {
 181              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 182                      'timestart' => ($this->event->timestart + $i * DAYSECS)));
 183              $this->assertTrue($result);
 184          }
 185  
 186          // This should generate 4 child event + 1 parent, since by then until bound would be hit.
 187          $until = $this->event->timestart + WEEKSECS * 4;
 188          $until = date('YmdThis', $until);
 189          $rrule = "FREQ=WEEKLY;BYDAY=MO;UNTIL=$until";
 190          $mang = new \core_calendar\rrule_manager($rrule);
 191          $mang->parse_rrule();
 192          $mang->create_events($this->event);
 193          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 194          $this->assertEquals(5, $count);
 195          for ($i = 0; $i < 5; $i++) {
 196              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 197                      'timestart' => ($this->event->timestart + $i * WEEKSECS)));
 198              $this->assertTrue($result);
 199          }
 200  
 201          // This should generate 4 events in total every monday and Wednesday of every 3rd week.
 202          $rrule = 'FREQ=WEEKLY;INTERVAL=3;BYDAY=MO,WE;COUNT=2';
 203          $mang = new \core_calendar\rrule_manager($rrule);
 204          $mang->parse_rrule();
 205          $mang->create_events($this->event);
 206          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 207          $this->assertEquals(4, $count);
 208          $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 209                  'timestart' => ($this->event->timestart + 3 * WEEKSECS))); // Monday event.
 210          $this->assertTrue($result);
 211          $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 212                  'timestart' => ($this->event->timestart + 2 * DAYSECS))); // Wednesday event.
 213          $this->assertTrue($result);
 214          $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 215                  'timestart' => ($this->event->timestart + 3 * WEEKSECS + 2 * DAYSECS))); // Wednesday event.
 216          $this->assertTrue($result);
 217  
 218          // Forever event. This should generate events over time() + 10 year period, every 50th monday.
 219          $rrule = 'FREQ=WEEKLY;BYDAY=MO;INTERVAL=50';
 220          $mang = new \core_calendar\rrule_manager($rrule);
 221          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 222          $mang->parse_rrule();
 223          $mang->create_events($this->event);
 224          for ($i = 0, $time = $this->event->timestart; $time < $until; $i++, $time = $this->event->timestart + 50 * WEEKSECS * $i) {
 225              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 226                      'timestart' => ($time)));
 227              $this->assertTrue($result);
 228          }
 229      }
 230  
 231      /**
 232       * Test recurrence rules for monthly frequency.
 233       */
 234      public function test_monthly_events() {
 235          global $DB;
 236          $rrule = 'FREQ=MONTHLY;COUNT=3;BYMONTHDAY=2'; // This should generate 3 events in total.
 237          $mang = new \core_calendar\rrule_manager($rrule);
 238          $mang->parse_rrule();
 239          $mang->create_events($this->event);
 240          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 241          $this->assertEquals(3, $count);
 242          for ($i = 0; $i < 3; $i++) {
 243              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 244                      'timestart' => (strtotime("+$i month", $this->event->timestart))));
 245              $this->assertTrue($result);
 246          }
 247  
 248          // This much seconds after the start of the day.
 249          $offset = $this->event->timestart - mktime(0, 0, 0, date("n", $this->event->timestart), date("j", $this->event->timestart),
 250                  date("Y", $this->event->timestart));
 251          $monthstart = mktime(0, 0, 0, date("n", $this->event->timestart), 1, date("Y", $this->event->timestart));
 252  
 253          $rrule = 'FREQ=MONTHLY;COUNT=3;BYDAY=1MO'; // This should generate 3 events in total, first monday of the month.
 254          $mang = new \core_calendar\rrule_manager($rrule);
 255          $mang->parse_rrule();
 256          $mang->create_events($this->event);
 257          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 258          $this->assertEquals(3, $count);
 259          $time = strtotime('1 Monday', strtotime("+1 months", $monthstart)) + $offset;
 260          $result = $DB->record_exists('event', array('repeatid' => $this->event->id, 'timestart' => $time));
 261          $this->assertTrue($result);
 262          $time = strtotime('1 Monday', strtotime("+2 months", $monthstart)) + $offset;
 263          $result = $DB->record_exists('event', array('repeatid' => $this->event->id, 'timestart' => $time));
 264          $this->assertTrue($result);
 265  
 266          // This should generate 10 child event + 1 parent, since by then until bound would be hit.
 267          $until = strtotime('+1 day +10 months', $this->event->timestart);
 268          $until = date('YmdThis', $until);
 269          $rrule = "FREQ=MONTHLY;BYMONTHDAY=2;UNTIL=$until";
 270          $mang = new \core_calendar\rrule_manager($rrule);
 271          $mang->parse_rrule();
 272          $mang->create_events($this->event);
 273          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 274          $this->assertEquals(11, $count);
 275          for ($i = 0; $i < 11; $i++) {
 276              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 277                      'timestart' => (strtotime("+$i month", $this->event->timestart))));
 278              $this->assertTrue($result);
 279          }
 280  
 281          // This should generate 10 child event + 1 parent, since by then until bound would be hit.
 282          $until = strtotime('+1 day +10 months', $this->event->timestart);
 283          $until = date('YmdThis', $until);
 284          $rrule = "FREQ=MONTHLY;BYDAY=1MO;UNTIL=$until";
 285          $mang = new \core_calendar\rrule_manager($rrule);
 286          $mang->parse_rrule();
 287          $mang->create_events($this->event);
 288          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 289          $this->assertEquals(10, $count);
 290          for ($i = 0; $i < 10; $i++) {
 291              $time = strtotime('1 Monday', strtotime("+$i months", $monthstart)) + $offset;
 292              $result = $DB->record_exists('event', array('repeatid' => $this->event->id, 'timestart' => $time));
 293              $this->assertTrue($result);
 294          }
 295  
 296          // This should generate 11 child event + 1 parent, since by then until bound would be hit.
 297          $until = strtotime('+10 day +10 months', $this->event->timestart); // 12 oct 2014.
 298          $until = date('YmdThis', $until);
 299          $rrule = "FREQ=MONTHLY;INTERVAL=2;BYMONTHDAY=2,5;UNTIL=$until";
 300          $mang = new \core_calendar\rrule_manager($rrule);
 301          $mang->parse_rrule();
 302          $mang->create_events($this->event);
 303          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 304          $this->assertEquals(12, $count);
 305          for ($i = 0; $i < 6; $i++) {
 306              $moffset = $i * 2;
 307              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 308                      'timestart' => (strtotime("+$moffset month", $this->event->timestart))));
 309              $this->assertTrue($result);
 310              // Event on the 5th of a month.
 311              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 312                      'timestart' => (strtotime("+3 days +$moffset month", $this->event->timestart))));
 313              $this->assertTrue($result);
 314          }
 315  
 316          // This should generate 11 child event + 1 parent, since by then until bound would be hit.
 317          $until = strtotime('+20 day +10 months', $this->event->timestart); // 22 oct 2014.
 318          $until = date('YmdTHis', $until);
 319          $rrule = "FREQ=MONTHLY;INTERVAL=2;BYDAY=1MO,3WE;UNTIL=$until";
 320          $mang = new \core_calendar\rrule_manager($rrule);
 321          $mang->parse_rrule();
 322          $mang->create_events($this->event);
 323          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 324          $this->assertEquals(12, $count);
 325          for ($i = 0; $i < 6; $i++) {
 326              $moffset = $i * 2;
 327              $time = strtotime("+$moffset month", $monthstart);
 328              $time2 = strtotime("+1 Monday", $time) + $offset;
 329              $result = $DB->record_exists('event', array('repeatid' => $this->event->id, 'timestart' => $time2));
 330              $this->assertTrue($result);
 331              $time2 = strtotime("+3 Wednesday", $time) + $offset;
 332              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 333                      'timestart' => $time2)); // Event on the 5th of a month.
 334              $this->assertTrue($result);
 335          }
 336  
 337          // Forever event. This should generate events over 10 year period, on 2nd of every 12th month.
 338          $rrule = 'FREQ=MONTHLY;INTERVAL=12;BYMONTHDAY=2';
 339          $mang = new \core_calendar\rrule_manager($rrule);
 340          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 341          $mang->parse_rrule();
 342          $mang->create_events($this->event);
 343          for ($i = 0, $time = $this->event->timestart; $time < $until; $i++, $moffset = $i * 12,
 344                  $time = strtotime("+$moffset month", $this->event->timestart)) {
 345              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 346                      'timestart' => ($time)));
 347              $this->assertTrue($result);
 348          }
 349  
 350          // Forever event. This should generate 10 child events + 1 parent over 10 year period, every 50th Monday.
 351          $rrule = 'FREQ=MONTHLY;BYDAY=1MO;INTERVAL=12';
 352          $mang = new \core_calendar\rrule_manager($rrule);
 353          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 354          $mang->parse_rrule();
 355          $mang->create_events($this->event);
 356          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 357          $this->assertEquals(11, $count);
 358          for ($i = 0, $moffset = 0, $time = $this->event->timestart; $time < $until; $i++, $moffset = $i * 12) {
 359              $result = $DB->record_exists('event', array('repeatid' => $this->event->id, 'timestart' => ($time)));
 360              $this->assertTrue($result);
 361              $time = strtotime("+$moffset month", $monthstart);
 362              $time = strtotime("+1 Monday", $time) + $offset;
 363          }
 364      }
 365  
 366      /**
 367       * Test recurrence rules for yearly frequency.
 368       */
 369      public function test_yearly_events() {
 370          global $DB;
 371  
 372          $rrule = 'FREQ=YEARLY;COUNT=3;BYMONTH=12'; // This should generate 3 events in total.
 373          $mang = new \core_calendar\rrule_manager($rrule);
 374          $mang->parse_rrule();
 375          $mang->create_events($this->event);
 376          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 377          $this->assertEquals(3, $count);
 378          for ($i = 0, $time = $this->event->timestart; $i < 3; $i++, $time = strtotime("+$i years", $this->event->timestart)) {
 379              $result = $DB->record_exists('event', array('repeatid' => $this->event->id, 'timestart' => $time));
 380              $this->assertTrue($result);
 381          }
 382  
 383          // Create an event every december, until the time limit is hit.
 384          $until = strtotime('+20 day +10 years', $this->event->timestart);
 385          $until = date('YmdTHis', $until);
 386          $rrule = "FREQ=YEARLY;BYMONTH=12;UNTIL=$until"; // Forever event.
 387          $mang = new \core_calendar\rrule_manager($rrule);
 388          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 389          $mang->parse_rrule();
 390          $mang->create_events($this->event);
 391          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 392          $this->assertEquals(11, $count);
 393          for ($i = 0, $time = $this->event->timestart; $time < $until; $i++, $yoffset = $i * 2,
 394              $time = strtotime("+$yoffset years", $this->event->timestart)) {
 395              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 396                      'timestart' => ($time)));
 397              $this->assertTrue($result);
 398          }
 399  
 400          // This should generate 5 events in total, every second year in the month of december.
 401          $rrule = 'FREQ=YEARLY;BYMONTH=12;INTERVAL=2;COUNT=5';
 402          $mang = new \core_calendar\rrule_manager($rrule);
 403          $mang->parse_rrule();
 404          $mang->create_events($this->event);
 405          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 406          $this->assertEquals(5, $count);
 407          for ($i = 0, $time = $this->event->timestart; $i < 5; $i++, $yoffset = $i * 2,
 408              $time = strtotime("+$yoffset years", $this->event->timestart)) {
 409              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 410                      'timestart' => ($time)));
 411              $this->assertTrue($result);
 412          }
 413  
 414          $rrule = 'FREQ=YEARLY;BYMONTH=12;INTERVAL=2'; // Forever event.
 415          $mang = new \core_calendar\rrule_manager($rrule);
 416          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 417          $mang->parse_rrule();
 418          $mang->create_events($this->event);
 419          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 420          $this->assertEquals(6, $count);
 421          for ($i = 0, $time = $this->event->timestart; $time < $until; $i++, $yoffset = $i * 2,
 422              $time = strtotime("+$yoffset years", $this->event->timestart)) {
 423              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 424                      'timestart' => ($time)));
 425              $this->assertTrue($result);
 426          }
 427  
 428          // This much seconds after the start of the day.
 429          $offset = $this->event->timestart - mktime(0, 0, 0, date("n", $this->event->timestart), date("j", $this->event->timestart),
 430                  date("Y", $this->event->timestart));
 431          $yearstart = mktime(0, 0, 0, 1, 1, date("Y", $this->event->timestart));
 432  
 433          $rrule = 'FREQ=YEARLY;COUNT=3;BYMONTH=12;BYDAY=1MO'; // This should generate 3 events in total.
 434          $mang = new \core_calendar\rrule_manager($rrule);
 435          $mang->parse_rrule();
 436          $mang->create_events($this->event);
 437          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 438          $this->assertEquals(3, $count);
 439          for ($i = 0; $i < 3; $i++) {
 440              $time = strtotime("+11 months +$i years", $yearstart);
 441              $time = strtotime("+1 Monday", $time) + $offset;
 442              $result = $DB->record_exists('event', array('repeatid' => $this->event->id, 'timestart' => $time));
 443              $this->assertTrue($result);
 444          }
 445  
 446          // Create an event every december, until the time limit is hit.
 447          $until = strtotime('+20 day +10 years', $this->event->timestart);
 448          $until = date('YmdTHis', $until);
 449          $rrule = "FREQ=YEARLY;BYMONTH=12;UNTIL=$until;BYDAY=1MO";
 450          $mang = new \core_calendar\rrule_manager($rrule);
 451          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 452          $mang->parse_rrule();
 453          $mang->create_events($this->event);
 454          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 455          $this->assertEquals(11, $count);
 456          for ($i = 0, $time = $this->event->timestart; $time < $until; $i++) {
 457              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 458                      'timestart' => ($time)));
 459              $this->assertTrue($result);
 460              $time = strtotime("+11 months +$i years", $yearstart);
 461              $time = strtotime("+1 Monday", $time) + $offset;
 462          }
 463  
 464          // This should generate 5 events in total, every second year in the month of december.
 465          $rrule = 'FREQ=YEARLY;BYMONTH=12;INTERVAL=2;COUNT=5;BYDAY=1MO';
 466          $mang = new \core_calendar\rrule_manager($rrule);
 467          $mang->parse_rrule();
 468          $mang->create_events($this->event);
 469          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 470          $this->assertEquals(5, $count);
 471          for ($i = $yoffset = 0, $time = $this->event->timestart; $i < 5; $i++, $yoffset = $i * 2) {
 472              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 473                      'timestart' => ($time)));
 474              $this->assertTrue($result);
 475              $time = strtotime("+11 months +$yoffset years", $yearstart);
 476              $time = strtotime("+1 Monday", $time) + $offset;
 477          }
 478  
 479          $rrule = 'FREQ=YEARLY;BYMONTH=12;INTERVAL=2;BYDAY=1MO'; // Forever event.
 480          $mang = new \core_calendar\rrule_manager($rrule);
 481          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 482          $mang->parse_rrule();
 483          $mang->create_events($this->event);
 484          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 485          $this->assertEquals(6, $count);
 486          for ($i = 0, $time = $this->event->timestart; $time < $until; $i++, $yoffset = $i * 2) {
 487              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 488                      'timestart' => ($time)));
 489              $this->assertTrue($result);
 490              $time = strtotime("+11 months +$yoffset years", $yearstart);
 491              $time = strtotime("+1 Monday", $time) + $offset;
 492          }
 493  
 494          $rrule = 'FREQ=YEARLY;INTERVAL=2'; // Forever event.
 495          $mang = new \core_calendar\rrule_manager($rrule);
 496          $until = time() + (YEARSECS * $mang::TIME_UNLIMITED_YEARS);
 497          $mang->parse_rrule();
 498          $mang->create_events($this->event);
 499          $count = $DB->count_records('event', array('repeatid' => $this->event->id));
 500          $this->assertEquals(6, $count);
 501          for ($i = 0, $time = $this->event->timestart; $time < $until; $i++, $yoffset = $i * 2) {
 502              $result = $DB->record_exists('event', array('repeatid' => $this->event->id,
 503                      'timestart' => ($time)));
 504              $this->assertTrue($result);
 505              $time = strtotime("+11 months +$yoffset years", $yearstart);
 506              $time = strtotime("+1 Monday", $time) + $offset;
 507          }
 508      }
 509  }
 510  
 511  /**
 512   * Class core_calendar_test_rrule_manager
 513   *
 514   * Wrapper to access protected vars for testing.
 515   *
 516   * @package core_calendar
 517   * @category test
 518   * @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
 519   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 520   */
 521  class core_tests_calendar_rrule_manager extends \core_calendar\rrule_manager{
 522  
 523      /**
 524       * Magic method to get properties.
 525       *
 526       * @param $prop string property
 527       *
 528       * @return mixed
 529       * @throws coding_exception
 530       */
 531      public function __get($prop) {
 532          if (property_exists($this, $prop)) {
 533              return $this->$prop;
 534          }
 535          throw new coding_exception('invalidproperty');
 536      }
 537  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1