You are here

function EntityBlockFieldUITest::testFieldUI in Entityblock 8

Tests that entityblock field UI functionality does not generate warnings.

File

src/Tests/EntityBlockFieldUITest.php, line 46
Contains Drupal\entityblock\Tests\EntityBlockFieldUITest.

Class

EntityBlockFieldUITest
Tests entityblock field UI functionality.

Namespace

Drupal\entityblock\Tests

Code

function testFieldUI() {
  $view_modes = \Drupal::service('entity_display.repository')
    ->getViewModes('node');

  // Add a content type.
  $type1 = $this
    ->drupalCreateContentType();
  $content_type1 = $type1
    ->id();
  $bundle_path1 = 'admin/structure/types/manage/' . $type1
    ->id();

  // Add a entityblock field to the newly-created type.
  $field_label = $this
    ->randomMachineName();
  $field_name = Unicode::strtolower($field_label);
  $this
    ->fieldUIAddNewField($bundle_path1, $field_name, $field_label, 'entityblock');

  // Load the formatter page to check that the settings summary does not
  // generate warnings.
  $this
    ->drupalGet("{$bundle_path1}/form-display");
  $this
    ->assertRaw(t('View modes: %view_modes', array(
    '%view_modes' => $view_modes['full']['label'],
  )));

  // Add another content type.
  $type2 = $this
    ->drupalCreateContentType();
  $content_type2 = $type2
    ->id();
  $bundle_path2 = 'admin/structure/types/manage/' . $content_type2;

  // Add an existing field to the second node type.
  $this
    ->fieldUIAddExistingField($bundle_path2, 'field_' . $field_name, $field_label);

  // Load the formatter page to check that the settings summary does not
  // generate warnings.
  $this
    ->drupalGet("{$bundle_path2}/form-display");
  $this
    ->assertRaw(t('View modes: %view_modes', array(
    '%view_modes' => $view_modes['full']['label'],
  )));

  // Test the summaries of the form display settings.
  \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('node.' . $content_type2 . '.default')
    ->setComponent('field_' . $field_name, array(
    'type' => 'entityblock_default',
    'settings' => array(
      'view_modes' => array(
        'teaser' => 'teaser',
      ),
      'force_enabled' => TRUE,
      'force_title' => TRUE,
    ),
  ))
    ->save();
  $this
    ->drupalGet("{$bundle_path2}/form-display");
  $this
    ->assertRaw(t('View modes: %view_modes', array(
    '%view_modes' => $view_modes['teaser']['label'],
  )));
  $this
    ->assertText(t('Force "Display as block"'));
  $this
    ->assertText(t('Force "Block title"'));

  // Delete fields.
  $this
    ->fieldUIDeleteField($bundle_path1, 'node.' . $content_type1 . '.field_' . $field_name, $field_label, $type1
    ->label());
  $this
    ->fieldUIDeleteField($bundle_path2, 'node.' . $content_type2 . '.field_' . $field_name, $field_label, $type2
    ->label());

  // Check that the field was deleted.
  $this
    ->assertNull(FieldConfig::loadByName('node', $type1
    ->id(), $field_name), 'Field was deleted.');

  // Check that the field storage was deleted too.
  $this
    ->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.');

  // Check that the field was deleted.
  $this
    ->assertNull(FieldConfig::loadByName('node', $type2
    ->id(), $field_name), 'Field was deleted.');

  // Check that the field storage was deleted too.
  $this
    ->assertNull(FieldStorageConfig::loadByName('node', $field_name), 'Field storage was deleted.');
}