You are here

public function AllowedFormatsTest::testAllowedFormats in Allowed Formats 8

Test widgets for fields with selected allowed formats.

File

tests/src/Functional/AllowedFormatsTest.php, line 65

Class

AllowedFormatsTest
Tests the basic functionality of Allowed Formats.

Namespace

Drupal\Tests\allowed_formats\Functional

Code

public function testAllowedFormats() {

  // Create one text format.
  $format1 = FilterFormat::create([
    'format' => mb_strtolower($this
      ->randomMachineName()),
    'name' => $this
      ->randomString(),
    'roles' => [
      $this->webUser
        ->getRoles()[0],
    ],
  ]);
  $format1
    ->save();

  // Create a second text format.
  $format2 = FilterFormat::create([
    'format' => mb_strtolower($this
      ->randomMachineName()),
    'name' => $this
      ->randomString(),
    'roles' => [
      $this->webUser
        ->getRoles()[0],
    ],
  ]);
  $format2
    ->save();

  // Change the Allowed Formats settings of the test field created by
  // entity_test_install().
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.field_test_text', [
    'third_party_settings[allowed_formats][' . $format1
      ->id() . ']' => TRUE,
    'third_party_settings[allowed_formats][' . $format2
      ->id() . ']' => TRUE,
  ], $this
    ->t('Save settings'));

  // Display the creation form.
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("field_test_text[0][value]", NULL, 'Widget is displayed');
  $this
    ->assertFieldByName("field_test_text[0][format]", NULL, 'Format selector is displayed');

  // Change field to allow only one format.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalPostForm('entity_test/structure/entity_test/fields/entity_test.entity_test.field_test_text', [
    'third_party_settings[allowed_formats][' . $format2
      ->id() . ']' => FALSE,
  ], $this
    ->t('Save settings'));

  // We shouldn't have the 'format' selector since only one format is allowed.
  $this
    ->drupalLogin($this->webUser);
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("field_test_text[0][value]", NULL, 'Widget is displayed');
  $this
    ->assertNoFieldByName("field_test_text[0][format]", NULL, 'Format selector is not displayed');
}