View source
<?php
namespace Drupal\Tests\date_recur\Kernel;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\date_recur\DateRecurOccurrences;
use Drupal\date_recur_entity_test\Entity\DrEntityTestRev;
use Drupal\field\Entity\FieldConfig;
use Drupal\KernelTests\KernelTestBase;
class DateRecurOccurrenceTableTest extends KernelTestBase {
protected $testEntityType;
protected $fieldName;
protected $fieldDefinition;
protected static $modules = [
'date_recur_entity_test',
'entity_test',
'datetime',
'datetime_range',
'date_recur',
'field',
'user',
];
protected function setUp() : void {
parent::setUp();
$this->testEntityType = 'dr_entity_test_rev';
$this
->installEntitySchema($this->testEntityType);
$this->fieldName = 'dr';
$efm = \Drupal::service('entity_field.manager');
$definitions = $efm
->getFieldStorageDefinitions($this->testEntityType);
$this->fieldDefinition = $definitions[$this->fieldName];
}
public function testTableRows() {
$preCreate = 'P1Y';
if ($this->fieldDefinition instanceof BaseFieldDefinition) {
$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} = [
'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();
$tz = new \DateTimeZone('Australia/Sydney');
$day = new \DateTime('9am 16th June 2014', $tz);
$until = new \DateTime('now');
$until
->add(new \DateInterval($preCreate));
$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();
$this
->assertGreaterThan(0, $actualCount);
$this
->assertEquals($count, $actualCount);
}
public function testGetOccurrenceTableName() {
$actual = DateRecurOccurrences::getOccurrenceCacheStorageTableName($this->fieldDefinition);
$entityTypeId = $this->fieldDefinition
->getTargetEntityTypeId();
$this
->assertEquals('date_recur__' . $entityTypeId . '__' . $this->fieldName, $actual);
}
public function testOccurrenceTableValues() {
$columnNameValue = $this->fieldName . '_value';
$columnNameEndValue = $this->fieldName . '_end_value';
$entity = $this
->createEntity();
$entityTypeId = $entity
->getEntityTypeId();
$entity->{$this->fieldName} = [
[
'value' => '2014-06-17T23:00:00',
'end_value' => '2014-06-18T07:00:00',
'rrule' => 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;COUNT=5',
'infinite' => '0',
'timezone' => 'Australia/Sydney',
],
[
'value' => '2015-07-17T02:00:00',
'end_value' => '2015-07-18T10:00:00',
'rrule' => 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;COUNT=2',
'infinite' => '0',
'timezone' => 'Indian/Cocos',
],
];
$entity
->save();
$tableName = 'date_recur__' . $entityTypeId . '__' . $this->fieldName;
$fields = [
'entity_id',
'revision_id',
'field_delta',
'delta',
$columnNameValue,
$columnNameEndValue,
];
$results = $this->container
->get('database')
->select($tableName, 'occurences')
->fields('occurences', $fields)
->execute()
->fetchAll();
$this
->assertCount(7, $results);
$assertExpected = [
[
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'field_delta' => 0,
'delta' => 0,
$columnNameValue => '2014-06-17T23:00:00',
$columnNameEndValue => '2014-06-18T07:00:00',
],
[
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'field_delta' => 0,
'delta' => 1,
$columnNameValue => '2014-06-18T23:00:00',
$columnNameEndValue => '2014-06-19T07:00:00',
],
[
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'field_delta' => 0,
'delta' => 2,
$columnNameValue => '2014-06-19T23:00:00',
$columnNameEndValue => '2014-06-20T07:00:00',
],
[
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'field_delta' => 0,
'delta' => 3,
$columnNameValue => '2014-06-22T23:00:00',
$columnNameEndValue => '2014-06-23T07:00:00',
],
[
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'field_delta' => 0,
'delta' => 4,
$columnNameValue => '2014-06-23T23:00:00',
$columnNameEndValue => '2014-06-24T07:00:00',
],
[
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'field_delta' => '1',
'delta' => '0',
$columnNameValue => '2015-07-17T02:00:00',
$columnNameEndValue => '2015-07-18T10:00:00',
],
[
'entity_id' => $entity
->id(),
'revision_id' => $entity
->getRevisionId(),
'field_delta' => '1',
'delta' => '1',
$columnNameValue => '2015-07-20T02:00:00',
$columnNameEndValue => '2015-07-21T10:00:00',
],
];
foreach ($results as $actualIndex => $actualValues) {
$expectedValues = $assertExpected[$actualIndex];
$actualValues = (array) $actualValues;
$this
->assertEquals($expectedValues, $actualValues);
}
}
protected function createEntity() {
return DrEntityTestRev::create();
}
}