You are here

public function FieldBlockTest::testUserFieldBlock in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/tests/src/FunctionalJavascript/FieldBlockTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\FieldBlockTest::testUserFieldBlock()
  2. 10 core/modules/layout_builder/tests/src/FunctionalJavascript/FieldBlockTest.php \Drupal\Tests\layout_builder\FunctionalJavascript\FieldBlockTest::testUserFieldBlock()

Tests configuring a field block for a user field.

File

core/modules/layout_builder/tests/src/FunctionalJavascript/FieldBlockTest.php, line 64

Class

FieldBlockTest
@coversDefaultClass \Drupal\layout_builder\Plugin\Block\FieldBlock

Namespace

Drupal\Tests\layout_builder\FunctionalJavascript

Code

public function testUserFieldBlock() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Assert that the field value is not displayed.
  $this
    ->drupalGet('admin');
  $assert_session
    ->pageTextNotContains('Sunday, November 19, 1978 - 16:00');
  $this
    ->drupalGet('admin/structure/block');
  $this
    ->clickLink('Place block');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Ensure that fields without any formatters are not available.
  $assert_session
    ->pageTextNotContains('Password');

  // Ensure that non-display-configurable fields are not available.
  $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 the default formatter configuration.
  $assert_session
    ->fieldValueEquals('settings[formatter][type]', 'datetime_default');
  $assert_session
    ->fieldValueEquals('settings[formatter][settings][format_type]', 'medium');

  // Change the formatter.
  $page
    ->selectFieldOption('settings[formatter][type]', 'datetime_time_ago');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Changing the formatter removes the old settings and introduces new ones.
  $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.'));

  // Configure the block and change the formatter again.
  $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.'));

  // Assert that the field value is updated.
  $this
    ->clickLink('Configure');
  $assert_session
    ->fieldValueEquals('settings[formatter][settings][format_type]', 'long');

  // Assert that the field block is configured as expected.
  $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'));

  // Assert that the block is displaying the user field.
  $this
    ->drupalGet('admin');
  $assert_session
    ->pageTextContains('Sunday, November 19, 1978 - 16:00');
}