View source
<?php
namespace Drupal\Tests\date_recur\Kernel;
use Drupal\Core\Field\Entity\BaseFieldOverride;
use Drupal\date_recur_entity_test\Entity\DrEntityTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
class DateRecurFieldItemDefaultValuesTest extends KernelTestBase {
protected static $modules = [
'date_recur_entity_test',
'entity_test',
'datetime',
'datetime_range',
'date_recur',
'field',
'user',
'system',
];
protected $baseFieldOverride;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('dr_entity_test');
$this
->installConfig([
'system',
]);
$entityFieldManager = \Drupal::service('entity_field.manager');
$baseFields = $entityFieldManager
->getBaseFieldDefinitions('dr_entity_test');
$this->baseFieldOverride = BaseFieldOverride::createFromBaseFieldDefinition($baseFields['dr'], 'dr_entity_test');
$user = User::create([
'uid' => 2,
'timezone' => 'Asia/Singapore',
]);
$this->container
->get('current_user')
->setAccount($user);
}
public function testDefaults() {
$this->baseFieldOverride
->setDefaultValue([
[
'default_date_type' => 'relative',
'default_date' => '12th April 2013 3pm',
'default_end_date_type' => 'relative',
'default_end_date' => '12th April 2013 4pm',
'default_date_time_zone' => 'Europe/Oslo',
'default_time_zone' => 'Indian/Christmas',
'default_rrule' => 'FREQ=DAILY;COUNT=100',
],
]);
$this->baseFieldOverride
->save();
$entity = DrEntityTest::create();
$this
->assertEquals('2013-04-12T13:00:00', $entity->dr->value);
$this
->assertEquals('2013-04-12T14:00:00', $entity->dr->end_value);
$this
->assertEquals('Indian/Christmas', $entity->dr->timezone);
$this
->assertEquals('FREQ=DAILY;COUNT=100', $entity->dr->rrule);
$entity
->save();
$this
->assertEquals(1, $entity->dr
->count());
}
public function testDefaultCurrentUser() {
$this->baseFieldOverride
->setDefaultValue([
[
'default_date_type' => 'relative',
'default_date' => '12th April 2013 3pm',
'default_end_date_type' => 'relative',
'default_end_date' => '12th April 2013 4pm',
'default_date_time_zone' => 'Europe/Oslo',
'default_time_zone' => '',
],
]);
$this->baseFieldOverride
->save();
$entity = DrEntityTest::create();
$this
->assertEquals('2013-04-12T13:00:00', $entity->dr->value);
$this
->assertEquals('2013-04-12T14:00:00', $entity->dr->end_value);
$this
->assertEquals('Asia/Singapore', $entity->dr->timezone);
}
}