You are here

public function LengthIndicatorTest::testLengthIndicator in Length Indicator 8

Tests the length indicator.

File

tests/src/FunctionalJavascript/LengthIndicatorTest.php, line 87

Class

LengthIndicatorTest
Tests the UI for entity displays.

Namespace

Drupal\Tests\length_indicator\FunctionalJavascript

Code

public function testLengthIndicator() {

  // Create a test entity.
  $entity = EntityTest::create([
    'name' => 'The name for this entity',
    'string' => [
      [
        'value' => 'A value in a string field',
      ],
    ],
    'string_long' => [
      [
        'value' => 'A value in a string_long field',
      ],
    ],
  ]);
  $entity
    ->save();
  $form_display = EntityFormDisplay::load('entity_test.entity_test.default');

  /** @var \Drupal\Core\Field\WidgetInterface $string */
  $values = $form_display
    ->getComponent('string');
  $values['third_party_settings']['length_indicator'] = [
    'indicator' => TRUE,
    'indicator_opt' => [
      'optimin' => 15,
      'optimax' => 30,
      'tolerance' => 6,
    ],
  ];
  $form_display
    ->setComponent('string', $values);
  $values = $form_display
    ->getComponent('string_long');
  $values['third_party_settings']['length_indicator'] = [
    'indicator' => TRUE,
    'indicator_opt' => [
      'optimin' => 100,
      'optimax' => 300,
      'tolerance' => 40,
    ],
  ];
  $form_display
    ->setComponent('string_long', $values);
  $form_display
    ->save();
  $this
    ->drupalGet('entity_test/manage/1/edit');

  // Test the string field.
  $this
    ->assertActiveElement('string', 'good');
  $this
    ->assertActiveElement('string', 'bad', '');
  $this
    ->assertActiveElement('string', 'bad', $this
    ->randomString(8));
  $this
    ->assertActiveElement('string', 'ok', $this
    ->randomString(9));
  $this
    ->assertActiveElement('string', 'ok', $this
    ->randomString(14));
  $this
    ->assertActiveElement('string', 'good', $this
    ->randomString(15));
  $this
    ->assertActiveElement('string', 'good', $this
    ->randomString(30));
  $this
    ->assertActiveElement('string', 'ok', $this
    ->randomString(31));
  $this
    ->assertActiveElement('string', 'ok', $this
    ->randomString(36));
  $this
    ->assertActiveElement('string', 'bad', $this
    ->randomString(37));

  // Test the string_long field.
  $this
    ->assertActiveElement('string_long', 'bad');
  $this
    ->assertActiveElement('string', 'bad', '');
  $this
    ->assertActiveElement('string_long', 'bad', $this
    ->randomString(59));
  $this
    ->assertActiveElement('string_long', 'ok', $this
    ->randomString(60));
  $this
    ->assertActiveElement('string_long', 'ok', $this
    ->randomString(99));
  $this
    ->assertActiveElement('string_long', 'good', $this
    ->randomString(100));
  $this
    ->assertActiveElement('string_long', 'good', $this
    ->randomString(300));
  $this
    ->assertActiveElement('string_long', 'ok', $this
    ->randomString(301));
  $this
    ->assertActiveElement('string_long', 'ok', $this
    ->randomString(340));
  $this
    ->assertActiveElement('string_long', 'bad', $this
    ->randomString(341));
}