You are here

public function DateRecurOccurrenceTableTest::testTableRows in Recurring Dates Field 3.1.x

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/DateRecurOccurrenceTableTest.php \Drupal\Tests\date_recur\Kernel\DateRecurOccurrenceTableTest::testTableRows()
  2. 3.x tests/src/Kernel/DateRecurOccurrenceTableTest.php \Drupal\Tests\date_recur\Kernel\DateRecurOccurrenceTableTest::testTableRows()
  3. 3.0.x tests/src/Kernel/DateRecurOccurrenceTableTest.php \Drupal\Tests\date_recur\Kernel\DateRecurOccurrenceTableTest::testTableRows()

Ensure occurrence table rows are created.

File

tests/src/Kernel/DateRecurOccurrenceTableTest.php, line 75

Class

DateRecurOccurrenceTableTest
Tests occurrence tables values.

Namespace

Drupal\Tests\date_recur\Kernel

Code

public function testTableRows() {
  $preCreate = 'P1Y';
  if ($this->fieldDefinition instanceof BaseFieldDefinition) {

    // Use BaseFieldOverride entity, similar to NodeType being able to
    // override some options of base fields.
    $fieldConfig = $this->fieldDefinition
      ->getConfig($this->testEntityType);
  }
  else {
    $fieldConfig = FieldConfig::loadByName($this->testEntityType, $this->testEntityType, $this->fieldName);
  }
  $fieldConfig
    ->setSetting('precreate', $preCreate);
  $fieldConfig
    ->save();
  $entity = $this
    ->createEntity();
  $entity->{$this->fieldName} = [
    // The duration is 8 hours.
    'value' => '2014-06-15T23:00:00',
    'end_value' => '2014-06-16T07:00:00',
    'rrule' => 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR',
    'infinite' => '1',
    'timezone' => 'Australia/Sydney',
  ];
  $entity
    ->save();

  // Calculate number of weekdays between first occurrence and end of
  // pre-create interval.
  $tz = new \DateTimeZone('Australia/Sydney');
  $day = new \DateTime('9am 16th June 2014', $tz);
  $until = new \DateTime('now');
  $until
    ->add(new \DateInterval($preCreate));

  // See BYDAY above.
  $countDays = [
    'Mon',
    'Tue',
    'Wed',
    'Thu',
    'Fri',
  ];
  $count = 0;
  while ($day <= $until) {
    if (in_array($day
      ->format('D'), $countDays)) {
      $count++;
    }
    $day
      ->modify('+1 day');
  }
  $tableName = DateRecurOccurrences::getOccurrenceCacheStorageTableName($this->fieldDefinition);
  $actualCount = $this->container
    ->get('database')
    ->select($tableName)
    ->countQuery()
    ->execute()
    ->fetchField();

  // Make sure more than zero rows created.
  $this
    ->assertGreaterThan(0, $actualCount);
  $this
    ->assertEquals($count, $actualCount);
}