View source
<?php
namespace Drupal\Tests\layout_builder\FunctionalJavascript;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class FieldBlockTest extends WebDriverTestBase {
protected static $modules = [
'block',
'datetime',
'layout_builder',
'user',
'layout_builder_fieldblock_test',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_date',
'entity_type' => 'user',
'type' => 'datetime',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'user',
'label' => 'Date field',
]);
$field
->save();
$user = $this
->drupalCreateUser([
'administer blocks',
'access administration pages',
]);
$user->field_date = '1978-11-19T05:00:00';
$user
->save();
$this
->drupalLogin($user);
}
public function testUserFieldBlock() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('admin');
$assert_session
->pageTextNotContains('Sunday, November 19, 1978 - 16:00');
$this
->drupalGet('admin/structure/block');
$this
->clickLink('Place block');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextNotContains('Password');
$assert_session
->pageTextNotContains('Initial email');
$assert_session
->pageTextContains('Date field');
$block_url = 'admin/structure/block/add/field_block_test%3Auser%3Auser%3Afield_date/classy';
$assert_session
->linkByHrefExists($block_url);
$this
->drupalGet($block_url);
$page
->fillField('region', 'content');
$assert_session
->fieldValueEquals('settings[formatter][type]', 'datetime_default');
$assert_session
->fieldValueEquals('settings[formatter][settings][format_type]', 'medium');
$page
->selectFieldOption('settings[formatter][type]', 'datetime_time_ago');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->fieldNotExists('settings[formatter][settings][format_type]');
$assert_session
->fieldExists('settings[formatter][settings][granularity]');
$page
->pressButton('Save block');
$this
->assertTrue($assert_session
->waitForText('The block configuration has been saved.'));
$this
->clickLink('Configure');
$page
->selectFieldOption('settings[formatter][type]', 'datetime_default');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->fieldValueEquals('settings[formatter][settings][format_type]', 'medium');
$page
->selectFieldOption('settings[formatter][settings][format_type]', 'long');
$page
->pressButton('Save block');
$this
->assertTrue($assert_session
->waitForText('The block configuration has been saved.'));
$this
->clickLink('Configure');
$assert_session
->fieldValueEquals('settings[formatter][settings][format_type]', 'long');
$expected = [
'label' => 'above',
'type' => 'datetime_default',
'settings' => [
'format_type' => 'long',
'timezone_override' => '',
],
'third_party_settings' => [],
];
$config = $this->container
->get('config.factory')
->get('block.block.datefield');
$this
->assertEquals($expected, $config
->get('settings.formatter'));
$this
->assertEquals([
'field.field.user.user.field_date',
], $config
->get('dependencies.config'));
$this
->drupalGet('admin');
$assert_session
->pageTextContains('Sunday, November 19, 1978 - 16:00');
}
public function testStatesFieldBlock() {
$page = $this
->getSession()
->getPage();
$timestamp_field_storage = FieldStorageConfig::create([
'field_name' => 'field_timestamp',
'entity_type' => 'user',
'type' => 'timestamp',
]);
$timestamp_field_storage
->save();
$timestamp_field = FieldConfig::create([
'field_storage' => $timestamp_field_storage,
'bundle' => 'user',
'label' => 'Timestamp',
]);
$timestamp_field
->save();
$this
->drupalGet('admin/structure/block/add/field_block_test%3Auser%3Auser%3Afield_timestamp/classy');
$this
->assertFalse($page
->findField('settings[formatter][settings][custom_date_format]')
->isVisible(), 'Custom date format is not visible');
$page
->selectFieldOption('settings[formatter][settings][date_format]', 'custom');
$this
->assertTrue($page
->findField('settings[formatter][settings][custom_date_format]')
->isVisible(), 'Custom date format is visible');
}
}