You are here

function EntityBlockFieldTest::testEntityBlock in Entityblock 8

Tests the link title settings of a link field.

File

src/Tests/EntityBlockFieldTest.php, line 55
Contains Drupal\entityblock\Tests\EntityBlockFieldTest.

Class

EntityBlockFieldTest
Tests entityblock field widgets and formatters.

Namespace

Drupal\entityblock\Tests

Code

function testEntityBlock() {
  $field_name = Unicode::strtolower($this
    ->randomMachineName());
  $title = $this
    ->randomString();

  // Create a field with settings to validate.
  $this->fieldStorage = FieldStorageConfig::create(array(
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => 'entityblock',
    'cardinality' => 1,
  ));
  $this->fieldStorage
    ->save();

  // Set field settings.
  $this->field = FieldConfig::create(array(
    'field_storage' => $this->fieldStorage,
    'bundle' => 'entity_test',
    'label' => 'Block',
    'required' => TRUE,
    'settings' => array(
      'enabled' => 1,
      'title' => $title,
    ),
  ));
  $this->field
    ->save();

  // Set form display.
  \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('entity_test.entity_test.default')
    ->setComponent($field_name, array(
    'type' => 'entityblock_default',
    'settings' => array(
      'view_modes' => array(
        'full' => 'full',
        'teaser' => 'teaser',
      ),
      'force_enabled' => FALSE,
      'force_title' => FALSE,
    ),
  ))
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$field_name}[0][enabled]", '', 'Display as block');

  // Test submitting the form without any values.
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->assertText('Display as block field is required.');
  $this
    ->assertText('Block title field is required.');
  $this
    ->assertText('Render the block with this view mode field is required.');

  // Test submitting the form with all values.
  $edit = array(
    "{$field_name}[0][enabled]" => TRUE,
    "{$field_name}[0][title]" => TRUE,
    "{$field_name}[0][view_mode]" => 'full',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertText("entity_test 1 has been created.", 'Entity saved');
}