View source
<?php
namespace Drupal\Tests\date_recur\Functional;
use Drupal\Core\Url;
use Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class DateRecurFieldSettingsTest extends WebDriverTestBase {
protected $defaultTheme = 'classy';
protected static $modules = [
'entity_test',
'datetime',
'datetime_range',
'date_recur',
'field_ui',
'field',
'user',
'system',
];
protected $fieldConfig;
protected function setUp() : void {
parent::setUp();
$fieldStorage = FieldStorageConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'foo',
'type' => 'date_recur',
'settings' => [
'datetime_type' => DateRecurItem::DATETIME_TYPE_DATETIME,
],
]);
$fieldStorage
->save();
$field = [
'field_name' => 'foo',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
];
$this->fieldConfig = FieldConfig::create($field);
$this->fieldConfig
->save();
$user = $this
->createUser([
'administer entity_test fields',
]);
$this
->drupalLogin($user);
}
public function testAllAllowed() {
$url = Url::fromRoute('entity.field_config.entity_test_field_edit_form', [
'bundle' => 'entity_test',
'field_config' => $this->fieldConfig
->id(),
]);
$this
->drupalGet($url);
$this
->submitForm([
'settings[parts][all]' => TRUE,
], 'Save settings');
$this
->assertSession()
->pageTextContains('Saved foo configuration.');
$this
->assertEquals([
'all' => TRUE,
'frequencies' => [
'SECONDLY' => [],
'MINUTELY' => [],
'HOURLY' => [],
'DAILY' => [],
'WEEKLY' => [],
'MONTHLY' => [],
'YEARLY' => [],
],
], $this
->getPartSettings());
}
public function testAllDisabled() {
$url = Url::fromRoute('entity.field_config.entity_test_field_edit_form', [
'bundle' => 'entity_test',
'field_config' => $this->fieldConfig
->id(),
]);
$this
->drupalGet($url);
$page = $this
->getSession()
->getPage();
$page
->uncheckField('settings[parts][all]');
$this
->submitForm([], 'Save settings');
$this
->assertSession()
->pageTextContains('Saved foo configuration.');
$this
->assertEquals([
'all' => FALSE,
'frequencies' => [
'SECONDLY' => [],
'MINUTELY' => [],
'HOURLY' => [],
'DAILY' => [],
'WEEKLY' => [],
'MONTHLY' => [],
'YEARLY' => [],
],
], $this
->getPartSettings());
}
public function testAllPartsInFrequency() {
$url = Url::fromRoute('entity.field_config.entity_test_field_edit_form', [
'bundle' => 'entity_test',
'field_config' => $this->fieldConfig
->id(),
]);
$this
->drupalGet($url);
$page = $this
->getSession()
->getPage();
$page
->uncheckField('settings[parts][all]');
$this
->submitForm([
'settings[parts][table][WEEKLY][setting]' => 'all-parts',
], 'Save settings');
$this
->assertSession()
->pageTextContains('Saved foo configuration.');
$this
->assertEquals([
'all' => FALSE,
'frequencies' => [
'SECONDLY' => [],
'MINUTELY' => [],
'HOURLY' => [],
'DAILY' => [],
'WEEKLY' => [
'*',
],
'MONTHLY' => [],
'YEARLY' => [],
],
], $this
->getPartSettings());
}
public function testSomePartsInFrequency() {
$url = Url::fromRoute('entity.field_config.entity_test_field_edit_form', [
'bundle' => 'entity_test',
'field_config' => $this->fieldConfig
->id(),
]);
$this
->drupalGet($url);
$page = $this
->getSession()
->getPage();
$page
->uncheckField('settings[parts][all]');
$this
->assertSession()
->fieldExists('settings[parts][table][WEEKLY][setting]')
->setValue('some-parts');
$page
->checkField('settings[parts][table][WEEKLY][parts][COUNT]');
$page
->checkField('settings[parts][table][WEEKLY][parts][DTSTART]');
$page
->checkField('settings[parts][table][WEEKLY][parts][BYDAY]');
$page
->checkField('settings[parts][table][WEEKLY][parts][BYSETPOS]');
$this
->submitForm([], 'Save settings');
$this
->assertSession()
->pageTextContains('Saved foo configuration.');
$this
->assertEquals([
'all' => FALSE,
'frequencies' => [
'SECONDLY' => [],
'MINUTELY' => [],
'HOURLY' => [],
'DAILY' => [],
'WEEKLY' => [
'BYDAY',
'BYSETPOS',
'COUNT',
'DTSTART',
],
'MONTHLY' => [],
'YEARLY' => [],
],
], $this
->getPartSettings());
}
protected function getPartSettings() {
$fieldConfig = FieldConfig::load($this->fieldConfig
->id());
return $fieldConfig
->getSetting('parts');
}
}