View source
<?php
namespace Drupal\Tests\date_recur\Kernel;
use Drupal\date_recur\DateRecurOccurrences;
use Drupal\date_recur_entity_test\Entity\DrEntityTest;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\User;
use Drupal\views\Entity\View;
use Drupal\views\Tests\ViewTestData;
class DateRecurViewsFieldTest extends ViewsKernelTestBase {
public static $modules = [
'date_recur_entity_test',
'date_recur_views_test',
'entity_test',
'datetime',
'datetime_range',
'date_recur',
'field',
'user',
];
public static $testViews = [
'dr_entity_test_list',
];
protected $map;
protected $entityType;
protected $fieldName;
protected function setUp($import_test_views = TRUE) {
parent::setUp(FALSE);
$this
->installEntitySchema('dr_entity_test');
ViewTestData::createTestViews(get_class($this), [
'date_recur_views_test',
]);
$this->map = [
'id' => 'id',
];
$this->fieldName = 'dr';
$this->entityType = 'dr_entity_test';
$user = User::create([
'uid' => 2,
'timezone' => 'Australia/Sydney',
]);
$this->container
->get('current_user')
->setAccount($user);
}
public function testField() {
$entity1 = $this
->createEntity();
$entity1->{$this->fieldName} = [
[
'value' => '2018-11-05T22:00:00',
'end_value' => '2018-11-06T06:00:00',
'rrule' => 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;COUNT=3',
'infinite' => '0',
'timezone' => 'Australia/Sydney',
],
];
$entity1
->save();
$entity2 = $this
->createEntity();
$entity2->{$this->fieldName} = [
[
'value' => '2018-11-05T22:05:00',
'end_value' => '2018-11-06T06:05:00',
'rrule' => 'FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;COUNT=3',
'infinite' => '0',
'timezone' => 'Australia/Sydney',
],
];
$entity2
->save();
$definitions = \Drupal::service('entity_field.manager')
->getFieldStorageDefinitions($this->entityType);
$baseTable = 'dr_entity_test';
$occurrenceTableName = DateRecurOccurrences::getOccurrenceCacheStorageTableName($definitions[$this->fieldName]);
$view = View::load('dr_entity_test_list');
$executable = $view
->getExecutable();
$viewsFieldName = $this->fieldName . '_occurrences';
$executable
->addHandler('default', 'relationship', $baseTable, $viewsFieldName, []);
$options = [];
$options['order'] = 'ASC';
$viewsFieldName = $this->fieldName . '_value';
$executable
->addHandler('default', 'sort', $occurrenceTableName, $viewsFieldName, $options);
$options = [];
$options['date_format'] = 'long';
$viewsFieldName = $this->fieldName . '_value';
$startFieldId = $executable
->addHandler('default', 'field', $occurrenceTableName, $viewsFieldName, $options);
$options = [];
$options['date_format'] = 'long';
$viewsFieldName = $this->fieldName . '_end_value';
$endFieldId = $executable
->addHandler('default', 'field', $occurrenceTableName, $viewsFieldName, $options);
$this
->executeView($executable);
$this
->assertCount(6, $executable->result);
$expectedResult = [
[
'entity_id' => 1,
'start' => '2018-11-05T22:00:00',
'end' => '2018-11-06T06:00:00',
],
[
'entity_id' => 2,
'start' => '2018-11-05T22:05:00',
'end' => '2018-11-06T06:05:00',
],
[
'entity_id' => 1,
'start' => '2018-11-06T22:00:00',
'end' => '2018-11-07T06:00:00',
],
[
'entity_id' => 2,
'start' => '2018-11-06T22:05:00',
'end' => '2018-11-07T06:05:00',
],
[
'entity_id' => 1,
'start' => '2018-11-07T22:00:00',
'end' => '2018-11-08T06:00:00',
],
[
'entity_id' => 2,
'start' => '2018-11-07T22:05:00',
'end' => '2018-11-08T06:05:00',
],
];
$this->map = [
'id' => 'entity_id',
$startFieldId => 'start',
$endFieldId => 'end',
];
$this
->assertIdenticalResultset($executable, $expectedResult, $this->map);
$assertRendered = [
[
'start' => 'Tuesday, November 6, 2018 - 09:00',
'end' => 'Tuesday, November 6, 2018 - 17:00',
],
[
'start' => 'Tuesday, November 6, 2018 - 09:05',
'end' => 'Tuesday, November 6, 2018 - 17:05',
],
[
'start' => 'Wednesday, November 7, 2018 - 09:00',
'end' => 'Wednesday, November 7, 2018 - 17:00',
],
[
'start' => 'Wednesday, November 7, 2018 - 09:05',
'end' => 'Wednesday, November 7, 2018 - 17:05',
],
[
'start' => 'Thursday, November 8, 2018 - 09:00',
'end' => 'Thursday, November 8, 2018 - 17:00',
],
[
'start' => 'Thursday, November 8, 2018 - 09:05',
'end' => 'Thursday, November 8, 2018 - 17:05',
],
];
$result = $executable->result;
foreach ($assertRendered as $rowIndex => $assertRender) {
$renderedStart = $executable->field[$startFieldId]
->advancedRender($result[$rowIndex]);
$this
->assertEquals($assertRender['start'], $renderedStart, 'Asserting start for start: index' . $rowIndex);
$renderedEnd = $executable->field[$endFieldId]
->advancedRender($result[$rowIndex]);
$this
->assertEquals($assertRender['end'], $renderedEnd, 'Asserting start for end: index' . $rowIndex);
}
}
protected function createEntity() {
return DrEntityTest::create();
}
}